diff --git a/cmd/thirdweb/nft_drop_commands.go b/cmd/thirdweb/nft_drop_commands.go index 48beed0..0f9a2b0 100644 --- a/cmd/thirdweb/nft_drop_commands.go +++ b/cmd/thirdweb/nft_drop_commands.go @@ -8,6 +8,7 @@ import ( "os" "github.com/spf13/cobra" + "github.com/thirdweb-dev/go-sdk/v2/thirdweb" ) @@ -83,7 +84,7 @@ var nftDropGetActiveCmd = &cobra.Command{ panic(err) } - active, err := nftDrop.ClaimConditions.GetActive() + active, err := nftDrop.ClaimConditions.GetActive(context.Background()) if err != nil { panic(err) } @@ -95,7 +96,7 @@ var nftDropGetActiveCmd = &cobra.Command{ fmt.Println("Price:", active.Price) fmt.Println("Wait In Seconds", active.WaitInSeconds) - all, err := nftDrop.ClaimConditions.GetAll() + all, err := nftDrop.ClaimConditions.GetAll(context.Background()) if err != nil { panic(err) } @@ -132,7 +133,7 @@ var nftDropClaimCmd = &cobra.Command{ emptyDrop, err := emptySdk.GetNFTDrop(nftDropContractAddress) address := thirdwebSDK.GetSignerAddress().String() - claimArgs, err := emptyDrop.GetClaimArguments(context.Background(), address, 1) + claimArgs, err := emptyDrop.GetClaimArguments(context.Background(), address, 1) if err != nil { panic(err) } diff --git a/thirdweb/common.go b/thirdweb/common.go index d24839d..ecef6ca 100644 --- a/thirdweb/common.go +++ b/thirdweb/common.go @@ -82,7 +82,7 @@ func convertToReadableQuantity(bn *big.Int, decimals int) string { } func convertQuantityToBigNumber(quantity string, decimals int) (*big.Int, error) { - if quantity == "unlimited" { + if quantity == "unlimited" { MaxUint256 := new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 256), common.Big1) return MaxUint256, nil } else { @@ -374,9 +374,9 @@ func prepareClaim( } priceInProof, err = normalizePriceValue( - ctx, - contractHelper.GetProvider(), - flt, + ctx, + contractHelper.GetProvider(), + flt, snapshotEntry.CurrencyAddress, ) } @@ -406,16 +406,16 @@ func prepareClaim( if pricePerToken.Cmp(big.NewInt(0)) > 0 { if isNativeToken(currencyAddress) { value = big.NewInt(0).Mul(big.NewInt(int64(quantity)), pricePerToken) - } + } } claimVerification := &ClaimVerification{ - Value: value, - Proofs: proofs, - MaxClaimable: maxClaimable, - Price: pricePerToken, - CurrencyAddress: currencyAddress, - PriceInProof: priceInProof, + Value: value, + Proofs: proofs, + MaxClaimable: maxClaimable, + Price: pricePerToken, + CurrencyAddress: currencyAddress, + PriceInProof: priceInProof, CurrencyAddressInProof: currencyAddressInProof, } @@ -443,7 +443,7 @@ func fetchSnapshotEntryForAddress( if err != nil { return nil, err } - + metadata := &ShardedMerkleTreeInfo{} if err := json.Unmarshal(body, &metadata); err != nil { return nil, err @@ -461,9 +461,7 @@ func fetchSnapshotEntryForAddress( func transformResultToClaimCondition( ctx context.Context, pm *abi.IClaimConditionClaimCondition, - merkleMetadata interface{}, provider *ethclient.Client, - storage storage, ) (*ClaimConditionOutput, error) { currencyValue, err := fetchCurrencyValue(ctx, provider, pm.Currency.String(), pm.PricePerToken) if err != nil { @@ -473,16 +471,16 @@ func transformResultToClaimCondition( startTime := time.Unix(pm.StartTimestamp.Int64(), 0) return &ClaimConditionOutput{ - StartTime: startTime, - MaxClaimableSupply: pm.MaxClaimableSupply, - MaxClaimablePerWallet: pm.QuantityLimitPerWallet, - CurrentMintSupply: pm.SupplyClaimed, - AvailableSupply: big.NewInt(0).Sub(pm.MaxClaimableSupply, pm.SupplyClaimed), - WaitInSeconds: big.NewInt(0), - Price: pm.PricePerToken, - CurrencyAddress: pm.Currency.String(), - CurrencyMetadata: currencyValue, - MerkleRootHash: pm.MerkleRoot, + StartTime: startTime, + MaxClaimableSupply: pm.MaxClaimableSupply, + MaxClaimablePerWallet: pm.QuantityLimitPerWallet, + CurrentMintSupply: pm.SupplyClaimed, + AvailableSupply: big.NewInt(0).Sub(pm.MaxClaimableSupply, pm.SupplyClaimed), + WaitInSeconds: big.NewInt(0), + Price: pm.PricePerToken, + CurrencyAddress: pm.Currency.String(), + CurrencyMetadata: currencyValue, + MerkleRootHash: pm.MerkleRoot, }, nil } diff --git a/thirdweb/edition_drop_claim_conditions.go b/thirdweb/edition_drop_claim_conditions.go index 25e5a97..ad22422 100644 --- a/thirdweb/edition_drop_claim_conditions.go +++ b/thirdweb/edition_drop_claim_conditions.go @@ -8,6 +8,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient" + "github.com/thirdweb-dev/go-sdk/v2/abi" ) @@ -51,17 +52,12 @@ func newEditionDropClaimConditions(address common.Address, provider *ethclient.C // fmt.Println("Price:", condition.Price) // fmt.Println("Wait In Seconds", condition.WaitInSeconds) func (claim *EditionDropClaimConditions) GetActive(ctx context.Context, tokenId int) (*ClaimConditionOutput, error) { - id, err := claim.abi.GetActiveClaimConditionId(&bind.CallOpts{}, big.NewInt(int64(tokenId))) - if err != nil { - return nil, err - } - - mc, err := claim.abi.GetClaimConditionById(&bind.CallOpts{}, big.NewInt(int64(tokenId)), id) + id, err := claim.abi.GetActiveClaimConditionId(&bind.CallOpts{Context: ctx}, big.NewInt(int64(tokenId))) if err != nil { return nil, err } - merkle, err := claim.GetMerkleMetadata() + mc, err := claim.abi.GetClaimConditionById(&bind.CallOpts{Context: ctx}, big.NewInt(int64(tokenId)), id) if err != nil { return nil, err } @@ -70,9 +66,7 @@ func (claim *EditionDropClaimConditions) GetActive(ctx context.Context, tokenId claimCondition, err := transformResultToClaimCondition( ctx, &mc, - merkle, provider, - claim.storage, ) if err != nil { return nil, err @@ -112,12 +106,7 @@ func (claim *EditionDropClaimConditions) GetAll(ctx context.Context, tokenId int conditions := []*ClaimConditionOutput{} for i := startId; i < count; i++ { - mc, err := claim.abi.GetClaimConditionById(&bind.CallOpts{}, big.NewInt(int64(tokenId)), big.NewInt(i)) - if err != nil { - return nil, err - } - - merkle, err := claim.GetMerkleMetadata() + mc, err := claim.abi.GetClaimConditionById(&bind.CallOpts{Context: ctx}, big.NewInt(int64(tokenId)), big.NewInt(i)) if err != nil { return nil, err } @@ -125,9 +114,7 @@ func (claim *EditionDropClaimConditions) GetAll(ctx context.Context, tokenId int claimCondition, err := transformResultToClaimCondition( ctx, &mc, - merkle, provider, - claim.storage, ) if err != nil { return nil, err @@ -140,22 +127,22 @@ func (claim *EditionDropClaimConditions) GetAll(ctx context.Context, tokenId int } func (claim *EditionDropClaimConditions) GetMerkleMetadata() (*map[string]string, error) { - uri, err := claim.abi.InternalContractURI(&bind.CallOpts{}); + uri, err := claim.abi.InternalContractURI(&bind.CallOpts{}) if err != nil { return nil, err } - body, err := claim.storage.Get(uri); + body, err := claim.storage.Get(uri) if err != nil { return nil, err } var rawMetadata struct { Merkle map[string]string `json:"merkle"` - }; + } if err := json.Unmarshal(body, &rawMetadata); err != nil { return nil, err } - return &rawMetadata.Merkle, nil; + return &rawMetadata.Merkle, nil } diff --git a/thirdweb/nft_drop.go b/thirdweb/nft_drop.go index e1b1a31..5cee949 100644 --- a/thirdweb/nft_drop.go +++ b/thirdweb/nft_drop.go @@ -30,8 +30,8 @@ import ( // contract, err := sdk.GetNFTDrop("{{contract_address}}") type NFTDrop struct { *ERC721 - Abi *abi.DropERC721 - Helper *contractHelper + Abi *abi.DropERC721 + Helper *contractHelper ClaimConditions *NFTDropClaimConditions Encoder *NFTDropEncoder Events *ContractEvents @@ -119,7 +119,7 @@ func (nft *NFTDrop) GetOwnedTokenIDs(ctx context.Context, address string) ([]*bi owner, err := nft.abi.OwnerOf(&bind.CallOpts{}, big.NewInt(int64(i))) if err != nil { return nil, err - } + } if strings.ToLower(owner.String()) == strings.ToLower(address) { tokenIds = append(tokenIds, big.NewInt(int64(i))) @@ -129,7 +129,6 @@ func (nft *NFTDrop) GetOwnedTokenIDs(ctx context.Context, address string) ([]*bi return tokenIds, nil } - // Get a list of all the NFTs that have been claimed from this contract. // // returns: a list of the metadatas of the claimed NFTs @@ -232,7 +231,7 @@ func (drop *NFTDrop) GetClaimInfo(ctx context.Context, address string) (*ClaimIn return nil, err } - active, err := drop.ClaimConditions.GetActive() + active, err := drop.ClaimConditions.GetActive(ctx) if err != nil { return nil, err } @@ -256,22 +255,22 @@ func (drop *NFTDrop) GetClaimInfo(ctx context.Context, address string) (*ClaimIn } return &ClaimInfo{ - PricePerToken: claimVerification.Price, + PricePerToken: claimVerification.Price, RemainingClaimable: remainingClaimable, - CurrencyAddress: common.HexToAddress(claimVerification.CurrencyAddress), + CurrencyAddress: common.HexToAddress(claimVerification.CurrencyAddress), }, nil } func (drop *NFTDrop) GetClaimIneligibilityReasons(ctx context.Context, quantity int, addressToCheck string) ([]ClaimEligibility, error) { reasons := []ClaimEligibility{} - active, err := drop.ClaimConditions.GetActive() + active, err := drop.ClaimConditions.GetActive(ctx) if err != nil { if strings.Contains(err.Error(), "!CONDITION") || strings.Contains(err.Error(), "no active mint condition") { reasons = append(reasons, NoClaimConditionSet) return reasons, nil - } - + } + return reasons, err } @@ -310,30 +309,29 @@ func (drop *NFTDrop) GetClaimIneligibilityReasons(ctx context.Context, quantity if err != nil { return reasons, err } - - if ( - (active.MaxClaimablePerWallet.Cmp(big.NewInt(0)) == 0 && - claimVerification.MaxClaimable.Cmp(MaxUint256) == 0) || - claimVerification.MaxClaimable.Cmp(big.NewInt(0)) == 0) { + + if (active.MaxClaimablePerWallet.Cmp(big.NewInt(0)) == 0 && + claimVerification.MaxClaimable.Cmp(MaxUint256) == 0) || + claimVerification.MaxClaimable.Cmp(big.NewInt(0)) == 0 { reasons = append(reasons, AddressNotAllowed) return reasons, nil } else if totalClaimedInPhase.Add(totalClaimedInPhase, big.NewInt(int64(quantity))).Cmp(claimVerification.MaxClaimable) > 0 { reasons = append(reasons, ExceedsMaxClaimable) return reasons, nil } - + activeConditionIndex, err := drop.Abi.GetActiveClaimConditionId(&bind.CallOpts{}) if err != nil { return reasons, err } - + proof := abi.IDropAllowlistProof{ - Proof: claimVerification.Proofs, + Proof: claimVerification.Proofs, QuantityLimitPerWallet: claimVerification.MaxClaimable, - PricePerToken: claimVerification.PriceInProof, - Currency: common.HexToAddress(claimVerification.CurrencyAddressInProof), + PricePerToken: claimVerification.PriceInProof, + Currency: common.HexToAddress(claimVerification.CurrencyAddressInProof), } - + isValid, err := drop.Abi.VerifyClaim( &bind.CallOpts{}, activeConditionIndex, @@ -343,7 +341,7 @@ func (drop *NFTDrop) GetClaimIneligibilityReasons(ctx context.Context, quantity claimVerification.Price, proof, ) - + if err != nil || !isValid { reasons = append(reasons, AddressNotAllowed) return reasons, nil @@ -391,7 +389,7 @@ func (drop *NFTDrop) GetClaimIneligibilityReasons(ctx context.Context, quantity return reasons, nil } } - + return reasons, nil } @@ -489,9 +487,9 @@ func (drop *NFTDrop) Claim(ctx context.Context, quantity int) (*types.Transactio // quantity = 1 // // tx, err := contract.ClaimTo(context.Background(), address, quantity) -func (drop *NFTDrop) ClaimTo(ctx context.Context, destinationAddress string, quantity int) (*types.Transaction, error) { +func (drop *NFTDrop) ClaimTo(ctx context.Context, destinationAddress string, quantity int) (*types.Transaction, error) { addressToClaim := drop.helper.GetSignerAddress().Hex() - + claimVerification, err := drop.prepareClaim(ctx, addressToClaim, quantity, true) if err != nil { return nil, err @@ -505,10 +503,10 @@ func (drop *NFTDrop) ClaimTo(ctx context.Context, destinationAddress string, qua txOpts.Value = claimVerification.Value proof := abi.IDropAllowlistProof{ - Proof: claimVerification.Proofs, + Proof: claimVerification.Proofs, QuantityLimitPerWallet: claimVerification.MaxClaimable, - PricePerToken: claimVerification.PriceInProof, - Currency: common.HexToAddress(claimVerification.CurrencyAddressInProof), + PricePerToken: claimVerification.PriceInProof, + Currency: common.HexToAddress(claimVerification.CurrencyAddressInProof), } tx, err := drop.Abi.Claim( @@ -541,25 +539,25 @@ func (drop *NFTDrop) GetClaimArguments( } proof := abi.IDropAllowlistProof{ - Proof: claimVerification.Proofs, + Proof: claimVerification.Proofs, QuantityLimitPerWallet: claimVerification.MaxClaimable, - PricePerToken: claimVerification.PriceInProof, - Currency: common.HexToAddress(claimVerification.CurrencyAddressInProof), - } - - return &ClaimArguments{ - claimVerification.Value, - common.HexToAddress(destinationAddress), - big.NewInt(int64(quantity)), - common.HexToAddress(claimVerification.CurrencyAddress), - claimVerification.Price, - proof, - []byte{}, + PricePerToken: claimVerification.PriceInProof, + Currency: common.HexToAddress(claimVerification.CurrencyAddressInProof), + } + + return &ClaimArguments{ + claimVerification.Value, + common.HexToAddress(destinationAddress), + big.NewInt(int64(quantity)), + common.HexToAddress(claimVerification.CurrencyAddress), + claimVerification.Price, + proof, + []byte{}, }, nil } func (drop *NFTDrop) prepareClaim(ctx context.Context, addressToClaim string, quantity int, handleApproval bool) (*ClaimVerification, error) { - active, err := drop.ClaimConditions.GetActive() + active, err := drop.ClaimConditions.GetActive(ctx) if err != nil { return nil, err } @@ -592,7 +590,7 @@ func (drop *NFTDrop) prepareClaim(ctx context.Context, addressToClaim string, qu } else { pricePerToken = claimVerification.Price } - + var currencyAddress string if claimVerification.CurrencyAddress != zeroAddress { currencyAddress = claimVerification.CurrencyAddress diff --git a/thirdweb/nft_drop_claim_conditions.go b/thirdweb/nft_drop_claim_conditions.go index e7e301c..d9e484c 100644 --- a/thirdweb/nft_drop_claim_conditions.go +++ b/thirdweb/nft_drop_claim_conditions.go @@ -10,6 +10,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient" + "github.com/thirdweb-dev/go-sdk/v2/abi" ) @@ -49,29 +50,22 @@ func newNFTDropClaimConditions(address common.Address, provider *ethclient.Clien // fmt.Println("Quantity Limit:", condition.QuantityLimitPerTransaction) // fmt.Println("Price:", condition.Price) // fmt.Println("Wait In Seconds", condition.WaitInSeconds) -func (claim *NFTDropClaimConditions) GetActive() (*ClaimConditionOutput, error) { - id, err := claim.abi.GetActiveClaimConditionId(&bind.CallOpts{}) - if err != nil { - return nil, err - } - - active, err := claim.abi.GetClaimConditionById(&bind.CallOpts{}, id) +func (claim *NFTDropClaimConditions) GetActive(ctx context.Context) (*ClaimConditionOutput, error) { + id, err := claim.abi.GetActiveClaimConditionId(&bind.CallOpts{Context: ctx}) if err != nil { return nil, err } - merkle, err := claim.getMerkleMetadata() + active, err := claim.abi.GetClaimConditionById(&bind.CallOpts{Context: ctx}, id) if err != nil { return nil, err } provider := claim.helper.GetProvider() claimCondition, err := transformResultToClaimCondition( - context.Background(), + ctx, &active, - merkle, provider, - claim.storage, ) if err != nil { return nil, err @@ -80,24 +74,18 @@ func (claim *NFTDropClaimConditions) GetActive() (*ClaimConditionOutput, error) return claimCondition, nil } -func (claim *NFTDropClaimConditions) Get(claimConditionId int) (*ClaimConditionOutput, error) { - condition, err := claim.abi.GetClaimConditionById(&bind.CallOpts{}, big.NewInt(int64(claimConditionId))) +func (claim *NFTDropClaimConditions) Get(ctx context.Context, claimConditionId int) (*ClaimConditionOutput, error) { + condition, err := claim.abi.GetClaimConditionById(&bind.CallOpts{Context: ctx}, big.NewInt(int64(claimConditionId))) if err != nil { return nil, err } provider := claim.helper.GetProvider() - merkle, err := claim.getMerkleMetadata() - if err != nil { - return nil, err - } claimCondition, err := transformResultToClaimCondition( - context.Background(), + ctx, &condition, - merkle, provider, - claim.storage, ) if err != nil { return nil, err @@ -122,8 +110,8 @@ func (claim *NFTDropClaimConditions) Get(claimConditionId int) (*ClaimConditionO // fmt.Println("Quantity Limit:", condition.QuantityLimitPerTransaction) // fmt.Println("Price:", condition.Price) // fmt.Println("Wait In Seconds", condition.WaitInSeconds) -func (claim *NFTDropClaimConditions) GetAll() ([]*ClaimConditionOutput, error) { - condition, err := claim.abi.ClaimCondition(&bind.CallOpts{}) +func (claim *NFTDropClaimConditions) GetAll(ctx context.Context) ([]*ClaimConditionOutput, error) { + condition, err := claim.abi.ClaimCondition(&bind.CallOpts{Context: ctx}) if err != nil { return nil, err } @@ -134,22 +122,15 @@ func (claim *NFTDropClaimConditions) GetAll() ([]*ClaimConditionOutput, error) { conditions := []*ClaimConditionOutput{} for i := startId; i < count; i++ { - mc, err := claim.abi.GetClaimConditionById(&bind.CallOpts{}, big.NewInt(i)) + mc, err := claim.abi.GetClaimConditionById(&bind.CallOpts{Context: ctx}, big.NewInt(i)) if err != nil { return nil, err } - merkle, err := claim.getMerkleMetadata() - if err != nil { - return nil, err - } - claimCondition, err := transformResultToClaimCondition( - context.Background(), + ctx, &mc, - merkle, provider, - claim.storage, ) if err != nil { return nil, err @@ -162,41 +143,41 @@ func (claim *NFTDropClaimConditions) GetAll() ([]*ClaimConditionOutput, error) { } func (claim *NFTDropClaimConditions) getMerkleMetadata() (*map[string]string, error) { - uri, err := claim.abi.InternalContractURI(&bind.CallOpts{}); + uri, err := claim.abi.InternalContractURI(&bind.CallOpts{}) if err != nil { return nil, err } - body, err := claim.storage.Get(uri); + body, err := claim.storage.Get(uri) if err != nil { return nil, err } var rawMetadata struct { Merkle map[string]string `json:"merkle"` - }; + } if err := json.Unmarshal(body, &rawMetadata); err != nil { return nil, err } - return &rawMetadata.Merkle, nil; + return &rawMetadata.Merkle, nil } func (claim *NFTDropClaimConditions) GetClaimerProofs( ctx context.Context, claimerAddress string, ) (*SnapshotEntryWithProof, error) { - claimCondition, err := claim.GetActive() + claimCondition, err := claim.GetActive(ctx) if err != nil { return nil, err } - + if !strings.HasPrefix(hex.EncodeToString(claimCondition.MerkleRootHash[:]), zeroAddress) { merkleMetadata, err := claim.getMerkleMetadata() if err != nil { return nil, err } - + return fetchSnapshotEntryForAddress( ctx, common.HexToAddress(claimerAddress), diff --git a/thirdweb/nft_drop_encoder.go b/thirdweb/nft_drop_encoder.go index 3b499de..32434c6 100644 --- a/thirdweb/nft_drop_encoder.go +++ b/thirdweb/nft_drop_encoder.go @@ -8,6 +8,7 @@ import ( "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/v2/abi" ) @@ -123,11 +124,11 @@ func (encoder *NFTDropEncoder) ApproveClaimTo(ctx context.Context, signerAddress // 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) { - active, err := encoder.claimConditions.GetActive() + active, err := encoder.claimConditions.GetActive(ctx) if err != nil { return nil, err } - + claimVerification, err := encoder.prepareClaim(ctx, quantity) if err != nil { return nil, err @@ -167,10 +168,10 @@ func (encoder *NFTDropEncoder) ClaimTo(ctx context.Context, signerAddress string } proof := abi.IDropAllowlistProof{ - Proof: claimVerification.Proofs, + Proof: claimVerification.Proofs, QuantityLimitPerWallet: claimVerification.MaxClaimable, - PricePerToken: claimVerification.Price, - Currency: common.HexToAddress(claimVerification.CurrencyAddress), + PricePerToken: claimVerification.Price, + Currency: common.HexToAddress(claimVerification.CurrencyAddress), } return encoder.abi.Claim( @@ -185,7 +186,7 @@ func (encoder *NFTDropEncoder) ClaimTo(ctx context.Context, signerAddress string } func (encoder *NFTDropEncoder) prepareClaim(ctx context.Context, quantity int) (*ClaimVerification, error) { - active, err := encoder.claimConditions.GetActive() + active, err := encoder.claimConditions.GetActive(ctx) if err != nil { return nil, err }