Skip to content

Feature/other declaration #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: feature/typescript-declaration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 4 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"root": true,
"parser": "babel-eslint",
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
Expand All @@ -15,13 +15,8 @@
"browser": true,
"jest": true
},
"plugins": [
"babel",
"import"
],
"extends": [
"eslint-config-airbnb-base"
],
"plugins": ["babel", "import"],
"extends": ["eslint-config-airbnb-base"],
"globals": {},
"rules": {
"no-console": ["warn"],
Expand All @@ -34,7 +29,7 @@
"no-underscore-dangle": "off",
"no-bitwise": "off",
"no-mixed-operators": "off",
"max-len": ["error", { "code": 120}],
"max-len": ["error", { "code": 120 }],
"class-methods-use-this": "off",
"no-plusplus": "off"
}
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "aelf-sdk",
"name": "aelf-web3",
"version": "3.2.41",
"description": "aelf-sdk js library",
"main": "dist/aelf.cjs.js",
Expand All @@ -24,7 +24,7 @@
"test:node:watch": "jest --config=jest.node.config.js --watch"
},
"keywords": [
"aelf-sdk"
"aelf-web3"
],
"files": [
"src",
Expand All @@ -41,10 +41,12 @@
"engines": {
"node": ">=10.5.0"
},
"types": "types",
"engineStrict": true,
"dependencies": {
"@aelfqueen/protobufjs": "^6.8.9",
"@babel/runtime": "^7.4.5",
"@typescript-eslint/parser": "^5.47.1",
"assert": "^2.0.0",
"bignumber.js": "^9.0.0",
"bip39": "^3.0.2",
Expand All @@ -65,6 +67,8 @@
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/plugin-transform-runtime": "^7.4.4",
"@babel/preset-env": "^7.4.5",
"@types/elliptic": "^6.4.14",
"@types/hdkey": "^2.0.1",
"babel-eslint": "^10.0.2",
"babel-loader": "^8.0.6",
"bundle-analyzer": "^0.0.6",
Expand Down
26 changes: 26 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"ts-node": {
"compilerOptions": {
"module": "commonjs"
}
},
"compilerOptions": {
"module": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"target": "es5",
"moduleResolution": "node",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noEmit": true,
"allowJs": false,
"allowSyntheticDefaultImports": false,
"baseUrl": ".",
"paths": {
"aelf-web3": ["types"]
},
"esModuleInterop": true,
"declaration": true
}
}
43 changes: 43 additions & 0 deletions types/chain/chainMethod.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as protobuf from "@aelfqueen/protobufjs/light";
import { RequestManager } from "types/util/requestManage";
type TInputAddressFormatter = (address: any) => string;
interface IChainMethodParams {
name: string;
call: string;
method?: string;
params?: Array<string | undefined>;
inputFormatter?: Array<TInputAddressFormatter | undefined>;
outputFormatter?: protobuf.Type | null;
}
export type TExtractArg = Function & {
isSync: boolean;
callback?: Function;
[k: string]: any;
};
export interface IExtractArgumentsIntoObjectResult {
method: Function;
requestMethod: string;
isSync: boolean;
callback: Function;
params: { [k in string]: any };
}
declare class ChainMethod {
constructor({
name,
call,
method,
params,
inputFormatter,
outputFormatter,
}: IChainMethodParams);
public formatInput(args: Array<any>): Array<any>;
public setRequestManager(manager: RequestManager): void;
public formatOutput(result: any): any;
public extractArgumentsIntoObject(
args: TExtractArg[]
): IExtractArgumentsIntoObjectResult;
public run(
args: TExtractArg[]
): { [k: string]: any } | Promise<{ [k: string]: any }>;
}
export default ChainMethod;
22 changes: 22 additions & 0 deletions types/chain/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Contract } from "types/contract";
import { RequestManager } from "types/util/requestManage";
import { IWallet } from "types/wallet";
import { IExtractArgumentsIntoObjectResult, TExtractArg } from "./chainMethod";

