Skip to content

Commit

Permalink
feat: optimize comments
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryTong65 committed Sep 6, 2024
1 parent 9644bc5 commit 9f9e60e
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 66 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,5 @@ jobs:

- uses: ./.github/actions/ci-setup

# - name: Build
# run: npm run build

- name: Run Test
run: npm run test
4 changes: 1 addition & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ module.exports = {
...pathsToModuleNameMapper({ '@/*': ['./src/*'] }, { prefix: '<rootDir>/' }),
'^(\\.{1,2}/.*)\\.js$': '$1',
},
// transformIgnorePatterns: ['node_modules/(?!(@bnb-chain/greenfield-cosmos-types)/)'],
extensionsToTreatAsEsm: ['.ts'],
transform: {
'^.+\\.ts?$': [
'ts-jest',
{
// tsconfig: './config/tsconfig-cjs.json',
useESM: true,
},
],
},
setupFilesAfterEnv: ['<rootDir>/tests/env.ts', '<rootDir>/tests/utils.ts']
};
};
77 changes: 38 additions & 39 deletions tests/paymaster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,96 +5,95 @@ import {
tokenAbi,
transformIsSponsorableResponse,
transformToGaslessTransaction,
delay, transformSponsorTxResponse,
delay, transformSponsorTxResponse, transformBundleResponse,
} from './utils'
import {TOKEN_CONTRACT_ADDRESS, CHAIN_ID, RECIPIENT_ADDRESS} from './env'
import {ethers} from 'ethers'


let TX_HASH = ''

/**
* test paymaster apis
* Testing suite for Paymaster API functionalities.
*/

