Skip to content

Commit 19a4860

Browse files
committed
update
1 parent 33c23e7 commit 19a4860

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

.changeset/silent-hats-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Add erc20Value to buyFromListing transaction

packages/thirdweb/src/extensions/marketplace/direct-listings/direct-listings.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {
99
} from "../../../../test/src/test-wallets.js";
1010
import { getContract } from "../../../contract/contract.js";
1111
import { parseEventLogs } from "../../../event/actions/parse-logs.js";
12+
import { getApprovalForTransaction } from "../../../extensions/erc20/write/getApprovalForTransaction.js";
1213
import { setApprovalForAll as setApprovalForAll721 } from "../../../extensions/erc721/__generated__/IERC721A/write/setApprovalForAll.js";
1314
import { mintTo as mintToErc1155 } from "../../../extensions/erc1155/write/mintTo.js";
15+
import { deployERC20Contract } from "../../../extensions/prebuilts/deploy-erc20.js";
1416
import { sendAndConfirmTransaction } from "../../../transaction/actions/send-and-confirm-transaction.js";
1517
import { balanceOf as balanceOfErc721 } from "../../erc721/__generated__/IERC721A/read/balanceOf.js";
1618
import { mintTo as mintToErc721 } from "../../erc721/write/mintTo.js";
@@ -384,5 +386,59 @@ describe.runIf(process.env.TW_SECRET_KEY)("Marketplace Direct Listings", () => {
384386
expect(nft1155BalanceOfAccountAAfterPurchase).toBe(1n);
385387
// expect the seller to have one less 1155 token
386388
expect(nft1155BalanceOfAccountCAfterPurchase).toBe(99n);
389+
390+
/**
391+
* buyFromListing transaction (for listing with ERC20 currency) should have proper erc20 value
392+
* so that getApprovalForTransaction can work properly
393+
*/
394+
const erc20Address = await deployERC20Contract({
395+
chain,
396+
client,
397+
account: TEST_ACCOUNT_C,
398+
type: "TokenERC20",
399+
params: {
400+
name: "MyToken",
401+
contractURI: TEST_CONTRACT_URI,
402+
},
403+
});
404+
// await sendAndConfirmTransaction({
405+
// transaction: approve({
406+
// contract: getContract({ address: erc20Address, chain, client }),
407+
// spender: marketplaceAddress,
408+
// amount: "10000000",
409+
// }),
410+
// account: TEST_ACCOUNT_B,
411+
// });
412+
await sendAndConfirmTransaction({
413+
transaction: createListing({
414+
contract: marketplaceContract,
415+
assetContractAddress: erc1155Contract.address,
416+
tokenId: 0n,
417+
pricePerToken: "0.01",
418+
quantity: 1n,
419+
currencyContractAddress: erc20Address,
420+
}),
421+
account: TEST_ACCOUNT_C,
422+
});
423+
// get the last listing id
424+
const _allListings = await getAllListings({
425+
contract: marketplaceContract,
426+
});
427+
const latestListing = _allListings.at(-1);
428+
expect(latestListing).toBeDefined();
429+
if (!latestListing) {
430+
throw new Error("Cannot get listings");
431+
}
432+
const buyListingTx = buyFromListing({
433+
contract: marketplaceContract,
434+
listingId: latestListing.id,
435+
quantity: 1n,
436+
recipient: TEST_ACCOUNT_C.address,
437+
});
438+
const approveTx = await getApprovalForTransaction({
439+
transaction: buyListingTx,
440+
account: TEST_ACCOUNT_A,
441+
});
442+
expect(approveTx).not.toBe(null);
387443
});
388444
});

packages/thirdweb/src/extensions/marketplace/direct-listings/write/buyFromListing.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ export function buyFromListing(
6666
? listing.pricePerToken * options.quantity
6767
: 0n,
6868
extraGas: 50_000n, // add extra gas to account for router call
69+
erc20Value: isNativeTokenAddress(listing.currencyContractAddress)
70+
? undefined
71+
: {
72+
amountWei: listing.pricePerToken * options.quantity,
73+
tokenAddress: listing.currencyContractAddress,
74+
},
6975
},
7076
};
7177
},

0 commit comments

Comments
 (0)