Skip to content

Commit

Permalink
feat: add support for index canister
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Feb 21, 2024
1 parent cd4d31a commit f94800d
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 2 deletions.
119 changes: 119 additions & 0 deletions cli/src/declarations/icp_index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import type {ActorMethod} from '@dfinity/agent';
import type {IDL} from '@dfinity/candid';
import type {Principal} from '@dfinity/principal';

export interface Account {
owner: Principal;
subaccount: [] | [Uint8Array | number[]];
}
export interface GetAccountIdentifierTransactionsArgs {
max_results: bigint;
start: [] | [bigint];
account_identifier: string;
}
export interface GetAccountIdentifierTransactionsError {
message: string;
}
export interface GetAccountIdentifierTransactionsResponse {
balance: bigint;
transactions: Array<TransactionWithId>;
oldest_tx_id: [] | [bigint];
}
export type GetAccountIdentifierTransactionsResult =
| {
Ok: GetAccountIdentifierTransactionsResponse;
}
| {Err: GetAccountIdentifierTransactionsError};
export interface GetAccountTransactionsArgs {
max_results: bigint;
start: [] | [bigint];
account: Account;
}
export interface GetBlocksRequest {
start: bigint;
length: bigint;
}
export interface GetBlocksResponse {
blocks: Array<Uint8Array | number[]>;
chain_length: bigint;
}
export interface HttpRequest {
url: string;
method: string;
body: Uint8Array | number[];
headers: Array<[string, string]>;
}
export interface HttpResponse {
body: Uint8Array | number[];
headers: Array<[string, string]>;
status_code: number;
}
export interface InitArg {
ledger_id: Principal;
}
export type Operation =
| {
Approve: {
fee: Tokens;
from: string;
allowance: Tokens;
expires_at: [] | [TimeStamp];
spender: string;
};
}
| {Burn: {from: string; amount: Tokens}}
| {Mint: {to: string; amount: Tokens}}
| {
Transfer: {
to: string;
fee: Tokens;
from: string;
amount: Tokens;
};
}
| {
TransferFrom: {
to: string;
fee: Tokens;
from: string;
amount: Tokens;
spender: string;
};
};
export interface Status {
num_blocks_synced: bigint;
}
export interface TimeStamp {
timestamp_nanos: bigint;
}
export interface Tokens {
e8s: bigint;
}
export interface Transaction {
memo: bigint;
icrc1_memo: [] | [Uint8Array | number[]];
operation: Operation;
created_at_time: [] | [TimeStamp];
}
export interface TransactionWithId {
id: bigint;
transaction: Transaction;
}
export interface _SERVICE {
get_account_identifier_balance: ActorMethod<[string], bigint>;
get_account_identifier_transactions: ActorMethod<
[GetAccountIdentifierTransactionsArgs],
GetAccountIdentifierTransactionsResult
>;
get_account_transactions: ActorMethod<
[GetAccountTransactionsArgs],
GetAccountIdentifierTransactionsResult
>;
get_blocks: ActorMethod<[GetBlocksRequest], GetBlocksResponse>;
http_request: ActorMethod<[HttpRequest], HttpResponse>;
icrc1_balance_of: ActorMethod<[Account], bigint>;
ledger_id: ActorMethod<[], Principal>;
status: ActorMethod<[], Status>;
}
export declare const idlFactory: IDL.InterfaceFactory;
export declare const init: ({IDL}: {IDL: IDL}) => IDL.Type[];
80 changes: 80 additions & 0 deletions cli/src/declarations/icp_index.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
type Account = record { owner : principal; subaccount : opt vec nat8 };
type GetAccountIdentifierTransactionsArgs = record {
max_results : nat64;
start : opt nat64;
account_identifier : text;
};
type GetAccountTransactionsArgs = record {
account : Account;
// The txid of the last transaction seen by the client.
// If None then the results will start from the most recent
// txid.
start : opt nat;
// Maximum number of transactions to fetch.
max_results : nat;
};
type GetAccountIdentifierTransactionsError = record { message : text };
type GetAccountIdentifierTransactionsResponse = record {
balance : nat64;
transactions : vec TransactionWithId;
oldest_tx_id : opt nat64;
};
type GetBlocksRequest = record { start : nat; length : nat };
type GetBlocksResponse = record { blocks : vec vec nat8; chain_length : nat64 };
type HttpRequest = record {
url : text;
method : text;
body : vec nat8;
headers : vec record { text; text };
};
type HttpResponse = record {
body : vec nat8;
headers : vec record { text; text };
status_code : nat16;
};
type InitArg = record { ledger_id : principal };
type Operation = variant {
Approve : record {
fee : Tokens;
from : text;
allowance : Tokens;
expires_at : opt TimeStamp;
spender : text;
};
Burn : record { from : text; amount : Tokens };
Mint : record { to : text; amount : Tokens };
Transfer : record { to : text; fee : Tokens; from : text; amount : Tokens };
TransferFrom : record {
to : text;
fee : Tokens;
from : text;
amount : Tokens;
spender : text;
};
};
type GetAccountIdentifierTransactionsResult = variant {
Ok : GetAccountIdentifierTransactionsResponse;
Err : GetAccountIdentifierTransactionsError;
};
type Status = record { num_blocks_synced : nat64 };
type TimeStamp = record { timestamp_nanos : nat64 };
type Tokens = record { e8s : nat64 };
type Transaction = record {
memo : nat64;
icrc1_memo : opt vec nat8;
operation : Operation;
created_at_time : opt TimeStamp;
};
type TransactionWithId = record { id : nat64; transaction : Transaction };
service : (InitArg) -> {
get_account_identifier_balance : (text) -> (nat64) query;
get_account_identifier_transactions : (
GetAccountIdentifierTransactionsArgs,
) -> (GetAccountIdentifierTransactionsResult) query;
get_account_transactions : (GetAccountTransactionsArgs) -> (GetAccountIdentifierTransactionsResult) query;
get_blocks : (GetBlocksRequest) -> (GetBlocksResponse) query;
http_request : (HttpRequest) -> (HttpResponse) query;
ledger_id : () -> (principal) query;
status : () -> (Status) query;
icrc1_balance_of : (Account) -> (nat64) query;
}
107 changes: 107 additions & 0 deletions cli/src/declarations/icp_index.idl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
export const idlFactory = ({IDL}) => {
const InitArg = IDL.Record({ledger_id: IDL.Principal});
const GetAccountIdentifierTransactionsArgs = IDL.Record({
max_results: IDL.Nat64,
start: IDL.Opt(IDL.Nat64),
account_identifier: IDL.Text
});
const Tokens = IDL.Record({e8s: IDL.Nat64});
const TimeStamp = IDL.Record({timestamp_nanos: IDL.Nat64});
const Operation = IDL.Variant({
Approve: IDL.Record({
fee: Tokens,
from: IDL.Text,
allowance: Tokens,
expires_at: IDL.Opt(TimeStamp),
spender: IDL.Text
}),
Burn: IDL.Record({from: IDL.Text, amount: Tokens}),
Mint: IDL.Record({to: IDL.Text, amount: Tokens}),
Transfer: IDL.Record({
to: IDL.Text,
fee: Tokens,
from: IDL.Text,
amount: Tokens
}),
TransferFrom: IDL.Record({
to: IDL.Text,
fee: Tokens,
from: IDL.Text,
amount: Tokens,
spender: IDL.Text
})
});
const Transaction = IDL.Record({
memo: IDL.Nat64,
icrc1_memo: IDL.Opt(IDL.Vec(IDL.Nat8)),
operation: Operation,
created_at_time: IDL.Opt(TimeStamp)
});
const TransactionWithId = IDL.Record({
id: IDL.Nat64,
transaction: Transaction
});
const GetAccountIdentifierTransactionsResponse = IDL.Record({
balance: IDL.Nat64,
transactions: IDL.Vec(TransactionWithId),
oldest_tx_id: IDL.Opt(IDL.Nat64)
});
const GetAccountIdentifierTransactionsError = IDL.Record({
message: IDL.Text
});
const GetAccountIdentifierTransactionsResult = IDL.Variant({
Ok: GetAccountIdentifierTransactionsResponse,
Err: GetAccountIdentifierTransactionsError
});
const Account = IDL.Record({
owner: IDL.Principal,
subaccount: IDL.Opt(IDL.Vec(IDL.Nat8))
});
const GetAccountTransactionsArgs = IDL.Record({
max_results: IDL.Nat,
start: IDL.Opt(IDL.Nat),
account: Account
});
const GetBlocksRequest = IDL.Record({
start: IDL.Nat,
length: IDL.Nat
});
const GetBlocksResponse = IDL.Record({
blocks: IDL.Vec(IDL.Vec(IDL.Nat8)),
chain_length: IDL.Nat64
});
const HttpRequest = IDL.Record({
url: IDL.Text,
method: IDL.Text,
body: IDL.Vec(IDL.Nat8),
headers: IDL.Vec(IDL.Tuple(IDL.Text, IDL.Text))
});
const HttpResponse = IDL.Record({
body: IDL.Vec(IDL.Nat8),
headers: IDL.Vec(IDL.Tuple(IDL.Text, IDL.Text)),
status_code: IDL.Nat16
});
const Status = IDL.Record({num_blocks_synced: IDL.Nat64});
return IDL.Service({
get_account_identifier_balance: IDL.Func([IDL.Text], [IDL.Nat64], ['query']),
get_account_identifier_transactions: IDL.Func(
[GetAccountIdentifierTransactionsArgs],
[GetAccountIdentifierTransactionsResult],
['query']
),
get_account_transactions: IDL.Func(
[GetAccountTransactionsArgs],
[GetAccountIdentifierTransactionsResult],
['query']
),
get_blocks: IDL.Func([GetBlocksRequest], [GetBlocksResponse], ['query']),
http_request: IDL.Func([HttpRequest], [HttpResponse], ['query']),
icrc1_balance_of: IDL.Func([Account], [IDL.Nat64], ['query']),
ledger_id: IDL.Func([], [IDL.Principal], ['query']),
status: IDL.Func([], [Status], ['query'])
});
};
export const init = ({IDL}) => {
const InitArg = IDL.Record({ledger_id: IDL.Principal});
return [InitArg];
};
37 changes: 37 additions & 0 deletions cli/src/modules/icp-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {IDL} from '@dfinity/candid';
import {Principal} from '@dfinity/principal';
import {assertNonNullish} from '@dfinity/utils';
import {init} from '../declarations/icp_index.idl';
import {Module} from '../services/modules.services';
import type {ModuleDescription, ModuleInstallParams} from '../types/module';

