Skip to content

Commit

Permalink
Updating to latest ICRC Standards
Browse files Browse the repository at this point in the history
  • Loading branch information
panindustrial-dev committed Mar 17, 2024
1 parent c1cce3a commit 9d5aab6
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 46 deletions.
16 changes: 8 additions & 8 deletions mops.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[dependencies]
base = "https://github.com/dfinity/motoko-base#moc-0.10.1@80ededb39954739b18b88fc0e5561264003cf9ab"
base = "0.11.0"
cert = "https://github.com/nomeata/ic-certification#v0.1.3@c403ffec0a60f658a1009976c785aa567ff0da77"
icrc1-mo = "0.0.10"
icrc2-mo = "0.0.8"
icrc3-mo = "0.2.3"
icrc4-mo = "0.0.1"
icrc1-mo = "0.0.14"
icrc2-mo = "0.0.12"
icrc3-mo = "0.2.4"
icrc4-mo = "0.0.4"

[dev-dependencies]
test = "1.2.0"
Expand All @@ -13,7 +13,7 @@ matchers = "https://github.com/kritzcreek/motoko-matchers#v1.3.0@3dac8a071b69e4e

[package]
name = "icrc-fungible"
version = "0.0.2"
description = "ICRC1, ICRC2, and ICRC3 fungible token for motoko"
version = "0.0.3"
description = "ICRC1, ICRC2, ICRC3, ICRC4, and ICRC61 fungible token for motoko"
repository = "hhttps://github.com/PanIndustrial-Org/ICRC_fungible"
keywords = [ "icrc2", "icrc1", "icrc3", "fungible", "token", "standard" ]
keywords = [ "icrc2", "icrc1", "icrc3", "icrc4", "icrc61", "fungible", "token", "standard" ]
6 changes: 6 additions & 0 deletions runners/prod_deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ icrc3 = opt record {
archiveIndexType = variant {Stable = null};
maxRecordsToArchive = 8000;
archiveCycles = 20_000_000_000_000;
supportedBlocks = vec {};
archiveControllers = null;
};
icrc4 = opt record {
max_balances = opt 200;
max_transfers = opt 200;
fee = opt variant { ICRC1 = null};
};})" --mode reinstall

# Fetch the canister ID after deployment
Expand Down
6 changes: 6 additions & 0 deletions runners/test_deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ icrc3 = opt record {
archiveIndexType = variant {Stable = null};
maxRecordsToArchive = 8000;
archiveCycles = 20_000_000_000_000;
supportedBlocks = vec {};
archiveControllers = null;
};
icrc4 = opt record {
max_balances = opt 200;
max_transfers = opt 200;
fee = opt variant { ICRC1 = null};
};})" --mode reinstall

ICRC_CANISTER=$(dfx canister id token)
Expand Down
101 changes: 89 additions & 12 deletions src/Token.mo
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Array "mo:base/Array";
import Buffer "mo:base/Buffer";
import D "mo:base/Debug";
import ExperimentalCycles "mo:base/ExperimentalCycles";
import Iter "mo:base/Iter";
import Option "mo:base/Option";

import Principal "mo:base/Principal";
import Time "mo:base/Time";

