-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
271 lines (220 loc) · 7.07 KB
/
params.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
package keeper
import (
"github.com/UptickNetwork/wasm-nft-convert/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"strconv"
)
const UPTICK_CW721_LABLE = "Uptick CW721"
const UPTICK_CW721_NAME = "Uptick CW721"
const UPTICK_CW721_SYMBOL = "UCW721"
const BASE_WASM_URL = "https://raw.githubusercontent.com/UptickNetwork/uptick/main/release/wasm/"
// GetParams returns the total set of erc20 parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramstore.GetParamSet(ctx, ¶ms)
return params
}
// SetParams sets the erc20 parameters to the param space.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, ¶ms)
}
// GetClassIDAndNFTID sets the erc20 parameters to the param space.
func (k Keeper) GetClassIDAndNFTID(ctx sdk.Context, msg *types.MsgConvertCW721) (string, []string, error) {
var (
nftIds []string
nftId string
classId string
err error
nftOrg string
)
for i, tokenId := range msg.TokenIds {
uTokenId := types.CreateTokenUID(msg.ContractAddress, tokenId)
savedNftId, savedClassId := types.GetNFTFromUID(string(k.GetNFTUIDPairByTokenUID(ctx, uTokenId)))
nftOrg = ""
if len(msg.NftIds) > i {
nftOrg = msg.NftIds[i]
}
nftId, err = getNftData(nftOrg, tokenId, savedNftId, 0)
nftIds = append(nftIds, nftId)
if err != nil {
return "", nil, err
}
classId, err = getNftData(msg.ClassId, msg.ContractAddress, savedClassId, 1)
if err != nil {
return "", nil, err
}
if msg.ClassId != "" && msg.ClassId != classId {
return "", nil, sdkerrors.Wrapf(types.ErrContractAddressNotCorrect,
"nft id is not correct expect %s - get %s",
msg.ClassId, classId)
}
}
return classId, nftIds, nil
}
func (k Keeper) LoadCw721Base(ctx sdk.Context) (error, uint64) {
// come to load cw721 base ...
var codeId uint64
var err error
resultBytes := k.GetWasmCode(ctx, types.AccModuleAddress.String())
codeId, _ = strconv.ParseUint(string(resultBytes), 10, 64)
if codeId <= 0 {
codeId, err = k.StoreWasmContract(ctx, BASE_WASM_URL+"cw721_base.wasm", types.AccModuleAddress.String())
if err != nil {
k.Logger(ctx).Error("LoadCw721Base ", "err:", err.Error())
return err, 0
} else {
k.Logger(ctx).Info("LoadCw721Base codeId ", "codeId is : ", codeId)
k.SetWasmCode(ctx, types.AccModuleAddress.String(), codeId)
}
}
return nil, codeId
}
// GetContractAddressAndTokenIds sets the CW721 parameters to the param space.
func (k Keeper) GetContractAddressAndTokenIds(ctx sdk.Context, msg *types.MsgConvertNFT) (string, []string, error) {
var (
contractAddress string
tokenIds []string
err error
)
pair, err := k.GetPair(ctx, msg.ClassId)
if err != nil {
var codeId uint64
resultBytes := k.GetWasmCode(ctx, types.AccModuleAddress.String())
codeId, _ = strconv.ParseUint(string(resultBytes), 10, 64)
err, codeId = k.LoadCw721Base(ctx)
if err != nil {
return "", nil, err
}
k.Logger(ctx).Info("GetContractAddressAndTokenIds", "codeId is ", codeId)
contractAddress, err = k.InstantiateWasmContract(
ctx,
types.AccModuleAddress.String(),
codeId,
UPTICK_CW721_LABLE,
UPTICK_CW721_NAME,
UPTICK_CW721_SYMBOL,
types.AccModuleAddress.String(),
)
k.Logger(ctx).Info("GetContractAddressAndTokenIds", "contractAddress", contractAddress)
if err == nil {
k.Logger(ctx).Info(" GetContractAddressAndTokenIds", "contractAddress", contractAddress, "NftIds", msg.NftIds)
return contractAddress, msg.NftIds, err
} else {
k.Logger(ctx).Error(" GetContractAddressAndTokenIds", "NftIds", msg.NftIds, "error", err.Error())
return "", msg.NftIds, err
}
} else {
var (
savedTokenIds []string
savedTokenId string
)
for _, nftId := range msg.NftIds {
uNftID := types.CreateNFTUID(msg.ClassId, nftId)
savedTokenId, _ = types.GetNFTFromUID(string(k.GetTokenUIDPairByNFTUID(ctx, uNftID)))
savedTokenIds = append(savedTokenIds, savedTokenId)
}
tokenIds, err = getNftDatas(msg.TokenIds, msg.NftIds, savedTokenIds, 2)
if err != nil {
return "", nil, err
}
k.Logger(ctx).Info(" GetContractAddressAndTokenIds", "tokenIds", tokenIds)
//
//contractAddress, err = getNftData(msg.ContractAddress, msg.ClassId, savedContractAddress, 3)
//k.Logger(ctx).Info(" GetContractAddressAndTokenIds", "contractAddress", contractAddress)
//
//if contractAddress == "" {
// contractAddress = pair.Cw721Address
//}
//k.Logger(ctx).Info(" GetContractAddressAndTokenIds", "pair.Cw721Address", pair.Cw721Address)
//
//if err != nil {
// return "", nil, err
//}
if msg.ContractAddress != "" && msg.ContractAddress != pair.Cw721Address {
return "", nil, sdkerrors.Wrapf(types.ErrContractAddressNotCorrect,
"nft id is not correct expect %s - get %s",
msg.ContractAddress, pair.Cw721Address)
}
return pair.Cw721Address, tokenIds, nil
}
}
func getNftDatas(nftOrgs []string, nftPairOrgs []string, nftSaveds []string, nftType int) ([]string, error) {
var rets []string
var nftSaved = ""
var nftOrg = ""
nftLen := len(nftPairOrgs)
for n := 0; n < nftLen; n++ {
if nftSaveds != nil {
nftSaved = nftSaveds[n]
}
if nftOrgs != nil && nftLen == len(nftOrgs) {
nftOrg = nftOrgs[n]
}
ret, err := getNftData(nftOrg, nftPairOrgs[n], nftSaved, nftType)
if err != nil {
return nil, err
} else {
rets = append(rets, ret)
}
}
return rets, nil
}
// getNftData nftType 0:nftId 1:classId 2:contract address 3:tokenId
func getNftData(nftOrg string, nftPairOrg string, nftSaved string, nftType int) (string, error) {
var nftRet string
if nftOrg == "" {
if nftSaved == "" {
// nftRet = createNftDataByType(nftPairOrg, nftType)
nftRet = nftPairOrg
} else {
nftRet = nftSaved
}
} else {
if nftSaved == "" {
nftRet = nftOrg
} else if nftSaved == nftOrg {
nftRet = nftOrg
} else {
return "", getNftDataErrorByType(nftSaved, nftOrg, nftType)
}
}
return nftRet, nil
}
// createNftDataByType nftType 0:nftId 1:classId 2:contract address 3:tokenId
func createNftDataByType(nftOrg string, nftType int) string {
switch nftType {
case 0:
return types.CreateNFTIDFromTokenID(nftOrg)
case 1:
return types.CreateClassIDFromContractAddress(nftOrg)
case 2:
return types.CreateTokenIDFromNFTID(nftOrg)
case 3:
return types.CreateContractAddressFromClassID(nftOrg)
default:
return ""
}
}
// createNftDataByType nftType 0:nftId 1:classId 2:contract address 3:tokenId
func getNftDataErrorByType(nftSaved string, nftOrg string, nftType int) error {
switch nftType {
case 0:
return sdkerrors.Wrapf(types.ErrNftIdNotCorrect,
"nft id is not correct expect %s - get %s",
nftSaved, nftOrg)
case 1:
return sdkerrors.Wrapf(types.ErrClassIdNotCorrect,
"class id is not correct expect %s - get %s",
nftSaved, nftOrg)
case 2:
return sdkerrors.Wrapf(types.ErrTokenIdNotCorrect,
"token id is not correct expect %s - get %s",
nftSaved, nftOrg)
case 3:
return sdkerrors.Wrapf(types.ErrContractAddressNotCorrect,
"contract address is not correct expect %s - get %s",
nftSaved, nftOrg)
default:
return nil
}
}