|
| 1 | +<p align="center"> |
| 2 | + <picture> |
| 3 | + <source media="(prefers-color-scheme: dark)" srcset=".docs/src-10-logo-dark-theme.png"> |
| 4 | + <img alt="SRC-10 logo" width="400px" src=".docs/src-10-logo-light-theme.png"> |
| 5 | + </picture> |
| 6 | +</p> |
| 7 | + |
| 8 | +# Abstract |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +# Motivation |
| 13 | + |
| 14 | +A standard interface for bridges intends to provide a safe and efficient bridge between the settlement or canonical chain and the Fuel Network. |
| 15 | + |
| 16 | +# Prior Art |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +# Specification |
| 23 | + |
| 24 | +The following functions MUST be implemented to follow the SRC-10; Native Bridge Standard: |
| 25 | + |
| 26 | +## Required Functions |
| 27 | + |
| 28 | +### - `fn register_token(token_address: b256, gateway_contract: b256)` |
| 29 | + |
| 30 | +The `register_token()` function compiles a message to be sent back to the canonical chain to register a token to be bridged. The `gateway_contract` contract on the canonical chain receives the `token_address` token address in the message such that when `token_addess` tokens are deposited on the canonical chain they are reported to prevent loss of funds. |
| 31 | + |
| 32 | +> **NOTE:*** Trying to deposit tokens to a contract ID that does not exist or does not implement the Fuel Messaging Portal would mean permanent loss of funds. |
| 33 | +
|
| 34 | +- This function MUST send a message on the canonical chain to the `gateway_contract` contract, registering the specified `token_address` token that exists on the canonical chain. |
| 35 | + |
| 36 | +### - `fn process_message(message_index: u64)` |
| 37 | + |
| 38 | +The `process_message()` function accepts incoming deposit messages from the canonical chain and issues the corresponding bridged asset. |
| 39 | + |
| 40 | +- This function MUST parse a message at the given `message_index` index. |
| 41 | +- This function SHALL mint a token that follows the [SRC-8; Bridged Asset Standard](https://github.com/FuelLabs/sway-standards/tree/master/standards/src_8). |
| 42 | +- This function SHALL issue a refund if there is an error in the bridging process. |
| 43 | + |
| 44 | +### - `fn withdraw(to_address: b256, sub_id: SubId, gateway_contract: b256)` |
| 45 | + |
| 46 | +The `withdraw()` function accepts and burns a bridged Native Asset on Fuel and sends a message to the `gateway_contract` contract on the canonical chain to release the originally deposited tokens to the `to_address` address. |
| 47 | + |
| 48 | +- This function SHALL send a message to the `gateway_contract` contract to release the bridged tokens to the `to_address` address on the canonical chain. |
| 49 | +- This function MUST ensure the `sha256(contract_id(), sub_id)` digest matches the asset's `AssetId` sent in the transaction. |
| 50 | +- This function SHALL burn all tokens sent in the transaction. |
| 51 | + |
| 52 | +### - `fn claim_refund(to_address: b256, token_address: b256, token_id: b256, gateway_contract: b256)` |
| 53 | + |
| 54 | +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. |
| 55 | + |
| 56 | +- 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. |
| 57 | +- This function MUST ensure a refund was issued. |
| 58 | + |
| 59 | +## Required Data Types |
| 60 | + |
| 61 | +### `MessageData` |
| 62 | + |
| 63 | +The following describes a struct that encapsulates various message metadata to a single type. There MUST be the following fields in the `MessageData` struct: |
| 64 | + |
| 65 | +#### - amount: `u256` |
| 66 | + |
| 67 | +The `amount` field MUST represent the number of tokens. |
| 68 | + |
| 69 | +#### - from: `b256` |
| 70 | + |
| 71 | +The `from` field MUST represent the bridging user’s address on the canonical chain. |
| 72 | + |
| 73 | +#### - len: `u16` |
| 74 | + |
| 75 | +The `len` field MUST represent the number of the deposit messages to discern between deposits that must be forwarded to an EOA vs deposits that must be forwarded to a contract. |
| 76 | + |
| 77 | +#### - to: `Identity` |
| 78 | + |
| 79 | +The `to` field MUST represent the bridging target destination `Address` or `ContractId` on the Fuel Chain. |
| 80 | + |
| 81 | +#### - token_address: `b256` |
| 82 | + |
| 83 | +The `token_address` field MUST represent the bridged token's address on the canonical chain. |
| 84 | + |
| 85 | +#### - token_id: `b256` |
| 86 | + |
| 87 | +The `token_id` field MUST represent the token's ID on the canonical chain. The `ZERO_B256` MUST be used if this is a fungible token and no token ID exists. |
| 88 | + |
| 89 | +### Example |
| 90 | + |
| 91 | +```sway |
| 92 | +struct MessageData { |
| 93 | + amount: b256, |
| 94 | + from: b256, |
| 95 | + len: u16, |
| 96 | + to: Identity, |
| 97 | + token_address: b256, |
| 98 | + token_id: b256, |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Required Standards |
| 103 | + |
| 104 | +Any contract that implements the SRC-10; Native Bridge Standard MUST implement the [SRC-8; Bridged Asset Standard](https://github.com/FuelLabs/sway-standards/tree/master/standards/src_8) for all bridged assets. |
| 105 | + |
| 106 | +# Rationale |
| 107 | + |
| 108 | +The SRC-10; Native Bridge Standard is designed to standardize the native bridge interface between all Fuel instances. |
| 109 | + |
| 110 | +# Backwards Compatibility |
| 111 | + |
| 112 | +This standard is compatible with the SRC-20 and SRC-8 standards. |
| 113 | + |
| 114 | +# Example ABI |
| 115 | + |
| 116 | +```sway |
| 117 | +abi SRC10 { |
| 118 | + fn register_token(token_address: b256, gateway_contract: b256); |
| 119 | + fn process_message(message_index: u64); |
| 120 | + fn withdraw(to_address: b256, sub_id: SubId, gateway_contract: b256); |
| 121 | + fn claim_refund(to_address: b256, token_address: b256, token_id: b256, gateway_contract: b256); |
| 122 | +} |
| 123 | +``` |
0 commit comments