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

Update wallet #2038

Merged
merged 2 commits into from
Sep 1, 2024
Merged
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
100 changes: 54 additions & 46 deletions tests/end_to_end/http_server/open_value_sharing/src/wallet.ts
Original file line number Diff line number Diff line change
@@ -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()
});
}
})
});
}
}
Loading