Skip to content

Commit

Permalink
Base Asset SRC-20 Contract (#172)
Browse files Browse the repository at this point in the history
# Description

Adds the SRC-20 implementation contract for the Fuel Network's Base
Asset, Ether.

Uses fuel-core v0.26.0 and forc v0.56.1

# Closes

Closes #160
  • Loading branch information
bitzoic authored May 23, 2024
1 parent 45c8a90 commit 85a54c9
Show file tree
Hide file tree
Showing 12 changed files with 1,720 additions and 851 deletions.
2,204 changes: 1,354 additions & 850 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
resolver = "2"
members = [
"packages/message-predicates/contract-message-predicate",
"packages/fungible-token/bridge-fungible-token"
"packages/fungible-token/bridge-fungible-token",
"packages/base-asset"
]

[workspace.package]
Expand Down
1 change: 1 addition & 0 deletions Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ members = [
"packages/fungible-token/bridge-fungible-token",
"packages/message-predicates/contract-message-predicate",
"packages/message-predicates/contract-message-receiver",
"packages/base-asset",
]
1 change: 1 addition & 0 deletions packages/base-asset/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
17 changes: 17 additions & 0 deletions packages/base-asset/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "base_asset_contract"
version = "0.1.0"
edition = "2021"
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"

[dev-dependencies]
fuels = { version = "0.58.0", features = ["fuel-core-lib"] }
tokio = { version = "1.12", features = ["rt", "macros"] }
hex = { version = "0.4.3" }
sha2 = { version = "0.10" }

[[test]]
harness = true
name = "integration_tests"
path = "tests/harness.rs"
21 changes: 21 additions & 0 deletions packages/base-asset/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[[package]]
name = "base-asset-contract"
source = "member"
dependencies = [
"standards",
"std",
]

[[package]]
name = "core"
source = "path+from-root-9889F771D40C5D34"

[[package]]
name = "standards"
source = "git+https://github.com/FuelLabs/sway-standards?tag=v0.4.3#6f63eb7dff2458a7d976184e565b5cbf26f61da2"
dependencies = ["std"]

[[package]]
name = "std"
source = "git+https://github.com/fuellabs/sway?tag=v0.53.0#b30f0e83d3f3d336007e3dfce45a48a87e731345"
dependencies = ["core"]
8 changes: 8 additions & 0 deletions packages/base-asset/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "base-asset-contract"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.4.3" }
46 changes: 46 additions & 0 deletions packages/base-asset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<p align="center">
<a href="https://crates.io/crates/forc/0.56.1" alt="forc">
<img src="https://img.shields.io/badge/forc-v0.56.1-orange" />
</a>
<a href="https://crates.io/crates/fuel-core/0.26.0" alt="fuel-core">
<img src="https://img.shields.io/badge/fuel--core-v0.26.0-blue" />
</a>
</p>

# Fuel Base Asset

The contract deployed on the Fuel Network which implements the SRC-20 standard for the base asset, Ether.

## Contract ID

The `ContractId` of the base asset SRC-20 implementation is `0xa20eb159e6460c47f438cb9e9f653a8d5084146ca115c774181c0620608d15a3`. This is calculated using the zero salt (`0x00..00`).

## Asset Id

The `AssetId` fo the base asset SRC-20 implementation is `0xa48cdc6b0bc20843b9a755a6fffc6ff9a0965b1aff0d58cc9247dc72b8bbd61f`. This calculated by taking the SHA256 hash digest of the ContractId and the zero SubId (`0x00..00`) i.e. sha256((contract_id, sub_id)).

## Compiled Output

The compiled output binaries of the base asset SRC-20 implementation is provided in the `/bin` folder and include both binaries and the hex representation of the binaries.

## Quickstart

### Building Sway

In the root of the repository run the following command to build all the Sway programs.

```bash
forc build --release
```

### Running Rust Tests

After the Sway programs have been built run the following command in the root of the repository.

```bash
cargo test
```

## License

The primary license for this repo is `Apache 2.0`, see [`LICENSE`](../../LICENSE).
Binary file added packages/base-asset/bin/base-asset-contract.bin
Binary file not shown.
1 change: 1 addition & 0 deletions packages/base-asset/bin/base-asset-contract.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1af030007400000200000000000014605dffc00110ffff00740000001aec500091000b285d43f015104100c01a4460005d4d10491b441000104934405d47f008104404405d4920001b44144010453440504fb0205fed10045fed20055047ba8872500010284535005047ba88504fb46072500010284d15005d453000504fb0105fed10025fed20035047ba2872480010284534805047ba285fed00005d43f0085fed00015043b2d0724800102843b480504bb650724c0010284914c05047b660724c0010284504c01ae920001ae5100020f8330058fbe00250fbe0047400041a1a43d00076400001740000a71a4060005d41004a1b441000104104405047ba9872480020284504805043ba985047b03072480020284504805043b0b072480020284114805047b95072480020284504805043b95071440006504bb0f8724c0020284914c05047b890724c0020284504c05043b8f0724c0020284124c0a1411420764000065043b2e05fec005c504bb7907244001028490440740000085043b2805fec10505045000f5c4bf0505e452000504bb79072440010284904405d43f00b264000001a4070005fed01365d43f00b5fed01375fec01385057b9b05043b53072440010284124405d43b0f213410000764000475d43b0a613410040764000025d43f00c364000005043b5305041000f5c4100001ae810001ae5500020f8330058fbe00250fbe0047400042e5d4550005d4950015d4d5002104d3040165124c076500001135124c0765000065047b3605fec006c504fb7a072480018284d14807400000d124d3480104d30401b4d24c0264c0000281d14801a447000504bb1185fec10235fed10245fed3025504fb7a072440018284d24405047b54072480018284534805d47b0f4134510007644000f5d47b0a813451040764400025d43f00c364000005047b54050451008504bb5405049200850492008724c0008285514c050455008724c0008284524c05d4550005d4950021b481480104514805e4500005d415002104100405f550002740000061ae800001ae5500020f8330058fbe00250fbe004740003ef5043b9b050450010504bb1e0724c0008284904c050412008724c0008284114c05043ba3872440010284124405043ba385047b6c072480010284504801ae9100020f8330058fbe00250fbe004740004311a43d0005047ba38504bb780724c0010284914c01ae9200020f8330058fbe00250fbe004740003c01a47d000254110005d43f016104100c05047ba28504bb3505fed006a5d43f00d5fed006b5043b3f0724c0010284124c0504bb670724c0010284914c05047b710724c0010284504c01ae920001ae5100020f8330058fbe00250fbe0047400035a1a43d000764000017400012f1a4060005d41004a1b441000104104405047bab872480020284504805043bab85047b05072480020284504805043b13072480020284114805047b97072480020284504805043b97071440006504bb150724c0020284914c05047b8b0724c0020284504c05043b910724c0020284124c0a1411420764000065043b3105fec0062504bb7b872440020284904407400002d5043b2905fec10525047b6405d4bf017104920c0724c0008284524c05d4bf008264800001a4c7000284d14805047b1f05fed303e5d4bf00f5fed203f504bb470724c0010284914c05d492001504fb48072500010284d15005d4530001b492040504fb4b05fed10965fed20975047b4c072480010284534805d451000504bb4d072500010284935005d4920015053b4e072540010285135405d4d40015fed10535fed20545fed3055504bb7b872440020284904405d43f00b264000001a4070005fed01395d43f00b5fed013a5fec013b5057b9c85043b58872440020284124405d43b0f713410000764000aa5d43b0b113410040764000025d43f00c364000005043b588504100085047baf872480018284504801ae810001ae5500020f8330058fbe00250fbe00474000347505bbaf85d43b1615d4550005d4950015d4d50025d53f008104d3500165124c076500001135124c0765000065047b3a85fec0075504fb7d872480018284d14807400000d124d3480104d30401b4d24c0264c0000281d14801a447000504bb1705fec102e5fed102f5fed3030504fb7d872440018284d24405047b5d872480018284534805d47b0fb134510007644000f5d47b0bb13451040764400025d43f00c364000005047b5d850451008504bb5d85049200850492008724c0008285514c050455008724c0008284524c05d4550005d4950021b481480104514805f4500005d4150025d47f008104104405f5500021a5c00005d41600216417400764000017400005f5d416002164505c076440001134505c07644000b5d416000104105c05047b2f05fec105e5c4100005049100f5e490000504bb7f07240001028491400740000055043b0705fec000e504bb7f072440010284904405043b62072440010284124405d43b0fe1341004076400001360000005043b6205041000f5c4100005d4550005d4950015d4d5002104d3040165124c076500001135124c0765000065047b3785fec006f504fb80072480018284d14807400000d124d3480104d30401b4d24c0264c0000281d14801a447000504bb2005fec10405fed10415fed3042504fb80072440018284d24405047b55872480018284534805d47b100134510007644000f5d47b0ab13451040764400025d43f00c364000005047b55850451008504bb5585049200850492008724c0008285514c050455008724c0008284524c05d4550005d4950021b481480104514805e4500005d415002104100405f550002105d70407500005b1ae800001ae5500020f8330058fbe00250fbe004740002a75043b9c850450010504bb260724c0008284904c050412008724c0008284114c05043ba4872440010284124405043ba485047b6d072480010284504801ae9100020f8330058fbe00250fbe004740002e91a43d0005047ba48504bb680724c0010284914c01ae9200020f8330058fbe00250fbe004740002781a47d000254110005d43f018104100c05047ba28504bb4005fed00805d43f0105fed00815043b410724c0010284124c0504bb720724c0010284914c05047b730724c0010284504c01ae920001ae5100020f8330058fbe00250fbe004740002121a43d000764000017400012f1a4060005d41004a1b441000104104405047bad872480020284504805043bad85047b08072480020284504805043b18872480020284114805047b99072480020284504805043b99071440006504bb1a8724c0020284914c05047b8d0724c0020284504c05043b930724c0020284124c0a1411420764000065043b3305fec0066504bb81872440020284904407400002d5043b2b05fec10565047b6485d4bf019104920c0724c0008284524c05d4bf008264800001a4c7000284d14805047b2185fed30435d4bf0125fed2044504bb4f0724c0010284914c05d492001504fb50072500010284d15005d4530001b492040504fb5105fed10a25fed20a35047b52072480010284534805d451000504bb49072500010284935005d4920015053b4a072540010285135405d4d40015fed10575fed20585fed3059504bb81872440020284904405d43f00b264000001a4070005fed013c5d43f00b5fed013d5fec013e5057b9e05043b5a872440020284124405d43b10313410000764000aa5d43b0b513410040764000025d43f00c364000005043b5a8504100085047bb1072480018284504801ae810001ae5500020f8330058fbe00250fbe004740001ff505bbb105d43b1645d4550005d4950015d4d50025d53f008104d3500165124c076500001135124c0765000065047b3c05fec0078504fb83872480018284d14807400000d124d3480104d30401b4d24c0264c0000281d14801a447000504bb1c85fec10395fed103a5fed303b504fb83872440018284d24405047b5f072480018284534805d47b107134510007644000f5d47b0be13451040764400025d43f00c364000005047b5f050451008504bb5f05049200850492008724c0008285514c050455008724c0008284524c05d4550005d4950021b481480104514805f4500005d4150025d47f008104104405f5500021a5c00005d41600216417400764000017400005f5d416002164505c076440001134505c07644000b5d416000104105c05047b3005fec10605c4100005049100f5e490000504bb8507240001028491400740000055043b0a05fec0014504bb85072440010284904405043b63072440010284124405d43b10a1341004076400001360000005043b6305041000f5c4100005d4550005d4950015d4d5002104d3040165124c076500001135124c0765000065047b3905fec0072504fb86072480018284d14807400000d124d3480104d30401b4d24c0264c0000281d14801a447000504bb2285fec10455fed10465fed3047504fb86072440018284d24405047b57072480018284534805d47b10c134510007644000f5d47b0ae13451040764400025d43f00c364000005047b57050451008504bb5705049200850492008724c0008285514c050455008724c0008284524c05d4550005d4950021b481480104514805e4500005d415002104100405f550002105d70407500005b1ae800001ae5500020f8330058fbe00250fbe0047400015f5043b9e050450010504bb270724c0008284904c050412008724c0008284114c05043ba5872440010284124405043ba585047b6e072480010284504801ae9100020f8330058fbe00250fbe004740001a11a43d0005047ba58504bb690724c0010284914c01ae9200020f8330058fbe00250fbe004740001301a47d000254110005d43f01a104100c05047ba28504bb4205fed00845d43f0135fed00855043b430724c0010284124c0504bb740724c0010284914c05047b750724c0010284504c01ae920001ae5100020f8330058fbe00250fbe004740000ca1a43d00076400001740000635d43f00b264000001a4070005fed013f5d43f00b5fed01405fec01415053b9f85d43b13f5d47b1405d4bb1415d4ff008104924c0164d1480764c0001134d1480764c00065043b3d85fec007b504bb87872440018284904407400000d12492440104920401b49148026480000281d04401a4070005047b0d05fec101a5fed001b5fed201c504bb87872400018284914005043b60872440018284124405d43b10f134100007640000f5d43b0c113410040764000025d43f00c364000005043b608504100085047b608504510085045100872480008285104805041400872480008284114805d4140005d4540021b441440104104401a4410005f4110005d4140025d47f008104104405f5100025043b9f850450010504bb240724c0008284904c050412008724c0008284114c05043ba6872440010284124405043ba685047b6f072480010284504801ae9100020f8330058fbe00250fbe004740001251a43d0005047ba68504bb6a0724c0010284914c01ae9200020f8330058fbe00250fbe004740000b41a47d000254110005d43f01b104100c05047ba28504bb4405fed00885d43f0135fed00895043b450724c0010284124c0504bb760724c0010284914c05047b770724c0010284504c01ae920001ae5100020f8330058fbe00250fbe0047400004e1a43d00076400001740000495043b2505fec004a5d47f00b264400001a4470005fed11425d47f00b5fed11435fec01445047ba10504bb5c8724c0010284904c05d43b04a13410000764000135d43b0b913410040764000025d43f00c364000005d43b0ba1ae810001ae5100020f8330058fbe00250fbe0047400008e1ae900001ae5100020f8330058fbe00250fbe00474000088740000061ae800001ae5100020f8330058fbe00250fbe004740000815043ba1050450010504bb0e8724c0008284904c050412008724c0008284114c05043ba7872440010284124405043ba785047b70072480010284504801ae9100020f8330058fbe00250fbe004740000c31a43d0005047ba78504bb6b0724c0010284914c01ae9200020f8330058fbe00250fbe004740000521a47d000254110005d43f014364000009500003f960800001aec5000910000501a43a0001a4790001a4be000724c001028ed04c01aebb00020f8330058fbe00250fbe004740000301a4fd0005053b01072540010285115401ae9400020f8330058fbe00250fbe004740000271a53d000134d3500134d30001a500000764c001c504fb03072500010284d05001ae9300020f8330058fbe00250fbe004740000811a4fd0005053b04072540010285115401ae9400020f8330058fbe00250fbe004740000781a47d0005053b02072540010285105401ae9400020f8330058fbe00250fbe004740000081a43d000295134501af54000920000501af92000980800009700003f4af8000095000007960800001aec5000910000101a43a0001a47e0007248001028ed04801a43b0005d4100011af50000920000101af9100098080000970000074af8000095000007960800001aec5000910000101a43a0001a47e0007248001028ed04801a43b0005d410001124100401af50000920000101af9100098080000970000074af800009500007f960800001aec5000910000601a4fa0001a5790001a53e0005d5950005d4150015d4550025d4bf00810451480164904407648000113490440764800065043b0185fec0003504bb04872440018284904407400000c12451400104510401b45044026440000281d64001a4070005fec10005fed00015fed1002504bb04872400018284bb4005043b03072440018284124405d43b009134100007640000f5d43b00613410040764000025d43f00c364000005043b030504100085047b030504510085045100872480008285504805041500872480008284114805d4150005d4550021b441440104104405f4130005d4150025d47f008104104405f5500021af40000920000601af94000980800009700007f4af8000095000007960800001aec5000910000101a43a0001a47e0007248001028ed04801a43b0005d4100001af50000920000101af9100098080000970000074af8000095000007960800001aec5000910000101a43a0001a47e0007248001028ed04801a43b0005d4100001af50000920000101af9100098080000970000074af8000047000000646563696d616c736e616d650000000073796d626f6c0000746f74616c5f61737365747300000000746f74616c5f737570706c790000000000000000000002480000000000000008000000000000025009000000000000000000000000000400cccccccccccc0002000000000000000445746865720000000000000000000005000000000000000645544800000000000000000000000003000000000000000c000000000000007b000000000000143800000000000010dc00000000000010440000000000000bc40000000000000b3c00000000000006ac00000000000004cc
162 changes: 162 additions & 0 deletions packages/base-asset/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
contract;

use std::string::String;
use standards::src20::SRC20;

configurable {
/// The decimals of the base asset.
DECIMALS: u8 = 9u8,
/// The base asset of the fuel network.
NAME: str[5] = __to_str_array("Ether"),
/// The symbol of the base asset of the fuel network.
SYMBOL: str[3] = __to_str_array("ETH"),
}

impl SRC20 for Contract {
/// The total number of assets minted by a contract, for the base asset this is always one.
///
/// # Additional Information
///
/// For the base asset contract, this is always one.
///
/// # Returns
///
/// * [u64] - The number of assets that this contract has minted.
///
/// # Examples
///
/// ```sway
/// use src20::SRC20;
///
/// fn foo(base_asset_contract: ContractId) {
/// let src_20_abi = abi(SRC20, base_asset_contract);
/// let assets = src_20_abi.total_assets();
/// assert(assets == 1);
/// }
/// ```
#[storage(read)]
fn total_assets() -> u64 {
1
}

/// Always returns none for the Base asset.
///
/// # Additional Information
///
/// This value is stored and managed by the bridge contract.
///
/// # Arguments
///
/// * `asset`: [AssetId] - The asset of which to query the total supply, this should be the default `SubId`.
///
/// # Returns
///
/// * [Option<u64>] - Always `None` for the base asset contract.
///
/// # Examples
///
/// ```sway
/// use src20::SRC20;
/// use std::constants::DEFAULT_SUB_ID;
///
/// fn foo(base_asset_contract: ContractId) {
/// let src_20_abi = abi(SRC20, base_asset_contract);
/// let supply = src_20_abi.total_supply(DEFAULT_SUB_ID);
/// assert(supply == None);
/// }
/// ```
#[storage(read)]
fn total_supply(asset: AssetId) -> Option<u64> {
None
}

/// Returns the name of the base asset, Ether.
///
/// # Arguments
///
/// * `asset`: [AssetId] - The asset of which to query the name, this should be the `AssetId::base()` for the base asset.
///
/// # Returns
///
/// * [Option<String>] - The name of the base asset.
///
/// # Examples
///
/// ```sway
/// use src20::SRC20;
///
/// fn foo(base_asset_contract: ContractId) {
/// let src_20_abi = abi(SRC20, base_asset_contract);
/// let name = src_20_abi.name(AssetId::base());
/// assert(name.unwrap() == String::from_ascii_str(from_str_array("Ether")));
/// }
/// ```
#[storage(read)]
fn name(asset: AssetId) -> Option<String> {
if asset == AssetId::base() {
Some(String::from_ascii_str(from_str_array(NAME)))
} else {
None
}
}

/// Returns the symbol of the asset.
///
/// # Arguments
///
/// * `asset`: [AssetId] - The asset of which to query the symbol, this should be the `AssetId::base()` for the base asset.
///
/// # Returns
///
/// * [Option<String>] - The symbol of the base asset.
///
/// # Examples
///
/// ```sway
/// use src20::SRC20;
///
/// fn foo(base_asset_contract: ContractId) {
/// let src_20_abi = abi(SRC20, base_asset_contract);
/// let symbol = src_20_abi.symbol(AssetId::base());
/// assert(symbol.unwrap()() == String::from_ascii_str(from_str_array("ETH")));
/// }
/// ```
#[storage(read)]
fn symbol(asset: AssetId) -> Option<String> {
if asset == AssetId::base() {
Some(String::from_ascii_str(from_str_array(SYMBOL)))
} else {
None
}
}

/// Returns the number of decimals the base asset uses.
///
/// # Arguments
///
/// * `asset`: [AssetId] - The asset of which to query the decimals, this should be the `AssetId::base()` for the base asset.
///
/// # Returns
///
/// * [Option<u8>] - The decimal precision used by the base asset.
///
/// # Examples
///
/// ```sway
/// use src20::SRC20;
///
/// fn foo(base_asset_contract: ContractId) {
/// let src_20_abi = abi(SRC20, base_asset_contract);
/// let decimals = src_20_abi.decimals(AssetId::base());
/// assert(decimals.unwrap() == 9u8);
/// }
/// ```
#[storage(read)]
fn decimals(asset: AssetId) -> Option<u8> {
if asset == AssetId::base() {
Some(DECIMALS)
} else {
None
}
}
}
Loading

0 comments on commit 85a54c9

Please sign in to comment.