Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the AssetId type #4955

Merged
merged 68 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
53d8cc9
init
SwayStar123 Aug 15, 2023
bcc2b9a
move const out of impl
SwayStar123 Aug 15, 2023
f2f69cb
convert b256 to AssetId in tests
SwayStar123 Aug 15, 2023
1a9ff24
fix downstream changes
SwayStar123 Aug 15, 2023
d94ef6f
remove pub
SwayStar123 Aug 15, 2023
9889fb6
change docs to comments inside code
SwayStar123 Aug 15, 2023
58e116b
move asset_id up in lib.sw
SwayStar123 Aug 15, 2023
aba1d1e
remove import assetid
SwayStar123 Aug 15, 2023
84dd12a
move assetid to contract_id.sw
SwayStar123 Aug 21, 2023
2a64897
Merge branch 'master' of https://github.com/FuelLabs/sway
SwayStar123 Aug 21, 2023
6775850
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 21, 2023
26ebdba
removve double external
SwayStar123 Aug 21, 2023
437ca3e
move base_asset_id constant into constants.sw
SwayStar123 Aug 21, 2023
cf80c64
fix ci errors
SwayStar123 Aug 21, 2023
a69dab2
remove extra bracket
SwayStar123 Aug 21, 2023
1a843c3
move assetid above contractid impl
SwayStar123 Aug 21, 2023
58c3408
move up impl hash
SwayStar123 Aug 21, 2023
84d26c3
impl eq for assetid
SwayStar123 Aug 21, 2023
ced270a
update example
SwayStar123 Aug 22, 2023
dd73bf8
update example 2 electric boogaloo
SwayStar123 Aug 22, 2023
cc052e1
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 22, 2023
ec67da9
undo conflict mistake
SwayStar123 Aug 22, 2023
92d9e47
update example 3
SwayStar123 Aug 22, 2023
53fb9be
example 4
SwayStar123 Aug 22, 2023
27364c0
update other examples
SwayStar123 Aug 22, 2023
5019ef2
update reference docs
SwayStar123 Aug 22, 2023
cfde26e
fix test import
SwayStar123 Aug 22, 2023
daa3202
formater and lsp error
SwayStar123 Aug 22, 2023
03ff24d
fix e2e tests
SwayStar123 Aug 22, 2023
b0975a9
fix inconsistent representation
SwayStar123 Aug 22, 2023
6a16728
sdk harness update return type
SwayStar123 Aug 22, 2023
a7b0ce9
update abi aswell
SwayStar123 Aug 22, 2023
efa69c4
change aid to sid
SwayStar123 Aug 22, 2023
a9083b1
update contract address
SwayStar123 Aug 22, 2023
ff762e9
update contractid tokenops
SwayStar123 Aug 22, 2023
fb095bb
update oracle.json
SwayStar123 Aug 23, 2023
f7b495e
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 23, 2023
720913d
revert changes to cargo lok
SwayStar123 Aug 23, 2023
22343e3
Merge branch 'asset_id_struct' of https://github.com/FuelLabs/sway in…
SwayStar123 Aug 23, 2023
986a28b
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 24, 2023
f01cec8
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 24, 2023
5402f56
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 24, 2023
9a6f95b
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 25, 2023
4c2b5b2
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 25, 2023
c11a008
remove old assetid file
SwayStar123 Aug 25, 2023
45acfb9
Update sway-lib-std/src/contract_id.sw
SwayStar123 Aug 25, 2023
851aa10
impl from
SwayStar123 Aug 26, 2023
d7022ea
Req changes
SwayStar123 Aug 26, 2023
0016639
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 26, 2023
0d8bdb9
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 28, 2023
a926da1
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 29, 2023
1d61262
remove unnecessary imports in consta.sw
SwayStar123 Aug 30, 2023
01eb223
Merge branch 'asset_id_struct' of https://github.com/FuelLabs/sway in…
SwayStar123 Aug 30, 2023
fdc258f
fix test
SwayStar123 Aug 30, 2023
d4fbaa7
Merge branch 'master' into asset_id_struct
SwayStar123 Aug 30, 2023
15e9f87
Merge branch 'master' into asset_id_struct
SwayStar123 Sep 5, 2023
7caec15
impl hash and use f rom
SwayStar123 Sep 5, 2023
93a6482
Merge branch 'asset_id_struct' of https://github.com/FuelLabs/sway in…
SwayStar123 Sep 5, 2023
0cdaf76
Update sway-lib-std/src/contract_id.sw
SwayStar123 Sep 5, 2023
e4b0db8
Update sway-lib-std/src/contract_id.sw
SwayStar123 Sep 5, 2023
f1727ea
Update sway-lib-std/src/contract_id.sw
SwayStar123 Sep 5, 2023
632bc00
lsp tets
SwayStar123 Sep 5, 2023
881fd7c
add contract_id and asset_id tests
SwayStar123 Sep 6, 2023
f1a0022
rename variables in test
SwayStar123 Sep 6, 2023
bcbf69c
add line and use method in test
SwayStar123 Sep 6, 2023
24e27a5
into b256 for assetid
SwayStar123 Sep 6, 2023
569f480
Merge branch 'master' into asset_id_struct
SwayStar123 Sep 6, 2023
0120d31
Merge branch 'master' into asset_id_struct
SwayStar123 Sep 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::token::transfer_to_address;
use std::token::force_transfer_to_contract;
// ANCHOR_END: force_transfer_to_contract_import

