|
| 1 | +import { |
| 2 | + type RpcProvider, |
| 3 | + type Account, |
| 4 | + Contract, |
| 5 | + PaymasterRpc, |
| 6 | + OutsideExecutionVersion, |
| 7 | + type TokenData, |
| 8 | + num, |
| 9 | + type PaymasterDetails, |
| 10 | + cairo, |
| 11 | + type PaymasterFeeEstimate, |
| 12 | +} from '../src'; |
| 13 | +import { describeIfTestnet, getTestProvider } from './config/fixtures'; |
| 14 | +import { getTestAccount, STRKtokenAddress } from './config/fixturesInit'; |
| 15 | + |
| 16 | +describeIfTestnet('Paymaster with Contract, in Testnet', () => { |
| 17 | + let provider: RpcProvider; |
| 18 | + let myAccount: Account; |
| 19 | + let strkContract: Contract; |
| 20 | + const feesDetails: PaymasterDetails = { |
| 21 | + feeMode: { mode: 'default', gasToken: STRKtokenAddress }, |
| 22 | + }; |
| 23 | + |
| 24 | + beforeAll(async () => { |
| 25 | + provider = getTestProvider(false); |
| 26 | + const paymasterRpc = new PaymasterRpc({ nodeUrl: 'https://sepolia.paymaster.avnu.fi' }); |
| 27 | + myAccount = getTestAccount(provider, undefined, paymasterRpc); |
| 28 | + // console.log(myAccount.paymaster); |
| 29 | + const isAccountCompatibleSnip9 = await myAccount.getSnip9Version(); |
| 30 | + expect(isAccountCompatibleSnip9).not.toBe(OutsideExecutionVersion.UNSUPPORTED); |
| 31 | + const isPaymasterAvailable = await myAccount.paymaster.isAvailable(); |
| 32 | + expect(isPaymasterAvailable).toBe(true); |
| 33 | + strkContract = new Contract({ |
| 34 | + abi: (await provider.getClassAt(STRKtokenAddress)).abi, |
| 35 | + address: STRKtokenAddress, |
| 36 | + providerOrAccount: myAccount, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + test('Get list of tokens', async () => { |
| 41 | + const supported: TokenData[] = await myAccount.paymaster.getSupportedTokens(); |
| 42 | + const containsStrk = supported.some( |
| 43 | + (data: TokenData) => data.token_address === num.cleanHex(STRKtokenAddress) |
| 44 | + ); |
| 45 | + expect(containsStrk).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + test('Estimate fee with Paymaster in a Contract', async () => { |
| 49 | + const estimation = (await strkContract.estimate( |
| 50 | + 'transfer', |
| 51 | + [ |
| 52 | + '0x010101', // random address |
| 53 | + cairo.uint256(10), // dust of STRK |
| 54 | + ], |
| 55 | + { |
| 56 | + paymasterDetails: feesDetails, |
| 57 | + } |
| 58 | + )) as PaymasterFeeEstimate; |
| 59 | + expect(estimation.suggested_max_fee_in_gas_token).toBeDefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + test('Contract invoke with Paymaster', async () => { |
| 63 | + const res1 = await strkContract.invoke('transfer', ['0x010101', cairo.uint256(100)], { |
| 64 | + paymasterDetails: feesDetails, |
| 65 | + }); |
| 66 | + const txR1 = await provider.waitForTransaction(res1.transaction_hash); |
| 67 | + expect(txR1.isSuccess()).toBe(true); |
| 68 | + const res2 = await strkContract.invoke('transfer', ['0x010101', cairo.uint256(101)], { |
| 69 | + paymasterDetails: feesDetails, |
| 70 | + maxFeeInGasToken: 2n * 10n ** 17n, |
| 71 | + }); |
| 72 | + const txR2 = await provider.waitForTransaction(res2.transaction_hash); |
| 73 | + expect(txR2.isSuccess()).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + test('Contract withOptions with Paymaster', async () => { |
| 77 | + const res1 = await strkContract |
| 78 | + .withOptions({ |
| 79 | + paymasterDetails: feesDetails, |
| 80 | + }) |
| 81 | + .transfer('0x010101', cairo.uint256(102)); |
| 82 | + const txR1 = await provider.waitForTransaction(res1.transaction_hash); |
| 83 | + expect(txR1.isSuccess()).toBe(true); |
| 84 | + const res2 = await strkContract |
| 85 | + .withOptions({ |
| 86 | + paymasterDetails: feesDetails, |
| 87 | + maxFeeInGasToken: 2n * 10n ** 17n, |
| 88 | + }) |
| 89 | + .transfer('0x010101', cairo.uint256(103)); |
| 90 | + const txR2 = await provider.waitForTransaction(res2.transaction_hash); |
| 91 | + expect(txR2.isSuccess()).toBe(true); |
| 92 | + }); |
| 93 | +}); |
0 commit comments