aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorGreg Hysen <greg.hysen@gmail.com>2018-11-08 06:18:05 +0800
committerGreg Hysen <greg.hysen@gmail.com>2018-11-29 08:38:10 +0800
commit7de7fe782398eded47598046699fdc0915597e33 (patch)
tree27c76041be49fc869fa6d432dc4ae18347ed7d54 /packages
parent5053c19599e96b9886e6b36173350162b92405a3 (diff)
downloaddexon-sol-tools-7de7fe782398eded47598046699fdc0915597e33.tar
dexon-sol-tools-7de7fe782398eded47598046699fdc0915597e33.tar.gz
dexon-sol-tools-7de7fe782398eded47598046699fdc0915597e33.tar.bz2
dexon-sol-tools-7de7fe782398eded47598046699fdc0915597e33.tar.lz
dexon-sol-tools-7de7fe782398eded47598046699fdc0915597e33.tar.xz
dexon-sol-tools-7de7fe782398eded47598046699fdc0915597e33.tar.zst
dexon-sol-tools-7de7fe782398eded47598046699fdc0915597e33.zip
Array of basic types works
Diffstat (limited to 'packages')
-rw-r--r--packages/order-utils/test/abi_encoder_test.ts53
1 files changed, 48 insertions, 5 deletions
diff --git a/packages/order-utils/test/abi_encoder_test.ts b/packages/order-utils/test/abi_encoder_test.ts
index bed75b935..a455e6a00 100644
--- a/packages/order-utils/test/abi_encoder_test.ts
+++ b/packages/order-utils/test/abi_encoder_test.ts
@@ -612,14 +612,16 @@ namespace AbiEncoder {
return;
}
- // Parse out array type and length
+ // Parse out array type/length and construct children
this.type = matches[1];
this.length = new BigNumber(matches[2], 10);
if (this.length.lessThan(0)) {
throw new Error(`Bad array length: ${JSON.stringify(this.length)}`);
}
+ this.constructChildren();
+ }
- // Construct children
+ private constructChildren() {
for (let idx = new BigNumber(0); idx.lessThan(this.length); idx = idx.plus(1)) {
const childDataItem = {
type: this.type,
@@ -631,8 +633,43 @@ namespace AbiEncoder {
}
public assignValue(value: any[]) {
- //const hexValue = ethUtil.bufferToHex(new Buffer(value));
- //this.assignHexValue(hexValue);
+ // Sanity check length
+ const valueLength = new BigNumber(value.length);
+ if (this.length !== SolArray.UNDEFINED_LENGTH && valueLength.equals(this.length) === false) {
+ throw new Error(
+ `Expected array of length ${JSON.stringify(this.length)}, but got array of length ${JSON.stringify(
+ valueLength,
+ )}`,
+ );
+ }
+
+ // Assign length if not already set
+ if (this.length === SolArray.UNDEFINED_LENGTH) {
+ this.length = valueLength;
+ this.constructChildren();
+ }
+
+ // Assign values to children
+ for (let idx = new BigNumber(0); idx.lessThan(this.length); idx = idx.plus(1)) {
+ const idxNumber = idx.toNumber();
+ this.children[idxNumber].assignValue(value[idxNumber]);
+ }
+ }
+
+ public getHexValue(): string {
+ const lengthBufUnpadded = ethUtil.toBuffer(`0x${this.length.toString(16)}`);
+ const lengthBuf = ethUtil.setLengthLeft(lengthBufUnpadded, 32);
+ let valueBuf = lengthBuf;
+ for (let idx = new BigNumber(0); idx.lessThan(this.length); idx = idx.plus(1)) {
+ const idxNumber = idx.toNumber();
+ const childValueHex = this.children[idxNumber].getHexValue();
+ const childValueBuf = ethUtil.toBuffer(childValueHex);
+ valueBuf = Buffer.concat([valueBuf, childValueBuf]);
+ }
+
+ // Convert value buffer to hex
+ const valueHex = ethUtil.bufferToHex(valueBuf);
+ return valueHex;
}
public static matchGrammar(type: string): boolean {
@@ -839,9 +876,15 @@ namespace AbiEncoder {
describe.only('ABI Encoder', () => {
describe.only('Array', () => {
it('sample', async () => {
- const testDataItem = { name: 'testArray', type: 'string[2]' };
+ const testDataItem = { name: 'testArray', type: 'int[2]' };
const dataType = new AbiEncoder.SolArray(testDataItem);
console.log(JSON.stringify(dataType, null, 4));
+ console.log('*'.repeat(60));
+ dataType.assignValue([new BigNumber(5), new BigNumber(6)]);
+ console.log(JSON.stringify(dataType, null, 4));
+ const hexValue = dataType.getHexValue();
+ console.log('*'.repeat(60));
+ console.log(hexValue);
});
});