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
/
nft_drop_encoder.go
209 lines (190 loc) · 6 KB
/
nft_drop_encoder.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
package thirdweb
import (
"context"
"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/thirdweb-dev/go-sdk/abi"
)
// The nft drop encoder class is used to get the unsigned transaction data for nft drop contract
// contract calls that can be signed at a later time after generation.
//
// It can be accessed from the SDK through the `Encoder` namespace of the nft drop contract:
//
// You can access the NFTDrop interface from the SDK as follows:
//
// import (
// "github.com/thirdweb-dev/go-sdk/thirdweb"
// )
//
// privateKey = "..."
//
// sdk, err := thirdweb.NewThirdwebSDK("mumbai", &thirdweb.SDKOptions{
// PrivateKey: privateKey,
// })
//
// contract, err := sdk.GetNFTDrop("{{contract_address}}")
//
// // Now the encoder can be accessed from the contract
// contract.Encoder.ClaimTo(...)
type NFTDropEncoder struct {
abi *abi.DropERC721
helper *contractHelper
storage storage
claimConditions *NFTDropClaimConditions
*ContractEncoder
}
func newNFTDropEncoder(
contractAbi *abi.DropERC721,
helper *contractHelper,
claimConditions *NFTDropClaimConditions,
storage storage,
) (*NFTDropEncoder, error) {
encoder, err := newContractEncoder(abi.DropERC721ABI, helper)
if err != nil {
return nil, err
}
return &NFTDropEncoder{
abi: contractAbi,
helper: helper,
claimConditions: claimConditions,
storage: storage,
ContractEncoder: encoder,
}, nil
}
// Get the data for the transaction data required to approve the ERC20 token transfers
// necessary to claim NFTs from this contract.
//
// signerAddress: the address intended to sign the transaction
//
// destinationAddress: the address of the wallet to claim the NFTs to
//
// quantity: the number of NFTs to claim
//
// returns: the transaction data of the token approval for the claim
//
// Example
//
// // Address of the wallet we expect to sign this message
// signerAddress := "0x..."
// // Address of the wallet we want to claim the NFTs to
// destinationAddress := "{{wallet_address}}"
// // Number of NFTs to claim
// quantity = 1
//
// tx, err := contract.Encoder.ApproveClaimTo(context.Background(), signerAddress, destinationAddress, quantity)
//
// // Now you can get all the standard transaction data as needed
// fmt.Println(tx.Data()) // Ex: get the data field or the nonce field (others are available)
// fmt.Println(tx.Nonce())
func (encoder *NFTDropEncoder) ApproveClaimTo(ctx context.Context, signerAddress string, destinationAddress string, quantity int) (*types.Transaction, error) {
claimVerification, err := encoder.prepareClaim(quantity)
if err != nil {
return nil, err
}
return setErc20AllowanceEncoder(
ctx,
encoder.helper,
signerAddress,
claimVerification.value,
claimVerification.currencyAddress,
)
}
// Get the data for the transaction required to claim NFTs from this contract.
//
// signerAddress: the address intended to sign the transaction
//
// destinationAddress: the address of the wallet to claim the NFTs to
//
// quantity: the number of NFTs to claim
//
// returns: the transaction data of the claim
//
// Example
//
// // Address of the wallet we expect to sign this message
// signerAddress := "0x..."
// // Address of the wallet we want to claim the NFTs to
// destinationAddress := "{{wallet_address}}"
// // Number of NFTs to claim
// quantity = 1
//
// tx, err := contract.Encoder.ClaimTo(context.Background(), signerAddress, destinationAddress, quantity)
//
// // Now you can get all the standard transaction data as needed
// fmt.Println(tx.Data()) // Ex: get the data field or the nonce field (others are available)
// fmt.Println(tx.Nonce())
func (encoder *NFTDropEncoder) ClaimTo(ctx context.Context, signerAddress string, destinationAddress string, quantity int) (*types.Transaction, error) {
claimVerification, err := encoder.prepareClaim(quantity)
if err != nil {
return nil, err
}
txOpts, err := encoder.helper.getUnsignedTxOptions(ctx, signerAddress)
if err != nil {
return nil, err
}
txOpts.Value = claimVerification.value
err = encoder.checkErc20Allowance(
signerAddress,
claimVerification.value,
claimVerification.currencyAddress,
)
if err != nil {
return nil, err
}
return encoder.abi.Claim(
txOpts,
common.HexToAddress(destinationAddress),
big.NewInt(int64(quantity)),
common.HexToAddress(claimVerification.currencyAddress),
claimVerification.price,
claimVerification.proofs,
big.NewInt(int64(claimVerification.maxQuantityPerTransaction)),
)
}
func (encoder *NFTDropEncoder) prepareClaim(quantity int) (*ClaimVerification, error) {
claimCondition, err := encoder.claimConditions.GetActive()
if err != nil {
return nil, err
}
claimVerification := &ClaimVerification{
proofs: [][32]byte{},
maxQuantityPerTransaction: 0,
price: claimCondition.Price,
currencyAddress: claimCondition.CurrencyAddress,
value: big.NewInt(0).Mul(big.NewInt(int64(quantity)), claimCondition.Price),
}
return claimVerification, nil
}
func (encoder *NFTDropEncoder) checkErc20Allowance(
signerAddress string,
value *big.Int,
currencyAddress string,
) error {
if !isNativeToken(currencyAddress) {
provider := encoder.helper.GetProvider()
erc20, err := abi.NewIERC20(common.HexToAddress(currencyAddress), provider)
if err != nil {
return err
}
owner := common.HexToAddress(signerAddress)
spender := encoder.helper.getAddress()
allowance, err := erc20.Allowance(&bind.CallOpts{}, owner, spender)
if err != nil {
return err
}
if allowance.Cmp(value) < 0 {
return fmt.Errorf(
"NFT Drop contract '%s' has insufficient allowance to spend ERC20 token '%s' on behalf of the user wallet '%s' "+
"Please approve the contract to spend tokens with the "+
"'marketplace.Encoder.ApproveClaimTo(signerAddress, destinationAddress, quantity)' method",
encoder.helper.getAddress().Hex(),
currencyAddress,
signerAddress,
)
}
}
return nil
}