Skip to content

Commit

Permalink
feat: add github test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryTong65 committed Sep 6, 2024
1 parent a43e94f commit 9644bc5
Show file tree
Hide file tree
Showing 10 changed files with 440 additions and 13 deletions.
12 changes: 12 additions & 0 deletions .github/actions/ci-setup/action.yml
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
28 changes: 28 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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: Build
# run: npm run build

- name: Run Test
run: npm run test
32 changes: 23 additions & 9 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
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',
},
// transformIgnorePatterns: ['node_modules/(?!(@bnb-chain/greenfield-cosmos-types)/)'],
extensionsToTreatAsEsm: ['.ts'],
transform: {
"^.+\\.(ts|tsx)$": "ts-jest"
'^.+\\.ts?$': [
'ts-jest',
{
// tsconfig: './config/tsconfig-cjs.json',
useESM: true,
},
],
},
moduleDirectories: ['node_modules', 'src'],
testTimeout: 30000,
}
setupFilesAfterEnv: ['<rootDir>/tests/env.ts', '<rootDir>/tests/utils.ts']
};
38 changes: 37 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"devDependencies": {
"@types/jest": "^29.2.4",
"@types/node": "^18.11.10",
"@types/ws": "^8.5.12",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"esbuild": "^0.15.18",
Expand All @@ -35,6 +36,7 @@
"typescript": "^4.9.3"
},
"dependencies": {
"ethers": "^6.3.0"
"dotenv": "^16.4.5",
"ethers": "^6.13.2"
}
}
18 changes: 18 additions & 0 deletions tests/env.ts
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'
100 changes: 100 additions & 0 deletions tests/paymaster.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {describe, expect, test} from '@jest/globals'
import {
paymasterProvider,
wallet,
tokenAbi,
transformIsSponsorableResponse,
transformToGaslessTransaction,
delay, transformSponsorTxResponse,
} from './utils'
import {TOKEN_CONTRACT_ADDRESS, CHAIN_ID, RECIPIENT_ADDRESS} from './env'
import {ethers} from 'ethers'


let TX_HASH = ''

/**
* test paymaster apis
*/

describe('paymasterQuery', () => {

describe('chainID', () => {
test('it works', async () => {
const res = await paymasterProvider.chainID()
expect(res).toEqual('0x61')
})
})

describe('isSponsorable', () => {
test('it works', async () => {
// Create contract instance
const tokenContract = new ethers.Contract(TOKEN_CONTRACT_ADDRESS, tokenAbi, wallet)

// Transaction details
const tokenAmount = ethers.parseUnits('1.0', 18) // Amount of tokens to send (adjust decimals as needed)
// Get the current nonce for the sender's address
const nonce = await paymasterProvider.getTransactionCount(wallet.address, 'pending')


// Create the transaction object
const transaction = await tokenContract.transfer.populateTransaction(RECIPIENT_ADDRESS.toLowerCase(), tokenAmount)

// Add nonce and gas settings
transaction.from = wallet.address
console.log('wallet.address:', transaction.from)
transaction.nonce = nonce
transaction.gasLimit = BigInt(100000) // Adjust gas limit as needed for token transfers
transaction.chainId = BigInt(CHAIN_ID)
transaction.gasPrice = BigInt(0) // Set gas price to 0

const safeTransaction = {
...transaction,
gasLimit: transaction.gasLimit.toString(),
chainId: transaction.chainId.toString(),
gasPrice: transaction.gasPrice.toString(),
}
console.log(safeTransaction)
const resRaw = await paymasterProvider.isSponsorable(safeTransaction)
const res = transformIsSponsorableResponse(resRaw)
console.log(res)
expect(res.Sponsorable).toEqual(true)

const signedTx = await wallet.signTransaction(safeTransaction)
try {
const tx = await paymasterProvider.sendRawTransaction(signedTx)
TX_HASH = tx
console.log('Transaction sent:', tx)
console.log('TX_HASH:', TX_HASH)
} catch (error) {
console.error('Error sending transaction:', error)
}
}, 100000)
})

describe('getGaslessTransactionByHash', () => {
test('it works', async () => {
console.log('Waiting for the transaction to be confirmed and queryable on the blockchain.')
await delay(8000)
console.log('getGaslessTransactionByHash TX_HASH:', TX_HASH)
const resRaw = await paymasterProvider.getGaslessTransactionByHash(TX_HASH)
const res = transformToGaslessTransaction(resRaw)
console.log(res)
expect(res.ToAddress).toEqual(TOKEN_CONTRACT_ADDRESS.toLowerCase())

console.log('getSponsorTxByBundleUuid res.BundleUUID:', res.BundleUUID)
const txRaw = await paymasterProvider.getSponsorTxByBundleUuid(res.BundleUUID)
const tx = transformSponsorTxResponse(txRaw)
expect(resRaw).not.toBeNull()
console.log(tx)

const bundle = await paymasterProvider.getBundleByUuid(res.BundleUUID)
expect(bundle).not.toBeNull()
console.log(bundle)

const sponsorTx = await paymasterProvider.getSponsorTxByTxHash(tx.TxHash)
console.log('sponsorTx: ', sponsorTx)
expect(sponsorTx).not.toBeNull()
}, 13000)
})
})
Loading

0 comments on commit 9644bc5

Please sign in to comment.