export const ICP_INDEX: ModuleDescription = {
key: 'icp_index',
name: 'ICP Index',
canisterId: 'qhbym-qaaaa-aaaaa-aaafq-cai'
};

export class IcpIndexModule extends Module {
override async install(context: ModuleInstallParams): Promise<void> {
const {state, ...rest} = context;

const canisterId = state.getModule('icp_ledger')?.canisterId;

assertNonNullish(
canisterId,
'Cannot configure ICP index because the ICP ledger id is unknown.'
);

// Type definitions generated by Candid are not clean enough.
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const arg = IDL.encode(init({IDL}), [{ledger_id: Principal.fromText(canisterId)}]);

await super.install({
state,
...rest,
arg
});
}
}

export const icpIndex = new IcpIndexModule(ICP_INDEX);
3 changes: 2 additions & 1 deletion cli/src/modules/modules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {icpIndex} from './icp-index';
import {icpLedger} from './icp-ledger';
import {internetIdentity} from './internet-identity';
import {satellite} from './satellite';

export const modules = [internetIdentity, icpLedger, satellite];
export const modules = [internetIdentity, icpLedger, icpIndex, satellite];
11 changes: 10 additions & 1 deletion modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@
"icp_ledger": {
"commit": "d87954601e4b22972899e9957e800406a0a6b929",
"sha256": "55accf02dfef466476372874daa9395545f932d2a114d999f8965928b36833ec",
"repo": "https://github.com/junobuild/juno",
"repo": "https://github.com/dfinity/ic",
"url": "https://download.dfinity.systems/ic/{commit}/canisters/ledger-canister.wasm.gz",
"candid": [
"https://raw.githubusercontent.com/dfinity/ic/{commit}/rs/rosetta-api/icp_ledger/ledger.did"
]
},
"icp_index": {
"commit": "d87954601e4b22972899e9957e800406a0a6b929",
"sha256": "e6aeca9c32d0b66d38966bf7a552ac34ea4b52f113b39a0449b95bef450e63b7",
"repo": "https://github.com/dfinity/ic",
"url": "https://download.dfinity.systems/ic/{commit}/canisters/ic-icp-index-canister.wasm.gz",
"candid": [
"https://raw.githubusercontent.com/dfinity/ic/{commit}/rs/rosetta-api/icp_ledger/index/index.did"
]
}
}

0 comments on commit f94800d

Please sign in to comment.