This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
multiwrap.go
334 lines (302 loc) · 9.38 KB
/
multiwrap.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
package thirdweb
import (
"context"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/thirdweb-dev/go-sdk/v2/abi"
)
// You can access the Multiwrap interface from the SDK as follows:
//
// import (
// "github.com/thirdweb-dev/go-sdk/v2/thirdweb"
// )
//
// privateKey := "..."
// secretKey := "..."
//
// sdk, err := thirdweb.NewThirdwebSDK("mumbai", &thirdweb.SDKOptions{
// PrivateKey: privateKey,
// SecretKey: secretKey
// })
//
// contract, err := sdk.GetMultiwrap("{{contract_address}}")
type Multiwrap struct {
*ERC721Standard
abi *abi.Multiwrap
Helper *contractHelper
Encoder *ContractEncoder
}
func newMultiwrap(provider *ethclient.Client, address common.Address, privateKey string, storage storage) (*Multiwrap, error) {
if contractAbi, err := abi.NewMultiwrap(address, provider); err != nil {
return nil, err
} else {
if helper, err := newContractHelper(address, provider, privateKey); err != nil {
return nil, err
} else {
if erc721, err := newERC721Standard(provider, address, privateKey, storage); err != nil {
return nil, err
} else {
encoder, err := newContractEncoder(abi.MultiwrapABI, helper)
if err != nil {
return nil, err
}
multiwrap := &Multiwrap{
erc721,
contractAbi,
helper,
encoder,
}
return multiwrap, nil
}
}
}
}
// Get the contents of a wrapped token bundle.
//
// wrappedTokenId: the ID of the wrapped token bundle
//
// returns: the contents of the wrapped token bundle
//
// Example
//
// tokenId := 0
// contents, err := contract.GetWrappedContents(tokenId)
// erc20Tokens := contents.Erc20Tokens
// erc721Tokens := contents.Erc721Tokens
// erc1155Tokens := contents.Erc1155Tokens
func (multiwrap *Multiwrap) GetWrappedContents(wrappedTokenId int) (*MultiwrapBundle, error) {
wrappedTokens, err := multiwrap.abi.GetWrappedContents(&bind.CallOpts{}, big.NewInt(int64(wrappedTokenId)))
if err != nil {
return nil, err
}
erc20Tokens := []*MultiwrapERC20{}
erc721Tokens := []*MultiwrapERC721{}
erc1155Tokens := []*MultiwrapERC1155{}
for _, wrappedToken := range wrappedTokens {
switch wrappedToken.TokenType {
case 0:
tokenMetadata, err := fetchCurrencyMetadata(context.Background(), multiwrap.Helper.GetProvider(), wrappedToken.AssetContract.String())
if err != nil {
return nil, err
}
erc20Tokens = append(erc20Tokens, &MultiwrapERC20{
ContractAddress: wrappedToken.AssetContract.String(),
Quantity: formatUnits(wrappedToken.TotalAmount, tokenMetadata.Decimals),
})
continue
case 1:
erc721Tokens = append(erc721Tokens, &MultiwrapERC721{
ContractAddress: wrappedToken.AssetContract.String(),
TokenId: int(wrappedToken.TokenId.Int64()),
})
case 2:
erc1155Tokens = append(erc1155Tokens, &MultiwrapERC1155{
ContractAddress: wrappedToken.AssetContract.String(),
TokenId: int(wrappedToken.TokenId.Int64()),
Quantity: int(wrappedToken.TotalAmount.Int64()),
})
}
}
tokens := &MultiwrapBundle{
ERC20Tokens: erc20Tokens,
ERC721Tokens: erc721Tokens,
ERC1155Tokens: erc1155Tokens,
}
return tokens, nil
}
// Wrap any number of ERC20, ERC721, or ERC1155 tokens into a single wrapped token
//
// contents: the tokens to wrap into a single wrapped token
//
// wrappedTokenMetadata: the NFT Metadata or URI to as the metadata for the wrapped token
//
// recipientAddress: the optional address to send the wrapped token to
//
// returns: the transaction receipt of the wrapping
//
// Example
//
// contents := &thirdweb.MultiwrapBundle{
// ERC20Tokens: []*thirdweb.MultiwrapERC20{
// &thirdweb.MultiwrapERC20{
// ContractAddress: "0x...",
// Quantity: 1,
// },
// },
// ERC721Tokens: []*thirdweb.MultiwrapERC721{
// &thirdweb.MultiwrapERC721{
// ContractAddress: "0x...",
// TokenId: 1,
// },
// },
// ERC1155Tokens: []*thirdweb.MultiwrapERC1155{
// &thirdweb.MultiwrapERC1155{
// ContractAddress: "0x...",
// TokenId: 1,
// Quantity: 1,
// },
// },
// }
//
// wrappedTokenMetadata := &thirdweb.NFTMetadataInput{
// Name: "Wrapped Token"
// }
//
// // This will mint the wrapped token to the connected wallet
// tx, err := contract.Wrap(context.Background(), contents, wrappedTokenMetadata, "")
func (multiwrap *Multiwrap) Wrap(ctx context.Context, contents *MultiwrapBundle, wrappedTokenMetadata interface{}, recipientAddress string) (*types.Transaction, error) {
uri, ok := wrappedTokenMetadata.(string)
if !ok {
tokenMetadata, ok := wrappedTokenMetadata.(*NFTMetadataInput)
if ok {
tokenUri, err := uploadOrExtractUri(ctx, tokenMetadata, multiwrap.erc721.storage)
if err != nil {
return nil, err
}
uri = tokenUri
} else {
return nil, errors.New("wrappedTokenMetadata must be a string or NFTMetadataInput")
}
}
if recipientAddress == "" {
recipientAddress = multiwrap.Helper.GetSignerAddress().String()
}
tokens, err := multiwrap.toTokenStructList(ctx, contents)
if err != nil {
return nil, err
}
txOpts, err := multiwrap.Helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := multiwrap.abi.Wrap(txOpts, tokens, uri, common.HexToAddress(recipientAddress))
if err != nil {
return nil, err
}
return multiwrap.Helper.AwaitTx(ctx, tx.Hash())
}
// Unwrap a wrapped token bundle into its contents
//
// wrappedTokenId: the ID of the wrapped token bundle
//
// recipientAddress: the optional address to send the wrapped token to
//
// returns: the contents of the wrapped token bundle
//
// Example
//
// tokenId := 0
// tx, err := contract.Unwrap(context.Background(), tokenId, "")
func (multiwrap *Multiwrap) Unwrap(ctx context.Context, wrappedTokenId int, recipientAddress string) (*types.Transaction, error) {
if recipientAddress == "" {
recipientAddress = multiwrap.Helper.GetSignerAddress().String()
}
txOpts, err := multiwrap.Helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := multiwrap.abi.Unwrap(txOpts, big.NewInt(int64(wrappedTokenId)), common.HexToAddress(recipientAddress))
if err != nil {
return nil, err
}
return multiwrap.Helper.AwaitTx(ctx, tx.Hash())
}
func (multiwrap *Multiwrap) toTokenStructList(ctx context.Context, contents *MultiwrapBundle) ([]abi.ITokenBundleToken, error) {
tokens := []abi.ITokenBundleToken{}
provider := multiwrap.Helper.GetProvider()
owner := multiwrap.Helper.GetSignerAddress()
for _, erc20 := range contents.ERC20Tokens {
normalizedQuantity, err := normalizePriceValue(
ctx,
provider,
erc20.Quantity,
erc20.ContractAddress,
)
if err != nil {
return nil, err
}
hasAllowance, err := hasErc20Allowance(
ctx,
multiwrap.Helper,
erc20.ContractAddress,
normalizedQuantity,
)
if err != nil {
return nil, err
}
if !hasAllowance {
return nil, fmt.Errorf(
fmt.Sprintf("ERC20 with contract address %v does not have enough allowance to transfer.", erc20.ContractAddress) +
"You can set allowance to the multiwrap contract to transfer these tokens by running:\n" +
fmt.Sprintf("token, _ := sdk.GetToken(\"%v\")\ntoken.SetAllowance(\"%v\", %f)", erc20.ContractAddress, owner.String(), erc20.Quantity),
)
}
token := abi.ITokenBundleToken{
TokenType: 0,
AssetContract: common.HexToAddress(erc20.ContractAddress),
TotalAmount: normalizedQuantity,
TokenId: big.NewInt(0),
}
tokens = append(tokens, token)
}
for _, erc721 := range contents.ERC721Tokens {
isApproved, err := isTokenApprovedForTransfer(
ctx,
provider,
multiwrap.Helper.getAddress().String(),
erc721.ContractAddress,
erc721.TokenId,
owner.String(),
)
if err != nil {
return nil, err
}
if !isApproved {
return nil, fmt.Errorf(
fmt.Sprintf("ERC721 with contract address %v does not have enough allowance to transfer.", erc721.ContractAddress) +
"You can set allowance to the multiwrap contract to transfer this token by running:\n" +
fmt.Sprintf("nft, _ := sdk.GetNFTCollection(\"%v\")\nnft.SetApprovalForToken(\"%v\", %d)", erc721.ContractAddress, multiwrap.Helper.getAddress().String(), erc721.TokenId),
)
}
token := abi.ITokenBundleToken{
TokenType: 1,
AssetContract: common.HexToAddress(erc721.ContractAddress),
TotalAmount: big.NewInt(0),
TokenId: big.NewInt(int64(erc721.TokenId)),
}
tokens = append(tokens, token)
}
for _, erc1155 := range contents.ERC1155Tokens {
isApproved, err := isTokenApprovedForTransfer(
ctx,
provider,
multiwrap.Helper.getAddress().String(),
erc1155.ContractAddress,
erc1155.TokenId,
owner.String(),
)
if err != nil {
return nil, err
}
if !isApproved {
return nil, fmt.Errorf(
fmt.Sprintf("ERC1155 with contract address %v does not have enough allowance to transfer.", erc1155.ContractAddress) +
"You can set allowance to the multiwrap contract to transfer this token by running:\n" +
fmt.Sprintf("edition, _ := sdk.GetEdition(\"%v\")\nedition.SetApprovalForAll(\"%v\", true)", erc1155.ContractAddress, multiwrap.Helper.getAddress().String()),
)
}
token := abi.ITokenBundleToken{
TokenType: 2,
AssetContract: common.HexToAddress(erc1155.ContractAddress),
TotalAmount: big.NewInt(int64(erc1155.Quantity)),
TokenId: big.NewInt(int64(erc1155.TokenId)),
}
tokens = append(tokens, token)
}
return tokens, nil
}