declare class Chain {
constructor(requestManager: RequestManager);
public extractArgumentsIntoObject(
args: TExtractArg[]
): IExtractArgumentsIntoObjectResult;
public contractAt(
address: string,
wallet: IWallet,
args: { [k in string]: any }
): Promise<Contract>;
public getMerklePath(
txId: string,
height: number,
args: { [k in string]: any }
): any[] | null;
}
export default Chain;
76 changes: 76 additions & 0 deletions types/contract/contractMethod.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import Chain from "types/chain";
import { IWallet } from "types/wallet";
import * as protobuf from "@aelfqueen/protobufjs";
import { ITransaction } from "types/util/proto";
import { Contract } from ".";
type TRawTx = ITransaction & {
refBlockNumber: string;
refBlockPrefix: string;
};

interface IExtractArgumentsIntoObject {
callback: Function;
isSync: boolean;
}
interface IRequestRes {
method: string;
callback: Function;
params: string;
format?: { [k: string]: any } | null;
}
declare class ContractMethod {
constructor(
chain: Chain,
method: protobuf.Method,
contractAddress: string,
walletInstance: IWallet
);
public packInput(input: { [k: string]: any }): Uint8Array;
public unpackPackedInput(
inputPacked?: ArrayBuffer | SharedArrayBuffer | null
): { [k: string]: any } | undefined | null;
public unpackOutput(
output?: ArrayBuffer | SharedArrayBuffer | null
): { [k: string]: any } | undefined | null;
public packOutput(result?: { [k: string]: any } | null): Uint8Array;
public handleTransaction(
height: string,
hash: string,
encoded: Uint8Array
): string;
public prepareParametersAsync(args: Array<any>): Promise<string>;
public prepareParameters(args: Array<any>): string;
public prepareParametersWithBlockInfo(args: Array<any>): string;
// TODO
public sendTransaction(
...args: Array<{ [k: string]: any }>
): { TransactionId: string } | Promise<{ TransactionId: string }>;
public callReadOnly(
...args: Array<{ [k: string]: any }>
):
| { [k: string]: any }
| undefined
| null
| Promise<{ [k: string]: any } | undefined | null>;
public extractArgumentsIntoObject(
...args: Array<{ [k: string]: any }>
): IExtractArgumentsIntoObject;

public getSignedTx(...args: Array<{ [k: string]: any }>): string | undefined;
public getRawTx(
blockHeightInput: string,
blockHashInput: string,
packedInput: Uint8Array
): TRawTx;
public request(...args: Array<any>): IRequestRes;
public run(
...args: Array<{ [k: string]: any }>
): { TransactionId: string } | Promise<{ TransactionId: string }>;
public bindMethodToContract(contract: Contract): void;
public getRawTx(
blockHeightInput: string,
blockHashInput: string,
packedInput: Uint8Array
): TRawTx;
}
export default ContractMethod;
22 changes: 22 additions & 0 deletions types/contract/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Chain from "types/chain";
import { IFileDescriptorSet } from "@aelfqueen/protobufjs/ext/descriptor";
import * as protobuf from "@aelfqueen/protobufjs/light";
import { IWallet } from "types/wallet";
export class Contract {
constructor(chain: Chain, services: Array<protobuf.Service>, address: string);
}
declare class ContractFactory {
constructor(
chain: Chain,
fileDescriptorSet: IFileDescriptorSet,
wallet: IWallet
);
public static bindMethodsToContract(
contract: Contract,
wallet: IWallet
): void;

public at(address: string, callback: Function): Contract;
}

