-
Notifications
You must be signed in to change notification settings - Fork 3
/
token.ts
202 lines (181 loc) · 6.51 KB
/
token.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { Protobuf, System } from "..";
import { token, error } from "@koinos/proto-as";
enum entries {
name_entry = 0x82a3537f,
symbol_entry = 0xb76a7ca1,
decimals_entry = 0xee80fd2f,
total_supply_entry = 0xb0da3934,
balance_of_entry = 0x5c721497,
transfer_entry = 0x27f576ca,
mint_entry = 0xdc6f17bb,
burn_entry = 0x859facc5,
}
export class Token {
_contractId: Uint8Array;
/**
* Create an instance of a token contract
* @example
* ```ts
* const token = new Token(Base58.decode('1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe'));
* ```
*/
constructor(contractId: Uint8Array) {
this._contractId = contractId;
}
/**
* Get the name of the token
* @returns string
* @example
* ```ts
* const token = new Token(Base58.decode('1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe'));
* const name = token.name();
* ```
*/
name(): string {
const args = new token.name_arguments();
const callRes = System.call(this._contractId, entries.name_entry, Protobuf.encode(args, token.name_arguments.encode));
System.require(callRes.code == 0, "failed to retrieve token name");
const res = Protobuf.decode<token.name_result>(callRes.res.object as Uint8Array, token.name_result.decode);
return res.value!;
}
/**
* Get the symbol of the token
* @returns string
* @example
* ```ts
* const token = new Token(Base58.decode('1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe'));
* const symbol = token.symbol();
* ```
*/
symbol(): string {
const args = new token.symbol_arguments();
const callRes = System.call(this._contractId, entries.symbol_entry, Protobuf.encode(args, token.symbol_arguments.encode));
System.require(callRes.code == 0, "failed to retrieve token symbol");
const res = Protobuf.decode<token.symbol_result>(callRes.res.object as Uint8Array, token.symbol_result.decode);
return res.value!;
}
/**
* Get the decimal of the token
* @returns u32
* @example
* ```ts
* const token = new Token(Base58.decode('1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe'));
* const decimals = token.decimals();
* ```
*/
decimals(): u32 {
const args = new token.decimals_arguments();
const callRes = System.call(this._contractId, entries.decimals_entry, Protobuf.encode(args, token.decimals_arguments.encode));
System.require(callRes.code == 0, "failed to retrieve token decimals");
const res = Protobuf.decode<token.decimals_result>(callRes.res.object as Uint8Array, token.decimals_result.decode);
return res.value;
}
/**
* Get the total supply of the token
* @returns u64
* @example
* ```ts
* const token = new Token(Base58.decode('1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe'));
* const totalSupply = token.totalSupply();
* ```
*/
totalSupply(): u64 {
const args = new token.total_supply_arguments();
const callRes = System.call(this._contractId, entries.total_supply_entry, Protobuf.encode(args, token.total_supply_arguments.encode));
System.require(callRes.code == 0, "failed to retrieve token supply");
const res = Protobuf.decode<token.total_supply_result>(callRes.res.object as Uint8Array, token.total_supply_result.decode);
return res.value;
}
/**
* Get the balance of the `owner` account
* @param owner owner account
* @returns u64 balance
* @example
* ```ts
* const ownerAccount = Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqE');
* const token = new Token(Base58.decode('1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe'));
* const balance = token.balanceOf(ownerAccount);
* ```
*/
balanceOf(owner: Uint8Array): u64 {
const args = new token.balance_of_arguments(owner);
const callRes = System.call(this._contractId, entries.balance_of_entry, Protobuf.encode(args, token.balance_of_arguments.encode));
System.require(callRes.code == 0, "failed to retrieve token balance");
const res = Protobuf.decode<token.balance_of_result>(callRes.res.object as Uint8Array, token.balance_of_result.decode);
return res.value;
}
/**
* Transfer tokens from `from` to `to`
* @param from from account
* @param to to account
* @param value number of tokens to transfer
* @returns bool transfer succeeded or not
* @example
* ```ts
* const from = Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe');
* const to = Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqE');
* const value = 12376182;
*
* const token = new Token(Base58.decode('1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe'));
* const success = token.transfer(from, to, value);
* if (success) {
* // transfer succeeded
* } else {
* // transfer failed
* }
* ```
*/
transfer(from: Uint8Array, to: Uint8Array, amount: u64): bool {
const args = new token.transfer_arguments(from, to, amount);
const callRes = System.call(this._contractId, entries.transfer_entry, Protobuf.encode(args, token.transfer_arguments.encode));
return callRes.code == error.error_code.success;
}
/**
* Mint tokens to `to` account
* @param to to account
* @param value number of tokens to mint
* @returns bool mint succeeded or not
* @example
* ```ts
* const to = Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqA');
* const value = 12376182;
*
* const token = new Token(Base58.decode('1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe'));
* const success = token.mint(to, value);
* if (success) {
* // mint succeeded
* } else {
* // mint failed
* }
* ```
*/
mint(to: Uint8Array, amount: u64): bool {
const args = new token.mint_arguments(to, amount);
const callRes = System.call(this._contractId, entries.mint_entry, Protobuf.encode(args, token.mint_arguments.encode));
return callRes.code == error.error_code.success;
}
/**
* Burn tokens from `from`
* @param from from account
* @param value number of tokens to burn
* @returns bool burn succeeded or not
* @example
* ```ts
* const from = Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe');
* const value = 12376182;
*
* const token = new Token(Base58.decode('1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe'));
* const success = token.burn(from, value);
* if (success) {
* // transfer succeeded
* } else {
* // transfer failed
* }
* ```
*/
burn(from: Uint8Array, amount: u64): bool {
const args = new token.burn_arguments(from, amount);
const callRes = System.call(this._contractId, entries.burn_entry, Protobuf.encode(args, token.burn_arguments.encode));
return callRes.code == error.error_code.success;
}
}