aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils
diff options
context:
space:
mode:
authorGreg Hysen <greg.hysen@gmail.com>2018-11-13 09:28:46 +0800
committerGreg Hysen <greg.hysen@gmail.com>2018-11-29 08:38:10 +0800
commit2a6b358717e3134aef3ff86cc5343b3b37edee7e (patch)
treeeff25778a8932d0c2c45b3b9fbbf477e0d511b20 /packages/order-utils
parentc5d252ba4a79a0f16e053a071b0173a7bd666328 (diff)
downloaddexon-sol-tools-2a6b358717e3134aef3ff86cc5343b3b37edee7e.tar
dexon-sol-tools-2a6b358717e3134aef3ff86cc5343b3b37edee7e.tar.gz
dexon-sol-tools-2a6b358717e3134aef3ff86cc5343b3b37edee7e.tar.bz2
dexon-sol-tools-2a6b358717e3134aef3ff86cc5343b3b37edee7e.tar.lz
dexon-sol-tools-2a6b358717e3134aef3ff86cc5343b3b37edee7e.tar.xz
dexon-sol-tools-2a6b358717e3134aef3ff86cc5343b3b37edee7e.tar.zst
dexon-sol-tools-2a6b358717e3134aef3ff86cc5343b3b37edee7e.zip
Decoder works for uints with width less than 256
Diffstat (limited to 'packages/order-utils')
-rw-r--r--packages/order-utils/test/abi/evm_data_types.ts24
1 files changed, 12 insertions, 12 deletions
diff --git a/packages/order-utils/test/abi/evm_data_types.ts b/packages/order-utils/test/abi/evm_data_types.ts
index a05c29b28..abb4fba14 100644
--- a/packages/order-utils/test/abi/evm_data_types.ts
+++ b/packages/order-utils/test/abi/evm_data_types.ts
@@ -146,34 +146,34 @@ abstract class Number extends PayloadDataType {
}
public decodeValue(calldata: RawCalldata): BigNumber {
- const decodedValueBuf = calldata.popWord();
- const decodedValueHex = ethUtil.bufferToHex(decodedValueBuf);
- let decodedValue = new BigNumber(decodedValueHex, 16);
+ const paddedValueBuf = calldata.popWord();
+ const paddedValueHex = ethUtil.bufferToHex(paddedValueBuf);
+ let value = new BigNumber(paddedValueHex, 16);
if (this instanceof Int) {
// Check if we're negative
const binBase = 2;
- const decodedValueBin = decodedValue.toString(binBase);
- if (decodedValueBin[0] === '1') {
+ const paddedValueBin = value.toString(binBase);
+ const valueBin = paddedValueBin.slice(paddedValueBin.length - this.width);
+ if (valueBin[0].startsWith('1')) {
// Negative
// Step 1/3: Invert binary value
- const bitsInEvmWord = 256;
- let invertedValueBin = '1'.repeat(bitsInEvmWord - decodedValueBin.length);
- _.each(decodedValueBin, (bit: string) => {
+ let invertedValueBin = '';
+ _.each(valueBin, (bit: string) => {
invertedValueBin += bit === '1' ? '0' : '1';
});
const invertedValue = new BigNumber(invertedValueBin, binBase);
// Step 2/3: Add 1 to inverted value
// The result is the two's-complement represent of the input value.
- const positiveDecodedValue = invertedValue.plus(binBase);
+ const positiveValue = invertedValue.plus(1);
// Step 3/3: Invert positive value
- const negativeDecodedValue = positiveDecodedValue.times(-1);
- decodedValue = negativeDecodedValue;
+ const negativeValue = positiveValue.times(-1);
+ value = negativeValue;
}
}
- return decodedValue;
+ return value;
}
public abstract getMaxValue(): BigNumber;