mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
tx-utils - add encoding utils
This commit is contained in:
parent
c063fab993
commit
92b8443824
@ -91,7 +91,7 @@ module.exports = class txProviderUtils {
|
|||||||
// builds ethTx from txParams object
|
// builds ethTx from txParams object
|
||||||
buildEthTxFromParams (txParams) {
|
buildEthTxFromParams (txParams) {
|
||||||
// apply gas multiplyer
|
// apply gas multiplyer
|
||||||
let gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice), 16)
|
let gasPrice = hexToBn(txParams.gasPrice)
|
||||||
// multiply and divide by 100 so as to add percision to integer mul
|
// multiply and divide by 100 so as to add percision to integer mul
|
||||||
txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber())
|
txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber())
|
||||||
// normalize values
|
// normalize values
|
||||||
|
@ -19,8 +19,8 @@ describe('txUtils', function() {
|
|||||||
// dummy gas limit: 0x3d4c52 (4 mil)
|
// dummy gas limit: 0x3d4c52 (4 mil)
|
||||||
const blockGasLimitHex = '0x3d4c52'
|
const blockGasLimitHex = '0x3d4c52'
|
||||||
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
||||||
const inputBn = new BN(ethUtil.stripHexPrefix(inputHex), 'hex')
|
const inputBn = hexToBn(inputHex)
|
||||||
const outputBn = new BN(ethUtil.stripHexPrefix(output), 'hex')
|
const outputBn = hexToBn(output)
|
||||||
const expectedBn = inputBn.muln(1.5)
|
const expectedBn = inputBn.muln(1.5)
|
||||||
assert(outputBn.eq(expectedBn), 'returns 1.5 the input value')
|
assert(outputBn.eq(expectedBn), 'returns 1.5 the input value')
|
||||||
})
|
})
|
||||||
@ -31,9 +31,9 @@ describe('txUtils', function() {
|
|||||||
// dummy gas limit: 0x0f4240 (1 mil)
|
// dummy gas limit: 0x0f4240 (1 mil)
|
||||||
const blockGasLimitHex = '0x0f4240'
|
const blockGasLimitHex = '0x0f4240'
|
||||||
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
||||||
const inputBn = new BN(ethUtil.stripHexPrefix(inputHex), 'hex')
|
const inputBn = hexToBn(inputHex)
|
||||||
const outputBn = new BN(ethUtil.stripHexPrefix(output), 'hex')
|
const outputBn = hexToBn(output)
|
||||||
const expectedBn = new BN(ethUtil.stripHexPrefix(inputHex), 'hex')
|
const expectedBn = hexToBn(inputHex)
|
||||||
assert(outputBn.eq(expectedBn), 'returns the original estimatedGas value')
|
assert(outputBn.eq(expectedBn), 'returns the original estimatedGas value')
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -43,10 +43,16 @@ describe('txUtils', function() {
|
|||||||
// dummy gas limit: 0x1e8480 (2 mil)
|
// dummy gas limit: 0x1e8480 (2 mil)
|
||||||
const blockGasLimitHex = '0x1e8480'
|
const blockGasLimitHex = '0x1e8480'
|
||||||
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
||||||
const inputBn = new BN(ethUtil.stripHexPrefix(inputHex), 'hex')
|
const inputBn = hexToBn(inputHex)
|
||||||
const outputBn = new BN(ethUtil.stripHexPrefix(output), 'hex')
|
const outputBn = hexToBn(output)
|
||||||
const expectedBn = new BN(ethUtil.stripHexPrefix(blockGasLimitHex), 'hex')
|
const expectedBn = hexToBn(blockGasLimitHex)
|
||||||
assert(outputBn.eq(expectedBn), 'returns the block gas limit value')
|
assert(outputBn.eq(expectedBn), 'returns the block gas limit value')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// util
|
||||||
|
|
||||||
|
function hexToBn(inputHex) {
|
||||||
|
return new BN(ethUtil.stripHexPrefix(inputHex), 16)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user