|
| 1 | +import {describe, expect, test} from '@jest/globals' |
| 2 | +import { |
| 3 | + paymasterClient, |
| 4 | + wallet, |
| 5 | + tokenAbi, |
| 6 | + transformIsSponsorableResponse, |
| 7 | + transformToGaslessTransaction, |
| 8 | + delay, transformSponsorTxResponse, transformBundleResponse, |
| 9 | +} from './utils' |
| 10 | +import {TOKEN_CONTRACT_ADDRESS, CHAIN_ID, RECIPIENT_ADDRESS} from './env' |
| 11 | +import {ethers} from 'ethers' |
| 12 | + |
| 13 | +let TX_HASH = '' |
| 14 | + |
| 15 | +/** |
| 16 | + * Testing suite for Paymaster API functionalities. |
| 17 | + */ |
| 18 | +describe('paymasterQuery', () => { |
| 19 | + |
| 20 | + /** |
| 21 | + * Test for retrieving chain ID from the paymaster provider. |
| 22 | + */ |
| 23 | + describe('chainID', () => { |
| 24 | + test('chainID should return the expected value', async () => { |
| 25 | + const res = await paymasterClient.chainID() |
| 26 | + expect(res).toEqual('0x61') |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + /** |
| 31 | + * Test for checking if a transaction is sponsorable. |
| 32 | + */ |
| 33 | + describe('isSponsorable', () => { |
| 34 | + test('should successfully determine if transaction is sponsorable', async () => { |
| 35 | + const tokenContract = new ethers.Contract(TOKEN_CONTRACT_ADDRESS, tokenAbi, wallet) |
| 36 | + const tokenAmount = ethers.parseUnits('0', 18) |
| 37 | + const nonce = await paymasterClient.getTransactionCount(wallet.address, 'pending') |
| 38 | + |
| 39 | + const transaction = await tokenContract.transfer.populateTransaction(RECIPIENT_ADDRESS.toLowerCase(), tokenAmount) |
| 40 | + transaction.from = wallet.address |
| 41 | + transaction.nonce = nonce |
| 42 | + transaction.gasLimit = BigInt(100000) |
| 43 | + transaction.chainId = BigInt(CHAIN_ID) |
| 44 | + transaction.gasPrice = BigInt(0) |
| 45 | + |
| 46 | + const safeTransaction = { |
| 47 | + ...transaction, |
| 48 | + gasLimit: transaction.gasLimit.toString(), |
| 49 | + chainId: transaction.chainId.toString(), |
| 50 | + gasPrice: transaction.gasPrice.toString(), |
| 51 | + } |
| 52 | + |
| 53 | + console.log('Prepared transaction:', safeTransaction) |
| 54 | + const resRaw = await paymasterClient.isSponsorable(safeTransaction) |
| 55 | + const res = transformIsSponsorableResponse(resRaw) |
| 56 | + expect(res.Sponsorable).toEqual(true) |
| 57 | + |
| 58 | + const signedTx = await wallet.signTransaction(safeTransaction) |
| 59 | + try { |
| 60 | + const tx = await paymasterClient.sendRawTransaction(signedTx) |
| 61 | + TX_HASH = tx |
| 62 | + console.log('Transaction hash received:', TX_HASH) |
| 63 | + } catch (error) { |
| 64 | + console.error('Transaction failed:', error) |
| 65 | + } |
| 66 | + }, 100000) // Extends the default timeout as this test involves network calls |
| 67 | + }) |
| 68 | + |
| 69 | + /** |
| 70 | + * Test for retrieving a gasless transaction by its hash and verifying related transactions. |
| 71 | + */ |
| 72 | + describe('getGaslessTransactionByHash', () => { |
| 73 | + test('should confirm and retrieve transaction details', async () => { |
| 74 | + console.log('Waiting for transaction confirmation...') |
| 75 | + await delay(8000) |
| 76 | + console.log('Querying gasless transaction by hash:', TX_HASH) |
| 77 | + const resRaw = await paymasterClient.getGaslessTransactionByHash(TX_HASH) |
| 78 | + const res = transformToGaslessTransaction(resRaw) |
| 79 | + expect(res.ToAddress).toEqual(TOKEN_CONTRACT_ADDRESS.toLowerCase()) |
| 80 | + console.log('Querying gasless transaction', res) |
| 81 | + |
| 82 | + console.log('Retrieving sponsor transaction by bundle UUID:', res.BundleUUID) |
| 83 | + const txRaw = await paymasterClient.getSponsorTxByBundleUuid(res.BundleUUID) |
| 84 | + const tx = transformSponsorTxResponse(txRaw) |
| 85 | + expect(txRaw).not.toBeNull() |
| 86 | + console.log('Sponsor transaction details:', tx) |
| 87 | + |
| 88 | + const bundleRaw = await paymasterClient.getBundleByUuid(res.BundleUUID) |
| 89 | + const bundle = transformBundleResponse(bundleRaw) |
| 90 | + expect(bundle.BundleUUID).toEqual(res.BundleUUID) |
| 91 | + console.log('Bundle details:', bundle) |
| 92 | + |
| 93 | + const sponsorTxRaw = await paymasterClient.getSponsorTxByTxHash(tx.TxHash) |
| 94 | + const sponsorTx = transformSponsorTxResponse(sponsorTxRaw) |
| 95 | + console.log('Sponsor transaction:', sponsorTx) |
| 96 | + expect(sponsorTx.TxHash).toEqual(tx.TxHash) |
| 97 | + }, 13000) |
| 98 | + }) |
| 99 | +}) |
0 commit comments