Skip to content

Commit

Permalink
change token list to be a soft requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderZhi committed Jun 19, 2024
1 parent d09ecb2 commit 279cfd6
Show file tree
Hide file tree
Showing 19 changed files with 2,933 additions and 2,784 deletions.
6 changes: 3 additions & 3 deletions contracts/iotube/TokenCashier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ contract TokenCashier is Pausable {
tokenSafes = _tokenSafes;
}

// function() external {
// revert();
// }
function() external {
revert();
}

function count(address _token) public view returns (uint256) {
return counts[_token];
Expand Down
61 changes: 37 additions & 24 deletions contracts/iotube/TokenCashierWithPayload.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ pragma solidity >= 0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";

interface ITokenList {
function isAllowed(address) external returns (bool);
function maxAmount(address) external returns (uint256);
function minAmount(address) external returns (uint256);
function isAllowed(address) external view returns (bool);
}

interface IWrappedCoin {
Expand All @@ -15,7 +13,8 @@ interface IWrappedCoin {

contract TokenCashierWithPayload is Ownable {
event Receipt(address indexed token, uint256 indexed id, address sender, address recipient, uint256 amount, uint256 fee, bytes payload);

event Pause();
event Unpause();
modifier whenNotPaused() {
require(!paused);
_;
Expand All @@ -40,6 +39,18 @@ contract TokenCashierWithPayload is Ownable {
revert();
}

function pause() public onlyOwner {
require(!paused, "already paused");
paused = true;
emit Pause();
}

function unpause() public onlyOwner {
require(paused, "already unpaused");
paused = false;
emit Unpause();
}

function count(address _token) public view returns (uint256) {
return counts[_token];
}
Expand All @@ -48,6 +59,15 @@ contract TokenCashierWithPayload is Ownable {
depositFee = _fee;
}

function getSafeAddress(address _token) public view returns (address) {
for (uint256 i = 0; i < tokenLists.length; i++) {
if (tokenLists[i].isAllowed(_token)) {
return tokenSafes[i];
}
}
return address(0);
}

function depositTo(address _token, address _to, uint256 _amount, bytes memory _payload) public whenNotPaused payable {
require(_to != address(0), "invalid destination");
bool isCoin = false;
Expand All @@ -60,28 +80,21 @@ contract TokenCashierWithPayload is Ownable {
isCoin = true;
}
require(fee >= depositFee, "insufficient fee");
for (uint256 i = 0; i < tokenLists.length; i++) {
if (tokenLists[i].isAllowed(_token)) {
require(_amount >= tokenLists[i].minAmount(_token), "amount too low");
require(_amount <= tokenLists[i].maxAmount(_token), "amount too high");
if (tokenSafes[i] == address(0)) {
require(!isCoin && safeTransferFrom(_token, msg.sender, address(this), _amount), "fail to transfer token to cashier");
// selector = bytes4(keccak256(bytes('burn(uint256)')))
(bool success, bytes memory data) = _token.call(abi.encodeWithSelector(0x42966c68, _amount));
require(success && (data.length == 0 || abi.decode(data, (bool))), "fail to burn token");
} else {
if (isCoin) {
require(safeTransfer(_token, tokenSafes[i], _amount), "failed to put into safe");
} else {
require(safeTransferFrom(_token, msg.sender, tokenSafes[i], _amount), "failed to put into safe");
}
}
counts[_token] += 1;
emit Receipt(_token, counts[_token], msg.sender, _to, _amount, fee, _payload);
return;
address safe = getSafeAddress(_token);
if (safe == address(0)) {
require(!isCoin && safeTransferFrom(_token, msg.sender, address(this), _amount), "fail to transfer token to cashier");
// selector = bytes4(keccak256(bytes('burn(uint256)')))
(bool success, bytes memory data) = _token.call(abi.encodeWithSelector(0x42966c68, _amount));
require(success && (data.length == 0 || abi.decode(data, (bool))), "fail to burn token");
} else {
if (isCoin) {
require(safeTransfer(_token, safe, _amount), "failed to put into safe");
} else {
require(safeTransferFrom(_token, msg.sender, safe, _amount), "failed to put into safe");
}
}
revert("not a whitelisted token");
counts[_token] += 1;
emit Receipt(_token, counts[_token], msg.sender, _to, _amount, fee, _payload);
}

function deposit(address _token, uint256 _amount, bytes memory _payload) public payable {
Expand Down
60 changes: 39 additions & 21 deletions contracts/iotube/TransferValidatorWithPayload.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ contract TransferValidatorWithPayload is Ownable {
event Settled(bytes32 indexed key, address[] witnesses);
event ReceiverAdded(address receiver);
event ReceiverRemoved(address receiver);
event Pause();
event Unpause();
modifier whenNotPaused() {
require(!paused);
_;
Expand All @@ -39,39 +41,55 @@ contract TransferValidatorWithPayload is Ownable {
witnessList = _witnessList;
}

function pause() public onlyOwner {
require(!paused, "already paused");
paused = true;
emit Pause();
}

function unpause() public onlyOwner {
require(paused, "already unpaused");
paused = false;
emit Unpause();
}

function generateKey(address cashier, address tokenAddr, uint256 index, address from, address to, uint256 amount, bytes memory payload) public view returns(bytes32) {
return keccak256(abi.encodePacked(address(this), cashier, tokenAddr, index, from, to, amount, payload));
}

function getMinter(address tokenAddr) public view returns (IMinter) {
for (uint256 i = 0; i < tokenLists.length; i++) {
if (tokenLists[i].isAllowed(tokenAddr)) {
return minters[i];
}
}
return minters[0];
}

function submit(address cashier, address tokenAddr, uint256 index, address from, address to, uint256 amount, bytes memory signatures, bytes memory payload) public whenNotPaused {
require(amount != 0, "amount cannot be zero");
require(to != address(0), "recipient cannot be zero");
require(signatures.length % 65 == 0, "invalid signature length");
bytes32 key = generateKey(cashier, tokenAddr, index, from, to, amount, payload);
require(settles[key] == 0, "transfer has been settled");
for (uint256 it = 0; it < tokenLists.length; it++) {
if (tokenLists[it].isAllowed(tokenAddr)) {
uint256 numOfSignatures = signatures.length / 65;
address[] memory witnesses = new address[](numOfSignatures);
for (uint256 i = 0; i < numOfSignatures; i++) {
address witness = recover(key, signatures, i * 65);
require(witnessList.isAllowed(witness), "invalid signature");
for (uint256 j = 0; j < i; j++) {
require(witness != witnesses[j], "duplicate witness");
}
witnesses[i] = witness;
}
require(numOfSignatures * 3 > witnessList.numOfActive() * 2, "insufficient witnesses");
settles[key] = block.number;
require(minters[it].mint(tokenAddr, to, amount), "failed to mint token");
if (receivers[to]) {
IReceiver(to).onReceive(from, tokenAddr, amount, payload);
}
emit Settled(key, witnesses);
return;
uint256 numOfSignatures = signatures.length / 65;
address[] memory witnesses = new address[](numOfSignatures);
for (uint256 i = 0; i < numOfSignatures; i++) {
address witness = recover(key, signatures, i * 65);
require(witnessList.isAllowed(witness), "invalid signature");
for (uint256 j = 0; j < i; j++) {
require(witness != witnesses[j], "duplicate witness");
}
witnesses[i] = witness;
}
require(numOfSignatures * 3 > witnessList.numOfActive() * 2, "insufficient witnesses");
IMinter minter = getMinter(tokenAddr);
settles[key] = block.number;
require(minter.mint(tokenAddr, to, amount), "failed to mint token");
if (receivers[to]) {
IReceiver(to).onReceive(from, tokenAddr, amount, payload);
}
revert("not a whitelisted token");
emit Settled(key, witnesses);
}

function numOfPairs() external view returns (uint256) {
Expand Down
2 changes: 1 addition & 1 deletion truffle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module.exports = {
enabled: true,
runs: 200
},
evmVersion: "byzantium"
// evmVersion: "byzantium"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion witness-service/cmd/relayer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var defaultConfig = Configuration{
PrivateKey: "",
SlackWebHook: "",
LarkWebHook: "",
Version: relayer.V1,
Version: relayer.NoPayload,
TransferTableName: "relayer.transfers",
WitnessTableName: "relayer.witnesses",
}
Expand Down
Loading

0 comments on commit 279cfd6

Please sign in to comment.