Skip to content

Commit

Permalink
Adding tests for mint and burn (FuelLabs#679)
Browse files Browse the repository at this point in the history
* Create empty token lib

* Add todos

* Add mint & burn funcs

* Add outline of abi, contract and test

* It builds

* Redeploy to get Id

* Add to mod.rs

* Add force_transfer() func

* Add force_transfer() func

* Update color to token_id and redeploy

* Apply formatting

* Start on tto

* fixup

* Fix ETH_COLOR to be ETH_ID

* Rough out transfer_to_output

* It builds

* Add notes to test

* Add new constants

* Switch to local constants

* Refactoring

* Start cleanup

* Clean up temp functions and asm blocks

* Update dep paths in toml files

* Simplify while loop

* Fixup

* Remove temp local variables and nested loop

* Fixup

* Fixup

* Formatting

* remove redundant abi & contract

* Fixup

* Update test setup

* Update magic number to use bytes instead of bits

* Update test token

* Update test file types

* Fix string to str

* Update toml file import

* Fix contract_id type

* update forc toml

* Update test with new balance_of_contract api

* Add notes to context.sw

* Add core to toml file

* Update types in fuel coin abi

* Fix test types

* Fixup

* Fix asm register name

* Update the fuelcoin contract deployed id

* Fix toml to use local path for development

* Update types in balance_of_contract calls

* Delete lib-std

* Update import paths in toml file

* Fix contract id in toml

* Use hardcoded contract id in test contract call

* Fix import path for std to use git path

* Fix import paths in toml files

* Rename token_id to asset_id

* Fix types in contract calls to use ContractId

* Update balance test contract id in toml

* Fixup

* Add json oracle file for contract

* Remove const NAME and just use hardcoded string

* Update balance_test_contract id in toml and test

* Fixup

* Update cargo lock file

* Update contract ids

* Temp changes to tests

* Restore tests

* Update contract id again

* Remove name func

* Minimize pr to just mint function

* Fixup

* Update contract-id and utxo id

* Fix test

* Cleanup toml

* Add burn function to abi and contract

* Update contract id and add burn test

* Update test/src/e2e_vm_tests/test_programs/test_fuel_coin_abi/Forc.toml

Co-authored-by: John Adler <[email protected]>

* Update test/src/e2e_vm_tests/test_programs/test_fuel_coin_abi/Forc.toml

Co-authored-by: John Adler <[email protected]>

* Update test/src/e2e_vm_tests/test_programs/test_fuel_coin_contract/Forc.toml

Co-authored-by: John Adler <[email protected]>

* Update test/src/e2e_vm_tests/test_programs/test_fuel_coin_contract/Forc.toml

Co-authored-by: John Adler <[email protected]>

* Update test/src/e2e_vm_tests/test_programs/token_ops_test/Forc.toml

Co-authored-by: John Adler <[email protected]>

* Update test/src/e2e_vm_tests/test_programs/token_ops_test/Forc.toml

Co-authored-by: John Adler <[email protected]>

* Update test/src/e2e_vm_tests/test_programs/token_ops_test/Forc.toml

Co-authored-by: John Adler <[email protected]>

Co-authored-by: John Adler <[email protected]>
  • Loading branch information
nfurfaro and adlerjohn authored Jan 24, 2022
1 parent d9b5a11 commit 49cfa77
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub fn run(filter_regex: Option<regex::Regex>) {
("context_testing_contract", "caller_context_test"),
("contract_abi_impl", "contract_call"),
("balance_test_contract", "bal_opcode"),
("test_fuel_coin_contract", "token_ops_test"),
];

let total_number_of_tests = positive_project_names.len()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
author = "Fuel Labs <[email protected]>"
license = "Apache-2.0"
name = "test_fuel_coin_abi"
entry = "main.sw"

[dependencies]
std = { git = "http://github.com/FuelLabs/sway-lib-std", version = "v0.0.1" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
library test_fuel_coin_abi;

use std::address::Address;
use std::contract_id::ContractId;

abi TestFuelCoin {
fn mint(gas: u64, coins: u64, asset_id: b256, mint_amount: u64);
fn burn(gas: u64, coins: u64, asset_id: b256, burn_amount: u64);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
author = "Fuel Labs <[email protected]>"
license = "Apache-2.0"
name = "test_fuel_coin_contract"
entry = "main.sw"

[dependencies]
std = { git = "http://github.com/FuelLabs/sway-lib-std", version = "v0.0.1" }
test_fuel_coin_abi = { path = "../test_fuel_coin_abi" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"components":null,"name":"gas","type":"u64"},{"components":null,"name":"coins","type":"u64"},{"components":null,"name":"asset_id","type":"b256"},{"components":null,"name":"mint_amount","type":"u64"}],"name":"mint","outputs":[{"components":null,"name":"","type":"()"}],"type":"function"},{"inputs":[{"components":null,"name":"gas","type":"u64"},{"components":null,"name":"coins","type":"u64"},{"components":null,"name":"asset_id","type":"b256"},{"components":null,"name":"burn_amount","type":"u64"}],"name":"burn","outputs":[{"components":null,"name":"","type":"()"}],"type":"function"},{"inputs":[{"components":null,"name":"gas","type":"u64"},{"components":null,"name":"coins","type":"u64"},{"components":null,"name":"asset_id","type":"b256"},{"components":[{"components":null,"name":"coins","type":"u64"},{"components":[{"components":null,"name":"value","type":"b256"}],"name":"asset_id","type":"struct ContractId"},{"components":[{"components":null,"name":"value","type":"b256"}],"name":"recipient","type":"struct Address"}],"name":"params","type":"struct ParamsTransferToOutput"}],"name":"transfer_to_output","outputs":[{"components":null,"name":"","type":"()"}],"type":"function"},{"inputs":[{"components":null,"name":"gas","type":"u64"},{"components":null,"name":"coins","type":"u64"},{"components":null,"name":"asset_id","type":"b256"},{"components":[{"components":null,"name":"coins","type":"u64"},{"components":[{"components":null,"name":"value","type":"b256"}],"name":"asset_id","type":"struct ContractId"},{"components":[{"components":null,"name":"value","type":"b256"}],"name":"c_id","type":"struct ContractId"}],"name":"params","type":"struct ParamsForceTransfer"}],"name":"force_transfer","outputs":[{"components":null,"name":"","type":"()"}],"type":"function"},{"inputs":[{"components":null,"name":"gas","type":"u64"},{"components":null,"name":"coins","type":"u64"},{"components":null,"name":"asset_id","type":"b256"},{"components":null,"name":"input","type":"()"}],"name":"name","outputs":[{"components":null,"name":"","type":"str[14]"}],"type":"function"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
contract;

use std::token::*;
use test_fuel_coin_abi::*;

impl TestFuelCoin for Contract {

// TODO add event logging
fn mint(gas: u64, coins: u64, asset_id: b256, mint_amount: u64) {
mint(coins);
}

fn burn(gas: u64, coins: u64, asset_id: b256, burn_amount: u64) {
burn(coins);
}
}
11 changes: 11 additions & 0 deletions test/src/e2e_vm_tests/test_programs/token_ops_test/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
author = "Fuel Labs <[email protected]>"
license = "Apache-2.0"
name = "token_ops_test"
entry = "main.sw"

[dependencies]
std = { git = "http://github.com/FuelLabs/sway-lib-std", version = "v0.0.1" }
core = { git = "http://github.com/FuelLabs/sway-lib-core", version = "v0.0.1" }
test_fuel_coin_abi = { path = "../test_fuel_coin_abi" }

46 changes: 46 additions & 0 deletions test/src/e2e_vm_tests/test_programs/token_ops_test/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
script;

// use std::constants::ETH_ID;
use std::context::balance_of_contract;
use std::chain::assert;
use std::address::Address;
use std::contract_id::ContractId;
use std::token::*;
use test_fuel_coin_abi::*;

struct Opts {
gas: u64,
coins: u64,
id: ContractId,
}

fn main() -> bool {

let default = Opts {
gas: 1_000_000_000_000,
coins: 0,
id: ~ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000),
};

// the deployed fuel_coin Contract_Id:
let fuelcoin_id = ~ContractId::from(0x49050d88db785fdf3b3fd35df44fbe644b9b5273ae97d9c7c19cc3e4aa361eaf);
// todo: use correct type ContractId
let fuel_coin = abi(TestFuelCoin, 0x49050d88db785fdf3b3fd35df44fbe644b9b5273ae97d9c7c19cc3e4aa361eaf);

let mut fuelcoin_balance = balance_of_contract(fuelcoin_id.value, fuelcoin_id);
assert(fuelcoin_balance == 0);

fuel_coin.mint(default.gas, default.coins, default.id.value, 11);

// check that the mint was successful
fuelcoin_balance = balance_of_contract(fuelcoin_id.value, fuelcoin_id);
assert(fuelcoin_balance == 11);

fuel_coin.burn(default.gas, default.coins, default.id.value, 7);

// check that the burn was successful
fuelcoin_balance = balance_of_contract(fuelcoin_id.value, fuelcoin_id);
assert(fuelcoin_balance == 4);

true
}

0 comments on commit 49cfa77

Please sign in to comment.