Skip to content

Commit

Permalink
chore: upgrade frontend wallet did to match latest canister changes (#89
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hansl authored Apr 30, 2021
1 parent 6427131 commit c3cbfc5
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 246 deletions.
77 changes: 0 additions & 77 deletions wallet_ui/canister/declaration.ts

This file was deleted.

45 changes: 36 additions & 9 deletions wallet_ui/canister/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@
* It is also useful because that puts all the code in one place, including the
* authentication logic. We do not use `window.ic` anywhere in this.
*/
import { convertIdlEventMap, factory } from "./wallet.did";
import { HttpAgent, Actor, Principal, ActorSubclass } from "@dfinity/agent";
import { AuthenticationClient } from "../utils/authClient";
import _SERVICE, { Event } from "../types/declaration";
import _SERVICE from "./wallet/wallet";
import factory, { Event } from "./wallet";

function convertIdlEventMap(idlEvent: any): Event {
return {
id: idlEvent.id,
timestamp: idlEvent.timestamp / BigInt(1000000),
kind: idlEvent.kind,
};
}

export * from "./wallet";

// Need to export the enumaration from wallet.did
export * from "./wallet.did";
// Need to export the enumeration from wallet.did
export { Principal } from "@dfinity/agent";

const authClient = new AuthenticationClient();
Expand Down Expand Up @@ -166,25 +175,43 @@ export const Wallet = {
cycles: number;
}): Promise<Principal> {
const result = await (await getWalletCanister()).wallet_create_canister({
controller: p.controller ? [p.controller] : [],
settings: {
compute_allocation: [],
controller: p.controller ? [p.controller] : [],
freezing_threshold: [],
memory_allocation: [],
},
cycles: BigInt(p.cycles),
});
return result.canister_id;
if ("Ok" in result) {
return result.Ok.canister_id;
} else {
throw result.Err;
}
},
async create_wallet(p: {
controller?: Principal;
cycles: number;
}): Promise<Principal> {
const result = await (await getWalletCanister()).wallet_create_wallet({
controller: p.controller ? [p.controller] : [],
settings: {
compute_allocation: [],
controller: p.controller ? [p.controller] : [],
freezing_threshold: [],
memory_allocation: [],
},
cycles: BigInt(p.cycles),
});
return result.canister_id;
if ("Ok" in result) {
return result.Ok.canister_id;
} else {
throw result.Err;
}
},
async send(p: { canister: Principal; amount: BigInt }): Promise<void> {
await (await getWalletCanister()).wallet_send({
canister: p.canister,
amount: p.amount,
amount: BigInt(p.amount),
});
},
};
6 changes: 6 additions & 0 deletions wallet_ui/canister/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import WalletIdlFactory from "./wallet.did";
import { IDL } from "@dfinity/agent";

export * from "./wallet";

export default WalletIdlFactory as IDL.InterfaceFactory;
Original file line number Diff line number Diff line change
@@ -1,76 +1,112 @@
// This file was generated using did-to-js tools, with some modifications.
// It is essentially hand written.
import type { IDL, Principal } from "@dfinity/agent";
import { Event } from "../types";

export function convertIdlEventMap(idlEvent: any): Event {
return {
id: idlEvent.id,
timestamp: idlEvent.timestamp / BigInt(1000000),
kind: idlEvent.kind,
};
}

