Skip to content

Commit

Permalink
pass context in a few more places (thirdweb-dev#96) (thirdweb-dev#98)
Browse files Browse the repository at this point in the history
* pass context in more places
remove a few unnecessary calls to GetMerkleMetadata()

* pass context in more places

Co-authored-by: ricebin <ricebin@users.noreply.github.com>
  • Loading branch information
adam-maj and ricebin authored Dec 6, 2022
1 parent 86645b1 commit 3589803
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 135 deletions.
7 changes: 4 additions & 3 deletions cmd/thirdweb/nft_drop_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"

"github.com/spf13/cobra"

"github.com/thirdweb-dev/go-sdk/v2/thirdweb"
)

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
46 changes: 22 additions & 24 deletions thirdweb/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -374,9 +374,9 @@ func prepareClaim(
}

priceInProof, err = normalizePriceValue(
ctx,
contractHelper.GetProvider(),
flt,
ctx,
contractHelper.GetProvider(),
flt,
snapshotEntry.CurrencyAddress,
)
}
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
29 changes: 8 additions & 21 deletions thirdweb/edition_drop_claim_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -112,22 +106,15 @@ 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
}

claimCondition, err := transformResultToClaimCondition(
ctx,
&mc,
merkle,
provider,
claim.storage,
)
if err != nil {
return nil, err
Expand All @@ -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
}
Loading

0 comments on commit 3589803

Please sign in to comment.