Skip to content

Commit

Permalink
feat: move views out of serivce (#98)
Browse files Browse the repository at this point in the history
* renamed folder and changed version

* npmignore

* npmignore

* change version

* using include pattern instead.

* Fixed most of the things least auhority suggested.

* made lint happy

* Apply suggestions from code review

* fixed some bugs

* added events

* rename set to transfer for distributor and operator

* changed standardized token to always allow token managers to mint/burn it.

* using immutable storage for remoteAddressValidator address to save gas

* Added some recommended changes

* added milap's suggested changes

* Fixed some names and some minor gas optimizations

* prettier and lint

* stash

* import .env in hardhat.config

* trying to fix .env.example

* Added some getters in IRemoteAddressValidator and removed useless check for distributor in the InterchainTokenService.

* removed ternary operators

* made lint happy

* made lint happy

* Added a new token manager to handle fee on transfer and added some tests for it as well

* fixed the liquidity pool check.

* fix a duplication bug

* lint

* added some more tests

* Added more tests

* Added proper re-entrancy protection for fee on transfer token managers.

* change to tx.origin for refunds

* Added support for more kinds of addresses.

* some minor gas opts

* some more gas optimizations.

* Added a getter for chain name to the remote address validator.

* moved the tokenManager getter functionality to a separate contract which saves almost a kilobyte of codesize.

* made lint happy

* Removed tokenManagerGetter and put params into tokenManagers

* Added separate tokenManager interfaces

* fix(RemoteAddressValidator): merge conflicts

---------

Co-authored-by: Milap Sheth <[email protected]>
Co-authored-by: Kiryl Yermakou <[email protected]>
  • Loading branch information
3 people authored Sep 29, 2023
1 parent 950fc9e commit dc5dc8f
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 98 deletions.
45 changes: 3 additions & 42 deletions contracts/interchain-token-service/InterchainTokenService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ contract InterchainTokenService is
IRemoteAddressValidator public immutable remoteAddressValidator;
address public immutable tokenManagerDeployer;
address public immutable standardizedTokenDeployer;
bytes32 internal immutable chainNameHash;
bytes32 internal immutable chainName;
bytes32 public immutable chainNameHash;

bytes32 internal constant PREFIX_CUSTOM_TOKEN_ID = keccak256('its-custom-token-id');
bytes32 internal constant PREFIX_STANDARDIZED_TOKEN_ID = keccak256('its-standardized-token-id');
Expand All @@ -78,16 +77,14 @@ contract InterchainTokenService is
* @param gasService_ the address of the AxelarGasService.
* @param remoteAddressValidator_ the address of the RemoteAddressValidator.
* @param tokenManagerImplementations this need to have exactly 3 implementations in the following order: Lock/Unlock, mint/burn and then liquidity pool.
* @param chainName_ the name of the current chain.
*/
constructor(
address tokenManagerDeployer_,
address standardizedTokenDeployer_,
address gateway_,
address gasService_,
address remoteAddressValidator_,
address[] memory tokenManagerImplementations,
string memory chainName_
address[] memory tokenManagerImplementations
) AxelarExecutable(gateway_) {
if (
remoteAddressValidator_ == address(0) ||
Expand All @@ -109,8 +106,7 @@ contract InterchainTokenService is
TokenManagerType.LOCK_UNLOCK_FEE_ON_TRANSFER
);
implementationLiquidityPool = _sanitizeTokenManagerImplementation(tokenManagerImplementations, TokenManagerType.LIQUIDITY_POOL);

chainName = chainName_.toBytes32();
string memory chainName_ = remoteAddressValidator.chainName();
chainNameHash = keccak256(bytes(chainName_));
}

Expand Down Expand Up @@ -229,41 +225,6 @@ contract InterchainTokenService is
}
}

/**
* @notice Getter function for the parameters of a lock/unlock TokenManager. Mainly to be used by frontends.
* @param operator the operator of the TokenManager.
* @param tokenAddress the token to be managed.
* @return params the resulting params to be passed to custom TokenManager deployments.
*/
function getParamsLockUnlock(bytes memory operator, address tokenAddress) public pure returns (bytes memory params) {
params = abi.encode(operator, tokenAddress);
}

/**
* @notice Getter function for the parameters of a mint/burn TokenManager. Mainly to be used by frontends.
* @param operator the operator of the TokenManager.
* @param tokenAddress the token to be managed.
* @return params the resulting params to be passed to custom TokenManager deployments.
*/
function getParamsMintBurn(bytes memory operator, address tokenAddress) public pure returns (bytes memory params) {
params = abi.encode(operator, tokenAddress);
}