export const factory: IDL.InterfaceFactory = ({ IDL }) => {
export default ({ IDL }) => {
const Kind = IDL.Variant({
User: IDL.Null,
Canister: IDL.Null,
Unknown: IDL.Null,
});
const Role = IDL.Variant({
Contact: IDL.Null,
Custodian: IDL.Null,
Contact: IDL.Null,
Controller: IDL.Null,
});
const AddressEntry = IDL.Record({
id: IDL.Principal,
kind: Kind,
name: IDL.Opt(IDL.Text),
role: Role,
});
const EventKind = IDL.Variant({
CyclesSent: IDL.Record({ to: IDL.Principal, amount: IDL.Nat64 }),
CyclesReceived: IDL.Record({
from: IDL.Principal,
amount: IDL.Nat64,
}),
AddressAdded: IDL.Record({
id: IDL.Principal,
name: IDL.Opt(IDL.Text),
role: Role,
}),
AddressRemoved: IDL.Record({
id: IDL.Principal,
CanisterCreated: IDL.Record({
cycles: IDL.Nat64,
canister: IDL.Principal,
}),
CanisterCreated: IDL.Record({ canister: IDL.Principal, cycles: IDL.Nat64 }),
CanisterCalled: IDL.Record({
cycles: IDL.Nat64,
method_name: IDL.Text,
canister: IDL.Principal,
}),
CyclesSent: IDL.Record({
to: IDL.Principal,
amount: IDL.Nat64,
refund: IDL.Nat64,
}),
AddressRemoved: IDL.Record({ id: IDL.Principal }),
WalletDeployed: IDL.Record({ canister: IDL.Principal }),
AddressAdded: IDL.Record({
id: IDL.Principal,
name: IDL.Opt(IDL.Text),
role: Role,
}),
});
const Event = IDL.Record({
id: IDL.Nat32,
kind: EventKind,
timestamp: IDL.Nat64,
});
const AddressEntry = IDL.Record({
id: IDL.Principal,
name: IDL.Opt(IDL.Text),
role: Role,
const ResultCall = IDL.Variant({
Ok: IDL.Record({ return: IDL.Vec(IDL.Nat8) }),
Err: IDL.Text,
});
const CanisterSettings = IDL.Record({
controller: IDL.Opt(IDL.Principal),
freezing_threshold: IDL.Opt(IDL.Nat),
memory_allocation: IDL.Opt(IDL.Nat),
compute_allocation: IDL.Opt(IDL.Nat),
});
const CreateCanisterArgs = IDL.Record({
cycles: IDL.Nat64,
settings: CanisterSettings,
});
const ResultCreate = IDL.Variant({
Ok: IDL.Record({ canister_id: IDL.Principal }),
Err: IDL.Text,
});
const ResultSend = IDL.Variant({ Ok: IDL.Null, Err: IDL.Text });
return IDL.Service({
name: IDL.Func([], [IDL.Opt(IDL.Text)], ["query"]),
wallet_create_canister: IDL.Func(
add_address: IDL.Func([AddressEntry], [], []),
add_controller: IDL.Func([IDL.Principal], [], []),
authorize: IDL.Func([IDL.Principal], [], []),
deauthorize: IDL.Func([IDL.Principal], [], []),
get_chart: IDL.Func(
[
IDL.Record({
controller: IDL.Opt(IDL.Principal),
cycles: IDL.Nat64,
}),
IDL.Opt(
IDL.Record({
count: IDL.Opt(IDL.Nat32),
precision: IDL.Opt(IDL.Nat64),
})
),
],
[IDL.Record({ canister_id: IDL.Principal })],
[]
[IDL.Vec(IDL.Tuple(IDL.Nat64, IDL.Nat64))],
["query"]
),
wallet_create_wallet: IDL.Func(
get_controllers: IDL.Func([], [IDL.Vec(IDL.Principal)], ["query"]),
get_custodians: IDL.Func([], [IDL.Vec(IDL.Principal)], ["query"]),
get_events: IDL.Func(
[
IDL.Record({
controller: IDL.Opt(IDL.Principal),
cycles: IDL.Nat64,
}),
IDL.Opt(
IDL.Record({
to: IDL.Opt(IDL.Nat32),
from: IDL.Opt(IDL.Nat32),
})
),
],
[IDL.Record({ canister_id: IDL.Principal })],
[]
[IDL.Vec(Event)],
["query"]
),
list_addresses: IDL.Func([], [IDL.Vec(AddressEntry)], ["query"]),
name: IDL.Func([], [IDL.Opt(IDL.Text)], ["query"]),
remove_address: IDL.Func([IDL.Principal], [], []),
remove_controller: IDL.Func([IDL.Principal], [], []),
set_name: IDL.Func([IDL.Text], [], []),
wallet_balance: IDL.Func(
[],
[IDL.Record({ amount: IDL.Nat64 })],
["query"]
),
get_controller: IDL.Func([], [IDL.Principal], ["query"]),
set_controller: IDL.Func([IDL.Principal], [], []),
wallet_call: IDL.Func(
[
IDL.Record({
Expand All @@ -80,48 +116,24 @@ export const factory: IDL.InterfaceFactory = ({ IDL }) => {
canister: IDL.Principal,
}),
],
[IDL.Record({ return: IDL.Vec(IDL.Nat8) })],
[ResultCall],
[]
),
wallet_create_canister: IDL.Func([CreateCanisterArgs], [ResultCreate], []),
wallet_create_wallet: IDL.Func([CreateCanisterArgs], [ResultCreate], []),
wallet_receive: IDL.Func([], [], []),
wallet_send: IDL.Func(
[IDL.Record({ canister: IDL.Principal, amount: IDL.Nat64 })],
[IDL.Variant({ Ok: IDL.Null, Err: IDL.Text })],
[ResultSend],
[]
),
authorize: IDL.Func([IDL.Principal], [], []),
wallet_balance: IDL.Func(
wallet_store_wallet_wasm: IDL.Func(
[IDL.Record({ wasm_module: IDL.Vec(IDL.Nat8) })],
[],
[IDL.Record({ amount: IDL.Nat64 })],
["query"]
),
wallet_receive: IDL.Func([], [IDL.Record({ accepted: IDL.Nat64 })], []),
deauthorize: IDL.Func([IDL.Principal], [], []),
get_custodians: IDL.Func([], [IDL.Vec(IDL.Principal)], ["query"]),

add_address: IDL.Func([AddressEntry], [], []),
list_addresses: IDL.Func([], [IDL.Vec(AddressEntry)], ["query"]),
remove_address: IDL.Func([IDL.Principal], [], []),

get_events: IDL.Func(
[
IDL.Opt(
IDL.Record({ from: IDL.Opt(IDL.Nat32), to: IDL.Opt(IDL.Nat32) })
),
],
[IDL.Vec(Event)],
["query"]
),
get_chart: IDL.Func(
[
IDL.Opt(
IDL.Record({
count: IDL.Opt(IDL.Nat32),
precision: IDL.Opt(IDL.Nat64),
})
),
],
[IDL.Vec(IDL.Tuple(IDL.Nat64, IDL.Nat64))],
["query"]
[]
),
});
};
export const init = ({ IDL }) => {
return [];
};
Loading

0 comments on commit c3cbfc5

Please sign in to comment.