Skip to content

Solidity API

TradeToken

This contract is based on OpenZeppelin's ERC20 implementation and includes security features like ownership and reentrancy protection.

_Implements an advanced and customizable ERC20 token with wrapping and unwrapping capabilities, wrap ratios and secure transfer handling.

The contract supports two types of tokens:

  • Mintable: New tokens can be created by the owner
  • Wrappable: Tokens that can be wrapped into other TradeToken

Security features:

  • Reentrancy protection on all state-modifying functions
  • Ownership controls for sensitive operations
  • Transfer acceptance checks for contract recipients
  • Twin contract verification for wrapping operations_

TradeTokenType

Defines the type of TradeToken

solidity
enum TradeTokenType {
  Mintable,
  Wrappable
}

tradeTokenType

solidity
enum TradeToken.TradeTokenType tradeTokenType

The type of this TradeToken instance

wrapRatios

solidity
mapping(address => uint256) wrapRatios

_ Mapping of TradeToken addresses to their wrap ratios_

attachments

solidity
mapping(string => address) attachments

Mapping to track IPFS Content Identifiers (CIDs) and their submitters Maps CID strings to the address who submitted them

AttachmentAdded

solidity
event AttachmentAdded(string cid, address submitter)

Emitted when a new attachment is added to the contract

Parameters

NameTypeDescription
cidstringIPFS Content Identifier of the attachment
submitteraddressAddress that submitted the attachment

WrapRatioAdded

solidity
event WrapRatioAdded(address childTradeTokenAddress, uint256 ratio)

Emitted when a new wrap ratio is added for a child Trade Token

Parameters

NameTypeDescription
childTradeTokenAddressaddressAddress of the child Trade Token
ratiouint256The wrap ratio scaled by 10^18

WrapRatioReceived

solidity
event WrapRatioReceived(address tradeTokenAddress, uint256 ratio)

Emitted when a wrap ratio is received from another Trade Token

Parameters

NameTypeDescription
tradeTokenAddressaddressAddress of the Trade Token setting the ratio
ratiouint256The received wrap ratio

TradeTokenMintingNotAllowed

solidity
error TradeTokenMintingNotAllowed()

TradeTokenUnauthorizedTradeTokenOwner

solidity
error TradeTokenUnauthorizedTradeTokenOwner()

TradeTokenInvalidTwinContract

solidity
error TradeTokenInvalidTwinContract(address target)

TradeTokenWrapRatioNotRegistered

solidity
error TradeTokenWrapRatioNotRegistered(address target)

TradeTokenWrapRatioAlreadyExists

solidity
error TradeTokenWrapRatioAlreadyExists(address target)

TradeTokenInsufficientBalanceForWrap

solidity
error TradeTokenInsufficientBalanceForWrap(uint256 available, uint256 required)

TradeTokenTransferFailed

solidity
error TradeTokenTransferFailed(address to, uint256 value)

TradeTokenAttachmentAlreadyAdded

solidity
error TradeTokenAttachmentAlreadyAdded(string cid)

TradeTokenNonZeroInitialSupplyForWrappable

solidity
error TradeTokenNonZeroInitialSupplyForWrappable()

InvalidCid

solidity
error InvalidCid()

constructor

solidity
constructor(address _initialOwner, string _tokenName, string _tokenSymbol, uint256 _initialSupply, enum TradeToken.TradeTokenType _tradeTokenType) public

Sets up the Trade Token with initial parameters

Parameters

NameTypeDescription
_initialOwneraddressAddress of the initial owner
_tokenNamestringName of the token
_tokenSymbolstringSymbol of the token
_initialSupplyuint256Initial supply to mint
_tradeTokenTypeenum TradeToken.TradeTokenTypeType of the Trade Token (Mintable or Wrappable)

mint

solidity
function mint(address _account, uint256 _value) external

Creates new tokens and assigns them to the specified account

Parameters

NameTypeDescription
_accountaddressAddress to receive the minted tokens
_valueuint256Amount of tokens to mint Requirements: - Caller must be the contract owner - Token must be of Mintable type

burn

solidity
function burn(uint256 _value) external

Burns sender's owned tokens

Parameters

