-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check tx gas against block gas limit
- Loading branch information
1 parent
f5e5dfd
commit 269e182
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/_tests/unit/qihdwallet-check-gas-limit.unit.test.ts
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,110 @@ | ||
import assert from 'assert'; | ||
import { QiHDWallet } from '../../wallet/qi-hdwallet.js'; | ||
import { Mnemonic } from '../../wallet/mnemonic.js'; | ||
import { Zone } from '../../constants/zones.js'; | ||
import { MockProvider } from './mockProvider.js'; | ||
import { QiTransaction } from '../../transaction/index.js'; | ||
import { Block } from '../../providers/index.js'; | ||
|
||
class TestQiHDWallet extends QiHDWallet { | ||
public async checkGasLimit(tx: QiTransaction, zone: Zone): Promise<boolean> { | ||
return this['_checkGasLimit'](tx, zone); | ||
} | ||
} | ||
|
||
interface GasLimitTestCase { | ||
name: string; | ||
mnemonic: string; | ||
zone: Zone; | ||
blockGasLimit: bigint; | ||
estimatedGas: bigint; | ||
expectedResult: boolean; | ||
} | ||
|
||
const testMnemonic = 'test test test test test test test test test test test junk'; | ||
|
||
describe('QiHDWallet: Gas Limit Tests', () => { | ||
const testCases: GasLimitTestCase[] = [ | ||
{ | ||
name: 'Gas limit is sufficient', | ||
mnemonic: testMnemonic, | ||
zone: Zone.Cyprus1, | ||
blockGasLimit: BigInt(30000), | ||
estimatedGas: BigInt(21000), | ||
expectedResult: true, | ||
}, | ||
{ | ||
name: 'Gas limit is insufficient', | ||
mnemonic: testMnemonic, | ||
zone: Zone.Cyprus1, | ||
blockGasLimit: BigInt(20000), | ||
estimatedGas: BigInt(21000), | ||
expectedResult: false, | ||
}, | ||
{ | ||
name: 'Gas limit equals estimated gas', | ||
mnemonic: testMnemonic, | ||
zone: Zone.Cyprus1, | ||
blockGasLimit: BigInt(21000), | ||
estimatedGas: BigInt(21000), | ||
expectedResult: true, | ||
}, | ||
]; | ||
|
||
testCases.forEach((testCase) => { | ||
it(testCase.name, async () => { | ||
const mnemonic = Mnemonic.fromPhrase(testCase.mnemonic); | ||
const wallet = TestQiHDWallet.fromMnemonic(mnemonic); | ||
|
||
const mockProvider = new MockProvider({ network: BigInt(1) }); | ||
|
||
mockProvider.getBlock = async () => { | ||
return { | ||
header: { | ||
gasLimit: testCase.blockGasLimit, | ||
}, | ||
} as Block; | ||
}; | ||
|
||
mockProvider.estimateGas = async () => { | ||
return testCase.estimatedGas; | ||
}; | ||
|
||
wallet.connect(mockProvider); | ||
|
||
const tx = new QiTransaction(); | ||
|
||
const result = await wallet.checkGasLimit(tx, testCase.zone); | ||
assert.equal( | ||
result, | ||
testCase.expectedResult, | ||
`Expected gas limit check to return ${testCase.expectedResult} but got ${result}`, | ||
); | ||
}); | ||
}); | ||
|
||
it('should throw error when provider is not set', async () => { | ||
const mnemonic = Mnemonic.fromPhrase(testMnemonic); | ||
const wallet = TestQiHDWallet.fromMnemonic(mnemonic); | ||
const tx = new QiTransaction(); | ||
|
||
await assert.rejects(async () => await wallet.checkGasLimit(tx, Zone.Cyprus1), { | ||
message: 'Provider is not set', | ||
}); | ||
}); | ||
|
||
it('should throw error when block cannot be retrieved', async () => { | ||
const mnemonic = Mnemonic.fromPhrase(testMnemonic); | ||
const wallet = TestQiHDWallet.fromMnemonic(mnemonic); | ||
const mockProvider = new MockProvider({ network: BigInt(1) }); | ||
|
||
mockProvider.getBlock = async () => null; | ||
|
||
wallet.connect(mockProvider); | ||
const tx = new QiTransaction(); | ||
|
||
await assert.rejects(async () => await wallet.checkGasLimit(tx, Zone.Cyprus1), { | ||
message: 'Failed to get the current block', | ||
}); | ||
}); | ||
}); |
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