Skip to content

Commit

Permalink
feat: add semantic type
Browse files Browse the repository at this point in the history
  • Loading branch information
AbigailDeng authored and AbigailDeng committed Nov 14, 2023
1 parent 898f938 commit c06d462
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 98 deletions.
2 changes: 1 addition & 1 deletion test/unit/chain/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import RequestManager from '../../../src/util/requestManage';
import HttpProvider from '../../../src/util/httpProvider';
import AElf from '../../../src';
import { noop } from '../../../src/util/utils';
const stageEndpoint = 'https://explorer-test-tdvw.aelf.io/chain';
const stageEndpoint = 'https://tdvw-test-node.aelf.io/';
let httpProvider, requestManager, chain;

describe('chain should work', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/contract/contractMethod.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ContractMethod from '../../../src/contract/contractMethod';
import ContractFactory from '../../../src/contract/index';
import AElf from '../../../src/index';
import { noop } from '../../../src/util/utils';
const stageEndpoint = 'https://explorer-test-tdvw.aelf.io/chain';
const stageEndpoint = 'https://tdvw-test-node.aelf.io/';
describe('token contract with transfer method', () => {
const aelf = new AElf(new AElf.providers.HttpProvider(stageEndpoint));
const wallet = AElf.wallet.getWalletByPrivateKey(
Expand Down
2 changes: 1 addition & 1 deletion test/unit/contract/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ContractFactory from '../../../src/contract/index';
const stageEndpoint = 'https://explorer-test-tdvw.aelf.io/chain';
const stageEndpoint = 'https://tdvw-test-node.aelf.io';
import AElf from '../../../src/index';
describe('contract factory', () => {
const aelf = new AElf(new AElf.providers.HttpProvider(stageEndpoint));
Expand Down
2 changes: 1 addition & 1 deletion test/unit/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AElf from '../../src/index';
const stageEndpoint = 'https://explorer-test-tdvw.aelf.io/chain';
const stageEndpoint = 'https://tdvw-test-node.aelf.io';
describe('test AElf', () => {
test('test AElf is connected', () => {
const aelf = new AElf(new AElf.providers.HttpProvider(stageEndpoint));
Expand Down
111 changes: 49 additions & 62 deletions types/chain/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Contract } from 'types/contract';
import { RequestManager } from 'types/util/requestManage';
import * as Bip39 from 'bip39';
import { IWalletInfo } from '../wallet';
import { IExtractArgumentsIntoObjectResult, TExtractArg } from './chainMethod';
import {
TAddress,
TBlockHash,
TBlockHeight,
TChainId,
TTransactionId,
} from '../util/proto';
export interface IError {
Error: {
Code: string | null;
Expand All @@ -22,65 +28,44 @@ export interface IError {
};
}
export interface IChainStatus {
ChainId: string;
TChainId: TChainId;
Branches: {
[key in string]: number;
};
NotLinkedBlocks: {
[key in string]: number;
};
LongestChainHeight: number;
LongestChainHash: string;
GenesisBlockHash: string;
GenesisContractAddress: string;
LastIrreversibleBlockHash: string;
LastIrreversibleBlockHeight: number;
BestChainHash: string;
BestChainHeight: number;
LongestChainHeight: TBlockHeight;
LongestChainHash: TBlockHash;
GenesisBlockHash: TBlockHash;
GenesisContractAddress: TAddress;
LastIrreversibleBlockHash: TBlockHash;
LastIrreversibleBlockHeight: TBlockHeight;
BestChainHash: TBlockHash;
BestChainHeight: TBlockHeight;
}

export interface IChainState {
BlockHash: string;
PreviousHash: string;
BlockHeight: number;
BlockHash: TBlockHash;
PreviousHash: TBlockHash;
BlockHeight: TBlockHeight;
Changes: {
[key in string]: string;
};
Deletes: [string];
}

export interface IBlock {
BlockHash: string;
BlockHash: TBlockHash;
Header: {
PreviousBlockHash: string;
PreviousBlockHash: TBlockHash;
MerkleTreeRootOfTransactions: string;
MerkleTreeRootOfWorldState: string;
MerkleTreeRootOfTransactionState: string;
Extra: string;
Height: number;
Height: TBlockHeight;
Time: string;
ChainId: string;
Bloom: string;
SignerPubkey: string;
};
Body: {
TransactionsCount: number;
Transactions: string[];
};
BlockSize: number;
}

export interface IBlockByHeight {
BlockHash: string;
Header: {
PreviousBlockHash: string;
MerkleTreeRootOfTransactions: string;
MerkleTreeRootOfWorldState: string;
MerkleTreeRootOfTransactionState: string;
Extra: string;
Height: number;
Time: string;
ChainId: string;
ChainId: TChainId;
Bloom: string;
SignerPubkey: string;
};
Expand All @@ -92,21 +77,21 @@ export interface IBlockByHeight {
}

export interface ITransactionResult {
TransactionId: string;
TransactionId: TTransactionId;
Status: string;
Logs: {
Address: string;
Address: TAddress;
Name: string;
Indexed: string[];
NonIndexed: string;
}[];
Bloom: string;
BlockNumber: number;
BlockHash: string;
BlockNumber: TBlockHeight;
BlockHash: TBlockHash;
Transaction: {
From: string;
To: string;
RefBlockNumber: number;
From: TAddress;
To: TAddress;
RefBlockNumber: TBlockHeight;
RefBlockPrefix: string;
MethodName: string;
Params: string;
Expand All @@ -118,7 +103,7 @@ export interface ITransactionResult {
}
export interface IMerklePathByTxId {
MerklePathNodes: {
Hash: string;
Hash: TBlockHash;
IsLeftChildNode: boolean;
}[];
}
Expand All @@ -137,7 +122,7 @@ export interface ITransactionPoolStatus {
}

export interface IPeer {
IpAddress: string;
IpAddress: TAddress;
ProtocolVersion: number;
ConnectionTime: number;
ConnectionStatus: string;
Expand All @@ -163,48 +148,50 @@ declare class Chain {
args: TExtractArg[]
): IExtractArgumentsIntoObjectResult;
public contractAt(
address: string,
address: TAddress,
wallet: IWalletInfo,
args: { [k in string]: any }
): Contract | Promise<Contract>;
public getMerklePath(
txId: string,
height: number,
height: TBlockHeight,
...args: { [k in string]: any }[]
): any[] | null | Promise<any[] | null>;
getChainStatus(): Promise<IChainStatus & IError>;
getChainState(blockHash: string): Promise<IChainState & IError>;
getContractFileDescriptorSet(address: string): Promise<string & IError>;
getBlockHeight(): number;
getBlockHeight(): TBlockHeight;
getBlock(
blockHash: string,
blockHash: TBlockHash,
includeTransactions: boolean
): Promise<IBlock & IError>;
getBlockByHeight(
blockHeight: number,
blockHeight: TBlockHeight,
includeTransactions?: boolean
): Promise<IBlockByHeight & IError>;
getTxResult(transactionId: string): Promise<ITransactionResult & IError>;
): Promise<IBlock & IError>;
getTxResult(
transactionId: TTransactionId
): Promise<ITransactionResult & IError>;
getTxResults(
blockHash: string,
blockHash: TBlockHash,
offset: number,
limit: number
): Promise<ITransactionResult[] & IError>;
getMerklePathByTxId(
transactionId: string
transactionId: TTransactionId
): Promise<IMerklePathByTxId & IError>;
getTransactionPoolStatus(): Promise<ITransactionPoolStatus & IError>;
sendTransaction(
RawTransaction: string
): Promise<{ TransactionId: string } & IError>;
RawTransaction: TRawTransaction
): Promise<{ TransactionId: TTransactionId } & IError>;
sendTransactions(RawTransaction: string): Promise<string[] & IError>;
calculateTransactionFee(
RawTransaction: string
RawTransaction: TRawTransaction
): Promise<ICalculateTransactionFee & IError>;
callReadOnly(RawTransaction: string): Promise<string & IError>;
callReadOnly(RawTransaction: TRawTransaction): Promise<string & IError>;
getPeers(withMetrics?: boolean): Promise<IPeer[] & IError>;
addPeer(Address: string): Promise<true & IError>;
removePeer(address: string): Promise<true & IError>;
addPeer(Address: TAddress): Promise<true & IError>;
removePeer(address: TAddress): Promise<true & IError>;
networkInfo(): Promise<
{
Version: string;
Expand Down
24 changes: 18 additions & 6 deletions types/contract/contractMethod.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import Chain from '../chain';
import { IWalletInfo } from '../wallet';
import * as protobuf from '@aelfqueen/protobufjs';
import { ITransaction } from '../util/proto';
import {
ITransaction,
TAddress,
TBlockHash,
TBlockHeight,
TTransactionId,
} from '../util/proto';
import { Contract } from '.';
import { GenericFunction } from '../util/utils';
export type TRawTx = ITransaction & {
Expand All @@ -23,7 +29,7 @@ declare class ContractMethod {
constructor(
chain: Chain,
method: protobuf.Method,
contractAddress: string,
contractAddress: TAddress,
walletInstance: IWalletInfo
);
public packInput(input?: any): Buffer | null;
Expand All @@ -32,22 +38,28 @@ declare class ContractMethod {
): any;
public unpackOutput(output?: ArrayBuffer | SharedArrayBuffer | null): any;
public packOutput(result?: any): Buffer | null;
public handleTransaction(height: string, hash: string, encoded: any): string;
public handleTransaction(
height: TBlockHeight,
hash: TBlockHash,
encoded: any
): string;
public prepareParametersAsync(args: Array<any>): Promise<string>;
public prepareParameters(args: Array<any>): string;
public prepareParametersWithBlockInfo(args: Array<any>): string;
public sendTransaction(
...args: Array<any>
): { TransactionId: string } | Promise<{ TransactionId: string }>;
):
| { TransactionId: TTransactionId }
| Promise<{ TransactionId: TTransactionId }>;
public callReadOnly(...args: Array<any>): any;
public extractArgumentsIntoObject(
...args: Array<any>
): IExtractArgumentsIntoObject;

public getSignedTx(...args: Array<any>): string;
public getRawTx(
blockHeightInput: string,
blockHashInput: string,
blockHeightInput: TBlockHeight,
blockHashInput: TBlockHash,
packedInput: any
): TRawTx;
public request(...args: Array<any>): IRequestResult;
Expand Down
5 changes: 3 additions & 2 deletions types/contract/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { IFileDescriptorSet } from '@aelfqueen/protobufjs/ext/descriptor';
import * as protobuf from '@aelfqueen/protobufjs/light';
import { IWalletInfo } from '../wallet';
import { GenericFunction } from '../util/utils';
import { TAddress } from '../util/proto';
export class Contract {
constructor(chain: Chain, services: Array<protobuf.Service>, address: string);
}

interface IContractFactory {
// just describe the public side of the class
at(address: string, callback: GenericFunction): Contract;
at(address: TAddress, callback: GenericFunction): Contract;
}
declare class ContractFactory implements IContractFactory {
constructor(
Expand All @@ -22,7 +23,7 @@ declare class ContractFactory implements IContractFactory {
wallet: IWalletInfo
): void;

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

export default ContractFactory;
6 changes: 4 additions & 2 deletions types/util/bloom.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export declare function isInBloom(bloom: string, hash: string): boolean;
import { TAddress, TBlockHash } from './proto';

export declare function isInBloom(bloom: string, hash: TBlockHash): boolean;
export declare function isEventInBloom(
bloom: string,
eventName: string
Expand All @@ -9,5 +11,5 @@ export declare function isIndexedInBloom(
): boolean;
export declare function isAddressInBloom(
bloom: string,
address: string
address: TAddress
): boolean;
7 changes: 3 additions & 4 deletions types/util/formatters.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Message } from '@aelfqueen/protobufjs';
export function inputAddressFormatter(address: string): string;
export function outputFileDescriptorSetFormatter(
result: string
): Message<object>;
import { IAddress, TAddress } from './proto';
export function inputAddressFormatter(address: TAddress | IAddress): string;
export function outputFileDescriptorSetFormatter(result: string): Message<{}>;
3 changes: 2 additions & 1 deletion types/util/keyStore.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IWalletInfo } from '../wallet';
import { TAddress } from './proto';

interface IOptions {
dklen: number;
Expand Down Expand Up @@ -33,7 +34,7 @@ interface IkeyStore {
version: number;
type: string;
nickName?: string;
address: string;
address: TAddress;
crypto: ICrypto;
}
export declare function getKeystore(
Expand Down
13 changes: 10 additions & 3 deletions types/util/proto.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import * as protobuf from '@aelfqueen/protobufjs/light';

export type TChainId = string;
export type TBlockHash = string;
export type TBlockHeight = number;
export type TAddress = string;
export type TTransactionId = string;
export type TRawTransaction = string;
export interface IAddress {
value: Buffer;
}
Expand All @@ -21,7 +28,7 @@ export function getFee(
): { [k: string]: any } | undefined | null;

interface ILog {
Address: string;
Address: TAddress;
Name: string;
Indexed: Array<string> | null;
NonIndexed: string;
Expand Down Expand Up @@ -49,8 +56,8 @@ export function getHashFromHex(
export function getHashObjectFromHex(hex: string): { [k: string]: any };
export function encodeTransaction(tx: { [k: string]: any }): Uint8Array;
export function getTransaction(
from: string,
to: string,
from: TAddress,
to: TAddress,
methodName: string,
params: any
): protobuf.Message<{
Expand Down
Loading

0 comments on commit c06d462

Please sign in to comment.