Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wrap payload #261

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
48f60e5
Added additional checks but code size is too large
Foivos Feb 6, 2024
fc8cb1a
Added value for contractCallWithToken and optimized contract size
Foivos Feb 7, 2024
ead719d
trying to fix tests
Foivos Feb 7, 2024
800ae3d
Update the create3address of ITS to use a custom bytecodehash.
Foivos Feb 9, 2024
cf016b0
prettier
Foivos Feb 12, 2024
ad53bc2
fix tests
Foivos Feb 12, 2024
7f610e7
Added a few tests
Foivos Feb 12, 2024
7a926fc
fixed one more test
Foivos Feb 12, 2024
83a396a
fixed all tests
Foivos Feb 13, 2024
c326d57
prettier
Foivos Feb 13, 2024
ca2bd91
made lint happy
Foivos Feb 13, 2024
1468d7e
working on slither
Foivos Feb 13, 2024
dccaf3c
made slither happy
Foivos Feb 14, 2024
927e1ce
prettier
Foivos Feb 14, 2024
1fc47a8
Using constant for the hash as well
Foivos Feb 14, 2024
f5823cc
addressed comments
Foivos Feb 14, 2024
c562c6a
added some tests
Foivos Feb 14, 2024
684fda9
Merge remote-tracking branch 'origin/main' into feat/additional-check…
Foivos Feb 15, 2024
f133cc9
added some coverage tests, found a bug too!
Foivos Feb 15, 2024
76a2545
a small style fix
Foivos Feb 16, 2024
74f09c6
fixed a bug
Foivos Feb 16, 2024
e896028
addressed some comments
Foivos Feb 20, 2024
b57cefc
prettier
Foivos Feb 20, 2024
5e311f2
fixed a test
Foivos Feb 20, 2024
f92443b
Merge branch 'main' into feat/additional-checks-for-execute-with-token
milapsheth Feb 21, 2024
0fcba29
remove modifier that should not exist
Foivos Feb 21, 2024
c5a5ba5
rename a function
Foivos Feb 21, 2024
dbc605d
Update contracts/InterchainTokenService.sol
Foivos Feb 21, 2024
b1c9266
reinteroduce the modifiers since they are needed after all
Foivos Feb 21, 2024
515be9c
Update contracts/utils/Create3AddressFixed.sol
milapsheth Feb 23, 2024
f787f13
add a docstring
Foivos Feb 23, 2024
10ed394
prettier and fixed tests
Foivos Mar 7, 2024
d7b0dd8
Merge remote-tracking branch 'origin/releases/1.2.x' into feat/merge-…
Foivos Mar 25, 2024
573569e
Merge remote-tracking branch 'origin/main' into feat/merge-new-token-…
Foivos Mar 25, 2024
ce3cf52
fixed tests
Foivos Mar 25, 2024
d804a7c
Merge branch 'main' into feat/merge-new-token-managers
Foivos Mar 26, 2024
52c75e6
Merge remote-tracking branch 'origin/main' into feat/additional-check…
Foivos Mar 26, 2024
acfbecb
added the changes
Foivos Mar 27, 2024
6b2fc97
Merge branch 'feat/additional-checks-for-execute-with-token' into fea…
Foivos Apr 11, 2024
a4a9a49
Added wrapping and unwrapping of payloads
Foivos Apr 11, 2024
f9734a0
Merge branch 'main' into feat/wrap-payload
Foivos Apr 15, 2024
e005136
Merge remote-tracking branch 'origin/main' into feat/wrap-payload
Foivos Apr 15, 2024
c394e13
fix merge
Foivos Apr 15, 2024
0b0e606
Merge branch 'feat/merge-new-token-managers' into feat/wrap-payload
Foivos Apr 15, 2024
5b0dd0f
fixed compilation
Foivos Apr 15, 2024
faddc28
Merge branch 'feat/reduce-codesize' into feat/wrap-payload
Foivos Apr 15, 2024
8abb2b2
fixed tests
Foivos Apr 15, 2024
f8c449a
Merge branch 'feat/reduce-codesize' into feat/wrap-payload
Foivos Apr 15, 2024
37ee0c4
optimize compilation runs
Foivos Apr 15, 2024
f18b722
make lint happy
Foivos Apr 15, 2024
2fe3777
Merge branch 'feat/reduce-codesize' into feat/wrap-payload
Foivos Apr 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions contracts/InterchainTokenFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ contract InterchainTokenFactory is IInterchainTokenFactory, ITokenManagerType, M
* @param chainNameHash_ The hash of the chain name.
* @param deployer The address of the deployer.
* @param salt A unique identifier to generate the salt.
* @return bytes32 The calculated salt for the interchain token.
* @return tokenSalt The calculated salt for the interchain token.
*/
function interchainTokenSalt(bytes32 chainNameHash_, address deployer, bytes32 salt) public pure returns (bytes32) {
return keccak256(abi.encode(PREFIX_INTERCHAIN_TOKEN_SALT, chainNameHash_, deployer, salt));
function interchainTokenSalt(bytes32 chainNameHash_, address deployer, bytes32 salt) public pure returns (bytes32 tokenSalt) {
tokenSalt = keccak256(abi.encode(PREFIX_INTERCHAIN_TOKEN_SALT, chainNameHash_, deployer, salt));
}

/**
Expand Down
182 changes: 137 additions & 45 deletions contracts/InterchainTokenService.sol

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion contracts/TokenHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ contract TokenHandler is ITokenHandler, ITokenManagerType, ReentrancyGuard {
address to,
uint256 amount
) external payable returns (uint256) {
/// @dev Track the flow amount being received via the message
ITokenManager(tokenManager).addFlowIn(amount);

if (tokenManagerType == uint256(TokenManagerType.MINT_BURN) || tokenManagerType == uint256(TokenManagerType.MINT_BURN_FROM)) {
_giveTokenMintBurn(tokenAddress, to, amount);
return amount;
Expand Down Expand Up @@ -86,7 +89,10 @@ contract TokenHandler is ITokenHandler, ITokenManagerType, ReentrancyGuard {
address tokenManager,
address from,
uint256 amount
) external payable returns (uint256) {
) external payable returns (uint256) {
/// @dev Track the flow amount being sent out as a message
ITokenManager(tokenManager).addFlowOut(amount);

if (tokenManagerType == uint256(TokenManagerType.MINT_BURN)) {
_takeTokenMintBurn(tokenAddress, from, amount);
return amount;
Expand Down
8 changes: 4 additions & 4 deletions contracts/interfaces/IInterchainTokenFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ interface IInterchainTokenFactory is IUpgradable, IMulticall {
* @param chainNameHash_ The hash of the chain name.
* @param deployer The address of the deployer.
* @param salt A unique identifier to generate the salt.
* @return bytes32 The calculated salt for the interchain token.
* @return tokenSalt The calculated salt for the interchain token.
*/
function interchainTokenSalt(bytes32 chainNameHash_, address deployer, bytes32 salt) external view returns (bytes32);
function interchainTokenSalt(bytes32 chainNameHash_, address deployer, bytes32 salt) external view returns (bytes32 tokenSalt);

/**
* @notice Computes the ID for an interchain token based on the deployer and a salt.
Expand Down Expand Up @@ -97,9 +97,9 @@ interface IInterchainTokenFactory is IUpgradable, IMulticall {
* @notice Calculates the salt for a canonical interchain token.
* @param chainNameHash_ The hash of the chain name.
* @param tokenAddress The address of the token.
* @return salt The calculated salt for the interchain token.
* @return tokenSalt The calculated salt for the interchain token.
*/
function canonicalInterchainTokenSalt(bytes32 chainNameHash_, address tokenAddress) external view returns (bytes32 salt);
function canonicalInterchainTokenSalt(bytes32 chainNameHash_, address tokenAddress) external view returns (bytes32 tokenSalt);

/**
* @notice Computes the ID for a canonical interchain token based on its address.
Expand Down
1 change: 1 addition & 0 deletions contracts/interfaces/IInterchainTokenService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface IInterchainTokenService is
error TokenHandlerFailed(bytes data);
error EmptyData();
error PostDeployFailed(bytes data);
error InvalidGatewayTokenTransfer(bytes32 tokenId, bytes payload, string tokenSymbol, uint256 amount);

event InterchainTransfer(
bytes32 indexed tokenId,
Expand Down
19 changes: 19 additions & 0 deletions contracts/test/utils/TestCreate3Fixed.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { Create3Fixed } from '../../utils/Create3Fixed.sol';

contract TestCreate3Fixed is Create3Fixed {
event Deployed(address addr);

function deploy(bytes memory code, bytes32 salt) public payable returns (address addr) {
addr = _create3(code, salt);

emit Deployed(addr);
}

function deployedAddress(bytes32 salt) public view returns (address addr) {
addr = _create3Address(salt);
}
}
29 changes: 29 additions & 0 deletions contracts/utils/Create3AddressFixed.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
* @title Create3AddressFixed contract
* @notice This contract can be used to predict the deterministic deployment address of a contract deployed with the `CREATE3` technique.
* It is equivalent to the Create3Address found in axelar-gmp-sdk-solidity repo but uses a fixed bytecode for CreateDeploy,
* which allows changing compilation options (like number of runs) without affecting the future deployment addresses.
*/
contract Create3AddressFixed {
// slither-disable-next-line too-many-digits
bytes internal constant CREATE_DEPLOY_BYTECODE =
hex'608060405234801561001057600080fd5b50610162806100206000396000f3fe60806040526004361061001d5760003560e01c806277436014610022575b600080fd5b61003561003036600461007b565b610037565b005b8051602082016000f061004957600080fd5b50565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b60006020828403121561008d57600080fd5b813567ffffffffffffffff808211156100a557600080fd5b818401915084601f8301126100b957600080fd5b8135818111156100cb576100cb61004c565b604051601f8201601f19908116603f011681019083821181831017156100f3576100f361004c565b8160405282815287602084870101111561010c57600080fd5b82602086016020830137600092810160200192909252509594505050505056fea264697066735822122094780ce55d28f1d568f4e0ab1b9dc230b96e952b73d2e06456fbff2289fa27f464736f6c63430008150033';
bytes32 internal constant CREATE_DEPLOY_BYTECODE_HASH = keccak256(CREATE_DEPLOY_BYTECODE);

/**
* @notice Compute the deployed address that will result from the `CREATE3` method.
* @param deploySalt A salt to influence the contract address
* @return deployed The deterministic contract address if it was deployed
*/
function _create3Address(bytes32 deploySalt) internal view returns (address deployed) {
address deployer = address(
uint160(uint256(keccak256(abi.encodePacked(hex'ff', address(this), deploySalt, CREATE_DEPLOY_BYTECODE_HASH))))
);

deployed = address(uint160(uint256(keccak256(abi.encodePacked(hex'd6_94', deployer, hex'01')))));
}
}
46 changes: 46 additions & 0 deletions contracts/utils/Create3Fixed.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { IDeploy } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IDeploy.sol';
import { ContractAddress } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/libs/ContractAddress.sol';
import { CreateDeploy } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/deploy/CreateDeploy.sol';
import { Create3AddressFixed } from './Create3AddressFixed.sol';

/**
* @title Create3Fixed contract
* @notice This contract can be used to deploy a contract with a deterministic address that depends only on
* the deployer address and deployment salt, not the contract bytecode and constructor parameters.
* It uses a fixed bytecode to allow changing the compilation settings without affecting the deployment address in the future.
*/
contract Create3Fixed is Create3AddressFixed, IDeploy {
using ContractAddress for address;

/**
* @notice Deploys a new contract using the `CREATE3` method.
* @dev This function first deploys the CreateDeploy contract using
* the `CREATE2` opcode and then utilizes the CreateDeploy to deploy the
* new contract with the `CREATE` opcode.
* @param bytecode The bytecode of the contract to be deployed
* @param deploySalt A salt to influence the contract address
* @return deployed The address of the deployed contract
*/
function _create3(bytes memory bytecode, bytes32 deploySalt) internal returns (address deployed) {
deployed = _create3Address(deploySalt);

if (bytecode.length == 0) revert EmptyBytecode();
if (deployed.isContract()) revert AlreadyDeployed();

// Deploy using create2
CreateDeploy createDeploy;
bytes memory createDeployBytecode_ = CREATE_DEPLOY_BYTECODE;
uint256 length = createDeployBytecode_.length;
assembly {
createDeploy := create2(0, add(createDeployBytecode_, 0x20), length, deploySalt)
}

if (address(createDeploy) == address(0)) revert DeployFailed();
// Deploy using create
createDeploy.deploy(bytecode);
}
}
4 changes: 2 additions & 2 deletions contracts/utils/InterchainTokenDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.0;

import { Create3 } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/deploy/Create3.sol';
import { Create3Fixed } from './Create3Fixed.sol';

import { IInterchainTokenDeployer } from '../interfaces/IInterchainTokenDeployer.sol';
import { IInterchainToken } from '../interfaces/IInterchainToken.sol';
Expand All @@ -11,7 +11,7 @@ import { IInterchainToken } from '../interfaces/IInterchainToken.sol';
* @title InterchainTokenDeployer
* @notice This contract is used to deploy new instances of the InterchainTokenProxy contract.
*/
contract InterchainTokenDeployer is IInterchainTokenDeployer, Create3 {
contract InterchainTokenDeployer is IInterchainTokenDeployer, Create3Fixed {
address public immutable implementationAddress;

/**
Expand Down
4 changes: 2 additions & 2 deletions contracts/utils/TokenManagerDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.0;

import { Create3 } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/deploy/Create3.sol';
import { Create3Fixed } from './Create3Fixed.sol';

import { ITokenManagerDeployer } from '../interfaces/ITokenManagerDeployer.sol';

Expand All @@ -12,7 +12,7 @@ import { TokenManagerProxy } from '../proxies/TokenManagerProxy.sol';
* @title TokenManagerDeployer
* @notice This contract is used to deploy new instances of the TokenManagerProxy contract.
*/
contract TokenManagerDeployer is ITokenManagerDeployer, Create3 {
contract TokenManagerDeployer is ITokenManagerDeployer, Create3Fixed {
/**
* @notice Deploys a new instance of the TokenManagerProxy contract
* @param tokenId The unique identifier for the token
Expand Down
12 changes: 12 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ const compilerSettings = {
optimizer: optimizerSettings,
},
};
const itsCompilerSettings = {
version: '0.8.21',
settings: {
evmVersion: process.env.EVM_VERSION || 'london',
optimizer: {
...optimizerSettings,
runs: 1, // Reduce runs to keep bytecode size under limit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

},
},
};

/**
* @type import('hardhat/config').HardhatUserConfig
Expand All @@ -45,6 +55,8 @@ module.exports = {
overrides: {
'contracts/proxies/Proxy.sol': compilerSettings,
'contracts/proxies/TokenManagerProxy.sol': compilerSettings,
'contracts/InterchainTokenService.sol': itsCompilerSettings,
'contracts/test/TestInterchainTokenService.sol': itsCompilerSettings,
},
},
defaultNetwork: 'hardhat',
Expand Down
Loading
Loading