NameTypeDescription
_valueuint256Amount of tokens to be burnt Requirements: - Caller must own atleast value amount of tokens

wrap

solidity
function wrap(uint256 _value, address _wrapperTradeToken, address _to) external

Wraps tokens into another TradeToken at the predefined ratio

Parameters

NameTypeDescription
_valueuint256Amount of tokens to wrap
_wrapperTradeTokenaddressAddress of the target TradeToken to wrap into
_toaddressAddress to receive the wrapped tokens Requirements: - Wrap ratio must be registered for the target TradeToken - Caller must have sufficient balance - Contract must not be reentrant

unwrap

solidity
function unwrap(uint256 _value, address _wrappedTradeToken, address _to) external

Unwraps tokens back to their original form

Parameters

NameTypeDescription
_valueuint256Amount of wrapped tokens to unwrap
_wrappedTradeTokenaddressAddress of the TradeToken containing the original tokens
_toaddressAddress to receive the unwrapped tokens Requirements: - Caller must be the contract owner - Wrap ratio must be registered - Contract must not be reentrant

onReceiveWrappedTokens

solidity
function onReceiveWrappedTokens(uint256 _value, address _to) external

Callback function for receiving wrapped tokens

Parameters

NameTypeDescription
_valueuint256Amount of tokens to mint
_toaddressAddress to receive the minted tokens Requirements: - Caller must be a verified twin contract - The call must not be reentrant

setWrapRatio

solidity
function setWrapRatio(address _childTradeTokenAddress, uint256 _ratio) external

Sets the wrap ratio for a child TradeToken

Parameters

NameTypeDescription
_childTradeTokenAddressaddressAddress of the child TradeToken
_ratiouint256Wrap ratio scaled by 10^18 Requirements: - Child TradeToken must be a verified twin contract - Caller must be the owner of the child TradeToken - Contract must not be reentrant - Wrap ratio must not already be set for the child TradeToken

getWrapRatio

solidity
function getWrapRatio(address _tradeTokenAddress) external view returns (uint256)

Returns the wrap ratio for a given TradeToken address

Parameters

NameTypeDescription
_tradeTokenAddressaddressAddress of the TradeToken to query

Return Values

NameTypeDescription
[0]uint256The wrap ratio scaled by 10^18

onReceiveWrapRatio

solidity
function onReceiveWrapRatio(uint256 _ratio) external

Callback function when receiving a wrap ratio from another TradeToken

Parameters

NameTypeDescription
_ratiouint256The received wrap ratio Requirements: - Caller must be another TradeToken contract with the same deployed bytecode

transfer

solidity
function transfer(address _to, uint256 _value) public virtual returns (bool)

Overrides ERC20 transfer function with additional security checks

Parameters

NameTypeDescription
_toaddressAddress to transfer tokens to
_valueuint256Amount of tokens to transfer

Return Values

NameTypeDescription
[0]boolbool indicating success Requirements: - Contract must not be reentrant - Recipient contract must properly implement onReceiveERC20 if it's a contract

transferFrom

solidity
function transferFrom(address _from, address _to, uint256 _value) public virtual returns (bool)

Overrides ERC20 transferFrom function with additional security checks

Parameters

NameTypeDescription
_fromaddressAddress to transfer tokens from
_toaddressAddress to transfer tokens to
_valueuint256Amount of tokens to transfer

Return Values

NameTypeDescription
[0]boolbool indicating success Requirements: - Contract must not be reentrant - Recipient contract must properly implement onReceiveERC20 if it's a contract

addAttachment

solidity
function addAttachment(string _cid) external

Adds a new IPFS attachment to the contract

Parameters

NameTypeDescription
_cidstringIPFS Content Identifier for the attachment Requirements: - Caller must be the contract owner - CID must not already be registered

_verifyTwinContractBytecode

solidity
function _verifyTwinContractBytecode(address _target) internal view

Verifies that the target contract has identical bytecode

Parameters

NameTypeDescription
_targetaddressAddress of the contract to verify

_extCodeHash

solidity
function _extCodeHash(address _account) private view

Retrieves the code hash of the contract deployed at the specified address.

Parameters

NameTypeDescription
_accountaddressAddress to query