Skip to content

Commit

Permalink
remove internal instantiations of context.background (thirdweb-dev#106)…
Browse files Browse the repository at this point in the history
… (thirdweb-dev#107)

context should be passed in by caller

Co-authored-by: ricebin <ricebin@users.noreply.github.com>
  • Loading branch information
adam-maj and ricebin authored Dec 12, 2022
1 parent 133295e commit 84c213f
Show file tree
Hide file tree
Showing 22 changed files with 144 additions and 125 deletions.
5 changes: 3 additions & 2 deletions cmd/thirdweb/token_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"

"github.com/spf13/cobra"

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

Expand All @@ -32,7 +33,7 @@ var tokenGetCmd = &cobra.Command{
panic(err)
}

data, err := token.Get()
data, err := token.Get(context.Background())
if err != nil {
panic(err)
}
Expand All @@ -41,7 +42,7 @@ var tokenGetCmd = &cobra.Command{
log.Println("Symbols: ", data.Symbol)
log.Println("Decimals: ", data.Decimals)

balance, err := token.Balance()
balance, err := token.Balance(context.Background())
if err != nil {
panic(err)
}
Expand Down
15 changes: 8 additions & 7 deletions thirdweb/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func setErc20AllowanceEncoder(
}

func setErc20Allowance(
ctx context.Context,
contractToApprove *contractHelper,
value *big.Int,
currencyAddress string,
Expand All @@ -236,7 +237,7 @@ func setErc20Allowance(

owner := contractToApprove.GetSignerAddress()
spender := contractToApprove.getAddress()
allowance, err := erc20.Allowance(&bind.CallOpts{}, owner, spender)
allowance, err := erc20.Allowance(&bind.CallOpts{Context: ctx}, owner, spender)
if err != nil {
return err
}
Expand All @@ -253,7 +254,7 @@ func setErc20Allowance(
return err
}

_, err = contractToApprove.AwaitTx(tx.Hash())
_, err = contractToApprove.AwaitTx(ctx, tx.Hash())
if err != nil {
return err
}
Expand Down Expand Up @@ -300,15 +301,15 @@ func approveErc20Allowance(
return err
}

if _, err := contractToApprove.AwaitTx(tx.Hash()); err != nil {
if _, err := contractToApprove.AwaitTx(ctx, tx.Hash()); err != nil {
return err
}
}

return nil
}

func hasErc20Allowance(contractToApprove *contractHelper, currencyAddress string, value *big.Int) (bool, error) {
func hasErc20Allowance(ctx context.Context, contractToApprove *contractHelper, currencyAddress string, value *big.Int) (bool, error) {
if isNativeToken(currencyAddress) {
return true, nil
} else {
Expand All @@ -320,7 +321,7 @@ func hasErc20Allowance(contractToApprove *contractHelper, currencyAddress string

owner := contractToApprove.GetSignerAddress()
spender := contractToApprove.getAddress()
allowance, err := contractAbi.Allowance(&bind.CallOpts{}, owner, spender)
allowance, err := contractAbi.Allowance(&bind.CallOpts{Context: ctx}, owner, spender)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -760,7 +761,7 @@ func handleTokenApproval(
return err
}

_, err = helper.AwaitTx(tx.Hash())
_, err = helper.AwaitTx(ctx, tx.Hash())
if err != nil {
return err
}
Expand Down Expand Up @@ -788,7 +789,7 @@ func handleTokenApproval(
return err
}

_, err = helper.AwaitTx(tx.Hash())
_, err = helper.AwaitTx(ctx, tx.Hash())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion thirdweb/contract_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (deployer *ContractDeployer) deployContract(ctx context.Context, contractTy
return "", err
}

receipt, err := deployer.helper.AwaitTx(tx.Hash())
receipt, err := deployer.helper.AwaitTx(ctx, tx.Hash())
if err != nil {
return "", err
}
Expand Down
14 changes: 9 additions & 5 deletions thirdweb/contract_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (helper *contractHelper) getUnsignedTxOptions(ctx context.Context, signerAd
var tipCap, feeCap *big.Int

provider := helper.GetProvider()
block, err := provider.BlockByNumber(context.Background(), nil)
block, err := provider.BlockByNumber(ctx, nil)
if err == nil && block.BaseFee() != nil {
tipCap, _ = big.NewInt(0).SetString("2500000000", 10)
baseFee := big.NewInt(0).Mul(block.BaseFee(), big.NewInt(2))
Expand Down Expand Up @@ -81,26 +81,30 @@ func (helper *contractHelper) getRawTxOptions(ctx context.Context, noSend bool)
var tipCap, feeCap *big.Int

provider := helper.GetProvider()
block, err := provider.BlockByNumber(context.Background(), nil)
block, err := provider.BlockByNumber(ctx, nil)
if err == nil && block.BaseFee() != nil {
tipCap, _ = big.NewInt(0).SetString("2500000000", 10)
baseFee := big.NewInt(0).Mul(block.BaseFee(), big.NewInt(2))
feeCap = big.NewInt(0).Add(baseFee, tipCap)
}

signer, err := helper.getSigner(ctx)
if err != nil {
return nil, err
}
txOpts := &bind.TransactOpts{
Context: ctx,
NoSend: noSend,
From: helper.GetSignerAddress(),
Signer: helper.getSigner(),
Signer: signer,
GasTipCap: tipCap,
GasFeeCap: feeCap,
}

return txOpts, nil
}

func (helper *contractHelper) AwaitTx(hash common.Hash) (*types.Transaction, error) {
func (helper *contractHelper) AwaitTx(ctx context.Context, hash common.Hash) (*types.Transaction, error) {
provider := helper.GetProvider()
wait := txWaitTimeBetweenAttempts
maxAttempts := uint8(txMaxAttempts)
Expand All @@ -113,7 +117,7 @@ func (helper *contractHelper) AwaitTx(hash common.Hash) (*types.Transaction, err
return nil, syncError
}

if tx, isPending, err := provider.TransactionByHash(context.Background(), hash); err != nil {
if tx, isPending, err := provider.TransactionByHash(ctx, hash); err != nil {
syncError = err
log.Printf("Failed to get tx %v, err = %v\n", hash.String(), err)
attempts += 1
Expand Down
6 changes: 3 additions & 3 deletions thirdweb/edition.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (edition *Edition) MintTo(ctx context.Context, address string, metadataWith
return nil, err
}

return edition.Helper.AwaitTx((tx.Hash()))
return edition.Helper.AwaitTx(ctx, tx.Hash())
}

// Mint additionaly supply of a token to the connected wallet.
Expand Down Expand Up @@ -174,7 +174,7 @@ func (edition *Edition) MintAdditionalSupplyTo(ctx context.Context, to string, t
return nil, err
}

return edition.Helper.AwaitTx(tx.Hash())
return edition.Helper.AwaitTx(ctx, tx.Hash())
}

// Mint a batch of NFTs to the connected wallet.
Expand Down Expand Up @@ -260,5 +260,5 @@ func (edition *Edition) MintBatchTo(ctx context.Context, to string, metadatasWit
return nil, err
}

return edition.Helper.AwaitTx(tx.Hash())
return edition.Helper.AwaitTx(ctx, tx.Hash())
}
6 changes: 3 additions & 3 deletions thirdweb/edition_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func newEditionDrop(provider *ethclient.Client, address common.Address, privateK
//
// tx, err := contract.MintBatchTo(context.Background(), "{{wallet_address}}", metadatasWithSupply)
func (drop *EditionDrop) CreateBatch(ctx context.Context, metadatas []*NFTMetadataInput) (*types.Transaction, error) {
startNumber, err := drop.abi.NextTokenIdToMint(&bind.CallOpts{})
startNumber, err := drop.abi.NextTokenIdToMint(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func (drop *EditionDrop) CreateBatch(ctx context.Context, metadatas []*NFTMetada
return nil, err
}

return drop.Helper.AwaitTx(tx.Hash())
return drop.Helper.AwaitTx(ctx, tx.Hash())
}

// Claim NFTs from this contract to the connect wallet.
Expand Down Expand Up @@ -220,7 +220,7 @@ func (drop *EditionDrop) ClaimTo(ctx context.Context, destinationAddress string,
return nil, err
}

return drop.Helper.AwaitTx(tx.Hash())
return drop.Helper.AwaitTx(ctx, tx.Hash())
}

func (drop *EditionDrop) prepareClaim(ctx context.Context, tokenId int, quantity int) (*ClaimVerification, error) {
Expand Down
6 changes: 3 additions & 3 deletions thirdweb/erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (erc1155 *ERC1155) Transfer(ctx context.Context, to string, tokenId int, am
); err != nil {
return nil, err
} else {
return erc1155.helper.AwaitTx(tx.Hash())
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
}

Expand Down Expand Up @@ -274,7 +274,7 @@ func (erc1155 *ERC1155) Burn(ctx context.Context, tokenId int, amount int) (*typ
); err != nil {
return nil, err
} else {
return erc1155.helper.AwaitTx(tx.Hash())
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
}

Expand All @@ -299,7 +299,7 @@ func (erc1155 *ERC1155) SetApprovalForAll(ctx context.Context, operator string,
); err != nil {
return nil, err
} else {
return erc1155.helper.AwaitTx(tx.Hash())
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
}

Expand Down
7 changes: 4 additions & 3 deletions thirdweb/erc1155_signature_minting.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (signature *ERC1155SignatureMinting) Mint(ctx context.Context, signedPayloa
return nil, err
}
if err := setErc20Allowance(
ctx,
signature.helper,
big.NewInt(message.PricePerToken.Int64()).Mul(message.PricePerToken, message.Quantity),
message.Currency.String(),
Expand All @@ -74,7 +75,7 @@ func (signature *ERC1155SignatureMinting) Mint(ctx context.Context, signedPayloa
return nil, err
}

return signature.helper.AwaitTx(tx.Hash())
return signature.helper.AwaitTx(ctx, tx.Hash())
}

// Mint a batch of token with the data in given payload.
Expand Down Expand Up @@ -126,7 +127,7 @@ func (signature *ERC1155SignatureMinting) MintBatch(ctx context.Context, signedP
return nil, err
}

return signature.helper.AwaitTx(tx.Hash())
return signature.helper.AwaitTx(ctx, tx.Hash())
}

// Verify that a signed payload is valid
Expand Down Expand Up @@ -352,7 +353,7 @@ func (signature *ERC1155SignatureMinting) GenerateBatchFromTokenIds(ctx context.
return nil, err
}

chainId, err := signature.helper.GetChainID()
chainId, err := signature.helper.GetChainID(ctx)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 84c213f

Please sign in to comment.