/**
* @notice Getter function for the parameters of a liquidity pool TokenManager. Mainly to be used by frontends.
* @param operator the operator of the TokenManager.
* @param tokenAddress the token to be managed.
* @param liquidityPoolAddress the liquidity pool to be used to store the bridged tokens.
* @return params the resulting params to be passed to custom TokenManager deployments.
*/
function getParamsLiquidityPool(
bytes memory operator,
address tokenAddress,
address liquidityPoolAddress
) public pure returns (bytes memory params) {
params = abi.encode(operator, tokenAddress, liquidityPoolAddress);
}

/**
* @notice Getter function for the flow limit of an existing token manager with a give token ID.
* @param tokenId the token ID of the TokenManager.
Expand Down
29 changes: 0 additions & 29 deletions contracts/interfaces/IInterchainTokenService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,35 +133,6 @@ interface IInterchainTokenService is ITokenManagerType, IExpressCallHandler, IAx
*/
function getCustomTokenId(address operator, bytes32 salt) external view returns (bytes32 tokenId);

/**
* @notice Returns the parameters for the lock/unlock operation.
* @param operator The operator address.
* @param tokenAddress The address of the token.
* @return params The parameters for the lock/unlock operation.
*/
function getParamsLockUnlock(bytes memory operator, address tokenAddress) external pure returns (bytes memory params);

/**
* @notice Returns the parameters for the mint/burn operation.
* @param operator The operator address.
* @param tokenAddress The address of the token.
* @return params The parameters for the mint/burn operation.
*/
function getParamsMintBurn(bytes memory operator, address tokenAddress) external pure returns (bytes memory params);

/**
* @notice Returns the parameters for the liquidity pool operation.
* @param operator The operator address.
* @param tokenAddress The address of the token.
* @param liquidityPoolAddress The address of the liquidity pool.
* @return params The parameters for the liquidity pool operation.
*/
function getParamsLiquidityPool(
bytes memory operator,
address tokenAddress,
address liquidityPoolAddress
) external pure returns (bytes memory params);

/**
* @notice Registers a canonical token and returns its associated tokenId.
* @param tokenAddress The address of the canonical token.
Expand Down
5 changes: 5 additions & 0 deletions contracts/interfaces/IRemoteAddressValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ interface IRemoteAddressValidator {
event GatewaySupportedChainAdded(string chain);
event GatewaySupportedChainRemoved(string chain);

/**
* @notice Returns the interchain token address
*/
function chainName() external view returns (string memory);

/**
* @notice Returns the interchain token address
*/
Expand Down
31 changes: 31 additions & 0 deletions contracts/interfaces/ITokenManagerLiquidityPool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { ITokenManager } from './ITokenManager.sol';

/**
* @title ITokenManager
* @notice This contract is responsible for handling tokens before initiating a cross chain token transfer, or after receiving one.
*/
interface ITokenManagerLiquidityPool is ITokenManager {
/**
* @notice Getter function for the parameters of a lock/unlock TokenManager. Mainly to be used by frontends.
* @param operator the operator of the TokenManager.
* @param tokenAddress the token to be managed.
* @return params the resulting params to be passed to custom TokenManager deployments.
*/
function getParams(bytes memory operator, address tokenAddress, address liquidityPool) external pure returns (bytes memory params);

/**
* @dev Reads the stored liquidity pool address from the specified storage slot
* @return liquidityPool_ The address of the liquidity pool
*/
function liquidityPool() external view returns (address liquidityPool_);

/**
* @dev Updates the address of the liquidity pool. Can only be called by the operator.
* @param newLiquidityPool The new address of the liquidity pool
*/
function setLiquidityPool(address newLiquidityPool) external;
}
19 changes: 19 additions & 0 deletions contracts/interfaces/ITokenManagerLockUnlock.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 { ITokenManager } from './ITokenManager.sol';

/**
* @title ITokenManager
* @notice This contract is responsible for handling tokens before initiating a cross chain token transfer, or after receiving one.
*/
interface ITokenManagerLockUnlock is ITokenManager {
/**
* @notice Getter function for the parameters of a lock/unlock TokenManager. Mainly to be used by frontends.
* @param operator the operator of the TokenManager.
* @param tokenAddress the token to be managed.
* @return params the resulting params to be passed to custom TokenManager deployments.
*/
function getParams(bytes memory operator, address tokenAddress) external pure returns (bytes memory params);
}
19 changes: 19 additions & 0 deletions contracts/interfaces/ITokenManagerLockUnlockFee.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 { ITokenManager } from './ITokenManager.sol';

