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

fix up variant and func types, fix icrc #1316

Merged
merged 1 commit into from
Sep 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ jobs:
"examples/guard_functions",
"examples/heartbeat",
"examples/ic_api",
"examples/icrc",
"examples/imports",
"examples/init",
"examples/inspect_message",
Expand Down
30 changes: 15 additions & 15 deletions canisters/icrc/icrc_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ import {
} from './errors';

// Number of nanoseconds since the UNIX epoch in UTC timezone.
export type ICRC1Timestamp = nat64;
export const ICRC1Timestamp = nat64;
export type Timestamp = nat64;
export const Timestamp = nat64;

export type ICRC1Subaccount = blob;
export const ICRC1Subaccount = blob;
export type Subaccount = blob;
export const Subaccount = blob;

export const ICRC1Account = Record({
export const Account = Record({
owner: Principal,
subaccount: Opt(ICRC1Subaccount)
subaccount: Opt(Subaccount)
});

export const ICRC1TransferArgs = Record({
from_subaccount: Opt(ICRC1Subaccount),
to: ICRC1Account,
export const TransferArgs = Record({
from_subaccount: Opt(Subaccount),
to: Account,
amount: nat,
fee: Opt(nat),
memo: Opt(blob),
created_at_time: Opt(ICRC1Timestamp)
created_at_time: Opt(Timestamp)
});

const CreatedInFuture = Record({
ledger_time: ICRC1Timestamp
ledger_time: Timestamp
});

export const ICRC1TransferError = Variant({
export const TransferError = Variant({
BadFee,
BadBurn,
InsufficientFunds,
Expand All @@ -54,12 +54,12 @@ export const ICRC1TransferError = Variant({
GenericError: GenericError
});

export const ICRC1TransferResult = Variant({
export const TransferResult = Variant({
Ok: nat,
Err: ICRC1TransferError
Err: TransferError
});

export const ICRC1Value = Variant({
export const Value = Variant({
Nat: nat,
Int: int,
Text: text,
Expand Down
36 changes: 16 additions & 20 deletions canisters/icrc/icrc_2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ import {
Duplicate,
GenericError
} from './errors';
import { Account } from './icrc_1';

export const ICRC2Account = Record({
owner: Principal,
subaccount: Opt(blob)
});

export const ICRC2ApproveArgs = Record({
export const ApproveArgs = Record({
from_subaccount: Opt(blob),
spender: ICRC2Account,
spender: Account,
amount: nat,
expected_allowance: Opt(nat),
expires_at: Opt(nat64),
Expand All @@ -49,7 +45,7 @@ export const InsufficientAllowance = Record({
allowance: nat
});

export const ICRC2ApproveError = Variant({
export const ApproveError = Variant({
BadFee,
InsufficientFunds,
AllowanceChanged,
Expand All @@ -61,16 +57,16 @@ export const ICRC2ApproveError = Variant({
GenericError: GenericError
});

export const ICRC2TransferFromArgs = Record({
from: ICRC2Account,
to: ICRC2Account,
export const TransferFromArgs = Record({
from: Account,
to: Account,
amount: nat,
fee: Opt(nat),
memo: Opt(blob),
created_at_time: Opt(nat64)
});

export const ICRC2TransferFromError = Variant({
export const TransferFromError = Variant({
BadFee,
BadBurn,
InsufficientFunds,
Expand All @@ -82,22 +78,22 @@ export const ICRC2TransferFromError = Variant({
GenericError
});

export const ICRC2AllowanceArgs = Record({
account: ICRC2Account,
spender: ICRC2Account
export const AllowanceArgs = Record({
account: Account,
spender: Account
});

export const ICRC2ApproveResult = Variant({
export const ApproveResult = Variant({
Ok: nat,
Err: ICRC2ApproveError
Err: ApproveError
});

export const ICRC2TransferFromResult = Variant({
export const TransferFromResult = Variant({
Ok: nat,
Err: ICRC2TransferFromError
Err: TransferFromError
});

export const ICRC2AllowanceResults = Record({
export const AllowanceResult = Record({
allowance: nat,
expires_at: Opt(nat64)
});
96 changes: 30 additions & 66 deletions canisters/icrc/index.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,45 @@
import {
Canister,
nat,
nat8,
Opt,
Record,
Service,
query,
update,
text,
Tuple,
Vec,
candid
} from '../../src/lib_new';
Vec
} from '../../src/lib_functional';
import { Account, TransferArgs, TransferResult, Value } from './icrc_1';
import {
ICRC1Account,
ICRC1TransferArgs,
ICRC1TransferResult,
ICRC1Value
} from './icrc_1';
import {
ICRC2AllowanceArgs,
ICRC2AllowanceResults,
ICRC2ApproveArgs,
ICRC2ApproveResult,
ICRC2TransferFromArgs,
ICRC2TransferFromResult
AllowanceArgs,
AllowanceResult,
ApproveArgs,
ApproveResult,
TransferFromArgs,
TransferFromResult
} from './icrc_2';

export class ICRC1SupportedStandard extends Record {
@candid(text)
name: text;

@candid(text)
url: text;
}

export class ICRC extends Service {
@query([], Vec(Tuple(text, ICRC1Value)))
icrc1_metadata: () => [text, ICRC1Value][];

@query([], text)
icrc1_name: () => text;

@query([], text)
icrc1_symbol: () => text;

@query([], nat8)
icrc1_decimals: () => nat8;

@query([], nat)
icrc1_fee: () => nat;

@query([], nat)
icrc1_total_supply: () => nat;

@query([], Opt(ICRC1Account))
icrc1_minting_account: () => Opt<ICRC1Account>;

@query([ICRC1Account], nat)
icrc1_balance_of: (account: ICRC1Account) => nat;

@update([ICRC1TransferArgs], ICRC1TransferResult)
icrc1_transfer: (transferArgs: ICRC1TransferArgs) => ICRC1TransferResult;

@query([], Vec(ICRC1SupportedStandard))
icrc1_supported_standards: () => Vec<ICRC1SupportedStandard>;

@update([ICRC2ApproveArgs], ICRC2ApproveResult)
icrc2_approve: (args: ICRC2ApproveArgs) => ICRC2ApproveResult;

@update([ICRC2TransferFromArgs], ICRC2TransferFromResult)
icrc2_transfer_from: (
args: ICRC2TransferFromArgs
) => ICRC2TransferFromResult;

@query([ICRC2AllowanceArgs], ICRC2AllowanceResults)
icrc2_allowance: (args: ICRC2AllowanceArgs) => ICRC2AllowanceResults;
}
export const SupportedStandard = Record({
name: text,
url: text
});

export const ICRC = Canister({
icrc1_metadata: query([], Vec(Tuple(text, Value))),
icrc1_name: query([], text),
icrc1_symbol: query([], text),
icrc1_decimals: query([], nat8),
icrc1_fee: query([], nat),
icrc1_total_supply: query([], nat),
icrc1_minting_account: query([], Opt(Account)),
icrc1_balance_of: query([Account], nat),
icrc1_transfer: update([TransferArgs], TransferResult),
icrc1_supported_standards: query([], Vec(SupportedStandard)),
icrc2_approve: update([ApproveArgs], ApproveResult),
icrc2_transfer_from: update([TransferFromArgs], TransferFromResult),
icrc2_allowance: query([AllowanceArgs], AllowanceResult)
});

export * from './icrc_1';
export * from './icrc_2';
18 changes: 6 additions & 12 deletions canisters/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ import {
Principal,
Func
} from '../../src/lib_functional';
import {
ICRC1Account,
ICRC1SupportedStandard,
ICRC1TransferArgs,
ICRC1TransferResult,
ICRC1Value
} from '../icrc';
import * as icrc from '../icrc';

// Amount of tokens, measured in 10^-8 of a token.
export const Tokens = Record({
Expand Down Expand Up @@ -316,16 +310,16 @@ export const Ledger = Canister({
decimals: query([], DecimalsResult),
// Returns the existing archive canisters information.
archives: query([], Archives),
icrc1_metadata: query([], Vec(Tuple(text, ICRC1Value))),
icrc1_metadata: query([], Vec(Tuple(text, icrc.Value))),
icrc1_name: query([], text),
icrc1_symbol: query([], text),
icrc1_decimals: query([], nat8),
icrc1_fee: query([], nat),
icrc1_total_supply: query([], nat),
icrc1_minting_account: query([], Opt(ICRC1Account)),
icrc1_balance_of: query([ICRC1Account], nat),
icrc1_transfer: update([ICRC1TransferArgs], ICRC1TransferResult),
icrc1_supported_standards: query([], Vec(ICRC1SupportedStandard))
icrc1_minting_account: query([], Opt(icrc.Account)),
icrc1_balance_of: query([icrc.Account], nat),
icrc1_transfer: update([icrc.TransferArgs], icrc.TransferResult),
icrc1_supported_standards: query([], Vec(icrc.SupportedStandard))
});

export {
Expand Down
4 changes: 3 additions & 1 deletion examples/icrc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ cd canisters/ICRC-1/test

# Change canisters/ICRC-1/test/suite/lib.rs test_burn assert_balance(&env, tmp_account, 0).await?; to assert_balance(&env, tmp_account, 10_000).await?;

cargo run -- -u http://127.0.0.1:8000 -c r7inp-6aaaa-aaaaa-aaabq-cai -s /home/lastmjs/.config/dfx/identity/test/identity.pem
# cargo run -- -u http://127.0.0.1:8000 -c r7inp-6aaaa-aaaaa-aaabq-cai -s /home/lastmjs/.config/dfx/identity/test/identity.pem

cargo run --bin runner -- -u http://127.0.0.1:8000 -c r7inp-6aaaa-aaaaa-aaabq-cai -s /home/lastmjs/.config/dfx/identity/test_unencrypted/identity.pem
```
Loading