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: try some different ways to reduce its code size #266

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
107 changes: 107 additions & 0 deletions contracts/InterchainTokenServiceCallContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { InterchainTokenServiceVirtual } from './InterchainTokenServiceVirtual.sol';
import { ICallContract } from './interfaces/ICallContract.sol';

/**
* @title The Interchain Token Service
* @notice This contract is responsible for facilitating interchain token transfers.
* It (mostly) does not handle tokens, but is responsible for the messaging that needs to occur for interchain transfers to happen.
* @dev The only storage used in this contract is for Express calls.
* Furthermore, no ether is intended to or should be sent to this contract except as part of deploy/interchainTransfer payable methods for gas payment.
*/
contract InterchainTokenServiceCallContract is InterchainTokenServiceVirtual {
error CallContractFailed(bytes returnData);

address public immutable callContract;

constructor(
address tokenManagerDeployer_,
address interchainTokenDeployer_,
address gateway_,
address gasService_,
address interchainTokenFactory_,
string memory chainName_,
address tokenManagerImplementation_,
address tokenHandler_,
address callContract_
)
InterchainTokenServiceVirtual(
tokenManagerDeployer_,
interchainTokenDeployer_,
gateway_,
gasService_,
interchainTokenFactory_,
chainName_,
tokenManagerImplementation_,
tokenHandler_
)
{
if (callContract_ == address(0)) revert ZeroAddress();

callContract = callContract_;
}

/**
* @notice Calls a contract on a specific destination chain with the given payload
* @param destinationChain The target chain where the contract will be called.
* @param payload The data payload for the transaction.
* @param gasValue The amount of gas to be paid for the transaction.
*/
function _callContract(
string calldata destinationChain,
bytes memory payload,
ICallContract.MetadataVersion metadataVersion,
uint256 gasValue
) internal override {
string memory destinationAddress = trustedAddress(destinationChain);
(bool success, bytes memory returnData) = callContract.delegatecall(
abi.encodeWithSelector(
ICallContract.callContract.selector,
destinationChain,
destinationAddress,
payload,
metadataVersion,
gasValue
)
);

if (!success) revert CallContractFailed(returnData);
}

/**
* @notice Calls a contract on a specific destination chain with the given payload and gateway token
* @param destinationChain The target chain where the contract will be called.
* @param payload The data payload for the transaction.
* @param symbol The gateway symbol of the token.
* @param amount The amount of token transfered.
* @param metadataVersion The metadata version
* @param gasValue The amount of gas to be paid for the transaction.
*/
function _callContractWithToken(
string calldata destinationChain,
bytes memory payload,
string memory symbol,
uint256 amount,
ICallContract.MetadataVersion metadataVersion,
uint256 gasValue
) internal override {
string memory destinationAddress = trustedAddress(destinationChain);
(bool success, bytes memory returnData) = callContract.delegatecall(
abi.encodeWithSelector(
ICallContract.callContractWithToken.selector,
destinationChain,
destinationAddress,
payload,
symbol,
amount,
metadataVersion,
gasValue
)
);

if (!success) revert CallContractFailed(returnData);
}
}
104 changes: 104 additions & 0 deletions contracts/InterchainTokenServiceImplementation.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { Ownable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/utils/Ownable.sol';
import { Pausable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/utils/Pausable.sol';
import { InterchainAddressTracker } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/utils/InterchainAddressTracker.sol';


import { Operator } from './utils/Operator.sol';
import { ITokenManager } from './interfaces/ITokenManager.sol';
import { IInterchainTokenServiceProxy } from './interfaces/IInterchainTokenServiceProxy.sol';
import { IInterchainTokenServiceImplementation } from './interfaces/IInterchainTokenServiceImplementation.sol';

Check warning on line 13 in contracts/InterchainTokenServiceImplementation.sol

View workflow job for this annotation

GitHub Actions / lint

imported name IInterchainTokenServiceImplementation is not used

contract InterchainTokenServiceImplementation is
Operator,
Ownable,
Pausable,
InterchainAddressTracker
{
/**
* @dev Since this is an implementation it does not need to have an owner in its storage.
*/
constructor() Ownable(address(this)) {}

/**
* @notice Getter function for the flow limit of an existing TokenManager with a given tokenId.
* @param tokenId The tokenId of the TokenManager.
* @return flowLimit_ The flow limit.
*/
function flowLimit(bytes32 tokenId) external view returns (uint256 flowLimit_) {
ITokenManager tokenManager_ = ITokenManager(IInterchainTokenServiceProxy(address(this)).validTokenManagerAddress(tokenId));
flowLimit_ = tokenManager_.flowLimit();
}

/**
* @notice Getter function for the flow out amount of an existing TokenManager with a given tokenId.
* @param tokenId The tokenId of the TokenManager.
* @return flowOutAmount_ The flow out amount.
*/
function flowOutAmount(bytes32 tokenId) external view returns (uint256 flowOutAmount_) {
ITokenManager tokenManager_ = ITokenManager(IInterchainTokenServiceProxy(address(this)).validTokenManagerAddress(tokenId));
flowOutAmount_ = tokenManager_.flowOutAmount();
}

/**
* @notice Getter function for the flow in amount of an existing TokenManager with a given tokenId.
* @param tokenId The tokenId of the TokenManager.
* @return flowInAmount_ The flow in amount.
*/
function flowInAmount(bytes32 tokenId) external view returns (uint256 flowInAmount_) {
ITokenManager tokenManager_ = ITokenManager(IInterchainTokenServiceProxy(address(this)).validTokenManagerAddress(tokenId));
flowInAmount_ = tokenManager_.flowInAmount();
}

/*************\
OWNER FUNCTIONS
\*************/

/**
* @notice Used to set a flow limit for a token manager that has the service as its operator.
* @param tokenIds An array of the tokenIds of the tokenManagers to set the flow limits of.
* @param flowLimits The flowLimits to set.
*/
function setFlowLimits(bytes32[] calldata tokenIds, uint256[] calldata flowLimits) external onlyRole(uint8(Roles.OPERATOR)) {
uint256 length = tokenIds.length;
if (length != flowLimits.length) revert LengthMismatch();

for (uint256 i; i < length; ++i) {
ITokenManager tokenManager_ = ITokenManager(IInterchainTokenServiceProxy(address(this)).validTokenManagerAddress(tokenIds[i]));
// slither-disable-next-line calls-loop
tokenManager_.setFlowLimit(flowLimits[i]);
}
}

/**
* @notice Used to set a trusted address for a chain.
* @param chain The chain to set the trusted address of.
* @param address_ The address to set as trusted.
*/
function setTrustedAddress(string memory chain, string memory address_) external onlyOwner {
_setTrustedAddress(chain, address_);
}

/**
* @notice Used to remove a trusted address for a chain.
* @param chain The chain to set the trusted address of.
*/
function removeTrustedAddress(string memory chain) external onlyOwner {
_removeTrustedAddress(chain);
}

/**
* @notice Allows the owner to pause/unpause the token service.
* @param paused Boolean value representing whether to pause or unpause.
*/
function setPauseStatus(bool paused) external onlyOwner {
if (paused) {
_pause();
} else {
_unpause();
}
}
}
Loading
Loading