From 8b36d38aa6304ed56f7395929b49f2a3e69a1910 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 29 Aug 2024 19:56:38 -0500 Subject: [PATCH] update wallet to class-based syntax --- .../open_value_sharing/src/wallet.ts | 100 ++++++++++-------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/tests/end_to_end/http_server/open_value_sharing/src/wallet.ts b/tests/end_to_end/http_server/open_value_sharing/src/wallet.ts index 818027b595..be036898e1 100644 --- a/tests/end_to_end/http_server/open_value_sharing/src/wallet.ts +++ b/tests/end_to_end/http_server/open_value_sharing/src/wallet.ts @@ -1,68 +1,76 @@ // TODO we should pull the wallet out into its own repo import { - Canister, - ic, - nat, - nat64, - Opt, + caller, + IDL, + msgCyclesAccept, + msgCyclesAvailable, Principal, query, - Record, - text, - update, - Vec, - Void -} from 'azle/experimental'; + time, + update +} from 'azle'; -let payments: Payment[] = []; - -let principalsWhitelist: string[] = []; - -const Payment = Record({ - time: nat64, - amount: nat, - principal: Principal +const Payment = IDL.Record({ + time: IDL.Nat64, + amount: IDL.Nat, + principal: IDL.Principal }); -type Payment = typeof Payment.tsType; +type Payment = { + time: bigint; + amount: bigint; + principal: Principal; +}; -const ReceiveOptions = Record({ - memo: Opt(text) +const ReceiveOptions = IDL.Record({ + memo: IDL.Opt(IDL.Text) }); -export default Canister({ - add_to_whitelist: update([Principal], Void, (principal) => { - principalsWhitelist.push(principal.toText()); - }), - remove_from_whitelist: update([Principal], Void, (principal) => { - principalsWhitelist = principalsWhitelist.filter( +export default class { + payments: Payment[] = []; + principalsWhitelist: string[] = []; + + @update([IDL.Principal]) + add_to_whitelist(principal: Principal): void { + this.principalsWhitelist.push(principal.toText()); + } + + @update([IDL.Principal]) + remove_from_whitelist(principal: Principal): void { + this.principalsWhitelist = this.principalsWhitelist.filter( (principalTextInList) => principalTextInList !== principal.toText() ); - }), - get_whitelist: query([], Vec(text), () => { - return principalsWhitelist; - }), - get_all_payments: query([], Vec(Payment), () => { - return payments; - }), - wallet_receive: update([Opt(ReceiveOptions)], Void, (_receiveOptions) => { + } + + @query([], IDL.Vec(IDL.Text)) + get_whitelist(): string[] { + return this.principalsWhitelist; + } + + @query([], IDL.Vec(Payment)) + get_all_payments(): Payment[] { + return this.payments; + } + + @update([IDL.Opt(ReceiveOptions)]) + wallet_receive(_receiveOptions: [{ memo: [string] | [] }] | []): void { console.info('wallet_receive'); - console.info(`cycles available: ${ic.msgCyclesAvailable()}`); + console.info(`cycles available: ${msgCyclesAvailable()}`); - const callerInWhitelist = principalsWhitelist.includes( - ic.caller().toText() + const callerInWhitelist = this.principalsWhitelist.includes( + caller().toText() ); if (callerInWhitelist) { - const cyclesAvailable = ic.msgCyclesAvailable(); + const cyclesAvailable = msgCyclesAvailable(); - ic.msgCyclesAccept(cyclesAvailable); + msgCyclesAccept(cyclesAvailable); - payments.push({ - time: ic.time(), + this.payments.push({ + time: time(), amount: cyclesAvailable, - principal: ic.caller() + principal: caller() }); } - }) -}); + } +}