|
| 1 | +import {describe, expect, test} from '@jest/globals' |
| 2 | +import { |
| 3 | + paymasterProvider, |
| 4 | + wallet, |
| 5 | + tokenAbi, |
| 6 | + transformIsSponsorableResponse, |
| 7 | + transformToGaslessTransaction, |
| 8 | + delay, transformSponsorTxResponse, |
| 9 | +} from './utils' |
| 10 | +import {TOKEN_CONTRACT_ADDRESS, CHAIN_ID, RECIPIENT_ADDRESS} from './env' |
| 11 | +import {ethers} from 'ethers' |
| 12 | + |
| 13 | + |
| 14 | +let TX_HASH = '' |
| 15 | + |
| 16 | +/** |
| 17 | + * test paymaster apis |
| 18 | + */ |
| 19 | + |
| 20 | +describe('paymasterQuery', () => { |
| 21 | + |
| 22 | + describe('chainID', () => { |
| 23 | + test('it works', async () => { |
| 24 | + const res = await paymasterProvider.chainID() |
| 25 | + expect(res).toEqual('0x61') |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + describe('isSponsorable', () => { |
| 30 | + test('it works', async () => { |
| 31 | + // Create contract instance |
| 32 | + const tokenContract = new ethers.Contract(TOKEN_CONTRACT_ADDRESS, tokenAbi, wallet) |
| 33 | + |
| 34 | + // Transaction details |
| 35 | + const tokenAmount = ethers.parseUnits('1.0', 18) // Amount of tokens to send (adjust decimals as needed) |
| 36 | + // Get the current nonce for the sender's address |
| 37 | + const nonce = await paymasterProvider.getTransactionCount(wallet.address, 'pending') |
| 38 | + |
| 39 | + |
| 40 | + // Create the transaction object |
| 41 | + const transaction = await tokenContract.transfer.populateTransaction(RECIPIENT_ADDRESS.toLowerCase(), tokenAmount) |
| 42 | + |
| 43 | + // Add nonce and gas settings |
| 44 | + transaction.from = wallet.address |
| 45 | + console.log('wallet.address:', transaction.from) |
| 46 | + transaction.nonce = nonce |
| 47 | + transaction.gasLimit = BigInt(100000) // Adjust gas limit as needed for token transfers |
| 48 | + transaction.chainId = BigInt(CHAIN_ID) |
| 49 | + transaction.gasPrice = BigInt(0) // Set gas price to 0 |
| 50 | + |
| 51 | + const safeTransaction = { |
| 52 | + ...transaction, |
| 53 | + gasLimit: transaction.gasLimit.toString(), |
| 54 | + chainId: transaction.chainId.toString(), |
| 55 | + gasPrice: transaction.gasPrice.toString(), |
| 56 | + } |
| 57 | + console.log(safeTransaction) |
| 58 | + const resRaw = await paymasterProvider.isSponsorable(safeTransaction) |
| 59 | + const res = transformIsSponsorableResponse(resRaw) |
| 60 | + console.log(res) |
| 61 | + expect(res.Sponsorable).toEqual(true) |
| 62 | + |
| 63 | + const signedTx = await wallet.signTransaction(safeTransaction) |
| 64 | + try { |
| 65 | + const tx = await paymasterProvider.sendRawTransaction(signedTx) |
| 66 | + TX_HASH = tx |
| 67 | + console.log('Transaction sent:', tx) |
| 68 | + console.log('TX_HASH:', TX_HASH) |
| 69 | + } catch (error) { |
| 70 | + console.error('Error sending transaction:', error) |
| 71 | + } |
| 72 | + }, 100000) |
| 73 | + }) |
| 74 | + |
| 75 | + describe('getGaslessTransactionByHash', () => { |
| 76 | + test('it works', async () => { |
| 77 | + console.log('Waiting for the transaction to be confirmed and queryable on the blockchain.') |
| 78 | + await delay(8000) |
| 79 | + console.log('getGaslessTransactionByHash TX_HASH:', TX_HASH) |
| 80 | + const resRaw = await paymasterProvider.getGaslessTransactionByHash(TX_HASH) |
| 81 | + const res = transformToGaslessTransaction(resRaw) |
| 82 | + console.log(res) |
| 83 | + expect(res.ToAddress).toEqual(TOKEN_CONTRACT_ADDRESS.toLowerCase()) |
| 84 | + |
| 85 | + console.log('getSponsorTxByBundleUuid res.BundleUUID:', res.BundleUUID) |
| 86 | + const txRaw = await paymasterProvider.getSponsorTxByBundleUuid(res.BundleUUID) |
| 87 | + const tx = transformSponsorTxResponse(txRaw) |
| 88 | + expect(resRaw).not.toBeNull() |
| 89 | + console.log(tx) |
| 90 | + |
| 91 | + const bundle = await paymasterProvider.getBundleByUuid(res.BundleUUID) |
| 92 | + expect(bundle).not.toBeNull() |
| 93 | + console.log(bundle) |
| 94 | + |
| 95 | + const sponsorTx = await paymasterProvider.getSponsorTxByTxHash(tx.TxHash) |
| 96 | + console.log('sponsorTx: ', sponsorTx) |
| 97 | + expect(sponsorTx).not.toBeNull() |
| 98 | + }, 13000) |
| 99 | + }) |
| 100 | +}) |
0 commit comments