Skip to content

Commit 9f9e60e

Browse files
committed
feat: optimize comments
1 parent 9644bc5 commit 9f9e60e

File tree

5 files changed

+123
-66
lines changed

5 files changed

+123
-66
lines changed

.github/workflows/test.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,5 @@ jobs:
2121

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

24-
# - name: Build
25-
# run: npm run build
26-
2724
- name: Run Test
2825
run: npm run test

jest.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ module.exports = {
1111
...pathsToModuleNameMapper({ '@/*': ['./src/*'] }, { prefix: '<rootDir>/' }),
1212
'^(\\.{1,2}/.*)\\.js$': '$1',
1313
},
14-
// transformIgnorePatterns: ['node_modules/(?!(@bnb-chain/greenfield-cosmos-types)/)'],
1514
extensionsToTreatAsEsm: ['.ts'],
1615
transform: {
1716
'^.+\\.ts?$': [
1817
'ts-jest',
1918
{
20-
// tsconfig: './config/tsconfig-cjs.json',
2119
useESM: true,
2220
},
2321
],
2422
},
2523
setupFilesAfterEnv: ['<rootDir>/tests/env.ts', '<rootDir>/tests/utils.ts']
26-
};
24+
};

tests/paymaster.spec.ts

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,96 +5,95 @@ import {
55
tokenAbi,
66
transformIsSponsorableResponse,
77
transformToGaslessTransaction,
8-
delay, transformSponsorTxResponse,
8+
delay, transformSponsorTxResponse, transformBundleResponse,
99
} from './utils'
1010
import {TOKEN_CONTRACT_ADDRESS, CHAIN_ID, RECIPIENT_ADDRESS} from './env'
1111
import {ethers} from 'ethers'
1212

13-
1413
let TX_HASH = ''
1514

