diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-21 04:58:49 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:11 +0800 |
commit | e6ab6f38bacdec90c960ff1db4781d161b1f4103 (patch) | |
tree | 7edec03e3f249edf49b9deafcc44416bf25f7dc0 /packages/utils/src/abi_encoder/evm_data_types/bool.ts | |
parent | 5934e5a57b5897594e22efc9e6ff28841c6951bf (diff) | |
download | dexon-sol-tools-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar dexon-sol-tools-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.gz dexon-sol-tools-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.bz2 dexon-sol-tools-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.lz dexon-sol-tools-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.xz dexon-sol-tools-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.zst dexon-sol-tools-e6ab6f38bacdec90c960ff1db4781d161b1f4103.zip |
Split EVM data types and factory into separate files
Diffstat (limited to 'packages/utils/src/abi_encoder/evm_data_types/bool.ts')
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/bool.ts | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts new file mode 100644 index 000000000..aee2727c7 --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -0,0 +1,50 @@ +/* tslint:disable prefer-function-over-method */ +import { DataItem } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { BigNumber } from '../../configured_bignumber'; +import { RawCalldata } from '../calldata'; +import * as Constants from '../constants'; +import { DataTypeFactory, PayloadDataType } from '../data_type'; + +export class Bool extends PayloadDataType { + private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; + + public static matchType(type: string): boolean { + return type === 'bool'; + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + super(dataItem, dataTypeFactory, Bool._SIZE_KNOWN_AT_COMPILE_TIME); + if (!Bool.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate Bool with bad input: ${dataItem}`); + } + } + + public getSignature(): string { + return 'bool'; + } + + public encodeValue(value: boolean): Buffer { + const encodedValue = value ? '0x1' : '0x0'; + const encodedValueBuf = ethUtil.setLengthLeft( + ethUtil.toBuffer(encodedValue), + Constants.EVM_WORD_WIDTH_IN_BYTES, + ); + return encodedValueBuf; + } + + public decodeValue(calldata: RawCalldata): boolean { + const valueBuf = calldata.popWord(); + const valueHex = ethUtil.bufferToHex(valueBuf); + const valueNumber = new BigNumber(valueHex, Constants.HEX_BASE); + if (!(valueNumber.equals(0) || valueNumber.equals(1))) { + throw new Error(`Failed to decode boolean. Expected 0x0 or 0x1, got ${valueHex}`); + } + /* tslint:disable boolean-naming */ + const value: boolean = valueNumber.equals(0) ? false : true; + /* tslint:enable boolean-naming */ + return value; + } +} |