From 6d7f609219103fa5955603c14818e3f47edd198a Mon Sep 17 00:00:00 2001 From: Foivos Date: Fri, 3 Nov 2023 16:02:25 +0200 Subject: [PATCH 1/2] Fixed token flow --- contracts/interfaces/ITokenRegistrar.sol | 12 +-- contracts/token-registrars/TokenRegistrar.sol | 28 +++---- test/TokenRegistrars.js | 76 ++++++++++++++++--- 3 files changed, 85 insertions(+), 31 deletions(-) diff --git a/contracts/interfaces/ITokenRegistrar.sol b/contracts/interfaces/ITokenRegistrar.sol index a8a11746..90a4d907 100644 --- a/contracts/interfaces/ITokenRegistrar.sol +++ b/contracts/interfaces/ITokenRegistrar.sol @@ -57,11 +57,13 @@ interface ITokenRegistrar { uint256 gasValue ) external payable; - function interchainTransferFrom( + function tokenTransferFrom( bytes32 tokenId, - string calldata destinationChain, - bytes calldata destinationAddress, - uint256 amount, - uint256 gasValue + uint256 amount + ) external payable; + + function tokenApprove( + bytes32 tokenId, + uint256 amount ) external payable; } diff --git a/contracts/token-registrars/TokenRegistrar.sol b/contracts/token-registrars/TokenRegistrar.sol index 1ba130ca..bb56b9d3 100644 --- a/contracts/token-registrars/TokenRegistrar.sol +++ b/contracts/token-registrars/TokenRegistrar.sol @@ -214,24 +214,24 @@ contract TokenRegistrar is ITokenRegistrar, ITokenManagerType, Multicall, Upgrad } } - function interchainTransferFrom( + function tokenTransferFrom( bytes32 tokenId, - string calldata destinationChain, - bytes calldata destinationAddress, - uint256 amount, - uint256 gasValue + uint256 amount ) external payable { - address tokenAddress = service.interchainTokenAddress(tokenId); + address tokenAddress = service.tokenAddress(tokenId); IStandardizedToken token = IStandardizedToken(tokenAddress); - if (bytes(destinationChain).length == 0) { - token.safeTransferFrom(msg.sender, destinationAddress.toAddress(), amount); - } else { - token.safeTransferFrom(msg.sender, address(this), amount); - if (!token.approve(address(service), amount)) revert NotApproved(tokenAddress); + token.safeTransferFrom(msg.sender, address(this), amount); + } - // slither-disable-next-line arbitrary-send-eth - service.interchainTransfer{ value: gasValue }(tokenId, destinationChain, destinationAddress, amount, new bytes(0)); - } + function tokenApprove( + bytes32 tokenId, + uint256 amount + ) external payable { + address tokenAddress = service.tokenAddress(tokenId); + IStandardizedToken token = IStandardizedToken(tokenAddress); + address tokenManager = service.tokenManagerAddress(tokenId); + + token.safeCall(abi.encodeWithSelector(token.approve.selector, tokenManager, amount)); } } diff --git a/test/TokenRegistrars.js b/test/TokenRegistrars.js index e63c43e8..3d03286b 100644 --- a/test/TokenRegistrars.js +++ b/test/TokenRegistrars.js @@ -96,7 +96,52 @@ describe('Token Registrars', () => { .withArgs(service.address, destinationChain, service.address, keccak256(payload), payload); }); + it('Should transfer some tokens to the registrar', async () => { + const amount = 123456; + + await deployToken(); + + const params = defaultAbiCoder.encode(['bytes', 'address'], ['0x', token.address]); + + await expect(tokenRegistrar.registerCanonicalToken(token.address)) + .to.emit(service, 'TokenManagerDeployed') + .withArgs(tokenId, tokenManagerAddress, LOCK_UNLOCK, params); + + await token.approve(tokenRegistrar.address, amount).then((tx) => tx.wait()); + + await expect( + tokenRegistrar.tokenTransferFrom(tokenId, amount), + ) + .to.emit(token, 'Transfer') + .withArgs(wallet.address, tokenRegistrar.address, amount); + }); + + + it('Should approve some tokens from the registrar to the token manager', async () => { + const amount = 123456; + + await deployToken(); + + const params = defaultAbiCoder.encode(['bytes', 'address'], ['0x', token.address]); + + await expect(tokenRegistrar.registerCanonicalToken(token.address)) + .to.emit(service, 'TokenManagerDeployed') + .withArgs(tokenId, tokenManagerAddress, LOCK_UNLOCK, params); + + tokenManagerAddress = await service.validTokenManagerAddress(tokenId); + + await expect( + tokenRegistrar.tokenApprove(tokenId, amount), + ) + .to.emit(token, 'Approval') + .withArgs(tokenRegistrar.address, tokenManagerAddress, amount) + }); + it('Should transfer some tokens through the registrar as the deployer', async () => { + const amount = 123456; + const destinationAddress = '0x57689403'; + const gasValue = 45960; + await deployToken(); const params = defaultAbiCoder.encode(['bytes', 'address'], ['0x', token.address]); @@ -105,20 +150,27 @@ describe('Token Registrars', () => { .to.emit(service, 'TokenManagerDeployed') .withArgs(tokenId, tokenManagerAddress, LOCK_UNLOCK, params); + await token.approve(tokenRegistrar.address, amount).then((tx) => tx.wait()); + tokenManagerAddress = await service.validTokenManagerAddress(tokenId); - // TODO: fix test - // await token.approve(tokenRegistrar.address, amount).then((tx) => tx.wait()); - - // await expect( - // tokenRegistrar.interchainTransferFrom(tokenId, '', arrayify(wallet.address), amount, 0), - // ) - // // .to.emit(service, 'InterchainTransfer') - // // .withArgs(tokenId, destinationChain, destinationAddress, amount) - // .to.emit(token, 'Transfer') - // .withArgs(wallet.address, tokenRegistrar.address, amount) - // .to.emit(token, 'Transfer') - // .withArgs(tokenRegistrar.address, wallet.address, amount); + const txs = []; + + txs.push(await tokenRegistrar.populateTransaction.tokenTransferFrom(tokenId, amount)); + txs.push(await tokenRegistrar.populateTransaction.tokenApprove(tokenId, amount)); + txs.push(await tokenRegistrar.populateTransaction.interchainTransfer(tokenId, destinationChain, destinationAddress, amount, gasValue)); + + await expect( + tokenRegistrar.multicall(txs.map(tx => tx.data), {value: gasValue}), + ) + .to.emit(token, 'Transfer') + .withArgs(wallet.address, tokenRegistrar.address, amount) + .and.to.emit(token, 'Approval') + .withArgs(tokenRegistrar.address, tokenManagerAddress, amount) + .and.to.emit(token, 'Transfer') + .withArgs(tokenRegistrar.address, tokenManagerAddress, amount) + .and.to.emit(service, 'InterchainTransfer') + .withArgs(tokenId, destinationChain, destinationAddress, amount) }); }); From 5203931d403e1f34aebad5bb3bb9784e32c2c73d Mon Sep 17 00:00:00 2001 From: Foivos Date: Fri, 3 Nov 2023 16:04:14 +0200 Subject: [PATCH 2/2] made lint happy --- contracts/interfaces/ITokenRegistrar.sol | 10 +---- contracts/token-registrars/TokenRegistrar.sol | 10 +---- test/TokenRegistrars.js | 40 +++++++++++-------- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/contracts/interfaces/ITokenRegistrar.sol b/contracts/interfaces/ITokenRegistrar.sol index 90a4d907..c38b2bfd 100644 --- a/contracts/interfaces/ITokenRegistrar.sol +++ b/contracts/interfaces/ITokenRegistrar.sol @@ -57,13 +57,7 @@ interface ITokenRegistrar { uint256 gasValue ) external payable; - function tokenTransferFrom( - bytes32 tokenId, - uint256 amount - ) external payable; + function tokenTransferFrom(bytes32 tokenId, uint256 amount) external payable; - function tokenApprove( - bytes32 tokenId, - uint256 amount - ) external payable; + function tokenApprove(bytes32 tokenId, uint256 amount) external payable; } diff --git a/contracts/token-registrars/TokenRegistrar.sol b/contracts/token-registrars/TokenRegistrar.sol index bb56b9d3..93d7445b 100644 --- a/contracts/token-registrars/TokenRegistrar.sol +++ b/contracts/token-registrars/TokenRegistrar.sol @@ -214,20 +214,14 @@ contract TokenRegistrar is ITokenRegistrar, ITokenManagerType, Multicall, Upgrad } } - function tokenTransferFrom( - bytes32 tokenId, - uint256 amount - ) external payable { + function tokenTransferFrom(bytes32 tokenId, uint256 amount) external payable { address tokenAddress = service.tokenAddress(tokenId); IStandardizedToken token = IStandardizedToken(tokenAddress); token.safeTransferFrom(msg.sender, address(this), amount); } - function tokenApprove( - bytes32 tokenId, - uint256 amount - ) external payable { + function tokenApprove(bytes32 tokenId, uint256 amount) external payable { address tokenAddress = service.tokenAddress(tokenId); IStandardizedToken token = IStandardizedToken(tokenAddress); address tokenManager = service.tokenManagerAddress(tokenId); diff --git a/test/TokenRegistrars.js b/test/TokenRegistrars.js index 3d03286b..8d896e66 100644 --- a/test/TokenRegistrars.js +++ b/test/TokenRegistrars.js @@ -109,17 +109,14 @@ describe('Token Registrars', () => { await token.approve(tokenRegistrar.address, amount).then((tx) => tx.wait()); - await expect( - tokenRegistrar.tokenTransferFrom(tokenId, amount), - ) - .to.emit(token, 'Transfer') - .withArgs(wallet.address, tokenRegistrar.address, amount); + await expect(tokenRegistrar.tokenTransferFrom(tokenId, amount)) + .to.emit(token, 'Transfer') + .withArgs(wallet.address, tokenRegistrar.address, amount); }); - it('Should approve some tokens from the registrar to the token manager', async () => { const amount = 123456; - + await deployToken(); const params = defaultAbiCoder.encode(['bytes', 'address'], ['0x', token.address]); @@ -130,18 +127,16 @@ describe('Token Registrars', () => { tokenManagerAddress = await service.validTokenManagerAddress(tokenId); - await expect( - tokenRegistrar.tokenApprove(tokenId, amount), - ) - .to.emit(token, 'Approval') - .withArgs(tokenRegistrar.address, tokenManagerAddress, amount) + await expect(tokenRegistrar.tokenApprove(tokenId, amount)) + .to.emit(token, 'Approval') + .withArgs(tokenRegistrar.address, tokenManagerAddress, amount); }); it('Should transfer some tokens through the registrar as the deployer', async () => { const amount = 123456; const destinationAddress = '0x57689403'; const gasValue = 45960; - + await deployToken(); const params = defaultAbiCoder.encode(['bytes', 'address'], ['0x', token.address]); @@ -158,11 +153,22 @@ describe('Token Registrars', () => { txs.push(await tokenRegistrar.populateTransaction.tokenTransferFrom(tokenId, amount)); txs.push(await tokenRegistrar.populateTransaction.tokenApprove(tokenId, amount)); - txs.push(await tokenRegistrar.populateTransaction.interchainTransfer(tokenId, destinationChain, destinationAddress, amount, gasValue)); + txs.push( + await tokenRegistrar.populateTransaction.interchainTransfer( + tokenId, + destinationChain, + destinationAddress, + amount, + gasValue, + ), + ); await expect( - tokenRegistrar.multicall(txs.map(tx => tx.data), {value: gasValue}), - ) + tokenRegistrar.multicall( + txs.map((tx) => tx.data), + { value: gasValue }, + ), + ) .to.emit(token, 'Transfer') .withArgs(wallet.address, tokenRegistrar.address, amount) .and.to.emit(token, 'Approval') @@ -170,7 +176,7 @@ describe('Token Registrars', () => { .and.to.emit(token, 'Transfer') .withArgs(tokenRegistrar.address, tokenManagerAddress, amount) .and.to.emit(service, 'InterchainTransfer') - .withArgs(tokenId, destinationChain, destinationAddress, amount) + .withArgs(tokenId, destinationChain, destinationAddress, amount); }); });