Skip to content

Commit f80e1e4

Browse files
author
Daniel Graczer
authored
making XTC DIP20 compatible (#45)
1 parent 0c24873 commit f80e1e4

File tree

11 files changed

+130
-206
lines changed

11 files changed

+130
-206
lines changed

candid/piggy-bank.did

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
type TransactionId = nat64;
22

3-
type MintError = variant {
4-
NotSufficientLiquidity;
5-
};
6-
7-
type MintResult = variant {
8-
Ok : TransactionId;
9-
Err: MintError;
3+
type TxError = variant {
4+
InsufficientAllowance;
5+
InsufficientBalance;
6+
ErrorOperationStyle;
7+
Unauthorized;
8+
LedgerTrap;
9+
ErrorTo;
10+
Other;
11+
BlockUsed;
12+
AmountTooSmall;
1013
};
14+
type TxReceipt = variant { Ok : nat; Err : TxError };
1115

1216
service : {
1317
balance: () -> (amount: nat64);
1418
get_available_cycles: () -> (amount: nat64);
15-
perform_mint: (record { canister: principal; account: opt principal; cycles: nat64 }) -> (MintResult);
19+
perform_mint: (record { canister: principal; account: opt principal; cycles: nat64 }) -> (TxReceipt);
1620
whoami : () -> (principal);
1721
}

candid/xtc.did

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,18 @@ type TxRecord =
3737
status: TransactionStatus;
3838
};
3939

40-
type TxReceipt =
41-
variant {
42-
Err: variant {
43-
InsufficientAllowance;
44-
InsufficientBalance;
45-
};
46-
Ok: nat;
47-
};
40+
type TxError = variant {
41+
InsufficientAllowance;
42+
InsufficientBalance;
43+
ErrorOperationStyle;
44+
Unauthorized;
45+
LedgerTrap;
46+
ErrorTo;
47+
Other;
48+
BlockUsed;
49+
AmountTooSmall;
50+
};
51+
type TxReceipt = variant { Ok : nat; Err : TxError };
4852

4953
////////// END ERC-20 //////////
5054

@@ -61,18 +65,6 @@ type BurnResult = variant {
6165
Err: BurnError;
6266
};
6367

64-
type TransferError = variant {
65-
InsufficientBalance;
66-
AmountTooLarge;
67-
CallFailed;
68-
Unknown;
69-
};
70-
71-
type TransferResponse = variant {
72-
Ok : TransactionId;
73-
Err: TransferError;
74-
};
75-
7668
type MintError = variant {
7769
NotSufficientLiquidity;
7870
};
@@ -143,13 +135,6 @@ type EventsConnection = record {
143135
next_canister_id: opt principal;
144136
};
145137

146-
type TokenMetaData = record {
147-
name : text;
148-
symbol : text;
149-
decimal : nat8;
150-
features: vec text;
151-
};
152-
153138
type Stats = record {
154139
supply: nat;
155140
fee: nat;
@@ -183,22 +168,17 @@ service : {
183168
historySize: () -> (nat) query;
184169
logo: () -> (text) query;
185170
nameErc20: () -> (text) query;
171+
name: () -> (text) query;
186172
symbol: () -> (text) query;
187173
totalSupply: () -> (nat) query;
188174
transferErc20: (principal, nat) -> (TxReceipt);
175+
transfer: (principal, nat) -> (TxReceipt);
189176
transferFrom: (principal, principal, nat) -> (TxReceipt);
177+
mint: (principal, nat) -> (MintResult);
190178
////////// END ERC-20 //////////
191179

192-
// Return all of the meta data of a token.
193-
meta : () -> (TokenMetaData) query;
194-
195-
// Return all of the meta data of a token as an update.
196-
meta_certified : () -> (TokenMetaData);
197-
198-
balance: (opt principal) -> (amount: nat64);
199-
transfer: (record { to: principal; amount: nat64 }) -> (TransferResponse);
200-
mint: (opt principal) -> (MintResult);
201180
burn: (record { canister_id: principal; amount: nat64 }) -> (BurnResult);
181+
balance: (opt principal) -> (amount: nat64);
202182

203183
// History
204184
get_transaction : (id: TransactionId) -> (opt Event);
@@ -212,8 +192,6 @@ service : {
212192

213193
// ----------- Cycles wallet compatible API
214194

215-
name : () -> (opt text) query;
216-
217195
wallet_balance: () -> (record { amount: nat64 }) query;
218196
wallet_send: (record { canister: principal; amount: nat64 }) -> (ResultSend);
219197

piggy-bank/src/lib.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ic_kit::candid::CandidType;
1+
use ic_kit::candid::{CandidType, Nat};
22
use ic_kit::macros::*;
33
use ic_kit::*;
44
use serde::*;
@@ -10,13 +10,23 @@ struct PerformMintArgs {
1010
cycles: u64,
1111
}
1212

13-
#[derive(CandidType, Deserialize, Debug)]
14-
enum MintError {
15-
NotSufficientLiquidity,
13+
#[derive(CandidType, Debug, Deserialize, Eq, PartialEq)]
14+
pub enum TxError {
15+
InsufficientAllowance,
16+
InsufficientBalance,
17+
ErrorOperationStyle,
18+
Unauthorized,
19+
LedgerTrap,
20+
ErrorTo,
21+
Other,
22+
BlockUsed,
23+
AmountTooSmall,
1624
}
1725

26+
pub type TxReceipt = Result<Nat, TxError>;
27+
1828
#[update]
19-
async fn perform_mint(args: PerformMintArgs) -> Result<u64, MintError> {
29+
async fn perform_mint(args: PerformMintArgs) -> TxReceipt {
2030
let ic = get_context();
2131

2232
let account = match args.account {
@@ -25,11 +35,11 @@ async fn perform_mint(args: PerformMintArgs) -> Result<u64, MintError> {
2535
};
2636

2737
if ic.balance() < args.cycles {
28-
return Err(MintError::NotSufficientLiquidity);
38+
return Err(TxError::InsufficientBalance);
2939
}
3040

3141
match ic
32-
.call_with_payment(args.canister, "mint", (Some(account),), args.cycles)
42+
.call_with_payment(args.canister, "mint", (account, Nat::from(0)), args.cycles)
3343
.await
3444
{
3545
Ok((r,)) => r,
@@ -66,8 +76,8 @@ mod tests {
6676
.with_handler(
6777
Method::new()
6878
.expect_cycles(300)
69-
.response::<Result<u64, MintError>>(Ok(17))
70-
.expect_arguments((Some(&alice),)),
79+
.response::<TxReceipt>(Ok(Nat::from(17)))
80+
.expect_arguments((alice, Nat::from(0))),
7181
)
7282
.inject();
7383

@@ -77,18 +87,17 @@ mod tests {
7787
account: None,
7888
cycles: 300
7989
})
80-
.await
81-
.unwrap(),
82-
17
90+
.await,
91+
Ok(Nat::from(17))
8392
);
8493

8594
MockContext::new()
8695
.with_caller(alice.clone())
8796
.with_handler(
8897
Method::new()
8998
.expect_cycles(140)
90-
.response::<Result<u64, MintError>>(Ok(18))
91-
.expect_arguments((Some(&bob),)),
99+
.response::<TxReceipt>(Ok(Nat::from(18)))
100+
.expect_arguments((bob, Nat::from(0))),
92101
)
93102
.inject();
94103

@@ -98,9 +107,8 @@ mod tests {
98107
account: Some(bob),
99108
cycles: 140
100109
})
101-
.await
102-
.unwrap(),
103-
18
110+
.await,
111+
Ok(Nat::from(18))
104112
);
105113
}
106114

0 commit comments

Comments
 (0)