|
| 1 | +# SRC-10: Native Bridge |
| 2 | + |
| 3 | +The following standard allows for the implementation of a standard API for Native Bridges using the Sway Language. The standardized design has the bridge contract send a message to the origin chain to register which token it accepts to prevent a loss of funds. |
| 4 | + |
| 5 | +## Motivation |
| 6 | + |
| 7 | +A standard interface for bridges intends to provide a safe and efficient bridge between the settlement or canonical chain and the Fuel Network. |
| 8 | + |
| 9 | +## Prior Art |
| 10 | + |
| 11 | +The standard is centered on Fuel’s [Bridge Architecture](https://github.com/FuelLabs/fuel-bridge/blob/main/docs/ARCHITECTURE.md). Fuel's bridge system is built on a message protocol that allows to send (and receive) messages between entities located in two different blockchains. |
| 12 | + |
| 13 | +The following standard takes reference from the [`FungibleBridge`](https://github.com/FuelLabs/fuel-bridge/blob/3971081850e7961d9b649edda4cad8a848ee248e/packages/fungible-token/bridge-fungible-token/src/interface.sw#L22) ABI defined in the fuel-bridge repository. |
| 14 | + |
| 15 | +## Specification |
| 16 | + |
| 17 | +The following functions MUST be implemented to follow the SRC-10; Native Bridge Standard: |
| 18 | + |
| 19 | +### Required Functions |
| 20 | + |
| 21 | +**`fn process_message(message_index: u64)`** |
| 22 | + |
| 23 | +The `process_message()` function accepts incoming deposit messages from the canonical chain and issues the corresponding bridged asset. |
| 24 | + |
| 25 | +- This function MUST parse a message at the given `message_index` index. |
| 26 | +- This function SHALL mint an asset that follows the [SRC-8; Bridged Asset Standard](https://docs.fuel.network/docs/sway-standards/src-8-bridged-asset/). |
| 27 | +- This function SHALL issue a refund if there is an error in the bridging process. |
| 28 | + |
| 29 | +**`fn withdraw(to_address: b256)`** |
| 30 | + |
| 31 | +The `withdraw()` function accepts and burns a bridged Native Asset on Fuel and sends a message to the bridge contract on the canonical chain to release the originally deposited tokens to the `to_address` address. |
| 32 | + |
| 33 | +- This function SHALL send a message to the bridge contract to release the bridged tokens to the `to_address` address on the canonical chain. |
| 34 | +- This function MUST ensure the asset's `AssetId` sent in the transaction matches a bridged asset. |
| 35 | +- This function SHALL burn all coins sent in the transaction. |
| 36 | + |
| 37 | +**`fn claim_refund(to_address: b256, token_address: b256, token_id: b256, gateway_contract: b256)`** |
| 38 | + |
| 39 | +The `claim_refund()` function is called if something goes wrong in the bridging process and an error occurs. It sends a message to the `gateway_contract` contract on the canonical chain to release the `token_address` token with token id `token_id` to the `to_address` address. |
| 40 | + |
| 41 | +- This function SHALL send a message to the `gateway_contract` contract to release the `token_address` token with id `token_id` to the `to_address` address on the canonical chain. |
| 42 | +- This function MUST ensure a refund was issued. |
| 43 | + |
| 44 | +### Required Data Types |
| 45 | + |
| 46 | +#### `DepositType` |
| 47 | + |
| 48 | +The `DepositType` enum describes whether the bridged deposit is made to a address, contract, or contract and contains additional metadata. There MUST be the following variants in the `DepositType` enum: |
| 49 | + |
| 50 | +**`Address`: `()`** |
| 51 | + |
| 52 | +The `Address` variant MUST represent when the deposit is made to an address on the Fuel chain. |
| 53 | + |
| 54 | +**`Contract`: `()`** |
| 55 | + |
| 56 | +The `Contract` variant MUST represent when the deposit is made to an contract on the Fuel chain. |
| 57 | + |
| 58 | +**`ContractWithData`: `()`** |
| 59 | + |
| 60 | +The `ContractWithData` variant MUST represent when the deposit is made to an contract and contains additional metadata for the Fuel chain. |
| 61 | + |
| 62 | +##### Example Deposit Type |
| 63 | + |
| 64 | +```sway |
| 65 | +pub enum DepositType { |
| 66 | + Address: (), |
| 67 | + Contract: (), |
| 68 | + ContractWithData: (), |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +#### `DepositMessage` |
| 73 | + |
| 74 | +The following describes a struct that encapsulates various deposit message metadata to a single type. There MUST be the following fields in the `DepositMessage` struct: |
| 75 | + |
| 76 | +**`amount`: `u256`** |
| 77 | + |
| 78 | +The `amount` field MUST represent the number of tokens. |
| 79 | + |
| 80 | +**`from`: `b256`** |
| 81 | + |
| 82 | +The `from` field MUST represent the bridging user’s address on the canonical chain. |
| 83 | + |
| 84 | +**`to`: `Identity`** |
| 85 | + |
| 86 | +The `to` field MUST represent the bridging target destination `Address` or `ContractId` on the Fuel Chain. |
| 87 | + |
| 88 | +**`token_address`: `b256`** |
| 89 | + |
| 90 | +The `token_address` field MUST represent the bridged token's address on the canonical chain. |
| 91 | + |
| 92 | +**`token_id`: `b256`** |
| 93 | + |
| 94 | +The `token_id` field MUST represent the token's ID on the canonical chain. The `b256::zero()` MUST be used if this is a fungible token and no token ID exists. |
| 95 | + |
| 96 | +**`decimals`: `u8`** |
| 97 | + |
| 98 | +The `decimals` field MUST represent the bridged token's decimals on the canonical chain. |
| 99 | + |
| 100 | +**`deposit_type`: `DepositType`** |
| 101 | + |
| 102 | +The `deposit_type` field MUST represent the type of bridge deposit made on the canonical chain. |
| 103 | + |
| 104 | +##### Example Deposit Message |
| 105 | + |
| 106 | +```sway |
| 107 | +pub struct DepositMessage { |
| 108 | + pub amount: b256, |
| 109 | + pub from: b256, |
| 110 | + pub to: Identity, |
| 111 | + pub token_address: b256, |
| 112 | + pub token_id: b256, |
| 113 | + pub decimals: u8, |
| 114 | + pub deposit_type: DepositType, |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +#### `MetadataMessage` |
| 119 | + |
| 120 | +The following describes a struct that encapsulates the metadata of token on the canonical chain to a single type. There MUST be the following fields in the `MetadataMessage` struct: |
| 121 | + |
| 122 | +**`token_address`: `b256`** |
| 123 | + |
| 124 | +The `token_address` field MUST represent the bridged token's address on the canonical chain. |
| 125 | + |
| 126 | +**`token_id`: `b256`** |
| 127 | + |
| 128 | +The `token_id` field MUST represent the token's ID on the canonical chain. The `b256::zero()` MUST be used if this is a fungible token and no token ID exists. |
| 129 | + |
| 130 | +**`name`: `String`** |
| 131 | + |
| 132 | +The `name` field MUST represent the bridged token's name field on the canonical chain. |
| 133 | + |
| 134 | +**`symbol`: `String`** |
| 135 | + |
| 136 | +The `symbol` field MUST represent the bridged token's symbol field on the canonical chain. |
| 137 | + |
| 138 | +##### Example Metadata Message |
| 139 | + |
| 140 | +```sway |
| 141 | +pub struct MetadataMessage { |
| 142 | + pub token_address: b256, |
| 143 | + pub token_id: b256, |
| 144 | + pub name: String, |
| 145 | + pub symbol: String, |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +## Required Standards |
| 150 | + |
| 151 | +Any contract that implements the SRC-10; Native Bridge Standard MUST implement the [SRC-8; Bridged Asset Standard](https://docs.fuel.network/docs/sway-standards/src-8-bridged-asset/) for all bridged assets. |
| 152 | + |
| 153 | +## Rationale |
| 154 | + |
| 155 | +The SRC-10; Native Bridge Standard is designed to standardize the native bridge interface between all Fuel instances. |
| 156 | + |
| 157 | +## Backwards Compatibility |
| 158 | + |
| 159 | +This standard is compatible with the SRC-20 and SRC-8 standards. |
| 160 | + |
| 161 | +## Example ABI |
| 162 | + |
| 163 | +```sway |
| 164 | +abi SRC10 { |
| 165 | + fn process_message(message_index: u64); |
| 166 | + fn withdraw(to_address: b256); |
| 167 | + fn claim_refund(to_address: b256, token_address: b256, token_id: b256, gateway_contract: b256); |
| 168 | +} |
| 169 | +``` |
0 commit comments