-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
496 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: 'CI setup' | ||
description: NPM install deps | ||
runs: | ||
using: composite | ||
steps: | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: '18.20.2' | ||
|
||
- name: Install dependencies | ||
shell: bash | ||
run: npm install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Test Case | ||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
jobs: | ||
|
||
run-tests: | ||
runs-on: ubuntu-latest | ||
|
||
env: | ||
ACCOUNT_ADDRESS: ${{ secrets.ACCOUNT_ADDRESS }} | ||
ACCOUNT_PRIVATEKEY: ${{ secrets.ACCOUNT_PRIVATEKEY }} | ||
OPEN_PLATFORM_PRIVATE_KEY: ${{ secrets.OPEN_PLATFORM_PRIVATE_KEY }} | ||
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
persist-credentials: false | ||
|
||
- uses: ./.github/actions/ci-setup | ||
|
||
- name: Run Test | ||
run: npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
const { pathsToModuleNameMapper } = require('ts-jest'); | ||
|
||
// Assuming you have some TypeScript path aliases defined in your tsconfig.json | ||
const { compilerOptions } = require('./tsconfig'); | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
roots: ['<rootDir>/src'], | ||
testMatch: [ | ||
"**/__tests__/**/*.+(ts|tsx|js)", | ||
"**/?(*.)+(spec|test).+(ts|tsx|js)" | ||
], | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
modulePathIgnorePatterns: ['<rootDir>/src/config.spec.ts'], | ||
moduleNameMapper: { | ||
...pathsToModuleNameMapper({ '@/*': ['./src/*'] }, { prefix: '<rootDir>/' }), | ||
'^(\\.{1,2}/.*)\\.js$': '$1', | ||
}, | ||
extensionsToTreatAsEsm: ['.ts'], | ||
transform: { | ||
"^.+\\.(ts|tsx)$": "ts-jest" | ||
'^.+\\.ts?$': [ | ||
'ts-jest', | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
moduleDirectories: ['node_modules', 'src'], | ||
testTimeout: 30000, | ||
} | ||
setupFilesAfterEnv: ['<rootDir>/tests/env.ts', '<rootDir>/tests/utils.ts'] | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import dotenv from 'dotenv' | ||
|
||
dotenv.config({ | ||
path: process.cwd() + '/tests/.env', | ||
}) | ||
|
||
// testnet env | ||
export const OPEN_PLATFORM_PRIVATE_KEY = process.env.OPEN_PLATFORM_PRIVATE_KEY | ||
export const SPONSOR_URL = `https://open-platform.nodereal.io/${OPEN_PLATFORM_PRIVATE_KEY}/megafuel-testnet` | ||
export const CHAIN_ID = '97' | ||
export const CHAIN_URL = `https://bsc-testnet.nodereal.io/v1/${OPEN_PLATFORM_PRIVATE_KEY}` | ||
export const PAYMASTER_URL = 'https://bsc-megafuel-testnet.nodereal.io/97' | ||
export const PRIVATE_KEY = process.env.PRIVATE_KEY as string | ||
export const POLICY_UUID = '72191372-5550-4cf6-956e-b70d1e4786cf' | ||
export const ACCOUNT_ADDRESS = '0xF9A8db17431DD8563747D6FC770297E438Aa12eB' | ||
export const CONTRACT_METHOD = '0xa9059cbb' | ||
export const TOKEN_CONTRACT_ADDRESS = '0xeD24FC36d5Ee211Ea25A80239Fb8C4Cfd80f12Ee' | ||
export const RECIPIENT_ADDRESS = '0xDE08B1Fd79b7016F8DD3Df11f7fa0FbfdF07c941' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import {describe, expect, test} from '@jest/globals' | ||
import { | ||
paymasterClient, | ||
wallet, | ||
tokenAbi, | ||
transformIsSponsorableResponse, | ||
transformToGaslessTransaction, | ||
delay, transformSponsorTxResponse, transformBundleResponse, | ||
} from './utils' | ||
import {TOKEN_CONTRACT_ADDRESS, CHAIN_ID, RECIPIENT_ADDRESS} from './env' | ||
import {ethers} from 'ethers' | ||
|
||
let TX_HASH = '' | ||
|
||
/** | ||
* Testing suite for Paymaster API functionalities. | ||
*/ | ||
describe('paymasterQuery', () => { | ||
|
||
/** | ||
* Test for retrieving chain ID from the paymaster provider. | ||
*/ | ||
describe('chainID', () => { | ||
test('chainID should return the expected value', async () => { | ||
const res = await paymasterClient.chainID() | ||
expect(res).toEqual('0x61') | ||
}) | ||
}) | ||
|
||
/** | ||
* Test for checking if a transaction is sponsorable. | ||
*/ | ||
describe('isSponsorable', () => { | ||
test('should successfully determine if transaction is sponsorable', async () => { | ||
const tokenContract = new ethers.Contract(TOKEN_CONTRACT_ADDRESS, tokenAbi, wallet) | ||
const tokenAmount = ethers.parseUnits('0', 18) | ||
const nonce = await paymasterClient.getTransactionCount(wallet.address, 'pending') | ||
|
||
const transaction = await tokenContract.transfer.populateTransaction(RECIPIENT_ADDRESS.toLowerCase(), tokenAmount) | ||
transaction.from = wallet.address | ||
transaction.nonce = nonce | ||
transaction.gasLimit = BigInt(100000) | ||
transaction.chainId = BigInt(CHAIN_ID) | ||
transaction.gasPrice = BigInt(0) | ||
|
||
const safeTransaction = { | ||
...transaction, | ||
gasLimit: transaction.gasLimit.toString(), | ||
chainId: transaction.chainId.toString(), | ||
gasPrice: transaction.gasPrice.toString(), | ||
} | ||
|
||
console.log('Prepared transaction:', safeTransaction) | ||
const resRaw = await paymasterClient.isSponsorable(safeTransaction) | ||
const res = transformIsSponsorableResponse(resRaw) | ||
expect(res.Sponsorable).toEqual(true) | ||
|
||
const signedTx = await wallet.signTransaction(safeTransaction) | ||
try { | ||
const tx = await paymasterClient.sendRawTransaction(signedTx) | ||
TX_HASH = tx | ||
console.log('Transaction hash received:', TX_HASH) | ||
} catch (error) { | ||
console.error('Transaction failed:', error) | ||
} | ||
}, 100000) // Extends the default timeout as this test involves network calls | ||
}) | ||
|
||
/** | ||
* Test for retrieving a gasless transaction by its hash and verifying related transactions. | ||
*/ | ||
describe('getGaslessTransactionByHash', () => { | ||
test('should confirm and retrieve transaction details', async () => { | ||
console.log('Waiting for transaction confirmation...') | ||
await delay(8000) | ||
console.log('Querying gasless transaction by hash:', TX_HASH) | ||
const resRaw = await paymasterClient.getGaslessTransactionByHash(TX_HASH) | ||
const res = transformToGaslessTransaction(resRaw) | ||
expect(res.ToAddress).toEqual(TOKEN_CONTRACT_ADDRESS.toLowerCase()) | ||
console.log('Querying gasless transaction', res) | ||
|
||
console.log('Retrieving sponsor transaction by bundle UUID:', res.BundleUUID) | ||
const txRaw = await paymasterClient.getSponsorTxByBundleUuid(res.BundleUUID) | ||
const tx = transformSponsorTxResponse(txRaw) | ||
expect(txRaw).not.toBeNull() | ||
console.log('Sponsor transaction details:', tx) | ||
|
||
const bundleRaw = await paymasterClient.getBundleByUuid(res.BundleUUID) | ||
const bundle = transformBundleResponse(bundleRaw) | ||
expect(bundle.BundleUUID).toEqual(res.BundleUUID) | ||
console.log('Bundle details:', bundle) | ||
|
||
const sponsorTxRaw = await paymasterClient.getSponsorTxByTxHash(tx.TxHash) | ||
const sponsorTx = transformSponsorTxResponse(sponsorTxRaw) | ||
console.log('Sponsor transaction:', sponsorTx) | ||
expect(sponsorTx.TxHash).toEqual(tx.TxHash) | ||
}, 13000) | ||
}) | ||
}) |
Oops, something went wrong.