Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add github test workflow #3

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 25 additions & 0 deletions .github/workflows/test.yaml
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# megafuel-js-sdk

This JavaScript SDK is thin wrapper of MegaFuel clients, offering a streamlined interface to interact with the MegaFuel. For more information, please refer to the [API documentation](https://docs.nodereal.io/docs/megafuel-api).
This JavaScript SDK is thin wrapper of MegaFuel clients, offering a streamlined interface to interact with the MegaFuel. For more information, please refer to the [API documentation](https://docs.nodereal.io/reference/pm-issponsorable).

## Network Endpoint

Expand Down
30 changes: 21 additions & 9 deletions jest.config.js
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']
};
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'
99 changes: 99 additions & 0 deletions tests/paymaster.spec.ts
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)
})
})
Loading
Loading