/**
* @title ITokenManager
* @notice This contract is responsible for handling tokens before initiating a cross chain token transfer, or after receiving one.
*/
interface ITokenManagerLockUnlockFee is ITokenManager {
/**
* @notice Getter function for the parameters of a lock/unlock TokenManager. Mainly to be used by frontends.
* @param operator the operator of the TokenManager.
* @param tokenAddress the token to be managed.
* @return params the resulting params to be passed to custom TokenManager deployments.
*/
function getParams(bytes memory operator, address tokenAddress) external pure returns (bytes memory params);
}
19 changes: 19 additions & 0 deletions contracts/interfaces/ITokenManagerMintBurn.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 { ITokenManager } from './ITokenManager.sol';

/**
* @title ITokenManager
* @notice This contract is responsible for handling tokens before initiating a cross chain token transfer, or after receiving one.
*/
interface ITokenManagerMintBurn is ITokenManager {
/**
* @notice Getter function for the parameters of a lock/unlock TokenManager. Mainly to be used by frontends.
* @param operator the operator of the TokenManager.
* @param tokenAddress the token to be managed.
* @return params the resulting params to be passed to custom TokenManager deployments.
*/
function getParams(bytes memory operator, address tokenAddress) external pure returns (bytes memory params);
}
6 changes: 3 additions & 3 deletions contracts/proxies/TokenManagerProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ITokenManagerProxy } from '../interfaces/ITokenManagerProxy.sol';
* @dev It implements ITokenManagerProxy.
*/
contract TokenManagerProxy is ITokenManagerProxy {
IInterchainTokenService public immutable interchainTokenServiceAddress;
IInterchainTokenService public immutable interchainTokenService;
uint256 public immutable implementationType;
bytes32 public immutable tokenId;

Expand All @@ -23,7 +23,7 @@ contract TokenManagerProxy is ITokenManagerProxy {
* @param params The initialization parameters for the token manager contract
*/
constructor(address interchainTokenServiceAddress_, uint256 implementationType_, bytes32 tokenId_, bytes memory params) {
interchainTokenServiceAddress = IInterchainTokenService(interchainTokenServiceAddress_);
interchainTokenService = IInterchainTokenService(interchainTokenServiceAddress_);
implementationType = implementationType_;
tokenId = tokenId_;
address impl = _getImplementation(IInterchainTokenService(interchainTokenServiceAddress_), implementationType_);
Expand All @@ -37,7 +37,7 @@ contract TokenManagerProxy is ITokenManagerProxy {
* @return impl The address of the current implementation
*/
function implementation() public view returns (address impl) {
impl = _getImplementation(interchainTokenServiceAddress, implementationType);
impl = _getImplementation(interchainTokenService, implementationType);
}

/**
Expand Down
28 changes: 16 additions & 12 deletions contracts/remote-address-validator/RemoteAddressValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ contract RemoteAddressValidator is IRemoteAddressValidator, Upgradable {
mapping(string => bytes32) public remoteAddressHashes;
mapping(string => string) public remoteAddresses;
mapping(string => bool) public supportedByGateway;
string public chainName;

address public immutable interchainTokenServiceAddress;
bytes32 public immutable interchainTokenServiceAddressHash;
Expand All @@ -32,7 +33,7 @@ contract RemoteAddressValidator is IRemoteAddressValidator, Upgradable {
* @dev Constructs the RemoteAddressValidator contract, both array parameters must be equal in length
* @param _interchainTokenServiceAddress Address of the interchain token service
*/
constructor(address _interchainTokenServiceAddress) {
constructor(address _interchainTokenServiceAddress, string memory chainName_) {
if (_interchainTokenServiceAddress == address(0)) revert ZeroAddress();

interchainTokenServiceAddress = _interchainTokenServiceAddress;
Expand All @@ -50,6 +51,9 @@ contract RemoteAddressValidator is IRemoteAddressValidator, Upgradable {

interchainTokenServiceAddress1 = p1;
interchainTokenServiceAddress2 = p2;

if (bytes(chainName_).length == 0) revert ZeroStringLength();
chainName = chainName_;
}

/**
Expand Down Expand Up @@ -152,12 +156,12 @@ contract RemoteAddressValidator is IRemoteAddressValidator, Upgradable {
*/
function addGatewaySupportedChains(string[] calldata chainNames) external onlyOwner {
uint256 length = chainNames.length;
string calldata chainName;
string calldata chainName_;
for (uint256 i; i < length; ++i) {
chainName = chainNames[i];
supportedByGateway[chainName] = true;
chainName_ = chainNames[i];
supportedByGateway[chainName_] = true;

emit GatewaySupportedChainAdded(chainName);
emit GatewaySupportedChainAdded(chainName_);
}
}

Expand All @@ -167,23 +171,23 @@ contract RemoteAddressValidator is IRemoteAddressValidator, Upgradable {
*/
function removeGatewaySupportedChains(string[] calldata chainNames) external onlyOwner {
uint256 length = chainNames.length;
string calldata chainName;
string calldata chainName_;

for (uint256 i; i < length; ++i) {
chainName = chainNames[i];
supportedByGateway[chainName] = false;
chainName_ = chainNames[i];
supportedByGateway[chainName_] = false;

emit GatewaySupportedChainRemoved(chainName);
emit GatewaySupportedChainRemoved(chainName_);
}
}

/**
* @dev Fetches the interchain token service address for the specified chain
* @param chainName Name of the chain
* @param chainName_ Name of the chain
* @return remoteAddress Interchain token service address for the specified chain
*/
function getRemoteAddress(string calldata chainName) external view returns (string memory remoteAddress) {
remoteAddress = remoteAddresses[chainName];
function getRemoteAddress(string calldata chainName_) external view returns (string memory remoteAddress) {
remoteAddress = remoteAddresses[chainName_];

if (bytes(remoteAddress).length == 0) {
remoteAddress = _interchainTokenServiceAddressString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity ^0.8.0;
import { TokenManagerAddressStorage } from './TokenManagerAddressStorage.sol';
import { NoReEntrancy } from '../../utils/NoReEntrancy.sol';
import { IERC20 } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IERC20.sol';
import { ITokenManagerLiquidityPool } from '../../interfaces/ITokenManagerLiquidityPool.sol';

import { SafeTokenTransferFrom } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/utils/SafeTransfer.sol';

Expand Down Expand Up @@ -100,4 +101,19 @@ contract TokenManagerLiquidityPool is TokenManagerAddressStorage, NoReEntrancy {

return IERC20(token).balanceOf(to) - balance;
}

/**
* @notice Getter function for the parameters of a liquidity pool TokenManager. Mainly to be used by frontends.
* @param operator the operator of the TokenManager.
* @param tokenAddress the token to be managed.
* @param liquidityPoolAddress the liquidity pool to be used to store the bridged tokens.
* @return params the resulting params to be passed to custom TokenManager deployments.
*/
function getParams(
bytes memory operator,
address tokenAddress,
address liquidityPoolAddress
) external pure returns (bytes memory params) {
params = abi.encode(operator, tokenAddress, liquidityPoolAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.0;

import { TokenManagerAddressStorage } from './TokenManagerAddressStorage.sol';
import { IERC20 } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IERC20.sol';
import { ITokenManagerLockUnlock } from '../../interfaces/ITokenManagerLockUnlock.sol';

import { SafeTokenTransferFrom, SafeTokenTransfer } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/utils/SafeTransfer.sol';

Expand All @@ -13,7 +14,7 @@ import { SafeTokenTransferFrom, SafeTokenTransfer } from '@axelar-network/axelar
* @dev This contract extends TokenManagerAddressStorage and provides implementation for its abstract methods.
* It uses the Axelar SDK to safely transfer tokens.
*/
contract TokenManagerLockUnlock is TokenManagerAddressStorage {
contract TokenManagerLockUnlock is TokenManagerAddressStorage, ITokenManagerLockUnlock {
/**
* @dev Constructs an instance of TokenManagerLockUnlock. Calls the constructor
* of TokenManagerAddressStorage which calls the constructor of TokenManager.
Expand Down Expand Up @@ -62,4 +63,14 @@ contract TokenManagerLockUnlock is TokenManagerAddressStorage {

return amount;
}

/**
* @notice Getter function for the parameters of a lock/unlock TokenManager. Mainly to be used by frontends.
* @param operator the operator of the TokenManager.
* @param tokenAddress the token to be managed.
* @return params the resulting params to be passed to custom TokenManager deployments.
*/
function getParams(bytes memory operator, address tokenAddress) external pure returns (bytes memory params) {
params = abi.encode(operator, tokenAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ contract TokenManagerLockUnlockFee is TokenManagerAddressStorage, NoReEntrancy {

return IERC20(token).balanceOf(to) - balance;
}

/**
* @notice Getter function for the parameters of a lock/unlock TokenManager. Mainly to be used by frontends.
* @param operator the operator of the TokenManager.
* @param tokenAddress the token to be managed.
* @return params the resulting params to be passed to custom TokenManager deployments.
*/
function getParams(bytes memory operator, address tokenAddress) external pure returns (bytes memory params) {
params = abi.encode(operator, tokenAddress);
}
}
Loading

0 comments on commit dc5dc8f

Please sign in to comment.