From 526c9cbad42a555b9a65b8e71ee6ee509c35e128 Mon Sep 17 00:00:00 2001 From: Adam Majmudar <64697628+adam-maj@users.noreply.github.com> Date: Mon, 7 Aug 2023 12:43:54 -0700 Subject: [PATCH] Add erc721.PrepareClaimTo (#139) * Expose PrepareClaim method * Add erc721.PrepareClaimTo * Update to make prepareClaim private * Rerun tests * Update test script * Add echo * Update * Update github actions to use secret key --- .github/workflows/run-tests.yml | 4 +-- Makefile | 4 ++- thirdweb/erc1155.go | 4 +-- thirdweb/erc721.go | 51 ++++++++++++++++++++++----------- thirdweb/types.go | 10 +++++++ 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index ac2b89d..64d23fd 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,6 +1,7 @@ name: Build & Test env: SDK_ALCHEMY_KEY: ${{ secrets.SDK_ALCHEMY_KEY }} + THIRDWEB_SECRET_KEY: ${{ secrets.THIRDWEB_SECRET_KEY }} on: [push, pull_request] jobs: @@ -19,7 +20,7 @@ jobs: uses: actions/setup-go@v2 with: go-version: ${{ matrix.go-version }} - + - name: Use Node.js uses: actions/setup-node@v1 with: @@ -30,4 +31,3 @@ jobs: - name: Test run: yarn add hardhat && make test - diff --git a/Makefile b/Makefile index 0acc215..c1814e0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ .PHONY: abi docs publish local-test +export THIRDWEB_SECRET_KEY + SHELL := /bin/bash abi: @@ -127,7 +129,7 @@ stop-docker: test: FORCE docker build . -t hardhat-mainnet-fork - docker start hardhat-node || docker run --name hardhat-node -d -p 8545:8545 -e SDK_ALCHEMY_KEY=${SDK_ALCHEMY_KEY} hardhat-mainnet-fork + docker start hardhat-node || docker run --name hardhat-node -d -p 8545:8545 -e "THIRDWEB_SECRET_KEY=${THIRDWEB_SECRET_KEY}" -e "SDK_ALCHEMY_KEY=${SDK_ALCHEMY_KEY}" hardhat-mainnet-fork sudo bash ./scripts/test/await-hardhat.sh go clean -testcache go test -v ./thirdweb diff --git a/thirdweb/erc1155.go b/thirdweb/erc1155.go index ad5090a..3ac0a5c 100644 --- a/thirdweb/erc1155.go +++ b/thirdweb/erc1155.go @@ -746,7 +746,7 @@ func (erc1155 *ERC1155) Claim(ctx context.Context, tokenId int, quantity int) (* // // tx, err := contract.ClaimTo(context.Background(), address, tokenId, quantity) func (erc1155 *ERC1155) ClaimTo(ctx context.Context, destinationAddress string, tokenId int, quantity int) (*types.Transaction, error) { - claimVerification, err := erc1155.prepareClaim(ctx, tokenId, quantity) + claimVerification, err := erc1155.PrepareClaim(ctx, tokenId, quantity) if err != nil { return nil, err } @@ -787,7 +787,7 @@ func (erc1155 *ERC1155) ClaimTo(ctx context.Context, destinationAddress string, return erc1155.helper.AwaitTx(ctx, tx.Hash()) } -func (erc1155 *ERC1155) prepareClaim(ctx context.Context, tokenId int, quantity int) (*ClaimVerification, error) { +func (erc1155 *ERC1155) PrepareClaim(ctx context.Context, tokenId int, quantity int) (*ClaimVerification, error) { addressToClaim := erc1155.helper.GetSignerAddress().Hex() claimCondition, err := erc1155.ClaimConditions.GetActive(ctx, tokenId) if err != nil { diff --git a/thirdweb/erc721.go b/thirdweb/erc721.go index bdf09c6..ca23fe4 100644 --- a/thirdweb/erc721.go +++ b/thirdweb/erc721.go @@ -867,9 +867,7 @@ func (erc721 *ERC721) Claim(ctx context.Context, quantity int) (*types.Transacti // // tx, err := contract.ERC721.ClaimTo(context.Background(), address, quantity) func (erc721 *ERC721) ClaimTo(ctx context.Context, destinationAddress string, quantity int) (*types.Transaction, error) { - addressToClaim := erc721.helper.GetSignerAddress().Hex() - - claimVerification, err := erc721.prepareClaim(ctx, addressToClaim, quantity, true) + preparedClaimTo, err := erc721.PrepareClaimTo(ctx, destinationAddress, quantity) if err != nil { return nil, err } @@ -879,23 +877,16 @@ func (erc721 *ERC721) ClaimTo(ctx context.Context, destinationAddress string, qu return nil, err } - txOpts.Value = claimVerification.Value - - proof := abi.IDropAllowlistProof{ - Proof: claimVerification.Proofs, - QuantityLimitPerWallet: claimVerification.MaxClaimable, - PricePerToken: claimVerification.PriceInProof, - Currency: common.HexToAddress(claimVerification.CurrencyAddressInProof), - } + txOpts.Value = preparedClaimTo.Value tx, err := erc721.drop.Claim( txOpts, - common.HexToAddress(destinationAddress), - big.NewInt(int64(quantity)), - common.HexToAddress(claimVerification.CurrencyAddress), - claimVerification.Price, - proof, - []byte{}, + preparedClaimTo.Receiver, + preparedClaimTo.Quantity, + preparedClaimTo.Currency, + preparedClaimTo.PricePerToken, + preparedClaimTo.AllowlistProof, + preparedClaimTo.Data, ) if err != nil { return nil, err @@ -904,6 +895,32 @@ func (erc721 *ERC721) ClaimTo(ctx context.Context, destinationAddress string, qu return erc721.helper.AwaitTx(ctx, tx.Hash()) } +func (erc721 *ERC721) PrepareClaimTo(ctx context.Context, destinationAddress string, quantity int) (*PreparedClaimTo, error) { + addressToClaim := erc721.helper.GetSignerAddress().Hex() + + claimVerification, err := erc721.prepareClaim(ctx, addressToClaim, quantity, true) + if err != nil { + return nil, err + } + + proof := abi.IDropAllowlistProof{ + Proof: claimVerification.Proofs, + QuantityLimitPerWallet: claimVerification.MaxClaimable, + PricePerToken: claimVerification.PriceInProof, + Currency: common.HexToAddress(claimVerification.CurrencyAddressInProof), + } + + return &PreparedClaimTo{ + Value: claimVerification.Value, + Receiver: common.HexToAddress(destinationAddress), + Quantity: big.NewInt(int64(quantity)), + Currency: common.HexToAddress(claimVerification.CurrencyAddress), + PricePerToken: claimVerification.Price, + AllowlistProof: proof, + Data: []byte{}, + }, nil +} + func (erc721 *ERC721) GetClaimArguments( ctx context.Context, destinationAddress string, diff --git a/thirdweb/types.go b/thirdweb/types.go index 58f6520..359e56e 100644 --- a/thirdweb/types.go +++ b/thirdweb/types.go @@ -67,6 +67,16 @@ type EditionMetadataInput struct { Supply int } +type PreparedClaimTo struct { + Receiver common.Address + Quantity *big.Int + Currency common.Address + PricePerToken *big.Int + AllowlistProof abi.IDropAllowlistProof + Data []byte + Value *big.Int +} + type ClaimVerification struct { Value *big.Int Proofs [][32]byte