1615
/**
17-
* test paymaster apis
16+
* Testing suite for Paymaster API functionalities.
1817
*/
19-
2018
describe('paymasterQuery', () => {
2119

20+
/**
21+
* Test for retrieving chain ID from the paymaster provider.
22+
*/
2223
describe('chainID', () => {
23-
test('it works', async () => {
24+
test('chainID should return the expected value', async () => {
2425
const res = await paymasterProvider.chainID()
2526
expect(res).toEqual('0x61')
2627
})
2728
})
2829

30+
/**
31+
* Test for checking if a transaction is sponsorable.
32+
*/
2933
describe('isSponsorable', () => {
30-
test('it works', async () => {
31-
// Create contract instance
34+
test('should successfully determine if transaction is sponsorable', async () => {
3235
const tokenContract = new ethers.Contract(TOKEN_CONTRACT_ADDRESS, tokenAbi, wallet)
33-
34-
// Transaction details
35-
const tokenAmount = ethers.parseUnits('1.0', 18) // Amount of tokens to send (adjust decimals as needed)
36-
// Get the current nonce for the sender's address
36+
const tokenAmount = ethers.parseUnits('1.0', 18)
3737
const nonce = await paymasterProvider.getTransactionCount(wallet.address, 'pending')
3838

39-
40-
// Create the transaction object
4139
const transaction = await tokenContract.transfer.populateTransaction(RECIPIENT_ADDRESS.toLowerCase(), tokenAmount)
42-
43-
// Add nonce and gas settings
4440
transaction.from = wallet.address
45-
console.log('wallet.address:', transaction.from)
4641
transaction.nonce = nonce
47-
transaction.gasLimit = BigInt(100000) // Adjust gas limit as needed for token transfers
42+
transaction.gasLimit = BigInt(100000)
4843
transaction.chainId = BigInt(CHAIN_ID)
49-
transaction.gasPrice = BigInt(0) // Set gas price to 0
44+
transaction.gasPrice = BigInt(0)
5045

5146
const safeTransaction = {
5247
...transaction,
5348
gasLimit: transaction.gasLimit.toString(),
5449
chainId: transaction.chainId.toString(),
5550
gasPrice: transaction.gasPrice.toString(),
5651
}
57-
console.log(safeTransaction)
52+
53+
console.log('Prepared transaction:', safeTransaction)
5854
const resRaw = await paymasterProvider.isSponsorable(safeTransaction)
5955
const res = transformIsSponsorableResponse(resRaw)
60-
console.log(res)
6156
expect(res.Sponsorable).toEqual(true)
6257

6358
const signedTx = await wallet.signTransaction(safeTransaction)
6459
try {
6560
const tx = await paymasterProvider.sendRawTransaction(signedTx)
6661
TX_HASH = tx
67-
console.log('Transaction sent:', tx)
68-
console.log('TX_HASH:', TX_HASH)
62+
console.log('Transaction hash received:', TX_HASH)
6963
} catch (error) {
70-
console.error('Error sending transaction:', error)
64+
console.error('Transaction failed:', error)
7165
}
72-
}, 100000)
66+
}, 100000) // Extends the default timeout as this test involves network calls
7367
})
7468

69+
/**
70+
* Test for retrieving a gasless transaction by its hash and verifying related transactions.
71+
*/
7572
describe('getGaslessTransactionByHash', () => {
76-
test('it works', async () => {
77-
console.log('Waiting for the transaction to be confirmed and queryable on the blockchain.')
73+
test('should confirm and retrieve transaction details', async () => {
74+
console.log('Waiting for transaction confirmation...')
7875
await delay(8000)
79-
console.log('getGaslessTransactionByHash TX_HASH:', TX_HASH)
76+
console.log('Querying gasless transaction by hash:', TX_HASH)
8077
const resRaw = await paymasterProvider.getGaslessTransactionByHash(TX_HASH)
8178
const res = transformToGaslessTransaction(resRaw)
82-
console.log(res)
8379
expect(res.ToAddress).toEqual(TOKEN_CONTRACT_ADDRESS.toLowerCase())
80+
console.log('Querying gasless transaction', res)
8481

85-
console.log('getSponsorTxByBundleUuid res.BundleUUID:', res.BundleUUID)
82+
console.log('Retrieving sponsor transaction by bundle UUID:', res.BundleUUID)
8683
const txRaw = await paymasterProvider.getSponsorTxByBundleUuid(res.BundleUUID)
8784
const tx = transformSponsorTxResponse(txRaw)
88-
expect(resRaw).not.toBeNull()
89-
console.log(tx)
90-
91-
const bundle = await paymasterProvider.getBundleByUuid(res.BundleUUID)
92-
expect(bundle).not.toBeNull()
93-
console.log(bundle)
94-
95-
const sponsorTx = await paymasterProvider.getSponsorTxByTxHash(tx.TxHash)
96-
console.log('sponsorTx: ', sponsorTx)
97-
expect(sponsorTx).not.toBeNull()
85+
expect(txRaw).not.toBeNull()
86+
console.log('Sponsor transaction details:', tx)
87+
88+
const bundleRaw = await paymasterProvider.getBundleByUuid(res.BundleUUID)
89+
const bundle = transformBundleResponse(bundleRaw)
90+
expect(bundle.BundleUUID).toEqual(res.BundleUUID)
91+
console.log('Bundle details:', bundle)
92+
93+
const sponsorTxRaw = await paymasterProvider.getSponsorTxByTxHash(tx.TxHash)
94+
const sponsorTx = transformSponsorTxResponse(sponsorTxRaw)
95+
console.log('Sponsor transaction:', sponsorTx)
96+
expect(sponsorTx.TxHash).toEqual(tx.TxHash)
9897
}, 13000)
9998
})
10099
})

tests/sponsor.spec.ts

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,79 @@ import {client} from './utils'
33
import {WhitelistType} from '../src'
44
import {POLICY_UUID, ACCOUNT_ADDRESS, CONTRACT_METHOD} from './env'
55

6-
76
/**
8-
* test sponsor apis
7+
* Test suite for Sponsor API methods involving whitelist management and spend data retrieval.
98
*/
10-
119
describe('sponsorQuery', () => {
10+
/**
11+
* Tests adding an account address to the 'From Account' whitelist.
12+
*/
1213
describe('addToWhitelist FromAccountWhitelist', () => {
13-
test('it works', async () => {
14+
test('should add an account address to FromAccountWhitelist successfully', async () => {
1415
const res = await client.addToWhitelist({
1516
PolicyUUID: POLICY_UUID,
1617
WhitelistType: WhitelistType.FromAccountWhitelist,
1718
Values: [ACCOUNT_ADDRESS],
1819
})
1920

2021
expect(res).toEqual(true)
21-
console.log(res)
22+
console.log('FromAccountWhitelist addition response:', res)
2223
})
2324
})
2425

26+
/**
27+
* Tests adding an account address to the 'To Account' whitelist.
28+
*/
2529
describe('addToWhitelist ToAccountWhitelist', () => {
26-
test('it works', async () => {
30+
test('should add an account address to ToAccountWhitelist successfully', async () => {
2731
const res = await client.addToWhitelist({
2832
PolicyUUID: POLICY_UUID,
2933
WhitelistType: WhitelistType.ToAccountWhitelist,
3034
Values: [ACCOUNT_ADDRESS],
3135
})
3236

3337
expect(res).toEqual(true)
34-
console.log(res)
38+
console.log('ToAccountWhitelist addition response:', res)
3539
})
3640
})
41+
42+
/**
43+
* Tests adding an account address to the BEP20 receiver whitelist.
44+
*/
3745
describe('addToWhitelist BEP20ReceiverWhiteList', () => {
38-
test('it works', async () => {
46+
test('should add an account address to BEP20ReceiverWhiteList successfully', async () => {
3947
const res = await client.addToWhitelist({
4048
PolicyUUID: POLICY_UUID,
4149
WhitelistType: WhitelistType.BEP20ReceiverWhiteList,
4250
Values: [ACCOUNT_ADDRESS],
4351
})
4452

4553
expect(res).toEqual(true)
46-
console.log(res)
54+
console.log('BEP20ReceiverWhiteList addition response:', res)
4755
})
4856
})
4957

58+
/**
59+
* Tests adding a contract method signature to the whitelist.
60+
*/
5061
describe('addToWhitelist ContractMethodSigWhitelist', () => {
51-
test('it works', async () => {
62+
test('should add a contract method signature to ContractMethodSigWhitelist successfully', async () => {
5263
const res = await client.addToWhitelist({
5364
PolicyUUID: POLICY_UUID,
5465
WhitelistType: WhitelistType.ContractMethodSigWhitelist,
5566
Values: [CONTRACT_METHOD],
5667
})
5768

5869
expect(res).toEqual(true)
70+
console.log('ContractMethodSigWhitelist addition response:', res)
5971
})
6072
})
6173

74+
/**
75+
* Tests retrieving whitelists of contract method signatures.
76+
*/
6277
describe('getWhitelist', () => {
63-
test('it works', async () => {
78+
test('should retrieve contract method signatures successfully', async () => {
6479
const res = await client.getWhitelist({
6580
PolicyUUID: POLICY_UUID,
6681
WhitelistType: WhitelistType.ContractMethodSigWhitelist,
@@ -69,23 +84,31 @@ describe('sponsorQuery', () => {
6984
})
7085

7186
expect(res[0]).toEqual(CONTRACT_METHOD)
87+
console.log('Retrieved ContractMethodSigWhitelist:', res)
7288
})
7389
})
7490

91+
/**
92+
* Tests removing an account address from a whitelist.
93+
*/
7594
describe('removeFromWhitelist', () => {
76-
test('it works', async () => {
95+
test('should remove an account address from FromAccountWhitelist successfully', async () => {
7796
const res = await client.removeFromWhitelist({
7897
PolicyUUID: POLICY_UUID,
7998
WhitelistType: WhitelistType.FromAccountWhitelist,
8099
Values: [ACCOUNT_ADDRESS],
81100
})
82101

83102
expect(res).toEqual(true)
103+
console.log('FromAccountWhitelist removal response:', res)
84104
})
85105
})
86106

107+
/**
108+
* Tests verifying the removal of an account address from a whitelist.
109+
*/
87110
describe('getWhitelist', () => {
88-
test('it works', async () => {
111+
test('should not contain account address post-removal', async () => {
89112
const res = await client.getWhitelist({
90113
PolicyUUID: POLICY_UUID,
91114
WhitelistType: WhitelistType.FromAccountWhitelist,
@@ -95,22 +118,30 @@ describe('sponsorQuery', () => {
95118
if (res !== null && res !== undefined) {
96119
expect(res).not.toContain(ACCOUNT_ADDRESS)
97120
}
121+
console.log('FromAccountWhitelist post-removal check:', res)
98122
})
99123
})
100124

125+
/**
126+
* Tests clearing all entries from a specific whitelist type.
127+
*/
101128
describe('emptyWhitelist', () => {
102-
test('it works', async () => {
129+
test('should clear all entries from BEP20ReceiverWhiteList successfully', async () => {
103130
const res = await client.emptyWhitelist({
104131
PolicyUUID: POLICY_UUID,
105132
WhitelistType: WhitelistType.BEP20ReceiverWhiteList,
106133
})
107134

108135
expect(res).toEqual(true)
136+
console.log('BEP20ReceiverWhiteList clearance response:', res)
109137
})
110138
})
111139

140+
/**
141+
* Tests verifying the emptiness of a whitelist.
142+
*/
112143
describe('getWhitelist', () => {
113-
test('it works', async () => {
144+
test('should confirm the whitelist is empty', async () => {
114145
const res = await client.getWhitelist({
115146
PolicyUUID: POLICY_UUID,
116147
WhitelistType: WhitelistType.BEP20ReceiverWhiteList,
@@ -119,34 +150,46 @@ describe('sponsorQuery', () => {
119150
})
120151

121152
expect(res).toBeNull()
153+
console.log('BEP20ReceiverWhiteList emptiness check:', res)
122154
})
123155
})
124156

157+
/**
158+
* Tests retrieving user spend data.
159+
*/
125160
describe('getUserSpendData', () => {
126-
test('it works', async () => {
161+
test('should return null for spend data when user has none', async () => {
127162
const res = await client.getUserSpendData(ACCOUNT_ADDRESS, POLICY_UUID)
128163

129164
expect(res).toBeNull()
165+
console.log('User spend data:', res)
130166
})
131167
})
132168

169+
/**
170+
* Tests retrieving policy spend data.
171+
*/
133172
describe('getPolicySpendData', () => {
134-
test('it works', async () => {
173+
test('should retrieve policy spend data successfully', async () => {
135174
const res = await client.getPolicySpendData(POLICY_UUID)
136175
expect(res.ChainID).not.toBeNull()
176+
console.log('Policy spend data:', res)
137177
})
138178
})
139179

180+
/**
181+
* Tests re-adding an account address to the 'From Account' whitelist after previous tests.
182+
*/
140183
describe('addToWhitelist FromAccountWhitelist', () => {
141-
test('it works', async () => {
184+
test('should re-add an account address to FromAccountWhitelist successfully after removal', async () => {
142185
const res = await client.addToWhitelist({
143186
PolicyUUID: POLICY_UUID,
144187
WhitelistType: WhitelistType.FromAccountWhitelist,
145188
Values: [ACCOUNT_ADDRESS],
146189
})
147190

148191
expect(res).toEqual(true)
149-
console.log(res)
192+
console.log('Re-addition to FromAccountWhitelist response:', res)
150193
})
151194
})
152195
})

0 commit comments

Comments
 (0)