spec
Subswap API Documentation
This document provides an API reference for the Subswap contracts, specifically for the ParentnetTreasury
and SubnetTreasury
contracts. These contracts facilitate cross-chain asset transfers by minting, burning, locking, and unlocking tokens between chains.
Restricted Access Functions
ParentnetTreasury
changeEndpoint(address endpoint) -> void
- Description: Allows the contract owner to set a new endpoint address.
- Parameters:
endpoint
: The address of the new endpoint.
- Access:
onlyOwner
setEndpoint(address endpoint) -> void
- Description: Sets a new endpoint address, restricted to calls from the current endpoint.
- Parameters:
endpoint
: The address of the new endpoint.
- Access:
onlyEndpoint
mint(...) -> void
- Description: Mints tokens on the
SubnetTreasury
chain in response to a cross-chain transfer. - Parameters:
originalToken
: Address of the original token.name
: Name of the token.symbol
: Symbol of the token.account
: Address of the account receiving the minted tokens.amount
: Number of tokens to mint.sid
: Source chain ID.
- Access:
onlyEndpoint
- Description: Mints tokens on the
SubnetTreasury
changeEndpoint(address endpoint) -> void
- Description: Allows the contract owner to set a new endpoint address.
- Parameters:
endpoint
: The address of the new endpoint.
- Access:
onlyOwner
setEndpoint(address endpoint) -> void
- Description: Sets a new endpoint address, restricted to calls from the current endpoint.
- Parameters:
endpoint
: The address of the new endpoint.
- Access:
onlyEndpoint
unlock(address token, uint256 amount, address recv) -> void
- Description: Unlocks tokens on the current chain, sending them to the specified address.
- Parameters:
token
: Address of the token to unlock.amount
: Amount of tokens to unlock.recv
: Address of the recipient.
- Access:
onlyEndpoint
Public Functions
ParentnetTreasury
burn(...) -> void
- Description: Burns tokens on the
ParentnetTreasury
side to initiate a cross-chain transfer, sending a message toSubnetTreasury
to mint tokens. - Parameters:
rid
: Destination chain ID.rua
: Receiver’s address on the destination chain.originalToken
: Address of the original token on the source chain.token
: Address of the Treasury token to burn.amount
: Number of tokens to burn.recv
: Address to receive tokens on the destination chain.
- Events:
- Emits a
Burn
event with details of the burned amount and target chain.
- Emits a
- Description: Burns tokens on the
test(uint256 rid, address rua, bytes memory data) -> void
- Description: Sends arbitrary data to the specified chain via the endpoint, for testing purposes.
- Parameters:
rid
: Destination chain ID.rua
: Receiver’s address on the destination chain.data
: Encoded data to send.
getEndpoint() -> address
- Description: Returns the current endpoint address.
SubnetTreasury
lock(...) -> void
- Description: Locks tokens on the
SubnetTreasury
side to initiate a cross-chain transfer, sending a message toParentnetTreasury
to mint tokens. - Parameters:
rid
: Destination chain ID.rua
: Receiver’s address on the destination chain.token
: Address of the token to lock.amount
: Amount of tokens to lock.recv
: Address to receive tokens on the destination chain.
- Events:
- Emits a
Lock
event with details of the locked amount and target chain.
- Emits a
- Description: Locks tokens on the
getChainId() -> uint256
- Description: Returns the chain ID of the current chain.
getEndpoint() -> address
- Description: Returns the current endpoint address.
Algorithms and Rules
Minting and Burning
Minting (ParentnetTreasury)
- When
SubnetTreasury
locks tokens on its chain, it sends a message toParentnetTreasury
to mint an equivalent amount on the destination chain. - If a Treasury token contract does not exist for the original token, a new one is created and mapped to the original token in
treasuryMapping
.
- When
Burning (ParentnetTreasury)
- To initiate a cross-chain transfer back to the original chain, the
burn
function is called to destroy tokens onParentnetTreasury
. - After burning, a message is sent to
SubnetTreasury
to unlock an equivalent amount on the destination chain.
- To initiate a cross-chain transfer back to the original chain, the
Locking and Unlocking
Locking (SubnetTreasury)
- Tokens are locked on
SubnetTreasury
by transferring them from the caller’s address to the contract. - The contract then sends a cross-chain message to
ParentnetTreasury
to mint equivalent tokens on the destination chain.
- Tokens are locked on
Unlocking (SubnetTreasury)
- In response to a burn action on
ParentnetTreasury
, theSubnetTreasury
unlocks tokens on its chain and sends them to the specified recipient.
- In response to a burn action on
Endpoint and Cross-Chain Communication
- All cross-chain messages are handled through the
IEndpoint
interface, which abstracts the low-level cross-chain communication. - Each function that initiates cross-chain actions (mint, burn, lock, unlock) encodes data using
abi.encodeWithSelector
to create a message payload, ensuring proper handling of contract-specific calls on the destination chain.