Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"rimraf": "^2.6.2",
"ts-jest": "^23.0.0",
"tslint": "^5.10.0",
"tslint-config-prettier": "^1.14.0",
"tslint-config-semistandard": "^7.0.0",
"typedoc": "^0.12.0",
"typedoc-plugin-markdown": "^1.1.13",
Expand Down
34 changes: 24 additions & 10 deletions packages/abi/src/decoder/decoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import { padU32 } from '../util/pad';

describe('decoder/Decoder', () => {
const stringToBytes = function (str: string) {
return str.match(/.{1,2}/g).map(code => parseInt(code, 16));
const matches = str.match(/.{1,2}/g);
if (!matches) {
throw new Error('stringToBytes: mo matches');
}
return matches.map(code => parseInt(code, 16));
};

const address1 =
Expand Down Expand Up @@ -118,9 +122,9 @@ describe('decoder/Decoder', () => {

describe('decodeParam', () => {
it('throws an error on non ParamType param', () => {
expect(() => Decoder.decodeParam({} as ParamType, undefined, undefined)).toThrow(
/ParamType/
);
expect(() =>
Decoder.decodeParam({} as ParamType, undefined, undefined)
).toThrow(/ParamType/);
});

it('throws an error on invalid param type', () => {
Expand Down Expand Up @@ -172,8 +176,11 @@ describe('decoder/Decoder', () => {

it('decodes fixedBytes', () => {
expect(
Decoder.decodeParam(new ParamType('fixedBytes', null, 2), [bytes1], 0)
.token
Decoder.decodeParam(
new ParamType('fixedBytes', undefined, 2),
[bytes1],
0
).token
).toEqual(tokenFixedBytes1);
});

Expand Down Expand Up @@ -209,10 +216,14 @@ describe('decoder/Decoder', () => {

it('decodes string (indexed)', () => {
expect(
Decoder.decodeParam(new ParamType('string', null, 0, true), [bytes1], 0)
Decoder.decodeParam(
new ParamType('string', undefined, 0, true),
[bytes1],
0
)
).toEqual(
Decoder.decodeParam(
new ParamType('fixedBytes', null, 32, true),
new ParamType('fixedBytes', undefined, 32, true),
[bytes1],
0
)
Expand All @@ -222,7 +233,7 @@ describe('decoder/Decoder', () => {

describe('decode', () => {
it('throws an error on invalid params', () => {
expect(() => Decoder.decode(null, '123')).toThrow(/array/);
expect(() => Decoder.decode(undefined, '123')).toThrow(/array/);
});

describe('address', () => {
Expand Down Expand Up @@ -300,7 +311,10 @@ describe('decoder/Decoder', () => {
describe('fixedBytes', () => {
it('decodes fixedBytes', () => {
expect(
Decoder.decode([new ParamType('fixedBytes', null, 2)], `${bytes1}`)
Decoder.decode(
[new ParamType('fixedBytes', undefined, 2)],
`${bytes1}`
)
).toEqual([tokenFixedBytes1]);
});
});
Expand Down
40 changes: 26 additions & 14 deletions packages/abi/src/decoder/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import ParamType from '../spec/paramType/paramType';
import { sliceData } from '../util/slice';
import { asAddress, asBool, asI32, asU32 } from '../util/sliceAs';
import { isArray, isInstanceOf } from '../util/types';
import { TokenValue } from '../types';
import { Slices } from '../types';

const NULL = '0000000000000000000000000000000000000000000000000000000000000000';

class Decoder {
static decode (params: ParamType[], data: string) {
static decode (params: ParamType[] | undefined, data: string) {
if (!isArray(params)) {
throw new Error('Parameters should be array of ParamType');
}
Expand All @@ -33,15 +33,15 @@ class Decoder {
});
}

static peek (slices: string[], position: number) {
static peek (slices: Slices, position: number) {
if (!slices || !slices[position]) {
return NULL;
}

return slices[position];
}

static takeBytes (slices: string[], position: number, length: number) {
static takeBytes (slices: Slices, position: number, length: number) {
const slicesLength = Math.floor((length + 31) / 32);
let bytesStr = '';

Expand All @@ -58,8 +58,8 @@ class Decoder {

static decodeParam (
param: ParamType,
slices: string[],
offset: number
slices: Slices,
offset: number = 0
): DecodeResult {
if (!isInstanceOf(param, ParamType)) {
throw new Error('param should be instanceof ParamType');
Expand Down Expand Up @@ -96,24 +96,24 @@ class Decoder {
offset + 1
);

case 'fixedBytes':
case 'fixedBytes': {
taken = Decoder.takeBytes(slices, offset, param.length);

return new DecodeResult(
new Token(param.type, taken.bytes),
taken.newOffset
);

case 'bytes':
}
case 'bytes': {
lengthOffset = asU32(Decoder.peek(slices, offset))
.div(32)
.toNumber();
length = asU32(Decoder.peek(slices, lengthOffset)).toNumber();
taken = Decoder.takeBytes(slices, lengthOffset + 1, length);

return new DecodeResult(new Token(param.type, taken.bytes), offset + 1);

case 'string':
}
case 'string': {
if (param.indexed) {
taken = Decoder.takeBytes(slices, offset, 32);

Expand Down Expand Up @@ -142,8 +142,14 @@ class Decoder {
}

return new DecodeResult(new Token(param.type, decoded), offset + 1);
}
case 'array': {
if (!param.subtype) {
throw new Error(
`decodeParam: param of type '${param.type}' must have a subtype`
);
}

case 'array':
lengthOffset = asU32(Decoder.peek(slices, offset))
.div(32)
.toNumber();
Expand All @@ -158,8 +164,14 @@ class Decoder {
}

return new DecodeResult(new Token(param.type, tokens), offset + 1);
}
case 'fixedArray': {
if (!param.subtype) {
throw new Error(
`decodeParam: param of type '${param.type}' must have a subtype`
);
}

case 'fixedArray':
newOffset = offset;

for (let index = 0; index < param.length; index++) {
Expand All @@ -170,7 +182,7 @@ class Decoder {
}

return new DecodeResult(new Token(param.type, tokens), newOffset);

}
default:
throw new Error(`Invalid param type ${param.type} in decodeParam`);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/abi/src/encoder/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '../types';

class Encoder {
static encode (tokens: Token[]) {
static encode (tokens?: Token[]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here

if (!isArray(tokens)) {
throw new Error('tokens should be array of Token');
}
Expand All @@ -44,8 +44,8 @@ class Encoder {
return `${inits}${closings}`;
}

static encodeToken (token: Token, index = 0): Mediate {
if (!isInstanceOf(token, Token)) {
static encodeToken (token?: Token, index = 0): Mediate {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason we're using the token?: Token syntax to define an optional parameter here, and we're using the ParamType[] | undefined syntax in the other file?

also out of curiosity, why are we setting the first parameter of encode and encodeToken as optional if they really are required?

if (!token || !isInstanceOf(token, Token)) {
throw new Error('token should be instanceof Token');
}

Expand Down
2 changes: 1 addition & 1 deletion packages/abi/src/spec/event/decodedLogParam.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('spec/event/DecodedLogParam', () => {

it('disallows kind not instanceof ParamType', () => {
expect(
() => new DecodedLogParam('test', 'param' as any, undefined)
() => new DecodedLogParam('test', 'param' as any, undefined as any)
).toThrow(/ParamType/);
});

Expand Down
10 changes: 5 additions & 5 deletions packages/abi/src/spec/event/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('spec/event/Event', () => {
it('returns all the types', () => {
expect(event.inputParamTypes()).toEqual([
new ParamType('bool'),
new ParamType('uint', null, 256, true)
new ParamType('uint', undefined, 256, true)
]);
});
});
Expand Down Expand Up @@ -98,12 +98,12 @@ describe('spec/event/Event', () => {
expect(decoded.params).toEqual([
new DecodedLogParam(
'a',
new ParamType('int', null, 256),
new ParamType('int', undefined, 256),
new Token('int', new BigNumber(3))
),
new DecodedLogParam(
'b',
new ParamType('int', null, 256, true),
new ParamType('int', undefined, 256, true),
new Token('int', new BigNumber(2))
),
new DecodedLogParam(
Expand All @@ -113,7 +113,7 @@ describe('spec/event/Event', () => {
),
new DecodedLogParam(
'd',
new ParamType('address', null, 0, true),
new ParamType('address', undefined, 0, true),
new Token('address', '0x1111111111111111111111111111111111111111')
)
]);
Expand All @@ -134,7 +134,7 @@ describe('spec/event/Event', () => {
expect(decoded.params).toEqual([
new DecodedLogParam(
'a',
new ParamType('int', null, 256),
new ParamType('int', undefined, 256),
new Token('int', new BigNumber(3))
)
]);
Expand Down
10 changes: 9 additions & 1 deletion packages/abi/src/spec/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class Event {
this.inputParamTypes()
);

if (!name) {
throw new Error(
`Event constructor: abi item does not have a name: ${JSON.stringify(
abi
)}`
);
}

this._id = id;
this._name = name;
this._signature = signature;
Expand Down Expand Up @@ -69,7 +77,7 @@ class Event {
const topicParams = this.indexedParams(true);
const dataParams = this.indexedParams(false);

let address;
let address = '';
let toSkip: number;

if (!this.anonymous) {
Expand Down
10 changes: 9 additions & 1 deletion packages/abi/src/spec/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Func {
constructor (abi: AbiItem) {
this._abi = abi;
this._constant = !!abi.constant;
this._payable = abi.payable;
this._payable = abi.payable || false;
this._inputs = Param.toParams(abi.inputs || []);
this._outputs = Param.toParams(abi.outputs || []);

Expand All @@ -32,6 +32,14 @@ class Func {
this.inputParamTypes()
);

if (!name) {
throw new Error(
`Event constructor: abi item does not have a name: ${JSON.stringify(
abi
)}`
);
}

this._id = id;
this._name = name;
this._signature = signature;
Expand Down
2 changes: 1 addition & 1 deletion packages/abi/src/spec/param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// SPDX-License-Identifier: MIT

import { AbiInput, TokenTypeEnum } from '../types';
import { AbiInput } from '../types';
import ParamType from './paramType';
import { toParamType } from './paramType/format';

Expand Down
Loading