Skip to content

[SDK] Add erc1155.transferBatch #2220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-frogs-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/sdk": patch
---

[SDK] Add erc1155.transferBatch
35 changes: 35 additions & 0 deletions packages/sdk/src/evm/core/classes/erc-1155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,41 @@ export class Erc1155<
},
);

/**
* Transfer multiple NFTs
*
* @remarks Transfer multiple NFTs from the connected wallet to another wallet.
*
* @example
* ```javascript
* // Address of the wallet you want to send the NFT to
* const toAddress = "{{wallet_address}}";
* // The token IDs of the NFTs you want to send
* const tokenIds = [0, 1, 2];
* // How many copies of the NFTs to transfer
* const amounts = [1, 2, 3];
* await contract.erc1155.transferBatch(toAddress, tokenIds, amounts);
* ```
*
* @twfeature ERC1155BatchTransferable
*/
transferBatch = /* @__PURE__ */ buildTransactionFunction(
async (
to: AddressOrEns,
tokenIds: BigNumberish[],
amounts: BigNumberish[],
fromAddress?: AddressOrEns,
data: BytesLike = [0],
) => {
const from = fromAddress ? await resolveAddress(fromAddress) : await this.contractWrapper.getSignerAddress();
return Transaction.fromContractWrapper({
contractWrapper: this.contractWrapper,
method: "safeBatchTransferFrom",
args: [from, await resolveAddress(to), tokenIds, amounts, data],
});
},
);

/**
* Transfer an NFT from a specific wallet
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ export class StandardErc1155<
},
);

/**
* Transfer multiple NFTs
*
* @remarks Transfer multiple NFTs from the connected wallet to another wallet.
*
* @example
* ```javascript
* // Address of the wallet you want to send the NFTs to
* const toAddress = "{{wallet_address}}";
* // Array of token IDs of the NFTs you want to send
* const tokenIds = ["0", "1", "2"];
* // Array of amounts of the NFTs you want to send
* const amounts = [1, 2, 3];
* await contract.transferBatch(toAddress, tokenIds, amounts);
* ```
*/
transferBatch = /* @__PURE__ */ buildTransactionFunction(
async (
to: AddressOrEns,
tokenIds: BigNumberish[],
amounts: BigNumberish[],
fromAddress?: AddressOrEns,
data: BytesLike = [0],
) => {
return this.erc1155.transferBatch.prepare(to, tokenIds, amounts, fromAddress, data);
},
);

/**
* Approve or remove operator as an operator for the caller. Operators can call transferFrom or safeTransferFrom for any token owned by the caller.
* @param operator - the operator's address
Expand Down
34 changes: 34 additions & 0 deletions packages/sdk/test/evm/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,40 @@ describe("Custom Contracts", async () => {
expect(balance.toString()).to.eq(initialBalance.add(1).toString());
});

it("should batch transfer erc1155", async () => {
sdk.updateSignerOrProvider(adminWallet);
const address = await sdk.deployer.deployEdition({
name: "Edition",
primary_sale_recipient: adminWallet.address,
});
const c = await sdk.getContract(address);

await c.erc1155.mintBatchTo(adminWallet.address, [
{
metadata: {
name: "Custom NFT",
},
supply: 100,
},
{
metadata: {
name: "Custom NFT",
},
supply: 100,
},
]);

const initialBalance = await c.erc1155.balanceOf(samWallet.address, 0);
const tx = await c.erc1155.transferBatch.prepare(
samWallet.address,
[0, 1],
[1, 1],
);
await tx.execute();
const balance = await c.erc1155.balanceOf(samWallet.address, 0);
expect(balance.toString()).to.eq(initialBalance.add(1).toString());
});

it("should detect feature: erc721 burnable", async () => {
const c = await sdk.getContract(nftContractAddress);
await c.erc721.mintTo(adminWallet.address, {
Expand Down