/
wasm.go
380 lines (314 loc) · 9.43 KB
/
wasm.go
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
377
378
379
380
package keeper
import (
"encoding/json"
"io/ioutil"
"net/http"
"sync"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
"math/big"
"github.com/UptickNetwork/wasm-nft-convert/types"
)
// ContractInfo {"data":{"name":"uptick test collection","symbol":"uptick-test-01"}}
type ContractInfo struct {
Data ContractInfoData `json:"data"`
}
type ContractInfoData struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
}
type Result struct {
Data *string `json:"data,omitempty"`
}
// QueryCW721 returns the data of a deployed CW721 contract
func (k Keeper) QueryCW721(
ctx sdk.Context,
contractAddress string,
) (types.CW721Data, error) {
contractInfo := make(map[string]Result)
contractInfo["contract_info"] = Result{}
jsonStr, err := json.Marshal(contractInfo)
if err != nil {
return types.CW721Data{}, err
} else {
contractInfoResult, err := k.QueryWasmState(ctx,
&wasmtypes.QuerySmartContractStateRequest{
Address: contractAddress,
QueryData: jsonStr,
})
if err != nil {
return types.CW721Data{}, err
}
var contractInfoResultJson ContractInfoData
json.Unmarshal(contractInfoResult.Data, &contractInfoResultJson)
return types.CW721Data{
Name: contractInfoResultJson.Name,
Symbol: contractInfoResultJson.Symbol,
}, nil
}
}
type AllNftInfo struct {
Access AllNftAccess `json:"access"`
Info AllNftData `json:"info"`
}
type AllNftAccess struct {
Owner string `json:"owner"`
}
type AllNftData struct {
TokenUri string `json:"token_uri"`
Extension interface{} `json:"extension"`
}
// QueryCW721AllNftInfo returns the owner of given tokenID
// QueryCW721TokenOwner -> QueryCW721TokenOwner
func (k Keeper) QueryCW721AllNftInfo(
ctx sdk.Context,
contractAddress string,
tokenId string,
) (AllNftInfo, error) {
// `{"all_nft_info":{"token_id":"abc125"}}`
allNftInfoCondition := make(map[string]map[string]string)
subCondition := make(map[string]string)
subCondition["token_id"] = tokenId
allNftInfoCondition["all_nft_info"] = subCondition
jsonConditionStr, err := json.Marshal(allNftInfoCondition)
if err != nil {
return AllNftInfo{}, err
} else {
allNftInfo, err := k.QueryWasmState(ctx,
&wasmtypes.QuerySmartContractStateRequest{
Address: contractAddress,
QueryData: jsonConditionStr,
})
if err != nil {
return AllNftInfo{}, err
}
var allContractInfoResultJson AllNftInfo
json.Unmarshal(allNftInfo.Data, &allContractInfoResultJson)
return allContractInfoResultJson, nil
}
}
type StoreCache struct {
sync.Mutex
contracts map[string][]byte
}
var contractsCache = StoreCache{contracts: make(map[string][]byte)}
func getContractBytes(contract string) ([]byte, error) {
contractsCache.Lock()
bz, found := contractsCache.contracts[contract]
contractsCache.Unlock()
if found {
return bz, nil
}
contractsCache.Lock()
defer contractsCache.Unlock()
var err error
bz, err = getBytesFromUrl(contract)
if err != nil {
return nil, err
}
contractsCache.contracts[contract] = bz
return bz, nil
}
func getBytesFromUrl(url string) ([]byte, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
bz, err := ioutil.ReadAll(response.Body)
// bz, err := ioutil.ReadFile("/Users/xuxinlai/my/mul/gon/v2/ics721-setup/cw721_base.wasm")
return bz, err
}
// StoreWasmContract creates and deploys an CW721 contract on the EVM with the
// erc721 module account as owner.
func (k Keeper) StoreWasmContract(
ctx sdk.Context,
contractFile string,
creator string,
) (uint64, error) {
bin, err := getContractBytes(contractFile)
if err != nil {
k.Logger(ctx).Error("getContractBytes ", "err :", err)
return 0, err
}
// wasmkeeper.NewDefaultPermissionKeeper()
wasmMsgServer := wasmkeeper.NewMsgServerImpl(&k.cwKeeper)
res, err := wasmMsgServer.StoreCode(sdk.WrapSDKContext(ctx), &wasmtypes.MsgStoreCode{
Sender: creator,
WASMByteCode: bin,
})
if err != nil {
k.Logger(ctx).Error("StoreCode ", "err :", err)
return 0, err
}
return res.CodeID, nil
}
type InstantiateInfo struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
Minter string `json:"minter"`
}
// InstantiateWasmContract creates and deploys an CW721 contract on the EVM with the
// erc721 module account as owner.
func (k Keeper) InstantiateWasmContract(
ctx sdk.Context,
senderAddr string,
codeId uint64,
label string,
name string,
symbol string,
minter string,
) (string, error) {
var instantiateInfo InstantiateInfo
instantiateInfo.Name = name
instantiateInfo.Symbol = symbol
instantiateInfo.Minter = minter
instantiateInfoJsonStr, err := json.Marshal(instantiateInfo)
if err != nil {
return "", err
}
wasmMsgServer := wasmkeeper.NewMsgServerImpl(&k.cwKeeper)
initMsg := wasmtypes.MsgInstantiateContract{
Sender: senderAddr, Admin: senderAddr, CodeID: codeId,
Label: label, Msg: wasmtypes.RawContractMessage(instantiateInfoJsonStr),
Funds: sdk.NewCoins(),
}
res, err := wasmMsgServer.InstantiateContract(sdk.WrapSDKContext(ctx), &initMsg)
if err != nil {
return "", err
}
return res.Address, nil
}
// MintInfo {"mint":{"token_id":"abc126","owner":"uptick100s3yp8l3atuuvx98jmftttxzy4ee5mg2n79fx","token_uri":"http://test.com"}}
type MintInfo struct {
Mint MintInfoData `json:"mint"`
}
type MintInfoData struct {
TokenId string `json:"token_id"`
Owner string `json:"owner"`
TokenUri string `json:"token_uri"`
}
// MintCw721 the contract and get the result
func (k Keeper) MintCw721(
ctx sdk.Context,
contractAddress string,
tokenId string,
sender string,
tokenUri string,
) (*wasmtypes.MsgExecuteContractResponse, error) {
var mintInfoData MintInfoData
var mintInfo MintInfo
mintInfoData.TokenId = tokenId
mintInfoData.Owner = sender
mintInfoData.TokenUri = tokenUri
mintInfo.Mint = mintInfoData
mintJsonStr, err := json.Marshal(mintInfo)
if err != nil {
return nil, err
} else {
// Execute the contract
funds := sdk.NewCoins(sdk.NewCoin("uptick", sdk.ZeroInt()))
// msgBytes := wasmtypes.RawContractMessage(`{"mint":{"token_id":"abc126","owner":"uptick100s3yp8l3atuuvx98jmftttxzy4ee5mg2n79fx","token_uri":"http://test.com"}}`)
execMsg := wasmtypes.MsgExecuteContract{
Sender: sender,
Contract: contractAddress,
Msg: wasmtypes.RawContractMessage(mintJsonStr),
Funds: funds,
}
_, err := k.ExecWasmMsg(ctx, &execMsg)
if err != nil {
return nil, err
} else {
return nil, nil
}
}
}
// TransferNftInfo
// {"transfer_nft":{"recipient":"uptick1n3t0zuwq4u47ke48qm3pfhj96f4ujhs70f52sg","token_id":"abc123"}}
type TransferNftInfo struct {
TransferNft TransferNftData `json:"transfer_nft"`
}
type TransferNftData struct {
Recipient string `json:"recipient"`
TokenId string `json:"token_id"`
}
// TransferCw721 the contract and get the result
func (k Keeper) TransferCw721(
ctx sdk.Context,
contractAddress string,
tokenId string,
recipient string,
sender string,
) (*wasmtypes.MsgExecuteContractResponse, error) {
var transferNftInfo TransferNftInfo
var transferNftData TransferNftData
transferNftData.TokenId = tokenId
transferNftData.Recipient = recipient
transferNftInfo.TransferNft = transferNftData
transferJsonStr, err := json.Marshal(transferNftInfo)
if err != nil {
return nil, err
} else {
// Execute the contract
funds := sdk.NewCoins(sdk.NewCoin("uptick", sdk.ZeroInt()))
execMsg := wasmtypes.MsgExecuteContract{
Sender: sender,
Contract: contractAddress,
Msg: wasmtypes.RawContractMessage(transferJsonStr),
Funds: funds,
}
_, err := k.ExecWasmMsg(ctx, &execMsg)
if err != nil {
return nil, err
} else {
return nil, nil
}
}
}
// ExecWasmMsg exec the contract and get the result
func (k Keeper) ExecWasmMsg(
ctx sdk.Context,
execMsg *wasmtypes.MsgExecuteContract) (*wasmtypes.MsgExecuteContractResponse, error) {
if err := execMsg.ValidateBasic(); err != nil {
return nil, sdkerrors.Wrapf(types.ErrABIPack, "nft class is invalid %s: %s", execMsg.Msg, err.Error())
}
wasmMsgServer := wasmkeeper.NewMsgServerImpl(&k.cwKeeper)
return wasmMsgServer.ExecuteContract(sdk.WrapSDKContext(ctx), execMsg)
}
// QueryWasmState for query (rsp *types.QuerySmartContractStateResponse, err error)
// func (q grpcQuerier) SmartContractState(c context.Context, req *types.QuerySmartContractStateRequest)
// (rsp *types.QuerySmartContractStateResponse, err error) {
func (k Keeper) QueryWasmState(
ctx sdk.Context,
req *wasmtypes.QuerySmartContractStateRequest) (*wasmtypes.QuerySmartContractStateResponse, error) {
return wasmkeeper.Querier(&k.cwKeeper).SmartContractState(ctx, req)
}
// ---------------------------------------------------------------------------------------------------------------------
// QueryClassEnhance returns the data of a deployed CW721 contract
// TODO
func (k Keeper) QueryClassEnhance(
ctx sdk.Context,
contract common.Address,
) (types.ClassEnhance, error) {
return types.ClassEnhance{}, nil
}
// QueryNFTEnhance returns the data of a deployed CW721 contract
// TODO
func (k Keeper) QueryNFTEnhance(
ctx sdk.Context,
contract common.Address,
tokenID *big.Int,
) (types.NFTEnhance, error) {
return types.NFTEnhance{}, nil
}
// QueryCW721Token returns the data of a CW721 token
// TODO
func (k Keeper) QueryCW721Token(
ctx sdk.Context,
contract common.Address,
) (types.CW721TokenData, error) {
return types.CW721TokenData{}, nil
}