|
1 | 1 | import { ZERO_ADDRESS } from "./../../packages/core-sdk/tests/mocks"; |
2 | 2 | import { BigNumberish } from "@ethersproject/bignumber"; |
| 3 | +import { parseEther } from "@ethersproject/units"; |
3 | 4 | import { Wallet, BigNumber, constants } from "ethers"; |
4 | 5 | import { OfferFieldsFragment } from "../../packages/core-sdk/src/subgraph"; |
5 | 6 | import { mockCreateOfferArgs } from "../../packages/common/tests/mocks"; |
@@ -815,6 +816,104 @@ describe("meta-tx", () => { |
815 | 816 | expect(metaTxReceipt.transactionHash).toBeTruthy(); |
816 | 817 | expect(BigNumber.from(metaTxReceipt.effectiveGasPrice).gt(0)).toBe(true); |
817 | 818 | }); |
| 819 | + |
| 820 | + test("non-native exchange token buyer-initiated offer", async () => { |
| 821 | + const exchangeToken = MOCK_ERC20_ADDRESS; |
| 822 | + const drFeeAmount = parseEther("0.001"); |
| 823 | + |
| 824 | + // Create a dispute resolver with an ERC20 fee |
| 825 | + const { fundedWallet: drFundedWallet } = |
| 826 | + await initCoreSDKWithFundedWallet(sellerWallet); |
| 827 | + const drAddress = drFundedWallet.address.toLowerCase(); |
| 828 | + const { disputeResolver } = await createDisputeResolver( |
| 829 | + drFundedWallet, |
| 830 | + deployerWallet, |
| 831 | + { |
| 832 | + assistant: drAddress, |
| 833 | + admin: drAddress, |
| 834 | + treasury: drAddress, |
| 835 | + metadataUri: "", |
| 836 | + escalationResponsePeriodInMS: |
| 837 | + 90 * MSEC_PER_DAY - 1 * MSEC_PER_SEC, |
| 838 | + fees: [ |
| 839 | + { |
| 840 | + feeAmount: drFeeAmount, |
| 841 | + tokenAddress: exchangeToken, |
| 842 | + tokenName: "ERC20" |
| 843 | + } |
| 844 | + ], |
| 845 | + sellerAllowList: [] |
| 846 | + } |
| 847 | + ); |
| 848 | + |
| 849 | + // Create fresh buyer/seller wallets |
| 850 | + const { |
| 851 | + sellerCoreSDK: sellerCoreSDKBuyer, |
| 852 | + buyerCoreSDK: buyerCoreSDKBuyer, |
| 853 | + sellerWallet: sellerFundedWallet |
| 854 | + } = await initSellerAndBuyerSDKs(sellerWallet); |
| 855 | + |
| 856 | + // Buyer creates a buyer-initiated offer with ERC20 exchange token. |
| 857 | + // sellerDeposit is 0 to simplify the test setup. |
| 858 | + const buyerInitiatedOffer = await createOffer(buyerCoreSDKBuyer, { |
| 859 | + creator: OfferCreator.Buyer, |
| 860 | + quantityAvailable: 1, |
| 861 | + disputeResolverId: disputeResolver.id, |
| 862 | + exchangeToken, |
| 863 | + sellerDeposit: "0" |
| 864 | + }); |
| 865 | + |
| 866 | + // Buyer approves ERC20 and deposits offer.price |
| 867 | + await approveErc20Token( |
| 868 | + buyerCoreSDKBuyer, |
| 869 | + exchangeToken, |
| 870 | + buyerInitiatedOffer.price |
| 871 | + ); |
| 872 | + const buyerDepositTx = await buyerCoreSDKBuyer.depositFunds( |
| 873 | + buyerInitiatedOffer.buyerId, |
| 874 | + buyerInitiatedOffer.price, |
| 875 | + exchangeToken |
| 876 | + ); |
| 877 | + await buyerCoreSDKBuyer.waitForGraphNodeIndexing(buyerDepositTx); |
| 878 | + |
| 879 | + // Seller creates a seller account |
| 880 | + const seller = await createSeller( |
| 881 | + sellerCoreSDKBuyer, |
| 882 | + sellerFundedWallet.address |
| 883 | + ); |
| 884 | + |
| 885 | + // Seller approves ERC20 and deposits drFeeAmount into the protocol |
| 886 | + await approveErc20Token(sellerCoreSDKBuyer, exchangeToken, drFeeAmount); |
| 887 | + const sellerDepositTx = await sellerCoreSDKBuyer.depositFunds( |
| 888 | + seller.id, |
| 889 | + drFeeAmount, |
| 890 | + exchangeToken |
| 891 | + ); |
| 892 | + await sellerCoreSDKBuyer.waitForGraphNodeIndexing(sellerDepositTx); |
| 893 | + |
| 894 | + const nonce = Date.now(); |
| 895 | + |
| 896 | + // Seller signs meta tx for commitToBuyerOffer |
| 897 | + const { r, s, v, functionName, functionSignature } = |
| 898 | + await sellerCoreSDKBuyer.signMetaTxCommitToBuyerOffer({ |
| 899 | + offerId: buyerInitiatedOffer.id, |
| 900 | + sellerParams: {}, |
| 901 | + nonce |
| 902 | + }); |
| 903 | + |
| 904 | + // Relayer executes meta tx on behalf of seller |
| 905 | + const metaTx = await sellerCoreSDKBuyer.relayMetaTransaction({ |
| 906 | + functionName, |
| 907 | + functionSignature, |
| 908 | + nonce, |
| 909 | + sigR: r, |
| 910 | + sigS: s, |
| 911 | + sigV: v |
| 912 | + }); |
| 913 | + const metaTxReceipt = await metaTx.wait(); |
| 914 | + expect(metaTxReceipt.transactionHash).toBeTruthy(); |
| 915 | + expect(BigNumber.from(metaTxReceipt.effectiveGasPrice).gt(0)).toBe(true); |
| 916 | + }); |
818 | 917 | }); |
819 | 918 |
|
820 | 919 | describe("#signMetaTxRedeemVoucher()", () => { |
|
0 commit comments