Skip to content

Commit

Permalink
replace the Result type with TransferResult
Browse files Browse the repository at this point in the history
  • Loading branch information
tomijaga committed Jan 21, 2023
1 parent d3155f1 commit 7af28bb
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 44 deletions.
4 changes: 2 additions & 2 deletions docs/ICRC1/Types.html

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion docs/ICRC1/Types.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,15 @@ type TransferError = TimeError or {#BadFee : { expected_fee : Balance }; #BadBur
```


## Type `TransferResult`
``` motoko no-repl
type TransferResult = {#Ok : TxIndex; #Err : TransferError}
```


## Type `TokenInterface`
``` motoko no-repl
type TokenInterface = actor { icrc1_name : shared query () -> async Text; icrc1_symbol : shared query () -> async Text; icrc1_decimals : shared query () -> async Nat8; icrc1_fee : shared query () -> async Balance; icrc1_metadata : shared query () -> async MetaData; icrc1_total_supply : shared query () -> async Balance; icrc1_minting_account : shared query () -> async ?Account; icrc1_balance_of : shared query (Account) -> async Balance; icrc1_transfer : shared (TransferArgs) -> async Result.Result<TxIndex, TransferError>; icrc1_supported_standards : shared query () -> async [SupportedStandard] }
type TokenInterface = actor { icrc1_name : shared query () -> async Text; icrc1_symbol : shared query () -> async Text; icrc1_decimals : shared query () -> async Nat8; icrc1_fee : shared query () -> async Balance; icrc1_metadata : shared query () -> async MetaData; icrc1_total_supply : shared query () -> async Balance; icrc1_minting_account : shared query () -> async ?Account; icrc1_balance_of : shared query (Account) -> async Balance; icrc1_transfer : shared (TransferArgs) -> async TransferResult; icrc1_supported_standards : shared query () -> async [SupportedStandard] }
```

Interface for the ICRC token canister
Expand Down
8 changes: 4 additions & 4 deletions docs/ICRC1/lib.html

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions docs/ICRC1/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ type ArchivedTransaction = T.ArchivedTransaction
```


## Type `TransferResult`
``` motoko no-repl
type TransferResult = T.TransferResult
```


## Value `MAX_TRANSACTIONS_IN_LEDGER`
``` motoko no-repl
let MAX_TRANSACTIONS_IN_LEDGER
Expand Down Expand Up @@ -285,21 +291,21 @@ Formats a float to a nat balance and applies the correct number of decimal place

## Function `transfer`
``` motoko no-repl
func transfer(token : T.TokenData, args : T.TransferArgs, caller : Principal) : async Result.Result<T.Balance, T.TransferError>
func transfer(token : T.TokenData, args : T.TransferArgs, caller : Principal) : async T.TransferResult
```

Transfers tokens from one account to another account (minting and burning included)

## Function `mint`
``` motoko no-repl
func mint(token : T.TokenData, args : T.Mint, caller : Principal) : async Result.Result<T.Balance, T.TransferError>
func mint(token : T.TokenData, args : T.Mint, caller : Principal) : async T.TransferResult
```

Helper function to mint tokens with minimum args

## Function `burn`
``` motoko no-repl
func burn(token : T.TokenData, args : T.BurnArgs, caller : Principal) : async Result.Result<T.Balance, T.TransferError>
func burn(token : T.TokenData, args : T.BurnArgs, caller : Principal) : async T.TransferResult
```

Helper function to burn tokens with minimum args
Expand Down
7 changes: 3 additions & 4 deletions example/icrc1/main.mo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Iter "mo:base/Iter";
import Option "mo:base/Option";
import Result "mo:base/Result";
import Time "mo:base/Time";

import ExperimentalCycles "mo:base/ExperimentalCycles";
Expand Down Expand Up @@ -59,15 +58,15 @@ shared ({ caller = _owner }) actor class Token(
ICRC1.supported_standards(token);
};

public shared ({ caller }) func icrc1_transfer(args : ICRC1.TransferArgs) : async Result.Result<ICRC1.Balance, ICRC1.TransferError> {
public shared ({ caller }) func icrc1_transfer(args : ICRC1.TransferArgs) : async ICRC1.TransferResult {
await ICRC1.transfer(token, args, caller);
};

public shared ({ caller }) func mint(args : ICRC1.Mint) : async Result.Result<ICRC1.Balance, ICRC1.TransferError> {
public shared ({ caller }) func mint(args : ICRC1.Mint) : async ICRC1.TransferResult {
await ICRC1.mint(token, args, caller);
};

public shared ({ caller }) func burn(args : ICRC1.BurnArgs) : async Result.Result<ICRC1.Balance, ICRC1.TransferError> {
public shared ({ caller }) func burn(args : ICRC1.BurnArgs) : async ICRC1.TransferResult {
await ICRC1.burn(token, args, caller);
};

Expand Down
5 changes: 1 addition & 4 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: test docs test_actor
.PHONY: test docs actor-test

test:
$(shell vessel bin)/moc -r $(shell vessel sources) -wasi-system-api ./tests/**/**.Test.mo
Expand All @@ -16,8 +16,5 @@ actor-test:
ref-test:
cd Dfnity-ICRC1-Reference && cargo run --bin runner -- -u http://127.0.0.1:8000 -c $(ID) -s ~/.config/dfx/identity/$(shell dfx identity whoami)/identity.pem

print:
@echo "Running reference test with ID: "

no-warn:
find src -type f -name '*.mo' -print0 | xargs -0 $(shell vessel bin)/moc -r $(shell vessel sources) -Werror -wasi-system-api
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ This repo contains the implementation of the
dfx start --background --clean
dfx deploy icrc1 --argument '( record {
name = \"<Insert Token Name>\";
symbol = \"<Insert Symbol>\";
name = "<Insert Token Name>";
symbol = "<Insert Symbol>";
decimals = 6;
fee = 1_000_000;
max_supply = 1_000_000_000_000;
initial_balances = vec {
record {
record {
owner = principal \"<Insert Principal>\";
owner = principal "<Insert Principal>";
subaccount = null;
};
100_000_000_000
100_000_000
}
};
min_burn_amount = 10_000_000;
min_burn_amount = 10_000;
minting_account = null;
advanced_settings = null;
})'
Expand Down
7 changes: 3 additions & 4 deletions src/ICRC1/Canisters/Token.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Array "mo:base/Array";
import Iter "mo:base/Iter";
import Option "mo:base/Option";
import Time "mo:base/Time";
import Result "mo:base/Result";

import ExperimentalCycles "mo:base/ExperimentalCycles";

Expand Down Expand Up @@ -64,15 +63,15 @@ shared ({ caller = _owner }) actor class Token(
ICRC1.supported_standards(token);
};

public shared ({ caller }) func icrc1_transfer(args : ICRC1.TransferArgs) : async Result.Result<ICRC1.Balance, ICRC1.TransferError> {
public shared ({ caller }) func icrc1_transfer(args : ICRC1.TransferArgs) : async ICRC1.TransferResult {
await ICRC1.transfer(token, args, caller);
};

public shared ({ caller }) func mint(args : ICRC1.Mint) : async Result.Result<ICRC1.Balance, ICRC1.TransferError> {
public shared ({ caller }) func mint(args : ICRC1.Mint) : async ICRC1.TransferResult {
await ICRC1.mint(token, args, caller);
};

public shared ({ caller }) func burn(args : ICRC1.BurnArgs) : async Result.Result<ICRC1.Balance, ICRC1.TransferError> {
public shared ({ caller }) func burn(args : ICRC1.BurnArgs) : async ICRC1.TransferResult {
await ICRC1.burn(token, args, caller);
};

Expand Down
7 changes: 6 additions & 1 deletion src/ICRC1/Types.mo
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ module {
#TemporarilyUnavailable;
#GenericError : { error_code : Nat; message : Text };
};

public type TransferResult = {
#Ok : TxIndex;
#Err : TransferError;
};

/// Interface for the ICRC token canister
public type TokenInterface = actor {
Expand Down Expand Up @@ -152,7 +157,7 @@ module {
icrc1_balance_of : shared query (Account) -> async Balance;

/// Transfers the given amount of tokens from the sender to the recipient
icrc1_transfer : shared (TransferArgs) -> async Result.Result<TxIndex, TransferError>;
icrc1_transfer : shared (TransferArgs) -> async TransferResult;

/// Returns the standards supported by this token's implementation
icrc1_supported_standards : shared query () -> async [SupportedStandard];
Expand Down
19 changes: 8 additions & 11 deletions src/ICRC1/lib.mo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Nat64 "mo:base/Nat64";
import Nat8 "mo:base/Nat8";
import Option "mo:base/Option";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import EC "mo:base/ExperimentalCycles";

import Itertools "mo:itertools/Iter";
Expand Down Expand Up @@ -59,6 +58,8 @@ module {
public type TransactionRange = T.TransactionRange;
public type ArchivedTransaction = T.ArchivedTransaction;

public type TransferResult = T.TransferResult;

public let MAX_TRANSACTIONS_IN_LEDGER = 2000;
public let MAX_TRANSACTION_BYTES : Nat64 = 196;
public let MAX_TRANSACTIONS_PER_REQUEST = 5000;
Expand Down Expand Up @@ -94,10 +95,6 @@ module {
Debug.trap("minting_account is invalid");
};

if (max_supply < 10 ** Nat8.toNat(decimals)) {
Debug.trap("max_supply must be >= 1");
};

let accounts : T.AccountBalances = StableTrieMap.new();

var _minted_tokens = _burned_tokens;
Expand Down Expand Up @@ -233,7 +230,7 @@ module {
token : T.TokenData,
args : T.TransferArgs,
caller : Principal,
) : async Result.Result<T.Balance, T.TransferError> {
) : async T.TransferResult {

let from = {
owner = caller;
Expand All @@ -252,7 +249,7 @@ module {

switch (Transfer.validate_request(token, tx_req)) {
case (#err(errorType)) {
return #err(errorType);
return #Err(errorType);
};
case (#ok(_)) {};
};
Expand Down Expand Up @@ -283,14 +280,14 @@ module {
// transfer transaction to archive if necessary
await update_canister(token);

#ok(tx.index);
#Ok(tx.index);
};

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

if (caller != token.minting_account.owner) {
return #err(
return #Err(
#GenericError {
error_code = 401;
message = "Unauthorized: Only the minting_account can mint tokens.";
Expand All @@ -307,7 +304,7 @@ module {
};

/// Helper function to burn tokens with minimum args
public func burn(token : T.TokenData, args : T.BurnArgs, caller : Principal) : async Result.Result<T.Balance, T.TransferError> {
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;
Expand Down
10 changes: 5 additions & 5 deletions tests/ICRC1/ICRC1.ActorTest.mo
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ module {
);

assertAllTrue([
res == #ok(0),
res == #Ok(0),
ICRC1.balance_of(token, user1) == mint_args.amount,
ICRC1.balance_of(token, args.minting_account) == 0,
ICRC1.total_supply(token) == mint_args.amount,
Expand Down Expand Up @@ -440,7 +440,7 @@ module {
let res = await ICRC1.burn(token, burn_args, user1.owner);

assertAllTrue([
res == #ok(1),
res == #Ok(1),
ICRC1.balance_of(token, user1) == ((prev_balance - burn_args.amount) : Nat),
ICRC1.total_supply(token) == ((prev_total_supply - burn_args.amount) : Nat),
]);
Expand All @@ -465,7 +465,7 @@ module {
let res = await ICRC1.burn(token, burn_args, user1.owner);

assertAllTrue([
res == #err(
res == #Err(
#InsufficientFunds {
balance = 0;
},
Expand Down Expand Up @@ -503,7 +503,7 @@ module {
let res = await ICRC1.burn(token, burn_args, user1.owner);

assertAllTrue([
res == #err(
res == #Err(
#BadBurn {
min_burn_amount = 10 * (10 ** 8);
},
Expand Down Expand Up @@ -552,7 +552,7 @@ module {


assertAllTrue([
res == #ok(1),
res == #Ok(1),
ICRC1.balance_of(token, user1) == ICRC1.balance_from_float(token, 145),
token._burned_tokens == ICRC1.balance_from_float(token, 5),
ICRC1.balance_of(token, user2) == ICRC1.balance_from_float(token, 50),
Expand Down

0 comments on commit 7af28bb

Please sign in to comment.