diff --git a/wallet_ui/canister/declaration.ts b/wallet_ui/canister/declaration.ts deleted file mode 100644 index 8a1ac95..0000000 --- a/wallet_ui/canister/declaration.ts +++ /dev/null @@ -1,77 +0,0 @@ -import type { Principal } from "@dfinity/agent"; -export interface AddressEntry { - id: Principal; - kind: Kind; - name: [] | [string]; - role: Role; -} -export interface Event { - id: number; - kind: EventKind; - timestamp: BigInt; -} -export type EventKind = - | { - CyclesReceived: { from: Principal; amount: BigInt }; - } - | { CanisterCreated: { cycles: BigInt; canister: Principal } } - | { - CanisterCalled: { - cycles: BigInt; - method_name: string; - canister: Principal; - }; - } - | { CyclesSent: { to: Principal; amount: BigInt } } - | { AddressRemoved: { id: Principal } } - | { WalletDeployed: { canister: Principal } } - | { - AddressAdded: { id: Principal; name: [] | [string]; role: Role }; - }; -export type Kind = { User: null } | { Canister: null } | { Unknown: null }; -export type Role = - | { Custodian: null } - | { Contact: null } - | { Controller: null }; -export default interface _SERVICE { - add_address: (arg_0: AddressEntry) => Promise; - add_controller: (arg_0: Principal) => Promise; - authorize: (arg_0: Principal) => Promise; - deauthorize: (arg_0: Principal) => Promise; - get_chart: ( - arg_0: [] | [{ count: [] | [number]; precision: [] | [BigInt] }] - ) => Promise>; - get_controllers: () => Promise>; - get_custodians: () => Promise>; - get_events: ( - arg_0: [] | [{ to: [] | [number]; from: [] | [number] }] - ) => Promise>; - list_addresses: () => Promise>; - name: () => Promise<[] | [string]>; - remove_address: (arg_0: Principal) => Promise; - remove_controller: (arg_0: Principal) => Promise; - set_name: (arg_0: string) => Promise; - wallet_balance: () => Promise<{ amount: BigInt }>; - wallet_call: (arg_0: { - args: Array; - cycles: BigInt; - method_name: string; - canister: Principal; - }) => Promise<{ return: Array }>; - wallet_create_canister: (arg_0: { - controller: [] | [Principal]; - cycles: BigInt; - }) => Promise<{ canister_id: Principal }>; - wallet_create_wallet: (arg_0: { - controller: [] | [Principal]; - cycles: BigInt; - }) => Promise<{ canister_id: Principal }>; - wallet_receive: () => Promise<{ accepted: BigInt }>; - wallet_send: (arg_0: { - canister: Principal; - amount: BigInt; - }) => Promise; - wallet_store_wallet_wasm: (arg_0: { - wasm_module: Array; - }) => Promise; -} diff --git a/wallet_ui/canister/index.ts b/wallet_ui/canister/index.ts index 2652acc..b79b816 100644 --- a/wallet_ui/canister/index.ts +++ b/wallet_ui/canister/index.ts @@ -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(); @@ -166,25 +175,43 @@ export const Wallet = { cycles: number; }): Promise { 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 { 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 { await (await getWalletCanister()).wallet_send({ canister: p.canister, - amount: p.amount, + amount: BigInt(p.amount), }); }, }; diff --git a/wallet_ui/canister/wallet/index.ts b/wallet_ui/canister/wallet/index.ts new file mode 100644 index 0000000..51a3538 --- /dev/null +++ b/wallet_ui/canister/wallet/index.ts @@ -0,0 +1,6 @@ +import WalletIdlFactory from "./wallet.did"; +import { IDL } from "@dfinity/agent"; + +export * from "./wallet"; + +export default WalletIdlFactory as IDL.InterfaceFactory; diff --git a/wallet_ui/canister/wallet.did.ts b/wallet_ui/canister/wallet/wallet.did.js similarity index 51% rename from wallet_ui/canister/wallet.did.ts rename to wallet_ui/canister/wallet/wallet.did.js index e548357..32a98e0 100644 --- a/wallet_ui/canister/wallet.did.ts +++ b/wallet_ui/canister/wallet/wallet.did.js @@ -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({ @@ -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 []; +}; diff --git a/wallet_ui/canister/wallet/wallet.ts b/wallet_ui/canister/wallet/wallet.ts new file mode 100644 index 0000000..d542e82 --- /dev/null +++ b/wallet_ui/canister/wallet/wallet.ts @@ -0,0 +1,92 @@ +import type { Principal } from '@dfinity/agent'; +export interface AddressEntry { + 'id' : Principal, + 'kind' : Kind, + 'name' : [] | [string], + 'role' : Role, +}; +export interface CanisterSettings { + 'controller' : [] | [Principal], + 'freezing_threshold' : [] | [bigint], + 'memory_allocation' : [] | [bigint], + 'compute_allocation' : [] | [bigint], +}; +export interface CreateCanisterArgs { + 'cycles' : bigint, + 'settings' : CanisterSettings, +}; +export interface Event { + 'id' : number, + 'kind' : EventKind, + 'timestamp' : bigint, +}; +export type EventKind = { + 'CyclesReceived' : { 'from' : Principal, 'amount' : bigint } + } | + { 'CanisterCreated' : { 'cycles' : bigint, 'canister' : Principal } } | + { + 'CanisterCalled' : { + 'cycles' : bigint, + 'method_name' : string, + 'canister' : Principal, + } + } | + { + 'CyclesSent' : { 'to' : Principal, 'amount' : bigint, 'refund' : bigint } + } | + { 'AddressRemoved' : { 'id' : Principal } } | + { 'WalletDeployed' : { 'canister' : Principal } } | + { + 'AddressAdded' : { 'id' : Principal, 'name' : [] | [string], 'role' : Role } + }; +export type Kind = { 'User' : null } | + { 'Canister' : null } | + { 'Unknown' : null }; +export type ResultCall = { 'Ok' : { 'return' : Array } } | + { 'Err' : string }; +export type ResultCreate = { 'Ok' : { 'canister_id' : Principal } } | + { 'Err' : string }; +export type ResultSend = { 'Ok' : null } | + { 'Err' : string }; +export type Role = { 'Custodian' : null } | + { 'Contact' : null } | + { 'Controller' : null }; +export default interface _SERVICE { + 'add_address' : (arg_0: AddressEntry) => Promise, + 'add_controller' : (arg_0: Principal) => Promise, + 'authorize' : (arg_0: Principal) => Promise, + 'deauthorize' : (arg_0: Principal) => Promise, + 'get_chart' : ( + arg_0: [] | [{ 'count' : [] | [number], 'precision' : [] | [bigint] }], + ) => Promise>, + 'get_controllers' : () => Promise>, + 'get_custodians' : () => Promise>, + 'get_events' : ( + arg_0: [] | [{ 'to' : [] | [number], 'from' : [] | [number] }], + ) => Promise>, + 'list_addresses' : () => Promise>, + 'name' : () => Promise<[] | [string]>, + 'remove_address' : (arg_0: Principal) => Promise, + 'remove_controller' : (arg_0: Principal) => Promise, + 'set_name' : (arg_0: string) => Promise, + 'wallet_balance' : () => Promise<{ 'amount' : bigint }>, + 'wallet_call' : ( + arg_0: { + 'args' : Array, + 'cycles' : bigint, + 'method_name' : string, + 'canister' : Principal, + }, + ) => Promise, + 'wallet_create_canister' : (arg_0: CreateCanisterArgs) => Promise< + ResultCreate + >, + 'wallet_create_wallet' : (arg_0: CreateCanisterArgs) => Promise, + 'wallet_receive' : () => Promise, + 'wallet_send' : ( + arg_0: { 'canister' : Principal, 'amount' : bigint }, + ) => Promise, + 'wallet_store_wallet_wasm' : ( + arg_0: { 'wasm_module' : Array }, + ) => Promise, +}; diff --git a/wallet_ui/components/panels/Transactions.tsx b/wallet_ui/components/panels/Transactions.tsx index 6dbdc1a..bca817b 100644 --- a/wallet_ui/components/panels/Transactions.tsx +++ b/wallet_ui/components/panels/Transactions.tsx @@ -8,7 +8,7 @@ import TableRow from "@material-ui/core/TableRow"; import Typography from "@material-ui/core/Typography"; import { Wallet } from "../../canister"; import ReactTimeago from "react-timeago"; -import { Event, EventKind } from "../../types"; +import { Event, EventKind } from "../../canister"; interface TransactionRowProps { event: Event; diff --git a/wallet_ui/components/routes/Dashboard.tsx b/wallet_ui/components/routes/Dashboard.tsx index eaf6c17..b11837a 100644 --- a/wallet_ui/components/routes/Dashboard.tsx +++ b/wallet_ui/components/routes/Dashboard.tsx @@ -20,7 +20,7 @@ import DialogContentText from "@material-ui/core/DialogContentText"; import DialogActions from "@material-ui/core/DialogActions"; import Button from "@material-ui/core/Button"; import { Wallet } from "../../canister"; -import type { Event } from "../../canister/declaration"; +import type { Event } from "../../canister/wallet/wallet"; import Canisters from "../panels/Canisters"; import { PrimaryButton } from "../Buttons"; import Events from "../panels/Transactions"; diff --git a/wallet_ui/types/declaration.d.ts b/wallet_ui/types/declaration.d.ts deleted file mode 100644 index 8a1ac95..0000000 --- a/wallet_ui/types/declaration.d.ts +++ /dev/null @@ -1,77 +0,0 @@ -import type { Principal } from "@dfinity/agent"; -export interface AddressEntry { - id: Principal; - kind: Kind; - name: [] | [string]; - role: Role; -} -export interface Event { - id: number; - kind: EventKind; - timestamp: BigInt; -} -export type EventKind = - | { - CyclesReceived: { from: Principal; amount: BigInt }; - } - | { CanisterCreated: { cycles: BigInt; canister: Principal } } - | { - CanisterCalled: { - cycles: BigInt; - method_name: string; - canister: Principal; - }; - } - | { CyclesSent: { to: Principal; amount: BigInt } } - | { AddressRemoved: { id: Principal } } - | { WalletDeployed: { canister: Principal } } - | { - AddressAdded: { id: Principal; name: [] | [string]; role: Role }; - }; -export type Kind = { User: null } | { Canister: null } | { Unknown: null }; -export type Role = - | { Custodian: null } - | { Contact: null } - | { Controller: null }; -export default interface _SERVICE { - add_address: (arg_0: AddressEntry) => Promise; - add_controller: (arg_0: Principal) => Promise; - authorize: (arg_0: Principal) => Promise; - deauthorize: (arg_0: Principal) => Promise; - get_chart: ( - arg_0: [] | [{ count: [] | [number]; precision: [] | [BigInt] }] - ) => Promise>; - get_controllers: () => Promise>; - get_custodians: () => Promise>; - get_events: ( - arg_0: [] | [{ to: [] | [number]; from: [] | [number] }] - ) => Promise>; - list_addresses: () => Promise>; - name: () => Promise<[] | [string]>; - remove_address: (arg_0: Principal) => Promise; - remove_controller: (arg_0: Principal) => Promise; - set_name: (arg_0: string) => Promise; - wallet_balance: () => Promise<{ amount: BigInt }>; - wallet_call: (arg_0: { - args: Array; - cycles: BigInt; - method_name: string; - canister: Principal; - }) => Promise<{ return: Array }>; - wallet_create_canister: (arg_0: { - controller: [] | [Principal]; - cycles: BigInt; - }) => Promise<{ canister_id: Principal }>; - wallet_create_wallet: (arg_0: { - controller: [] | [Principal]; - cycles: BigInt; - }) => Promise<{ canister_id: Principal }>; - wallet_receive: () => Promise<{ accepted: BigInt }>; - wallet_send: (arg_0: { - canister: Principal; - amount: BigInt; - }) => Promise; - wallet_store_wallet_wasm: (arg_0: { - wasm_module: Array; - }) => Promise; -} diff --git a/wallet_ui/types/index.d.ts b/wallet_ui/types/index.d.ts index b095968..81ed67b 100644 --- a/wallet_ui/types/index.d.ts +++ b/wallet_ui/types/index.d.ts @@ -2,4 +2,3 @@ declare module "*.png" { const value: any; export = value; } -export * from "./declaration";