Skip to content

Commit ec755ac

Browse files
committed
chore: simplify admin_add_supported_payment API
1 parent c3042d2 commit ec755ac

8 files changed

Lines changed: 52 additions & 55 deletions

File tree

declarations/anda_x402_canister/anda_x402_canister.did

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ type PaymentLogInfo = record {
2727
};
2828
type Result = variant { Ok; Err : text };
2929
type Result_1 = variant { Ok : nat; Err : text };
30-
type Result_2 = variant { Ok : State; Err : text };
30+
type Result_2 = variant { Ok : StateInfo; Err : text };
3131
type Result_3 = variant { Ok : PayerStateInfo; Err : text };
3232
type Result_4 = variant { Ok : vec PaymentLogInfo; Err : text };
3333
type Result_5 = variant { Ok : nat64; Err : text };
3434
type Result_6 = variant { Ok : text; Err : text };
3535
type Scheme = variant { Exact; Upto };
36-
type State = record {
36+
type StateInfo = record {
3737
total_withdrawn_fees : vec record { principal; nat };
3838
supported_payments : vec SupportedPaymentKind;
3939
total_collected_fees : vec record { principal; nat };
@@ -53,18 +53,18 @@ type UpgradeArgs = record {
5353
};
5454
type X402Version = variant { V1 };
5555
service : (opt CanisterArgs) -> {
56-
admin_add_supported_payment : (X402Version, Scheme) -> (Result);
56+
admin_add_supported_payment : (nat8, text) -> (Result);
5757
admin_collect_fees : (principal, principal, nat) -> (Result_1);
5858
admin_remove_supported_asset : (principal) -> (Result);
59-
admin_remove_supported_payment : (X402Version, Scheme) -> (Result);
59+
admin_remove_supported_payment : (nat8, text) -> (Result);
6060
admin_update_supported_asset : (principal, nat) -> (Result);
6161
info : () -> (Result_2) query;
6262
my_info : () -> (Result_3) query;
6363
my_payment_logs : (nat32, opt nat64) -> (Result_4) query;
6464
next_nonce : () -> (Result_5) query;
65-
validate_admin_add_supported_payment : (X402Version, Scheme) -> (Result_6);
65+
validate_admin_add_supported_payment : (nat8, text) -> (Result_6);
6666
validate_admin_collect_fees : (principal, principal, nat) -> (Result_6);
67-
validate_admin_remove_supported_payment : (X402Version, Scheme) -> (Result_6);
67+
validate_admin_remove_supported_payment : (nat8, text) -> (Result_6);
6868
validate_admin_update_supported_asset : (principal, nat) -> (Result_6);
6969
validate_remove_update_supported_asset : (principal) -> (Result_6);
7070
}

declarations/anda_x402_canister/anda_x402_canister.did.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export type Result = { 'Ok' : null } |
3737
{ 'Err' : string };
3838
export type Result_1 = { 'Ok' : bigint } |
3939
{ 'Err' : string };
40-
export type Result_2 = { 'Ok' : State } |
40+
export type Result_2 = { 'Ok' : StateInfo } |
4141
{ 'Err' : string };
4242
export type Result_3 = { 'Ok' : PayerStateInfo } |
4343
{ 'Err' : string };
@@ -49,7 +49,7 @@ export type Result_6 = { 'Ok' : string } |
4949
{ 'Err' : string };
5050
export type Scheme = { 'Exact' : null } |
5151
{ 'Upto' : null };
52-
export interface State {
52+
export interface StateInfo {
5353
'total_withdrawn_fees' : Array<[Principal, bigint]>,
5454
'supported_payments' : Array<SupportedPaymentKind>,
5555
'total_collected_fees' : Array<[Principal, bigint]>,
@@ -69,25 +69,25 @@ export interface UpgradeArgs {
6969
}
7070
export type X402Version = { 'V1' : null };
7171
export interface _SERVICE {
72-
'admin_add_supported_payment' : ActorMethod<[X402Version, Scheme], Result>,
72+
'admin_add_supported_payment' : ActorMethod<[number, string], Result>,
7373
'admin_collect_fees' : ActorMethod<[Principal, Principal, bigint], Result_1>,
7474
'admin_remove_supported_asset' : ActorMethod<[Principal], Result>,
75-
'admin_remove_supported_payment' : ActorMethod<[X402Version, Scheme], Result>,
75+
'admin_remove_supported_payment' : ActorMethod<[number, string], Result>,
7676
'admin_update_supported_asset' : ActorMethod<[Principal, bigint], Result>,
7777
'info' : ActorMethod<[], Result_2>,
7878
'my_info' : ActorMethod<[], Result_3>,
7979
'my_payment_logs' : ActorMethod<[number, [] | [bigint]], Result_4>,
8080
'next_nonce' : ActorMethod<[], Result_5>,
8181
'validate_admin_add_supported_payment' : ActorMethod<
82-
[X402Version, Scheme],
82+
[number, string],
8383
Result_6
8484
>,
8585
'validate_admin_collect_fees' : ActorMethod<
8686
[Principal, Principal, bigint],
8787
Result_6
8888
>,
8989
'validate_admin_remove_supported_payment' : ActorMethod<
90-
[X402Version, Scheme],
90+
[number, string],
9191
Result_6
9292
>,
9393
'validate_admin_update_supported_asset' : ActorMethod<

declarations/anda_x402_canister/anda_x402_canister.did.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ export const idlFactory = ({ IDL }) => {
1111
'Upgrade' : UpgradeArgs,
1212
'Init' : InitArgs,
1313
});
14-
const X402Version = IDL.Variant({ 'V1' : IDL.Null });
15-
const Scheme = IDL.Variant({ 'Exact' : IDL.Null, 'Upto' : IDL.Null });
1614
const Result = IDL.Variant({ 'Ok' : IDL.Null, 'Err' : IDL.Text });
1715
const Result_1 = IDL.Variant({ 'Ok' : IDL.Nat, 'Err' : IDL.Text });
16+
const Scheme = IDL.Variant({ 'Exact' : IDL.Null, 'Upto' : IDL.Null });
17+
const X402Version = IDL.Variant({ 'V1' : IDL.Null });
1818
const SupportedPaymentKind = IDL.Record({
1919
'scheme' : Scheme,
2020
'network' : IDL.Text,
@@ -28,7 +28,7 @@ export const idlFactory = ({ IDL }) => {
2828
'payment_fee' : IDL.Nat,
2929
'symbol' : IDL.Text,
3030
});
31-
const State = IDL.Record({
31+
const StateInfo = IDL.Record({
3232
'total_withdrawn_fees' : IDL.Vec(IDL.Tuple(IDL.Principal, IDL.Nat)),
3333
'supported_payments' : IDL.Vec(SupportedPaymentKind),
3434
'total_collected_fees' : IDL.Vec(IDL.Tuple(IDL.Principal, IDL.Nat)),
@@ -37,7 +37,7 @@ export const idlFactory = ({ IDL }) => {
3737
'supported_assets' : IDL.Vec(IDL.Tuple(IDL.Principal, AssetInfo)),
3838
'key_name' : IDL.Text,
3939
});
40-
const Result_2 = IDL.Variant({ 'Ok' : State, 'Err' : IDL.Text });
40+
const Result_2 = IDL.Variant({ 'Ok' : StateInfo, 'Err' : IDL.Text });
4141
const PayerStateInfo = IDL.Record({
4242
'next_nonce' : IDL.Nat64,
4343
'logs' : IDL.Vec(IDL.Nat64),
@@ -64,7 +64,7 @@ export const idlFactory = ({ IDL }) => {
6464
const Result_6 = IDL.Variant({ 'Ok' : IDL.Text, 'Err' : IDL.Text });
6565
return IDL.Service({
6666
'admin_add_supported_payment' : IDL.Func(
67-
[X402Version, Scheme],
67+
[IDL.Nat8, IDL.Text],
6868
[Result],
6969
[],
7070
),
@@ -75,7 +75,7 @@ export const idlFactory = ({ IDL }) => {
7575
),
7676
'admin_remove_supported_asset' : IDL.Func([IDL.Principal], [Result], []),
7777
'admin_remove_supported_payment' : IDL.Func(
78-
[X402Version, Scheme],
78+
[IDL.Nat8, IDL.Text],
7979
[Result],
8080
[],
8181
),
@@ -93,7 +93,7 @@ export const idlFactory = ({ IDL }) => {
9393
),
9494
'next_nonce' : IDL.Func([], [Result_5], ['query']),
9595
'validate_admin_add_supported_payment' : IDL.Func(
96-
[X402Version, Scheme],
96+
[IDL.Nat8, IDL.Text],
9797
[Result_6],
9898
[],
9999
),
@@ -103,7 +103,7 @@ export const idlFactory = ({ IDL }) => {
103103
[],
104104
),
105105
'validate_admin_remove_supported_payment' : IDL.Func(
106-
[X402Version, Scheme],
106+
[IDL.Nat8, IDL.Text],
107107
[Result_6],
108108
[],
109109
),

declarations/anda_x402_canister/anda_x402_canister.mo

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ module {
3131
};
3232
public type Result = { #Ok; #Err : Text };
3333
public type Result_1 = { #Ok : Nat; #Err : Text };
34-
public type Result_2 = { #Ok : State; #Err : Text };
34+
public type Result_2 = { #Ok : StateInfo; #Err : Text };
3535
public type Result_3 = { #Ok : PayerStateInfo; #Err : Text };
3636
public type Result_4 = { #Ok : [PaymentLogInfo]; #Err : Text };
3737
public type Result_5 = { #Ok : Nat64; #Err : Text };
3838
public type Result_6 = { #Ok : Text; #Err : Text };
3939
public type Scheme = { #Exact; #Upto };
40-
public type State = {
40+
public type StateInfo = {
4141
total_withdrawn_fees : [(Principal, Nat)];
4242
supported_payments : [SupportedPaymentKind];
4343
total_collected_fees : [(Principal, Nat)];
@@ -54,30 +54,27 @@ module {
5454
public type UpgradeArgs = { governance_canister : ?Principal; name : ?Text };
5555
public type X402Version = { #V1 };
5656
public type Self = ?CanisterArgs -> async actor {
57-
admin_add_supported_payment : shared (X402Version, Scheme) -> async Result;
57+
admin_add_supported_payment : shared (Nat8, Text) -> async Result;
5858
admin_collect_fees : shared (Principal, Principal, Nat) -> async Result_1;
5959
admin_remove_supported_asset : shared Principal -> async Result;
60-
admin_remove_supported_payment : shared (
61-
X402Version,
62-
Scheme,
63-
) -> async Result;
60+
admin_remove_supported_payment : shared (Nat8, Text) -> async Result;
6461
admin_update_supported_asset : shared (Principal, Nat) -> async Result;
6562
info : shared query () -> async Result_2;
6663
my_info : shared query () -> async Result_3;
6764
my_payment_logs : shared query (Nat32, ?Nat64) -> async Result_4;
6865
next_nonce : shared query () -> async Result_5;
6966
validate_admin_add_supported_payment : shared (
70-
X402Version,
71-
Scheme,
67+
Nat8,
68+
Text,
7269
) -> async Result_6;
7370
validate_admin_collect_fees : shared (
7471
Principal,
7572
Principal,
7673
Nat,
7774
) -> async Result_6;
7875
validate_admin_remove_supported_payment : shared (
79-
X402Version,
80-
Scheme,
76+
Nat8,
77+
Text,
8178
) -> async Result_6;
8279
validate_admin_update_supported_asset : shared (
8380
Principal,

rs/anda_x402_canister/anda_x402_canister.did

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ type PaymentLogInfo = record {
2727
};
2828
type Result = variant { Ok; Err : text };
2929
type Result_1 = variant { Ok : nat; Err : text };
30-
type Result_2 = variant { Ok : State; Err : text };
30+
type Result_2 = variant { Ok : StateInfo; Err : text };
3131
type Result_3 = variant { Ok : PayerStateInfo; Err : text };
3232
type Result_4 = variant { Ok : vec PaymentLogInfo; Err : text };
3333
type Result_5 = variant { Ok : nat64; Err : text };
3434
type Result_6 = variant { Ok : text; Err : text };
3535
type Scheme = variant { Exact; Upto };
36-
type State = record {
36+
type StateInfo = record {
3737
total_withdrawn_fees : vec record { principal; nat };
3838
supported_payments : vec SupportedPaymentKind;
3939
total_collected_fees : vec record { principal; nat };
@@ -53,18 +53,18 @@ type UpgradeArgs = record {
5353
};
5454
type X402Version = variant { V1 };
5555
service : (opt CanisterArgs) -> {
56-
admin_add_supported_payment : (X402Version, Scheme) -> (Result);
56+
admin_add_supported_payment : (nat8, text) -> (Result);
5757
admin_collect_fees : (principal, principal, nat) -> (Result_1);
5858
admin_remove_supported_asset : (principal) -> (Result);
59-
admin_remove_supported_payment : (X402Version, Scheme) -> (Result);
59+
admin_remove_supported_payment : (nat8, text) -> (Result);
6060
admin_update_supported_asset : (principal, nat) -> (Result);
6161
info : () -> (Result_2) query;
6262
my_info : () -> (Result_3) query;
6363
my_payment_logs : (nat32, opt nat64) -> (Result_4) query;
6464
next_nonce : () -> (Result_5) query;
65-
validate_admin_add_supported_payment : (X402Version, Scheme) -> (Result_6);
65+
validate_admin_add_supported_payment : (nat8, text) -> (Result_6);
6666
validate_admin_collect_fees : (principal, principal, nat) -> (Result_6);
67-
validate_admin_remove_supported_payment : (X402Version, Scheme) -> (Result_6);
67+
validate_admin_remove_supported_payment : (nat8, text) -> (Result_6);
6868
validate_admin_update_supported_asset : (principal, nat) -> (Result_6);
6969
validate_remove_update_supported_asset : (principal) -> (Result_6);
7070
}

rs/anda_x402_canister/src/api_admin.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
use anda_cloud_cdk::x402::{Scheme, SupportedPaymentKind, X402Version};
22
use candid::{Nat, Principal};
33
use icrc_ledger_types::icrc1::account::Account;
4+
use std::str::FromStr;
45

56
use crate::{
67
helper::{pretty_format, token_info, transfer_token_to},
78
store,
89
};
910

1011
#[ic_cdk::update(guard = "is_controller")]
11-
fn admin_add_supported_payment(x402_version: X402Version, scheme: Scheme) -> Result<(), String> {
12+
fn admin_add_supported_payment(x402_version: u8, scheme: String) -> Result<(), String> {
1213
let payment = SupportedPaymentKind {
13-
x402_version,
14-
scheme,
14+
x402_version: X402Version::try_from(x402_version).map_err(|err| err.to_string())?,
15+
scheme: Scheme::from_str(&scheme).map_err(|err| err.to_string())?,
1516
network: "icp".to_string(),
1617
};
1718

18-
if scheme == Scheme::Upto {
19+
if payment.scheme == Scheme::Upto {
1920
return Err("Scheme::Upto is not supported".to_string());
2021
}
2122
store::state::with_mut(|s| {
@@ -26,33 +27,31 @@ fn admin_add_supported_payment(x402_version: X402Version, scheme: Scheme) -> Res
2627

2728
#[ic_cdk::update]
2829
fn validate_admin_add_supported_payment(
29-
x402_version: X402Version,
30-
scheme: Scheme,
30+
x402_version: u8,
31+
scheme: String,
3132
) -> Result<String, String> {
32-
if scheme == Scheme::Upto {
33+
if scheme == Scheme::Upto.to_string() {
3334
return Err("Scheme::Upto is not supported".to_string());
3435
}
3536

3637
pretty_format(&(x402_version, scheme))
3738
}
3839

3940
#[ic_cdk::update(guard = "is_controller")]
40-
fn admin_remove_supported_payment(x402_version: X402Version, scheme: Scheme) -> Result<(), String> {
41-
let payment = SupportedPaymentKind {
42-
x402_version,
43-
scheme,
44-
network: "icp".to_string(),
45-
};
41+
fn admin_remove_supported_payment(x402_version: u8, scheme: String) -> Result<(), String> {
42+
let x402_version = X402Version::try_from(x402_version).map_err(|err| err.to_string())?;
43+
let scheme = Scheme::from_str(&scheme).map_err(|err| err.to_string())?;
4644
store::state::with_mut(|s| {
47-
s.supported_payments.retain(|p| p != &payment);
45+
s.supported_payments
46+
.retain(|p| p.x402_version != x402_version || p.scheme != scheme);
4847
Ok(())
4948
})
5049
}
5150

5251
#[ic_cdk::update]
5352
fn validate_admin_remove_supported_payment(
54-
x402_version: X402Version,
55-
scheme: Scheme,
53+
x402_version: u8,
54+
scheme: String,
5655
) -> Result<String, String> {
5756
pretty_format(&(x402_version, scheme))
5857
}

rs/anda_x402_canister/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use anda_cloud_cdk::x402::{Scheme, X402Version};
21
use candid::{Nat, Principal};
32

43
use crate::api_init::CanisterArgs;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export const prerender = true
1+
export const prerender = 'auto'
2+
export const ssr = false
3+
export const trailingSlash = 'never'

0 commit comments

Comments
 (0)