EVM Smart Contracts
The EVM smart contracts are deployed on the EVM networks integrated into the NoM Multi-chain Infrastructure.
Contract Anatomy
The code is written in Solidity v0.8.19 (latest as March 2023).
Imports
The smart contract uses the following openzeppelin standard contracts:
import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
-
using ECDSA for bytes32- used to add therecoverECDSA signature functionality -
using SafeERC20 for IERC20- used to add thesafeTransferandsafeTransferFromfunctionalities -
contract Bridge is Context- inherit_msgSender()method to get the sender
Modifiers
-
onlyAdministratorcan only be called by owner of theadministratorprivate key -
isNotHaltedcan only be called if the bridge is not halted
Structs
-
TokenInfois used to store information about the bridged tokens -
RedeemInfois used for redeeming funds and information regarding time challenges
Events
event RegisteredRedeem(uint256 indexed nonce, address indexed to, address indexed token, uint256 amount);
event Redeemed(uint256 indexed nonce, address indexed to, address indexed token, uint256 amount);
event Unwrapped(address indexed from, address indexed token, string to, uint256 amount);
event Halted();
event Unhalted();
event RevokedRedeem(uint256 indexed nonce);
event PendingAdministrator(address indexed newAdministrator);
event SetAdministrator(address indexed newAdministrator, address oldAdministrator);
event PendingTss(address indexed newTss);
event SetTss(address indexed newTss, address oldTss);
event PendingGuardians();
event SetGuardians();
-
RegisteredRedeememitted when a redeem request is registered -
Redeemedemitted when a redeem is completed -
Unwrappedemitted when an unwrap request is registered -
Haltedemitted when the bridge is halted -
Unhaltedemitted when the bridge is unhalted -
RevokedRedeememitted when the administrator revokes an invalid redeem -
PendingAdministratoremitted when a request for changing the administrator address is made -
SetAdministratoremitted when the administrator address is set -
PendingTssemitted when a request for changing the tss address is made -
SetTssemitted when the tss address is set -
PendingGuardiansemitted when a request for changing the guardian addresses is made -
SetGuardiansemitted when the addresses of the guardians are set
Constants
-
uint256 constant uint256maxstorestype(uint256).max -
uint32 private constant networkClassfor EVM networks is hard coded to2 -
uint8 private constant minNominatedGuardiansmust coincide with theMinGuardiansconstant from the embedded contract and is hard coded to5
Variables
-
estimatedBlockTimeis set for each network depending on the block time interval; it is used by theorchestratorto know how much to wait for one confirmation -
confirmationsToFinalityis the number of confirmations required to achieve finality for a given network; it is used by theorchestratorto confirm an event -
haltedindicates if the bridge is halted or not -
allowKeyGenindicates wether or not the tss address can be changed with a valid signature; theadmincan always change the tss address -
administratoris the EVM compatible address of theadmin -
administratorDelayis the delay required for changing theadmin; it cannot be lower thanminAdministratorDelay -
minAdministratorDelayis the minimum delay required for changing theadmin -
tssis the TSS address jointly created during the key generation ceremony -
softDelayis the delay required for the time challenge security primitive; it cannot be lower thanminSoftDelay -
minSoftDelayis the minimum delay required for the time challenge security primitive -
guardiansis the array containing the addresses of the guardians -
nominatedGuardiansis the array containing the addresses of the nominated guardians -
guardiansVotesis the array containing the votes for the guardians -
votesCountis a mapping containing the proposedadminvotes of each guardian -
unhaltedAtis the last block height at which the bridge was unhalted -
unhaltDurationis the duration in blocks during which the bridge is still halted after theunhaltedAtblock height; it cannot be lower thanminUnhaltDuration -
minUnhaltDurationis the minimum duration in blocks during which the bridge is still halted after theunhaltedAtblock height -
actionsNonceis the nonce required for a valid signature at a particular state of the bridge; must be always incremented after a signature is validated -
contractDeploymentHeightis the block height at which the contract was deployed; it is used by theorchestratorto know the height to scan events
Contract implementation
Constructor
In the constructor we set the following variables:
administratorminUnhaltDurationunhaltDurationminAdministratorDelayadministratorDelayminSoftDelaysoftDelayguardiansguardiansVotesestimatedBlockTimeconfirmationsToFinalitycontractDeploymentHeight
redeem method
The first and the second step of the redeem process. During the first step a time challenge will start. During the second step, after softDelay, the funds will be released/minted. The bridge must not be halted, the request must not have been revoked before, the token should be redeemable and the TSS signature valid.
Parameters
to- EVM address that will receive the fundstoken-EVM-20token addressamount- amount of tokennonce- unique identifier of the redeem requestsignature- signature generated by the current TSS
Returns
- None
unwrap method
Register an unwrap request. Tokens will be locked/burned. The bridge must not be halted and the token must be bridgeable.
Parameters
token-EVM-20token addressamount- amount of tokento- NoM address
Returns
- None
setTokenInfo method
Adds or edits an existing tokenInfo. It contains information about permitted tokens to swap or redeem and their delays. Can be called only by the administrator. Guarded by a time challenge.
Parameters
token-EVM-20token addressminAmount- minimum amount of the token for unwrappingredeemDelay- delay in blocks after the first redeem step in order to receive the fundsbridgeable- whether the token can be unwrappedredeemable- whether the token can be redeemedisOwned- whether this contract has owner rights for theEVM-20
Returns
- None
halt method
Halts the network. It does not require a signature if called by the administrator. Otherwise, a TSS signature is needed.
Parameters
signature- current TSS signature
Returns
- None
unhalt method
Sets halted to false and updates the haltedAt block such that unhaltDelay starts. Can only be called by the administrator.
Parameters
- None
Returns
- None
revokeRedeems method
Revokes invalid redeem requests.
Parameters
nonces- an array containing the unique identifiers for each request
Returns
- None
setAdministrator method
Changes the administrator address. Can only be called by the administrator. Guarded by a time challenge.
Parameters
newAdministrator-newadministrator address
Returns
- None
setTss method
Changes the TSS address. If called by the administrator, no signature is required and the time challenge starts. Otherwise, a valid TSS signature is required in order to validate the new address.
Parameters
newTss-newTSS addressoldSignature- signature from the current TSSnewSignature- signature from the new TSS
Returns
- None
emergency method
Sets administrator to address(0), TSS to address(0) and halts the bridge. Can only be called by the administrator. It enables guardians to propose.
Parameters
- None
Returns
- None
nominateGuardians
Nominate the guardians that are responsible to propose a new administrator in case of an emergency. Guarded by a time challenge. Can only be called by the administrator.
Parameters
newGuardians- an array containing the newguardianaddresses
Returns
- None
proposeAdministrator method
Will vote for a new administrator address. Can only be called by a guardian only if the bridge is in emergency state (the address of the administrator is address(0)).
Parameters
newAdministrator- newadministratoraddress
Returns
- None
setSoftDelay method
Sets the delay for the time challenge. Can only be called by the administrator.
Parameters
delay- delay in blocks
Returns
- None
setUnhaltDuration method
Sets the delay during which the bridge remains halted after calling unhalt. Can only be called by the administrator.
Parameters
duration- duration in blocks
Returns
- None
setEstimatedBlockTime method
Sets the delay in which the bridge remains halted after calling unhalt. Can only be called by the administrator.
Parameters
blockTime- block time in seconds; it is used by theorchestratorlayer
Returns
- None
setAllowKeyGen method
Sets allowKeyGen to true that permits the TSS address to be changed by an address different from the administrator. Can only be called by the administrator.
Parameters
value-trueorfalse
Returns
- None
setConfirmationsToFinality method
Sets the confirmations required by an event to be confirmed. Used by the orchestrator layer. Can only be called by the administrator.
Parameters
confirmations- number of confirmations in blocks
Returns
- None