Expand All @@ -17,7 +16,7 @@ import ICRC4 "mo:icrc4-mo/ICRC4";
shared ({ caller = _owner }) actor class Token (args: ?{
icrc1 : ?ICRC1.InitArgs;
icrc2 : ?ICRC2.InitArgs;
icrc3 : ?ICRC3.InitArgs;
icrc3 : ICRC3.InitArgs; //already typed nullable
icrc4 : ?ICRC4.InitArgs;
}
) = this{
Expand Down Expand Up @@ -62,11 +61,29 @@ shared ({ caller = _owner }) actor class Token (args: ?{
maxRecordsToArchive = 8000;
archiveCycles = 20_000_000_000_000;
archiveControllers = null; //??[put cycle ops prinicpal here];
supportedBlocks = [
{
block_type = "1xfer";
url="https://github.com/dfinity/ICRC-1/tree/main/standards/ICRC-3";
},
{
block_type = "1approve";
url="https://github.com/dfinity/ICRC-1/tree/main/standards/ICRC-3";
},
{
block_type = "1mint";
url="https://github.com/dfinity/ICRC-1/tree/main/standards/ICRC-3";
},
{
block_type = "1burn";
url="https://github.com/dfinity/ICRC-1/tree/main/standards/ICRC-3";
}
];
};

let default_icrc4_args : ICRC4.InitArgs = {
max_balances = ?3000;
max_transfers = ?3000;
max_balances = ?200;
max_transfers = ?200;
fee = ?#ICRC1;
};

Expand Down Expand Up @@ -107,7 +124,7 @@ shared ({ caller = _owner }) actor class Token (args: ?{
case(?args){
switch(args.icrc3){
case(null) default_icrc3_args;
case(?val) val;
case(?val) ?val;
};
};
};
Expand Down Expand Up @@ -155,7 +172,11 @@ shared ({ caller = _owner }) actor class Token (args: ?{
let initclass : ICRC1.ICRC1 = ICRC1.ICRC1(?icrc1_migration_state, Principal.fromActor(this), get_icrc1_environment());
ignore initclass.register_supported_standards({
name = "ICRC-3";
url = "https://github.com/dfinity/ICRC-1/tree/icrc-3/standards/ICRC-3"
url = "https://github.com/dfinity/ICRC/ICRCs/icrc-3/"
});
ignore initclass.register_supported_standards({
name = "ICRC-61";
url = "https://github.com/dfinity/ICRC/ICRCs/icrc-61/"
});
_icrc1 := ?initclass;
initclass;
Expand Down Expand Up @@ -239,11 +260,51 @@ shared ({ caller = _owner }) actor class Token (args: ?{
};
};

func ensure_block_types(icrc3Class: ICRC3.ICRC3) : () {
let supportedBlocks = Buffer.fromIter<ICRC3.BlockType>(icrc3Class.supported_block_types().vals());

let blockequal = func(a : {block_type: Text}, b : {block_type: Text}) : Bool {
a.block_type == b.block_type;
};

if(Buffer.indexOf<ICRC3.BlockType>({block_type = "1xfer"; url="";}, supportedBlocks, blockequal) == null){
supportedBlocks.add({
block_type = "1xfer";
url="https://github.com/dfinity/ICRC-1/tree/main/standards/ICRC-3";
});
};

if(Buffer.indexOf<ICRC3.BlockType>({block_type = "1approve";url="";}, supportedBlocks, blockequal) == null){
supportedBlocks.add({
block_type = "1approve";
url="https://github.com/dfinity/ICRC-1/tree/main/standards/ICRC-3";
});
};

if(Buffer.indexOf<ICRC3.BlockType>({block_type = "1mint";url="";}, supportedBlocks, blockequal) == null){
supportedBlocks.add({
block_type = "1mint";
url="https://github.com/dfinity/ICRC-1/tree/main/standards/ICRC-3";
});
};

if(Buffer.indexOf<ICRC3.BlockType>({block_type = "1burn";url="";}, supportedBlocks, blockequal) == null){
supportedBlocks.add({
block_type = "1burn";
url="https://github.com/dfinity/ICRC-1/tree/main/standards/ICRC-3";
});
};

icrc3Class.update_supported_blocks(Buffer.toArray(supportedBlocks));
};

func icrc3() : ICRC3.ICRC3 {
switch(_icrc3){
case(null){
let initclass : ICRC3.ICRC3 = ICRC3.ICRC3(?icrc3_migration_state, Principal.fromActor(this), get_icrc3_environment());
_icrc3 := ?initclass;
ensure_block_types(initclass);

initclass;
};
case(?val) val;
Expand Down Expand Up @@ -300,6 +361,10 @@ shared ({ caller = _owner }) actor class Token (args: ?{
icrc1().supported_standards();
};

public shared query func icrc61_supported_standards() : async [ICRC1.SupportedStandard] {
icrc1().supported_standards();
};

public shared ({ caller }) func icrc1_transfer(args : ICRC1.TransferArgs) : async ICRC1.TransferResult {
switch(await* icrc1().transfer_tokens(caller, args, false, null)){
case(#trappable(val)) val;
Expand Down Expand Up @@ -363,23 +428,35 @@ shared ({ caller = _owner }) actor class Token (args: ?{
return icrc3().get_tip_certificate();
};

public query func icrc3_supported_block_types() : async [ICRC3.BlockType] {
return icrc3().supported_block_types();
};

public query func get_tip() : async ICRC3.Tip {
return icrc3().get_tip();
};

public shared ({ caller }) func icrc4_transfer_batch(args: ICRC4.TransferBatchArgs) : async ICRC4.TransferBatchResult {
public shared ({ caller }) func icrc4_transfer_batch(args: ICRC4.TransferBatchArgs) : async ICRC4.TransferBatchResults {
switch(await* icrc4().transfer_batch_tokens(caller, args, null, null)){
case(#trappable(val)) val;
case(#awaited(val)) val;
case(#err(#trappable(err))) D.trap(err);
case(#err(#awaited(err))) D.trap(err);
case(#err(#trappable(err))) err;
case(#err(#awaited(err))) err;
};
};

public shared query func icrc4_balance_of_batch(request : ICRC4.BalanceQueryArgs) : async ICRC4.BalanceQueryResult {
icrc4().balance_of_batch(request);
};

public shared query func icrc4_maximum_update_batch_size() : async ?Nat {
?icrc4().get_state().ledger_info.max_transfers;
};

public shared query func icrc4_maximum_query_batch_size() : async ?Nat {
?icrc4().get_state().ledger_info.max_balances;
};

public shared ({ caller }) func admin_update_owner(new_owner : Principal) : async Bool {
if(caller != owner){ D.trap("Unauthorized")};
owner := new_owner;
Expand Down Expand Up @@ -444,7 +521,7 @@ shared ({ caller = _owner }) actor class Token (args: ?{
// Deposit cycles into this canister.
public shared func deposit_cycles() : async () {
let amount = ExperimentalCycles.available();
let accepted = ExperimentalCycles.accept(amount);
let accepted = ExperimentalCycles.accept<system>(amount);
assert (accepted == amount);
};

Expand Down
Loading

0 comments on commit 9d5aab6

Please sign in to comment.