use std::constants::ZERO_B256;
use std::constants::{BASE_ASSET_ID, ZERO_B256};

fn minting() {
// ANCHOR: mint
Expand Down Expand Up @@ -77,7 +77,7 @@ fn transferring_to_address() {
// ANCHOR: transfer_to_address
let amount = 10;
let address = 0x0000000000000000000000000000000000000000000000000000000000000001;
let asset = ZERO_B256;
let asset = BASE_ASSET_ID;
let user = Address::from(address);

transfer_to_address(user, asset, amount);
Expand All @@ -88,7 +88,7 @@ fn transferring_to_contract() {
// ANCHOR: force_transfer_to_contract
let amount = 10;
let address = 0x0000000000000000000000000000000000000000000000000000000000000001;
let asset = ZERO_B256;
let asset = BASE_ASSET_ID;
let pool = ContractId::from(address);

force_transfer_to_contract(pool, asset, amount);
Expand All @@ -99,7 +99,7 @@ fn transferring_to() {
// ANCHOR: transfer
let amount = 10;
let address = 0x0000000000000000000000000000000000000000000000000000000000000001;
let asset = ZERO_B256;
let asset = BASE_ASSET_ID;
let user = Identity::Address(Address::from(address));
let pool = Identity::ContractId(ContractId::from(address));

Expand Down
6 changes: 4 additions & 2 deletions examples/liquidity_pool/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ abi LiquidityPool {
fn withdraw(recipient: Address);
}

const BASE_TOKEN = 0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c;
const BASE_TOKEN: AssetId = AssetId {
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
value: 0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c,
};

impl LiquidityPool for Contract {
fn deposit(recipient: Address) {
Expand All @@ -34,7 +36,7 @@ impl LiquidityPool for Contract {
}

fn withdraw(recipient: Address) {
let asset_id = sha256((contract_id(), ZERO_B256));
let asset_id = AssetId::default(contract_id());
assert(msg_asset_id() == asset_id);
assert(msg_amount() > 0);

Expand Down
12 changes: 6 additions & 6 deletions examples/native_token/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::{constants::ZERO_B256, context::*, token::*};
abi NativeAssetToken {
fn mint_coins(mint_amount: u64);
fn burn_coins(burn_amount: u64);
fn force_transfer_coins(coins: u64, asset_id: b256, target: ContractId);
fn transfer_coins_to_output(coins: u64, asset_id: b256, recipient: Address);
fn force_transfer_coins(coins: u64, asset_id: AssetId, target: ContractId);
fn transfer_coins_to_output(coins: u64, asset_id: AssetId, recipient: Address);
fn deposit();
fn get_balance(target: ContractId, asset_id: b256) -> u64;
fn get_balance(target: ContractId, asset_id: AssetId) -> u64;
fn mint_and_send_to_contract(amount: u64, destination: ContractId);
fn mint_and_send_to_address(amount: u64, recipient: Address);
}
Expand All @@ -25,17 +25,17 @@ impl NativeAssetToken for Contract {
}

/// Transfer coins to a target contract.
fn force_transfer_coins(coins: u64, asset_id: b256, target: ContractId) {
fn force_transfer_coins(coins: u64, asset_id: AssetId, target: ContractId) {
force_transfer_to_contract(target, asset_id, coins);
}

/// Transfer coins to a transaction output to be spent later.
fn transfer_coins_to_output(coins: u64, asset_id: b256, recipient: Address) {
fn transfer_coins_to_output(coins: u64, asset_id: AssetId, recipient: Address) {
transfer_to_address(recipient, asset_id, coins);
}

/// Get the internal balance of a specific coin at a specific contract.
fn get_balance(target: ContractId, asset_id: b256) -> u64 {
fn get_balance(target: ContractId, asset_id: AssetId) -> u64 {
balance_of(target, asset_id)
}

Expand Down
7 changes: 4 additions & 3 deletions sway-lib-std/src/address.sw
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! A wrapper around the `b256` type to help enhance type-safety.
library;

use ::alias::{AssetId, SubId};
use ::alias::SubId;
use ::call_frames::contract_id;
use ::contract_id::AssetId;
use ::convert::From;
use ::hash::*;
use ::error_signals::FAILED_TRANSFER_TO_ADDRESS_SIGNAL;
Expand Down Expand Up @@ -105,7 +106,7 @@ impl Address {
while index < number_of_outputs {
if let Output::Variable = output_type(index) {
if output_amount(index) == 0 {
asm(r1: self.value, r2: index, r3: amount, r4: asset_id) {
asm(r1: self.value, r2: index, r3: amount, r4: asset_id.value) {
tro r1 r2 r3 r4;
};
return;
Expand Down Expand Up @@ -141,7 +142,7 @@ impl Address {
asm(r1: amount, r2: sub_id) {
mint r1 r2;
};
self.transfer(sha256((contract_id(), sub_id)), amount);
self.transfer(AssetId::new(contract_id(), sub_id), amount);
}
}

Expand Down
2 changes: 0 additions & 2 deletions sway-lib-std/src/alias.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
library;

/// The `AssetId` type is simply an alias for `b256` that represents the ID of a native asset.
pub type AssetId = b256;
/// The `SubId` type is simply an alias for `b256` that represents the sub identifier of a native asset.
pub type SubId = b256;
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 0 additions & 17 deletions sway-lib-std/src/asset_id.sw

This file was deleted.

15 changes: 9 additions & 6 deletions sway-lib-std/src/call_frames.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//! [Call frames](https://fuellabs.github.io/fuel-specs/master/vm#call-frames) store metadata across untrusted inter-contract calls.
library;

use ::alias::AssetId;
use ::registers::frame_ptr;
use ::contract_id::ContractId;
use ::contract_id::{AssetId, ContractId};
use ::intrinsics::is_reference_type;
use ::registers::frame_ptr;

// Note that everything when serialized is padded to word length.
//
Expand Down Expand Up @@ -63,9 +62,13 @@ pub fn contract_id() -> ContractId {
/// }
/// ```
pub fn msg_asset_id() -> AssetId {
asm(asset_id) {
addi asset_id fp i32;
asset_id: b256
AssetId {
value: {
asm(asset_id) {
addi asset_id fp i32;
asset_id: b256
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions sway-lib-std/src/constants.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Base asset and zero address constants.
library;

use ::contract_id::ContractId;
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
use ::contract_id::AssetId;

/// The `BASE_ASSET_ID` represents the base asset of a chain.
///
Expand All @@ -18,7 +18,7 @@ use ::contract_id::ContractId;
/// assert(BASE_ASSET_ID == msg_asset_id());
/// }
/// ```
pub const BASE_ASSET_ID = ZERO_B256;
pub const BASE_ASSET_ID: AssetId = AssetId::from(ZERO_B256);

/// A B256 of zero value.
///
Expand Down
5 changes: 2 additions & 3 deletions sway-lib-std/src/context.sw
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Functionality for accessing context-specific information about the current contract or message.
library;

use ::alias::AssetId;
use ::contract_id::ContractId;
use ::call_frames::contract_id;
use ::contract_id::{AssetId, ContractId};
use ::registers::balance;

/// Get the balance of coin `asset_id` for the current contract.
Expand Down Expand Up @@ -52,7 +51,7 @@ pub fn this_balance(asset_id: AssetId) -> u64 {
/// }
/// ```
pub fn balance_of(target: ContractId, asset_id: AssetId) -> u64 {
asm(balance, token: asset_id, id: target.value) {
asm(balance, token: asset_id.value, id: target.value) {
bal balance token id;
balance: u64
}
Expand Down