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

Adding await star #6

Merged
merged 2 commits into from Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 3 additions & 8 deletions dfx.json
@@ -1,6 +1,6 @@
{
"version": 1,
"dfx": "0.12.1",
"dfx": "0.13.1",
"canisters": {
"icrc1": {
"type": "motoko",
Expand All @@ -18,14 +18,9 @@
},
"defaults": {
"build": {
"packtool": "mops sources",
"packtool": "vessel sources",
"args": ""
}
},
"networks": {
"local": {
"bind": "127.0.0.1:8000",
"type": "ephemeral"
}
}

}
8 changes: 4 additions & 4 deletions src/ICRC1/Canisters/Token.mo
Expand Up @@ -64,15 +64,15 @@ shared ({ caller = _owner }) actor class Token(
};

public shared ({ caller }) func icrc1_transfer(args : ICRC1.TransferArgs) : async ICRC1.TransferResult {
await ICRC1.transfer(token, args, caller);
await* ICRC1.transfer(token, args, caller);
};

public shared ({ caller }) func mint(args : ICRC1.Mint) : async ICRC1.TransferResult {
await ICRC1.mint(token, args, caller);
await* ICRC1.mint(token, args, caller);
};

public shared ({ caller }) func burn(args : ICRC1.BurnArgs) : async ICRC1.TransferResult {
await ICRC1.burn(token, args, caller);
await* ICRC1.burn(token, args, caller);
};

// Functions for integration with the rosetta standard
Expand All @@ -82,7 +82,7 @@ shared ({ caller = _owner }) actor class Token(

// Additional functions not included in the ICRC1 standard
public shared func get_transaction(i : ICRC1.TxIndex) : async ?ICRC1.Transaction {
await ICRC1.get_transaction(token, i);
await* ICRC1.get_transaction(token, i);
};

// Deposit cycles into this canister.
Expand Down
4 changes: 2 additions & 2 deletions src/ICRC1/Transfer.mo
Expand Up @@ -167,7 +167,7 @@ module {
return #err(
#GenericError({
error_code = 0;
message = "Invalid account entered for sender.";
message = "Invalid account entered for sender. " # debug_show(tx_req.from);
}),
);
};
Expand All @@ -176,7 +176,7 @@ module {
return #err(
#GenericError({
error_code = 0;
message = "Invalid account entered for recipient";
message = "Invalid account entered for recipient " # debug_show(tx_req.to);
}),
);
};
Expand Down
20 changes: 10 additions & 10 deletions src/ICRC1/lib.mo
Expand Up @@ -230,7 +230,7 @@ module {
token : T.TokenData,
args : T.TransferArgs,
caller : Principal,
) : async T.TransferResult {
) : async* T.TransferResult {

let from = {
owner = caller;
Expand Down Expand Up @@ -278,13 +278,13 @@ module {
SB.add(token.transactions, tx);

// transfer transaction to archive if necessary
await update_canister(token);
await* update_canister(token);

#Ok(tx.index);
};

/// Helper function to mint tokens with minimum args
public func mint(token : T.TokenData, args : T.Mint, caller : Principal) : async T.TransferResult {
public func mint(token : T.TokenData, args : T.Mint, caller : Principal) : async* T.TransferResult {

if (caller != token.minting_account.owner) {
return #Err(
Expand All @@ -300,18 +300,18 @@ module {
fee = null;
};

await transfer(token, transfer_args, caller);
await* transfer(token, transfer_args, caller);
};

/// Helper function to burn tokens with minimum args
public func burn(token : T.TokenData, args : T.BurnArgs, caller : Principal) : async T.TransferResult {
public func burn(token : T.TokenData, args : T.BurnArgs, caller : Principal) : async* T.TransferResult {

let transfer_args : T.TransferArgs = {
args with to = token.minting_account;
fee = null;
};

await transfer(token, transfer_args, caller);
await* transfer(token, transfer_args, caller);
};

/// Returns the total number of transactions that have been processed by the given token.
Expand All @@ -321,7 +321,7 @@ module {
};

/// Retrieves the transaction specified by the given `tx_index`
public func get_transaction(token : T.TokenData, tx_index : T.TxIndex) : async ?T.Transaction {
public func get_transaction(token : T.TokenData, tx_index : T.TxIndex) : async* ?T.Transaction {
let { archive; transactions } = token;

let archived_txs = archive.stored_txs;
Expand Down Expand Up @@ -395,17 +395,17 @@ module {
// Updates the token's data and manages the transactions
//
// **added at the end of any function that creates a new transaction**
func update_canister(token : T.TokenData) : async () {
func update_canister(token : T.TokenData) : async* () {
let txs_size = SB.size(token.transactions);

if (txs_size >= MAX_TRANSACTIONS_IN_LEDGER) {
await append_transactions(token);
await* append_transactions(token);
};
};

// Moves the transactions from the ICRC1 canister to the archive canister
// and returns a boolean that indicates the success of the data transfer
func append_transactions(token : T.TokenData) : async () {
func append_transactions(token : T.TokenData) : async* () {
let { archive; transactions } = token;

if (archive.stored_txs == 0) {
Expand Down
28 changes: 14 additions & 14 deletions tests/ICRC1/ICRC1.ActorTest.mo
Expand Up @@ -193,7 +193,7 @@ module {

func create_mints(token : T.TokenData, minting_principal : Principal, n : Nat) : async () {
for (i in Itertools.range(0, n)) {
ignore await ICRC1.mint(
ignore await* ICRC1.mint(
token,
{
to = user1;
Expand Down Expand Up @@ -389,7 +389,7 @@ module {
created_at_time = null;
};

let res = await ICRC1.mint(
let res = await* ICRC1.mint(
token,
mint_args,
args.minting_account.owner,
Expand Down Expand Up @@ -421,7 +421,7 @@ module {
created_at_time = null;
};

ignore await ICRC1.mint(
ignore await* ICRC1.mint(
token,
mint_args,
args.minting_account.owner,
Expand All @@ -437,7 +437,7 @@ module {
let prev_balance = ICRC1.balance_of(token, user1);
let prev_total_supply = ICRC1.total_supply(token);

let res = await ICRC1.burn(token, burn_args, user1.owner);
let res = await* ICRC1.burn(token, burn_args, user1.owner);

assertAllTrue([
res == #Ok(1),
Expand All @@ -462,7 +462,7 @@ module {

let prev_balance = ICRC1.balance_of(token, user1);
let prev_total_supply = ICRC1.total_supply(token);
let res = await ICRC1.burn(token, burn_args, user1.owner);
let res = await* ICRC1.burn(token, burn_args, user1.owner);

assertAllTrue([
res == #Err(
Expand All @@ -487,7 +487,7 @@ module {
created_at_time = null;
};

ignore await ICRC1.mint(
ignore await* ICRC1.mint(
token,
mint_args,
args.minting_account.owner,
Expand All @@ -500,7 +500,7 @@ module {
created_at_time = null;
};

let res = await ICRC1.burn(token, burn_args, user1.owner);
let res = await* ICRC1.burn(token, burn_args, user1.owner);

assertAllTrue([
res == #Err(
Expand Down Expand Up @@ -529,7 +529,7 @@ module {
created_at_time = null;
};

ignore await ICRC1.mint(
ignore await* ICRC1.mint(
token,
mint_args,
args.minting_account.owner,
Expand All @@ -544,7 +544,7 @@ module {
created_at_time = null;
};

let res = await ICRC1.transfer(
let res = await* ICRC1.transfer(
token,
transfer_args,
user1.owner,
Expand Down Expand Up @@ -590,23 +590,23 @@ module {
do {
assertAllTrue([
is_opt_tx_equal(
(await ICRC1.get_transaction(token, 0)),
(await* ICRC1.get_transaction(token, 0)),
?mock_tx(user1, 0),
),
is_opt_tx_equal(
(await ICRC1.get_transaction(token, 1234)),
(await* ICRC1.get_transaction(token, 1234)),
?mock_tx(user1, 1234),
),
is_opt_tx_equal(
(await ICRC1.get_transaction(token, 2000)),
(await* ICRC1.get_transaction(token, 2000)),
?mock_tx(user1, 2000),
),
is_opt_tx_equal(
(await ICRC1.get_transaction(token, 4100)),
(await* ICRC1.get_transaction(token, 4100)),
?mock_tx(user1, 4100),
),
is_opt_tx_equal(
(await ICRC1.get_transaction(token, 4122)),
(await* ICRC1.get_transaction(token, 4122)),
?mock_tx(user1, 4122),
),
]);
Expand Down