export default ContractFactory;
101 changes: 101 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import * as protobuf from "@aelfqueen/protobufjs/light";
import HttpProvider from "./util/httpProvider";
import Wallet from "./wallet/index";
import * as proto from "./util/proto";
import * as utils from "./util/utils";
import { sha256 } from "js-sha256";
import * as transform from "./util/transform";
import {
arrayToHex,
padLeft,
padRight,
decodeAddressRep,
encodeAddressRep,
isBigNumber,
isString,
isFunction,
isObject,
isBoolean,
isJson,
toBigNumber,
getValueOfUnit,
fromWei,
toWei,
toTwosComplement,
uint8ArrayToHex,
noop,
setPath,
unpackSpecifiedTypeData,
deserializeTransaction,
getAuthorization,
} from "./util/utils";
import {
isInBloom,
isEventInBloom,
isIndexedInBloom,
isAddressInBloom,
} from "./util/bloom";
interface IUtils {
base58: utils.base58;
chainIdConvertor: utils.chainIdConvertor;
arrayToHex: typeof arrayToHex;
padLeft: typeof padLeft;
padRight: typeof padRight;
decodeAddressRep: typeof decodeAddressRep;
encodeAddressRep: typeof encodeAddressRep;
isBigNumber: typeof isBigNumber;
isString: typeof isString;
isFunction: typeof isFunction;
isObject: typeof isObject;
isBoolean: typeof isBoolean;
isJson: typeof isJson;
toBigNumber: typeof toBigNumber;
getValueOfUnit: typeof getValueOfUnit;
fromWei: typeof fromWei;
toWei: typeof toWei;
toTwosComplement: typeof toTwosComplement;
uint8ArrayToHex: typeof uint8ArrayToHex;
noop: typeof noop;
setPath: typeof setPath;
unpackSpecifiedTypeData: typeof unpackSpecifiedTypeData;
deserializeTransaction: typeof deserializeTransaction;
getAuthorization: typeof getAuthorization;
}
import Settings from "./util/settings";
interface IBloom {
isInBloom: typeof isInBloom;
isEventInBloom: typeof isEventInBloom;
isIndexedInBloom: typeof isIndexedInBloom;
isAddressInBloom: typeof isAddressInBloom;
}
type TUtil = IUtils &
IBloom & {
sha256: typeof sha256;
} & {
transform: typeof transform;
};
interface IVersion {
api?: string;
}
declare namespace AElf {}
export declare class AElf {
constructor(provider: HttpProvider);
static version?: string;
static providers: {
HttpProvider: HttpProvider;
};
static pbjs: typeof protobuf;
static pbUtils: typeof proto;
static wallet: Wallet;
static utils: TUtil;
providers: {
HttpProvider: HttpProvider;
};
settings: Settings;
version: IVersion;
isConnected(): boolean;
// TODO:
// reset 方法不存在
setProvider(provider: HttpProvider): void;
}
export default AElf;
13 changes: 13 additions & 0 deletions types/util/bloom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export declare function isInBloom(bloom: string, hash: string): boolean;
export declare function isEventInBloom(
bloom: string,
eventName: string
): boolean;
export declare function isIndexedInBloom(
bloom: string,
indexed: string
): boolean;
export declare function isAddressInBloom(
bloom: string,
address: string
): boolean;
9 changes: 9 additions & 0 deletions types/util/errors.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface IRequestError {
InvalidNumberOfRPCParams(): Error;
InvalidConnection(host: string): Error;
InvalidProvider(): Error;
InvalidResponse(error: Error, result: { [k: string]: any }): Error;
ConnectionTimeout(ms: string | number): Error;
}
declare const RequestError: IRequestError;
export { RequestError };
3 changes: 3 additions & 0 deletions types/util/formatters.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Message } from "@aelfqueen/protobufjs";
export function inputAddressFormatter(address: string): string;
export function outputFileDescriptorSetFormatter(result: string): Message<{}>;
4 changes: 4 additions & 0 deletions types/util/hash.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare function keccak256(bits: Buffer): string;
export declare function keccak512(bits: Buffer): string;
export declare function keccak256s(bits: Buffer): string;
export declare function keccak512s(bits: Buffer): string;
Loading