Skip to content

Commit

Permalink
refactor: fix types and add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
moraleinside committed Aug 17, 2022
1 parent 7e99408 commit 7b36a6c
Show file tree
Hide file tree
Showing 27 changed files with 1,212 additions and 111 deletions.
5 changes: 3 additions & 2 deletions build/writePkgJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ function copyFile({ fromPath, name }) {
fs.writeFileSync(packageFile, `${ JSON.stringify(packageJsonContent, null, 4) }\n`);

const tsConfigFile = path.join(fromPath, './tsconfig.json');
delete currTsConfigJsonContent.compilerOptions.baseUrl;
delete currTsConfigJsonContent.compilerOptions.paths;
currTsConfigJsonContent.compilerOptions.baseUrl = '../';
// delete currTsConfigJsonContent.compilerOptions.baseUrl;
// delete currTsConfigJsonContent.compilerOptions.paths;
delete currTsConfigJsonContent.compilerOptions.outDir;
delete currTsConfigJsonContent.include;
delete currTsConfigJsonContent.exclude;
Expand Down
57 changes: 57 additions & 0 deletions src/HTTP/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,63 @@
"sourceMap": false,
"moduleResolution": "node",
"resolveJsonModule": true,
"baseUrl": "../",
"paths": {
"~@vite/vitejs-abi": [
"abi/index.ts"
],
"~@vite/vitejs-abi/*": [
"abi/*"
],
"~@vite/vitejs-accountblock": [
"accountBlock/index.ts"
],
"~@vite/vitejs-accountblock/*": [
"accountBlock/*"
],
"~@vite/vitejs-communication": [
"communication/index.js"
],
"~@vite/vitejs-communication/*": [
"communication/*"
],
"~@vite/vitejs-constant": [
"constant/index.ts"
],
"~@vite/vitejs-error": [
"error/index.ts"
],
"~@vite/vitejs-keystore": [
"keystore/index.ts"
],
"~@vite/vitejs-viteapi": [
"viteAPI/index.ts"
],
"~@vite/vitejs-viteapi/*": [
"viteAPI/*"
],
"~@vite/vitejs-wallet": [
"wallet/index.ts"
],
"~@vite/vitejs-wallet/*": [
"wallet/*"
],
"~@vite/vitejs-utils": [
"utils/index.ts"
],
"~@vite/vitejs-ws": [
"WS/index.js"
],
"~@vite/vitejs-http": [
"HTTP/index.js"
],
"~@vite/vitejs-ipc": [
"IPC/index.js"
],
"~@vite/vitejs": [
"vitejs/index.ts"
]
},
"lib": [
"es5",
"es2017",
Expand Down
57 changes: 57 additions & 0 deletions src/IPC/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,63 @@
"sourceMap": false,
"moduleResolution": "node",
"resolveJsonModule": true,
"baseUrl": "../",
"paths": {
"~@vite/vitejs-abi": [
"abi/index.ts"
],
"~@vite/vitejs-abi/*": [
"abi/*"
],
"~@vite/vitejs-accountblock": [
"accountBlock/index.ts"
],
"~@vite/vitejs-accountblock/*": [
"accountBlock/*"
],
"~@vite/vitejs-communication": [
"communication/index.js"
],
"~@vite/vitejs-communication/*": [
"communication/*"
],
"~@vite/vitejs-constant": [
"constant/index.ts"
],
"~@vite/vitejs-error": [
"error/index.ts"
],
"~@vite/vitejs-keystore": [
"keystore/index.ts"
],
"~@vite/vitejs-viteapi": [
"viteAPI/index.ts"
],
"~@vite/vitejs-viteapi/*": [
"viteAPI/*"
],
"~@vite/vitejs-wallet": [
"wallet/index.ts"
],
"~@vite/vitejs-wallet/*": [
"wallet/*"
],
"~@vite/vitejs-utils": [
"utils/index.ts"
],
"~@vite/vitejs-ws": [
"WS/index.js"
],
"~@vite/vitejs-http": [
"HTTP/index.js"
],
"~@vite/vitejs-ipc": [
"IPC/index.js"
],
"~@vite/vitejs": [
"vitejs/index.ts"
]
},
"lib": [
"es5",
"es2017",
Expand Down
57 changes: 57 additions & 0 deletions src/WS/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,63 @@
"sourceMap": false,
"moduleResolution": "node",
"resolveJsonModule": true,
"baseUrl": "../",
"paths": {
"~@vite/vitejs-abi": [
"abi/index.ts"
],
"~@vite/vitejs-abi/*": [
"abi/*"
],
"~@vite/vitejs-accountblock": [
"accountBlock/index.ts"
],
"~@vite/vitejs-accountblock/*": [
"accountBlock/*"
],
"~@vite/vitejs-communication": [
"communication/index.js"
],
"~@vite/vitejs-communication/*": [
"communication/*"
],
"~@vite/vitejs-constant": [
"constant/index.ts"
],
"~@vite/vitejs-error": [
"error/index.ts"
],
"~@vite/vitejs-keystore": [
"keystore/index.ts"
],
"~@vite/vitejs-viteapi": [
"viteAPI/index.ts"
],
"~@vite/vitejs-viteapi/*": [
"viteAPI/*"
],
"~@vite/vitejs-wallet": [
"wallet/index.ts"
],
"~@vite/vitejs-wallet/*": [
"wallet/*"
],
"~@vite/vitejs-utils": [
"utils/index.ts"
],
"~@vite/vitejs-ws": [
"WS/index.js"
],
"~@vite/vitejs-http": [
"HTTP/index.js"
],
"~@vite/vitejs-ipc": [
"IPC/index.js"
],
"~@vite/vitejs": [
"vitejs/index.ts"
]
},
"lib": [
"es5",
"es2017",
Expand Down
12 changes: 11 additions & 1 deletion src/abi/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,12 @@ export class Abi {
if (!isObject(value)) {
throw new Error(`type error, expect object but got ${ typeof value }`);
}
const result = param.components.reduce((accum, item) => accum.concat(encodeTopic(item, value[item.name], true)), '');
const result = param.components.reduce((accum, item) => {
if (value[item.name] === undefined) {
throw new Error(`missing tuple property: ${ item.name }`);
}
return accum.concat(encodeTopic(item, value[item.name], true));
}, '');
if (_isSubElement) {
return result;
}
Expand All @@ -439,6 +444,11 @@ export class Abi {
};
// only get indexed inputs
const _inputs = _fragment.inputs.filter(item => item.indexed);

if (values.length > _inputs.length) {
throw new Error(`too many indexed arguments for ${ _fragment.format() }: ${ values }`);
}

values.forEach((value, index) => {
const param = _inputs[index];

Expand Down
6 changes: 3 additions & 3 deletions src/abi/coder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export abstract class Coder {
arrayValues = coders.map(coder => {
const name = coder.localName;
if (!name) {
throw new Error(`cannot encode object for signature with missing names: ${ coder }`);
throw new Error(`cannot encode object for signature with missing names: ${ coder.type }`);
}

if (unique[name]) {
throw new Error(`cannot encode object for signature with duplicate names: ${ coder }`);
throw new Error(`cannot encode object for signature with duplicate names: ${ name }`);
}

unique[name] = true;
Expand Down Expand Up @@ -287,7 +287,7 @@ export class Reader {
if (this.allowLoose && loose && this._offset + length <= this._data.length) {
alignedLength = length;
} else {
throw new Error(`data out-of-bounds ${ this._data.length } ${ this._offset + alignedLength }`);
throw new Error(`data out-of-bounds: ${ this._offset + alignedLength }, actual: ${ this._data.length }`);
}
}
return this._data.slice(this._offset, this._offset + alignedLength);
Expand Down
64 changes: 32 additions & 32 deletions src/abi/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { isArray, isObject } from '~@vite/vitejs-utils';

import { defaultAbiCoder } from './abicoder';
import { Abi } from './abi';
import { Fragment, JsonFragment, JsonParamType, ParamType } from './fragments';
import * as utils from './utils';

export type AbiFragment = Fragment | JsonFragment | string;
export type AbiParam = ParamType | JsonParamType | string;
export type JsonInterface = Array<JsonFragment> | JsonFragment;
export { Abi, utils };

export function encodeLogSignature(abiFragment: Array<AbiFragment> | AbiFragment, eventName?: string): string {
Expand All @@ -16,6 +15,7 @@ export function encodeLogSignature(abiFragment: Array<AbiFragment> | AbiFragment
export function encodeFunctionSignature(abiFragment: Array<AbiFragment> | AbiFragment, methodName?: string) {
return Abi.from(abiFragment).getSighash(methodName);
}

export function encodeFunctionCall(abiFragment: Array<AbiFragment> | AbiFragment, params?: Array<any>, methodName?: string) {
return Abi.from(abiFragment).encodeFunctionData(methodName, params);
}
Expand Down Expand Up @@ -51,6 +51,7 @@ export function decodeParameter(type: AbiParam, data: string) {
export function encodeParameters(types: Array<AbiParam | AbiFragment> | AbiParam | AbiFragment, params?: Array<any>, methodName?: string) {
return defaultAbiCoder.encode(getTypes(types, methodName), params || []);
}

export function decodeParameters(types: Array<AbiParam | AbiFragment> | AbiParam | AbiFragment, data: string, methodName?: string) {
return defaultAbiCoder.decode(getTypes(types, methodName), data);
}
Expand All @@ -64,51 +65,35 @@ export function encodeLogFilter(abiFragment: Array<AbiFragment> | AbiFragment, v
}

/**
* Return matched JSON fragment according to type, or return null if the type is not found
* @param jsonFragment
* Return the first matched JSON fragment according to type, or return null if the type is not found
* @param jsonInterface
* @param type
* @return JsonFragment
*/
export function getAbiByType(jsonFragment: Array<JsonFragment> | JsonFragment, type: string): JsonFragment {
if (!jsonFragment || !type) {
export function getAbiByType(jsonInterface: JsonInterface | string, type: string): JsonFragment {
if (!jsonInterface || !type) {
return null;
}
const _frags: Array<JsonFragment> = [ ];
_parseJson(jsonInterface, _frags);

if (!(isArray(jsonFragment) || isObject(jsonFragment))) {
throw new Error('jsonFragment should be an array or object');
}

// jsonFragment is an object
if (!Array.isArray(jsonFragment)) {
return (jsonFragment.type === type) ? jsonFragment : null;
}

// jsonFragment is an array
return jsonFragment.find(item => item.type === type) || null;
return _frags.find(item => item.type === type) || null;
}

/**
* Return matched JSON fragment according to method name, or return null if the method is not found
* @param jsonFragment
* Return the first matched JSON fragment according to method name, or return null if the method is not found
* @param jsonInterface
* @param methodName
* @return JsonFragment
*/
export function getAbiByName(jsonFragment: Array<JsonFragment> | JsonFragment, methodName: string): JsonFragment {
if (!jsonFragment || !methodName) {
export function getAbiByName(jsonInterface: JsonInterface | string, methodName: string): JsonFragment {
if (!jsonInterface || !methodName) {
return null;
}
const _frags: Array<JsonFragment> = [ ];
_parseJson(jsonInterface, _frags);

if (!(isArray(jsonFragment) || isObject(jsonFragment))) {
throw new Error('jsonFragment should be an array or object');
}

// jsonFragment is an object
if (!Array.isArray(jsonFragment)) {
return (jsonFragment.name === methodName) ? jsonFragment : null;
}

// jsonFragment is an array
return jsonFragment.find(item => item.name === methodName) || null;
return _frags.find(item => item.name === methodName) || null;
}

// Parse input fragments or types and return matched type array
Expand Down Expand Up @@ -152,3 +137,18 @@ function getTypes(types: Array<ParamType | AbiFragment> | ParamType | AbiFragmen
}
return _types;
}

function _parseJson<T>(inputs: Array<T | string> | T | string, result: Array<T>): void {
if (typeof (inputs) === 'string') {
try {
inputs = JSON.parse(inputs) as T;
} catch (e) {
throw new Error(`invalid jsonFragment: ${ inputs }`);
}
}
if (!Array.isArray(inputs)) {
result.push(inputs);
return;
}
inputs.forEach(item => _parseJson(item, result));
}
Loading

0 comments on commit 7b36a6c

Please sign in to comment.