Skip to content

Commit 92cbbf1

Browse files
ahramymilapsheth
andauthored
fix: preventing original chain name & destination chain name are the same (#277)
Co-authored-by: Milap Sheth <[email protected]>
1 parent b680103 commit 92cbbf1

8 files changed

+294
-64
lines changed

contracts/InterchainTokenFactory.sol

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ contract InterchainTokenFactory is IInterchainTokenFactory, ITokenManagerType, M
166166

167167
/**
168168
* @notice Deploys a remote interchain token on a specified destination chain.
169-
* @param originalChainName The name of the chain where the token originally exists.
170169
* @param salt The unique salt for deploying the token.
171170
* @param minter The address to receive the minter and operator role of the token, in addition to ITS. If the address is `address(0)`,
172171
* no additional minter is set on the token. Reverts if the minter does not have mint permission for the token.
@@ -175,46 +174,60 @@ contract InterchainTokenFactory is IInterchainTokenFactory, ITokenManagerType, M
175174
* @return tokenId The tokenId corresponding to the deployed InterchainToken.
176175
*/
177176
function deployRemoteInterchainToken(
178-
string calldata originalChainName,
179177
bytes32 salt,
180178
address minter,
181179
string memory destinationChain,
182180
uint256 gasValue
183-
) external payable returns (bytes32 tokenId) {
181+
) public payable returns (bytes32 tokenId) {
184182
string memory tokenName;
185183
string memory tokenSymbol;
186184
uint8 tokenDecimals;
187185
bytes memory minter_ = new bytes(0);
188186

189-
{
190-
bytes32 chainNameHash_;
191-
if (bytes(originalChainName).length == 0) {
192-
chainNameHash_ = chainNameHash;
193-
} else {
194-
chainNameHash_ = keccak256(bytes(originalChainName));
195-
}
187+
salt = interchainTokenSalt(chainNameHash, msg.sender, salt);
188+
tokenId = interchainTokenService.interchainTokenId(TOKEN_FACTORY_DEPLOYER, salt);
196189

197-
address sender = msg.sender;
198-
salt = interchainTokenSalt(chainNameHash_, sender, salt);
199-
tokenId = interchainTokenService.interchainTokenId(TOKEN_FACTORY_DEPLOYER, salt);
190+
IInterchainToken token = IInterchainToken(interchainTokenService.interchainTokenAddress(tokenId));
200191

201-
IInterchainToken token = IInterchainToken(interchainTokenService.interchainTokenAddress(tokenId));
202-
203-
tokenName = token.name();
204-
tokenSymbol = token.symbol();
205-
tokenDecimals = token.decimals();
192+
tokenName = token.name();
193+
tokenSymbol = token.symbol();
194+
tokenDecimals = token.decimals();
206195

207-
if (minter != address(0)) {
208-
if (!token.isMinter(minter)) revert NotMinter(minter);
209-
if (minter == address(interchainTokenService)) revert InvalidMinter(minter);
196+
if (minter != address(0)) {
197+
if (!token.isMinter(minter)) revert NotMinter(minter);
198+
if (minter == address(interchainTokenService)) revert InvalidMinter(minter);
210199

211-
minter_ = minter.toBytes();
212-
}
200+
minter_ = minter.toBytes();
213201
}
214202

215203
tokenId = _deployInterchainToken(salt, destinationChain, tokenName, tokenSymbol, tokenDecimals, minter_, gasValue);
216204
}
217205

206+
/**
207+
* @notice Deploys a remote interchain token on a specified destination chain.
208+
* This method is deprecated and will be removed in the future. Please use the above method instead.
209+
* @dev originalChainName is only allowed to be '', i.e the current chain.
210+
* Other source chains are not supported anymore to simplify ITS token deployment behaviour.
211+
* @param originalChainName The name of the chain where the token originally exists.
212+
* @param salt The unique salt for deploying the token.
213+
* @param minter The address to receive the minter and operator role of the token, in addition to ITS. If the address is `address(0)`,
214+
* no additional minter is set on the token. Reverts if the minter does not have mint permission for the token.
215+
* @param destinationChain The name of the destination chain.
216+
* @param gasValue The amount of gas to send for the deployment.
217+
* @return tokenId The tokenId corresponding to the deployed InterchainToken.
218+
*/
219+
function deployRemoteInterchainToken(
220+
string calldata originalChainName,
221+
bytes32 salt,
222+
address minter,
223+
string memory destinationChain,
224+
uint256 gasValue
225+
) external payable returns (bytes32 tokenId) {
226+
if (bytes(originalChainName).length != 0) revert NotSupported();
227+
228+
tokenId = deployRemoteInterchainToken(salt, minter, destinationChain, gasValue);
229+
}
230+
218231
/**
219232
* @notice Deploys a new interchain token with specified parameters.
220233
* @param salt The unique salt for deploying the token.
@@ -263,33 +276,23 @@ contract InterchainTokenFactory is IInterchainTokenFactory, ITokenManagerType, M
263276

264277
/**
265278
* @notice Deploys a canonical interchain token on a remote chain.
266-
* @param originalChain The name of the chain where the token originally exists.
267279
* @param originalTokenAddress The address of the original token on the original chain.
268280
* @param destinationChain The name of the chain where the token will be deployed.
269281
* @param gasValue The gas amount to be sent for deployment.
270282
* @return tokenId The tokenId corresponding to the deployed InterchainToken.
271283
*/
272284
function deployRemoteCanonicalInterchainToken(
273-
string calldata originalChain,
274285
address originalTokenAddress,
275286
string calldata destinationChain,
276287
uint256 gasValue
277-
) external payable returns (bytes32 tokenId) {
288+
) public payable returns (bytes32 tokenId) {
278289
bytes32 salt;
279290
IInterchainToken token;
280291

281-
{
282-
bytes32 chainNameHash_;
283-
if (bytes(originalChain).length == 0) {
284-
chainNameHash_ = chainNameHash;
285-
} else {
286-
chainNameHash_ = keccak256(bytes(originalChain));
287-
}
288-
// This ensures that the token manager has been deployed by this address, so it's safe to trust it.
289-
salt = canonicalInterchainTokenSalt(chainNameHash_, originalTokenAddress);
290-
tokenId = interchainTokenService.interchainTokenId(TOKEN_FACTORY_DEPLOYER, salt);
291-
token = IInterchainToken(interchainTokenService.validTokenAddress(tokenId));
292-
}
292+
// This ensures that the token manager has been deployed by this address, so it's safe to trust it.
293+
salt = canonicalInterchainTokenSalt(chainNameHash, originalTokenAddress);
294+
tokenId = interchainTokenService.interchainTokenId(TOKEN_FACTORY_DEPLOYER, salt);
295+
token = IInterchainToken(interchainTokenService.validTokenAddress(tokenId));
293296

294297
// The 3 lines below will revert if the token does not exist.
295298
string memory tokenName = token.name();
@@ -299,6 +302,28 @@ contract InterchainTokenFactory is IInterchainTokenFactory, ITokenManagerType, M
299302
tokenId = _deployInterchainToken(salt, destinationChain, tokenName, tokenSymbol, tokenDecimals, '', gasValue);
300303
}
301304

305+
/**
306+
* @notice Deploys a canonical interchain token on a remote chain.
307+
* This method is deprecated and will be removed in the future. Please use the above method instead.
308+
* @dev originalChain is only allowed to be '', i.e the current chain.
309+
* Other source chains are not supported anymore to simplify ITS token deployment behaviour.
310+
* @param originalChain The name of the chain where the token originally exists.
311+
* @param originalTokenAddress The address of the original token on the original chain.
312+
* @param destinationChain The name of the chain where the token will be deployed.
313+
* @param gasValue The gas amount to be sent for deployment.
314+
* @return tokenId The tokenId corresponding to the deployed InterchainToken.
315+
*/
316+
function deployRemoteCanonicalInterchainToken(
317+
string calldata originalChain,
318+
address originalTokenAddress,
319+
string calldata destinationChain,
320+
uint256 gasValue
321+
) external payable returns (bytes32 tokenId) {
322+
if (bytes(originalChain).length != 0) revert NotSupported();
323+
324+
tokenId = deployRemoteCanonicalInterchainToken(originalTokenAddress, destinationChain, gasValue);
325+
}
326+
302327
/**
303328
* @notice Register 'canonical' gateway tokens. The same salt needs to be used for the same gateway token on every chain.
304329
* @param tokenIdentifier A gateway token identifier to be used for the token registration. Should be the same for all chains.

contracts/InterchainTokenService.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ contract InterchainTokenService is
306306
if (bytes(destinationChain).length == 0) {
307307
_deployTokenManager(tokenId, tokenManagerType, params);
308308
} else {
309+
if (chainNameHash == keccak256(bytes(destinationChain))) revert CannotDeployRemotelyToSelf();
310+
309311
_deployRemoteTokenManager(tokenId, destinationChain, gasValue, tokenManagerType, params);
310312
}
311313
}
@@ -348,6 +350,8 @@ contract InterchainTokenService is
348350

349351
_deployTokenManager(tokenId, TokenManagerType.NATIVE_INTERCHAIN_TOKEN, abi.encode(minter, tokenAddress));
350352
} else {
353+
if (chainNameHash == keccak256(bytes(destinationChain))) revert CannotDeployRemotelyToSelf();
354+
351355
_deployRemoteInterchainToken(tokenId, name, symbol, decimals, minter, destinationChain, gasValue);
352356
}
353357
}

contracts/interfaces/IInterchainTokenFactory.sol

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface IInterchainTokenFactory is IUpgradable, IMulticall {
2020
error GatewayToken(address tokenAddress);
2121
error NotServiceOwner(address sender);
2222
error NotGatewayToken(string symbol);
23+
error NotSupported();
2324

2425
/**
2526
* @notice Returns the address of the interchain token service.
@@ -79,6 +80,23 @@ interface IInterchainTokenFactory is IUpgradable, IMulticall {
7980

8081
/**
8182
* @notice Deploys a remote interchain token on a specified destination chain.
83+
* @param salt The unique salt for deploying the token.
84+
* @param minter The address to distribute the token on the destination chain.
85+
* @param destinationChain The name of the destination chain.
86+
* @param gasValue The amount of gas to send for the deployment.
87+
* @return tokenId The tokenId corresponding to the deployed InterchainToken.
88+
*/
89+
function deployRemoteInterchainToken(
90+
bytes32 salt,
91+
address minter,
92+
string memory destinationChain,
93+
uint256 gasValue
94+
) external payable returns (bytes32 tokenId);
95+
96+
/**
97+
* @notice Deploys a remote interchain token on a specified destination chain.
98+
* @dev originalChainName is only allowed to be '', i.e the current chain.
99+
* Other source chains are not supported anymore to simplify ITS token deployment behaviour.
82100
* @param originalChainName The name of the chain where the token originally exists.
83101
* @param salt The unique salt for deploying the token.
84102
* @param minter The address to distribute the token on the destination chain.
@@ -118,6 +136,21 @@ interface IInterchainTokenFactory is IUpgradable, IMulticall {
118136

119137
/**
120138
* @notice Deploys a canonical interchain token on a remote chain.
139+
* @param originalTokenAddress The address of the original token on the original chain.
140+
* @param destinationChain The name of the chain where the token will be deployed.
141+
* @param gasValue The gas amount to be sent for deployment.
142+
* @return tokenId The tokenId corresponding to the deployed canonical InterchainToken.
143+
*/
144+
function deployRemoteCanonicalInterchainToken(
145+
address originalTokenAddress,
146+
string calldata destinationChain,
147+
uint256 gasValue
148+
) external payable returns (bytes32 tokenId);
149+
150+
/**
151+
* @notice Deploys a canonical interchain token on a remote chain.
152+
* @dev originalChain is only allowed to be '', i.e the current chain.
153+
* Other source chains are not supported anymore to simplify ITS token deployment behaviour.
121154
* @param originalChain The name of the chain where the token originally exists.
122155
* @param originalTokenAddress The address of the original token on the original chain.
123156
* @param destinationChain The name of the chain where the token will be deployed.

contracts/interfaces/IInterchainTokenService.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ interface IInterchainTokenService is
4848
error PostDeployFailed(bytes data);
4949
error ZeroAmount();
5050
error CannotDeploy(TokenManagerType);
51+
error CannotDeployRemotelyToSelf();
5152
error InvalidGatewayTokenTransfer(bytes32 tokenId, bytes payload, string tokenSymbol, uint256 amount);
5253
error InvalidPayload();
5354
error GatewayCallFailed(bytes data);

hardhat.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const itsCompilerSettings = {
4040
evmVersion: process.env.EVM_VERSION || 'london',
4141
optimizer: {
4242
...optimizerSettings,
43-
runs: 150, // Reduce runs to keep bytecode size under limit
43+
runs: 100, // Reduce runs to keep bytecode size under limit
4444
},
4545
},
4646
};

0 commit comments

Comments
 (0)