describe('paymasterQuery', () => {

/**
* Test for retrieving chain ID from the paymaster provider.
*/
describe('chainID', () => {
test('it works', async () => {
test('chainID should return the expected value', async () => {
const res = await paymasterProvider.chainID()
expect(res).toEqual('0x61')
})
})

/**
* Test for checking if a transaction is sponsorable.
*/
describe('isSponsorable', () => {
test('it works', async () => {
// Create contract instance
test('should successfully determine if transaction is sponsorable', async () => {
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 tokenAmount = ethers.parseUnits('1.0', 18)
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.gasLimit = BigInt(100000)
transaction.chainId = BigInt(CHAIN_ID)
transaction.gasPrice = BigInt(0) // Set gas price to 0
transaction.gasPrice = BigInt(0)

const safeTransaction = {
...transaction,
gasLimit: transaction.gasLimit.toString(),
chainId: transaction.chainId.toString(),
gasPrice: transaction.gasPrice.toString(),
}
console.log(safeTransaction)

console.log('Prepared transaction:', 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)
console.log('Transaction hash received:', TX_HASH)
} catch (error) {
console.error('Error sending transaction:', error)
console.error('Transaction failed:', error)
}
}, 100000)
}, 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('it works', async () => {
console.log('Waiting for the transaction to be confirmed and queryable on the blockchain.')
test('should confirm and retrieve transaction details', async () => {
console.log('Waiting for transaction confirmation...')
await delay(8000)
console.log('getGaslessTransactionByHash TX_HASH:', TX_HASH)
console.log('Querying gasless transaction by 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('Querying gasless transaction', res)

console.log('getSponsorTxByBundleUuid res.BundleUUID:', res.BundleUUID)
console.log('Retrieving sponsor transaction by bundle UUID:', 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()
expect(txRaw).not.toBeNull()
console.log('Sponsor transaction details:', tx)

const bundleRaw = await paymasterProvider.getBundleByUuid(res.BundleUUID)
const bundle = transformBundleResponse(bundleRaw)
expect(bundle.BundleUUID).toEqual(res.BundleUUID)
console.log('Bundle details:', bundle)

const sponsorTxRaw = await paymasterProvider.getSponsorTxByTxHash(tx.TxHash)
const sponsorTx = transformSponsorTxResponse(sponsorTxRaw)
console.log('Sponsor transaction:', sponsorTx)
expect(sponsorTx.TxHash).toEqual(tx.TxHash)
}, 13000)
})
})
81 changes: 62 additions & 19 deletions tests/sponsor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,79 @@ import {client} from './utils'
import {WhitelistType} from '../src'
import {POLICY_UUID, ACCOUNT_ADDRESS, CONTRACT_METHOD} from './env'


/**
* test sponsor apis
* Test suite for Sponsor API methods involving whitelist management and spend data retrieval.
*/

describe('sponsorQuery', () => {
/**
* Tests adding an account address to the 'From Account' whitelist.
*/
describe('addToWhitelist FromAccountWhitelist', () => {
test('it works', async () => {
test('should add an account address to FromAccountWhitelist successfully', async () => {
const res = await client.addToWhitelist({
PolicyUUID: POLICY_UUID,
WhitelistType: WhitelistType.FromAccountWhitelist,
Values: [ACCOUNT_ADDRESS],
})

expect(res).toEqual(true)
console.log(res)
console.log('FromAccountWhitelist addition response:', res)
})
})

/**
* Tests adding an account address to the 'To Account' whitelist.
*/
describe('addToWhitelist ToAccountWhitelist', () => {
test('it works', async () => {
test('should add an account address to ToAccountWhitelist successfully', async () => {
const res = await client.addToWhitelist({
PolicyUUID: POLICY_UUID,
WhitelistType: WhitelistType.ToAccountWhitelist,
Values: [ACCOUNT_ADDRESS],
})

expect(res).toEqual(true)
console.log(res)
console.log('ToAccountWhitelist addition response:', res)
})
})

/**
* Tests adding an account address to the BEP20 receiver whitelist.
*/
describe('addToWhitelist BEP20ReceiverWhiteList', () => {
test('it works', async () => {
test('should add an account address to BEP20ReceiverWhiteList successfully', async () => {
const res = await client.addToWhitelist({
PolicyUUID: POLICY_UUID,
WhitelistType: WhitelistType.BEP20ReceiverWhiteList,
Values: [ACCOUNT_ADDRESS],
})

expect(res).toEqual(true)
console.log(res)
console.log('BEP20ReceiverWhiteList addition response:', res)
})
})

/**
* Tests adding a contract method signature to the whitelist.
*/
describe('addToWhitelist ContractMethodSigWhitelist', () => {
test('it works', async () => {
test('should add a contract method signature to ContractMethodSigWhitelist successfully', async () => {
const res = await client.addToWhitelist({
PolicyUUID: POLICY_UUID,
WhitelistType: WhitelistType.ContractMethodSigWhitelist,
Values: [CONTRACT_METHOD],
})

expect(res).toEqual(true)
console.log('ContractMethodSigWhitelist addition response:', res)
})
})

/**
* Tests retrieving whitelists of contract method signatures.
*/
describe('getWhitelist', () => {
test('it works', async () => {
test('should retrieve contract method signatures successfully', async () => {
const res = await client.getWhitelist({
PolicyUUID: POLICY_UUID,
WhitelistType: WhitelistType.ContractMethodSigWhitelist,
Expand All @@ -69,23 +84,31 @@ describe('sponsorQuery', () => {
})

expect(res[0]).toEqual(CONTRACT_METHOD)
console.log('Retrieved ContractMethodSigWhitelist:', res)
})
})

/**
* Tests removing an account address from a whitelist.
*/
describe('removeFromWhitelist', () => {
test('it works', async () => {
test('should remove an account address from FromAccountWhitelist successfully', async () => {
const res = await client.removeFromWhitelist({
PolicyUUID: POLICY_UUID,
WhitelistType: WhitelistType.FromAccountWhitelist,
Values: [ACCOUNT_ADDRESS],
})

expect(res).toEqual(true)
console.log('FromAccountWhitelist removal response:', res)
})
})

/**
* Tests verifying the removal of an account address from a whitelist.
*/
describe('getWhitelist', () => {
test('it works', async () => {
test('should not contain account address post-removal', async () => {
const res = await client.getWhitelist({
PolicyUUID: POLICY_UUID,
WhitelistType: WhitelistType.FromAccountWhitelist,
Expand All @@ -95,22 +118,30 @@ describe('sponsorQuery', () => {
if (res !== null && res !== undefined) {
expect(res).not.toContain(ACCOUNT_ADDRESS)
}
console.log('FromAccountWhitelist post-removal check:', res)
})
})

/**
* Tests clearing all entries from a specific whitelist type.
*/
describe('emptyWhitelist', () => {
test('it works', async () => {
test('should clear all entries from BEP20ReceiverWhiteList successfully', async () => {
const res = await client.emptyWhitelist({
PolicyUUID: POLICY_UUID,
WhitelistType: WhitelistType.BEP20ReceiverWhiteList,
})

expect(res).toEqual(true)
console.log('BEP20ReceiverWhiteList clearance response:', res)
})
})

/**
* Tests verifying the emptiness of a whitelist.
*/
describe('getWhitelist', () => {
test('it works', async () => {
test('should confirm the whitelist is empty', async () => {
const res = await client.getWhitelist({
PolicyUUID: POLICY_UUID,
WhitelistType: WhitelistType.BEP20ReceiverWhiteList,
Expand All @@ -119,34 +150,46 @@ describe('sponsorQuery', () => {
})

expect(res).toBeNull()
console.log('BEP20ReceiverWhiteList emptiness check:', res)
})
})

/**
* Tests retrieving user spend data.
*/
describe('getUserSpendData', () => {
test('it works', async () => {
test('should return null for spend data when user has none', async () => {
const res = await client.getUserSpendData(ACCOUNT_ADDRESS, POLICY_UUID)

expect(res).toBeNull()
console.log('User spend data:', res)
})
})

/**
* Tests retrieving policy spend data.
*/
describe('getPolicySpendData', () => {
test('it works', async () => {
test('should retrieve policy spend data successfully', async () => {
const res = await client.getPolicySpendData(POLICY_UUID)
expect(res.ChainID).not.toBeNull()
console.log('Policy spend data:', res)
})
})

/**
* Tests re-adding an account address to the 'From Account' whitelist after previous tests.
*/
describe('addToWhitelist FromAccountWhitelist', () => {
test('it works', async () => {
test('should re-add an account address to FromAccountWhitelist successfully after removal', async () => {
const res = await client.addToWhitelist({
PolicyUUID: POLICY_UUID,
WhitelistType: WhitelistType.FromAccountWhitelist,
Values: [ACCOUNT_ADDRESS],
})

expect(res).toEqual(true)
console.log(res)
console.log('Re-addition to FromAccountWhitelist response:', res)
})
})
})
Loading

0 comments on commit 9f9e60e

Please sign in to comment.