forked from jonnifer-six/cw-plus-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cw721-base.ts
376 lines (324 loc) · 11.9 KB
/
cw721-base.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import { calculateFee } from "@cosmjs/stargate"
/*
* This is a set of helpers meant for use with @cosmjs/cli
* Look at https://raw.githubusercontent.com/InterWasm/cw-plus-helpers/main/base.ts on how to setup a wallet
* With these you can easily use the cw1 contract without worrying about forming messages and parsing queries.
*
* Usage: npx @cosmjs/cli@^0.26 --init https://raw.githubusercontent.com/InterWasm/cw-plus-helpers/main/base.ts --init https://raw.githubusercontent.com/InterWasm/cw-plus-helpers/main/cw721-base.ts
*
* Create a client:
* const [addr, client] = await useOptions(uniOptions).setup('password');
*
* Get the mnemonic:
* await useOptions(uniOptions).recoverMnemonic(password);
*
* Create contract:
* const contract = CW721(client, uniOptions);
*
* Upload contract:
* const codeId = await contract.upload(addr, uniOptions);
*
* Instantiate contract example:
* const initMsg = {
* name: "Potato Coin",
* symbol: "TATER",
* minter: addr
* };
*
* const instance = await contract.instantiate(addr, codeId, initMsg, 'Potato Coin!', uniOptions);
*
* If you want to use this code inside an app, you will need several imports from https://github.com/CosmWasm/cosmjs
*/
type TokenId = string
interface Balances {
readonly address: string
readonly amount: string // decimal as string
}
interface MintInfo {
readonly minter: string
readonly cap?: string // decimal as string
}
interface ContractInfo {
readonly name: string
readonly symbol: string
}
interface NftInfo {
readonly name: string
readonly description: string
readonly image: any
}
interface Access {
readonly owner: string
readonly approvals: []
}
interface AllNftInfo {
readonly access: Access
readonly info: NftInfo
}
interface Operators {
readonly operators: []
}
interface Count {
readonly count: number
}
interface InitMsg {
readonly name: string
readonly symbol: string
readonly minter: string
}
// Better to use this interface?
interface MintMsg {
readonly token_id: TokenId
readonly owner: string
readonly name: string
readonly description?: string
readonly image?: string
}
type Expiration = { readonly at_height: number } | { readonly at_time: number } | { readonly never: {} }
interface AllowanceResponse {
readonly allowance: string // integer as string
readonly expires: Expiration
}
interface AllowanceInfo {
readonly allowance: string // integer as string
readonly spender: string // bech32 address
readonly expires: Expiration
}
interface AllAllowancesResponse {
readonly allowances: readonly AllowanceInfo[]
}
interface AllAccountsResponse {
// list of bech32 address that have a balance
readonly accounts: readonly string[]
}
interface TokensResponse {
readonly tokens: readonly string[]
}
interface CW721Instance {
readonly contractAddress: string
// queries
allowance: (owner: string, spender: string) => Promise<AllowanceResponse>
allAllowances: (owner: string, startAfter?: string, limit?: number) => Promise<AllAllowancesResponse>
allAccounts: (startAfter?: string, limit?: number) => Promise<readonly string[]>
minter: () => Promise<MintInfo>
contractInfo: () => Promise<ContractInfo>
nftInfo: (tokenId: TokenId) => Promise<NftInfo>
allNftInfo: (tokenId: TokenId) => Promise<AllNftInfo>
ownerOf: (tokenId: TokenId) => Promise<Access>
approvedForAll: (owner: string, include_expired?: boolean, start_after?: string, limit?: number) => Promise<Operators>
numTokens: () => Promise<Count>
tokens: (owner: string, startAfter?: string, limit?: number) => Promise<TokensResponse>
allTokens: (startAfter?: string, limit?: number) => Promise<TokensResponse>
// actions
mint: (
senderAddress: string,
tokenId: TokenId,
owner: string,
name: string,
level: number,
description?: string,
image?: string,
) => Promise<string>
transferNft: (senderAddress: string, recipient: string, tokenId: TokenId) => Promise<string>
sendNft: (senderAddress: string, contract: string, token_id: TokenId, msg?: BinaryType) => Promise<string>
approve: (senderAddress: string, spender: string, tokenId: TokenId, expires?: Expiration) => Promise<string>
approveAll: (senderAddress: string, operator: string, expires?: Expiration) => Promise<string>
revoke: (senderAddress: string, spender: string, tokenId: TokenId) => Promise<string>
revokeAll: (senderAddress: string, operator: string) => Promise<string>
}
interface CW721Contract {
// upload a code blob and returns a codeId
upload: (senderAddress: string, options: Options) => Promise<number>
// instantiates a cw721 contract
// codeId must come from a previous deploy
// label is the public name of the contract in listing
// if you set admin, you can run migrations on this contract (likely client.senderAddress)
instantiate: (
senderAddress: string,
codeId: number,
initMsg: Record<string, unknown>,
label: string,
options: Options,
admin?: string,
) => Promise<CW721Instance>
use: (contractAddress: string) => CW721Instance
}
export const CW721 = (client: SigningCosmWasmClient, options: Options): CW721Contract => {
const use = (contractAddress: string): CW721Instance => {
const allowance = async (owner: string, spender: string): Promise<AllowanceResponse> => {
return client.queryContractSmart(contractAddress, { allowance: { owner, spender } })
}
const allAllowances = async (
owner: string,
startAfter?: string,
limit?: number,
): Promise<AllAllowancesResponse> => {
return client.queryContractSmart(contractAddress, { all_allowances: { owner, start_after: startAfter, limit } })
}
const allAccounts = async (startAfter?: string, limit?: number): Promise<readonly string[]> => {
const accounts: AllAccountsResponse = await client.queryContractSmart(contractAddress, {
all_accounts: { start_after: startAfter, limit },
})
return accounts.accounts
}
const minter = async (): Promise<MintInfo> => {
return client.queryContractSmart(contractAddress, { minter: {} })
}
const contractInfo = async (): Promise<ContractInfo> => {
return client.queryContractSmart(contractAddress, { contract_info: {} })
}
const nftInfo = async (token_id: TokenId): Promise<NftInfo> => {
return client.queryContractSmart(contractAddress, { nft_info: { token_id } })
}
const allNftInfo = async (token_id: TokenId): Promise<AllNftInfo> => {
return client.queryContractSmart(contractAddress, { all_nft_info: { token_id } })
}
const ownerOf = async (token_id: TokenId): Promise<Access> => {
return await client.queryContractSmart(contractAddress, { owner_of: { token_id } })
}
const approvedForAll = async (
owner: string,
include_expired?: boolean,
start_after?: string,
limit?: number,
): Promise<Operators> => {
return await client.queryContractSmart(contractAddress, {
approved_for_all: { owner, include_expired, start_after, limit },
})
}
// total number of tokens issued
const numTokens = async (): Promise<Count> => {
return client.queryContractSmart(contractAddress, { num_tokens: {} })
}
// list all token_ids that belong to a given owner
const tokens = async (owner: string, start_after?: string, limit?: number): Promise<TokensResponse> => {
return client.queryContractSmart(contractAddress, { tokens: { owner, start_after, limit } })
}
const allTokens = async (start_after?: string, limit?: number): Promise<TokensResponse> => {
return client.queryContractSmart(contractAddress, { all_tokens: { start_after, limit } })
}
// actions
const mint = async (
senderAddress: string,
token_id: TokenId,
owner: string,
name: string,
level: number,
description?: string,
image?: string,
): Promise<string> => {
const fee = calculateFee(options.fees.exec, options.gasPrice)
const result = await client.execute(
senderAddress,
contractAddress,
{ mint: { token_id, owner, name, level, description, image } },
fee,
)
return result.transactionHash
}
// transfers ownership, returns transactionHash
const transferNft = async (senderAddress: string, recipient: string, token_id: TokenId): Promise<string> => {
const fee = calculateFee(options.fees.exec, options.gasPrice)
const result = await client.execute(
senderAddress,
contractAddress,
{ transfer_nft: { recipient, token_id } },
fee,
)
return result.transactionHash
}
// sends an nft token to another contract (TODO: msg type any needs to be revisited once receiveNft is implemented)
const sendNft = async (senderAddress: string, contract: string, token_id: TokenId, msg?: any): Promise<string> => {
const fee = calculateFee(options.fees.exec, options.gasPrice)
const result = await client.execute(
senderAddress,
contractAddress,
{ send_nft: { contract, token_id, msg } },
fee,
)
return result.transactionHash
}
const approve = async (
senderAddress: string,
spender: string,
token_id: TokenId,
expires?: Expiration,
): Promise<string> => {
const fee = calculateFee(options.fees.exec, options.gasPrice)
const result = await client.execute(
senderAddress,
contractAddress,
{ approve: { spender, token_id, expires } },
fee,
)
return result.transactionHash
}
const approveAll = async (senderAddress: string, operator: string, expires?: Expiration): Promise<string> => {
const fee = calculateFee(options.fees.exec, options.gasPrice)
const result = await client.execute(senderAddress, contractAddress, { approve_all: { operator, expires } }, fee)
return result.transactionHash
}
const revoke = async (senderAddress: string, spender: string, token_id: TokenId): Promise<string> => {
const fee = calculateFee(options.fees.exec, options.gasPrice)
const result = await client.execute(senderAddress, contractAddress, { revoke: { spender, token_id } }, fee)
return result.transactionHash
}
const revokeAll = async (senderAddress: string, operator: string): Promise<string> => {
const fee = calculateFee(options.fees.exec, options.gasPrice)
const result = await client.execute(senderAddress, contractAddress, { revoke_all: { operator } }, fee)
return result.transactionHash
}
return {
contractAddress,
allowance,
allAllowances,
allAccounts,
minter,
contractInfo,
nftInfo,
allNftInfo,
ownerOf,
approvedForAll,
numTokens,
tokens,
allTokens,
mint,
transferNft,
sendNft,
approve,
approveAll,
revoke,
revokeAll,
}
}
const downloadWasm = async (url: string): Promise<Uint8Array> => {
const r = await axios.get(url, { responseType: "arraybuffer" })
if (r.status !== 200) {
throw new Error(`Download error: ${r.status}`)
}
return r.data
}
const upload = async (senderAddress: string, options: Options): Promise<number> => {
const sourceUrl = "https://github.com/CosmWasm/cw-nfts/releases/download/v0.10.0/cw721_base.wasm"
const wasm = await downloadWasm(sourceUrl)
const fee = calculateFee(options.fees.upload, options.gasPrice)
const result = await client.upload(senderAddress, wasm, fee)
return result.codeId
}
const instantiate = async (
senderAddress: string,
codeId: number,
initMsg: Record<string, unknown>,
label: string,
options: Options,
admin?: string,
): Promise<CW721Instance> => {
const fee = calculateFee(options.fees.init, options.gasPrice)
const result = await client.instantiate(senderAddress, codeId, initMsg, label, fee, {
memo: `Init ${label}`,
admin,
})
return use(result.contractAddress)
}
return { upload, instantiate, use }
}