-
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.
- Loading branch information
1 parent
a43e94f
commit 9644bc5
Showing
10 changed files
with
440 additions
and
13 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,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 |
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,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'] | ||
}; |
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,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) | ||
}) | ||
}) |
Oops, something went wrong.