Skip to content

Commit

Permalink
fix(contract): structure change
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Mar 10, 2022
1 parent cc87943 commit 1ef9d2f
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"class-methods-use-this": 0,
"import/prefer-default-export": 0,
"@typescript-eslint/naming-convention": 0,
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"func-names": 0
}
}
72 changes: 44 additions & 28 deletions src/contract.ts → src/contract/default.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import BN from 'bn.js';
import assert from 'minimalistic-assert';

import { Account } from './account';
import { Provider, defaultProvider } from './provider';
import { Abi, AbiEntry, Calldata, FunctionAbi, Invocation, ParsedStruct, StructAbi } from './types';
import { getSelectorFromName } from './utils/hash';
import { BigNumberish, toBN, toFelt } from './utils/number';

export type Struct = {
type: 'struct';
[k: string]: BigNumberish;
};
export type Args = {
[inputName: string]: BigNumberish | BigNumberish[] | ParsedStruct | ParsedStruct[];
};

export type AsyncContractFunction<T = any> = (...args: Array<any>) => Promise<T>;
export type ContractFunction = (...args: Array<any>) => any;
export interface Result extends Array<any> {
[key: string]: any;
}
import { Account } from '../account';
import { Provider, defaultProvider } from '../provider';
import {
Abi,
AbiEntry,
AddTransactionResponse,
Args,
AsyncContractFunction,
Calldata,
ContractFunction,
FunctionAbi,
Invocation,
ParsedStruct,
Result,
StructAbi,
} from '../types';
import { getSelectorFromName } from '../utils/hash';
import { BigNumberish, toBN, toFelt } from '../utils/number';
import { ContractInterface } from './interface';

function parseFelt(candidate: string): BN {
try {
Expand All @@ -29,38 +29,58 @@ function parseFelt(candidate: string): BN {
}
}

/**
* Adds call methods to the contract
*
*/
function buildCall(contract: Contract, functionAbi: FunctionAbi): AsyncContractFunction {
return async function (...args: Array<any>): Promise<any> {
return contract.call(functionAbi.name, args);
};
}

/**
* Adds invoke methods to the contract
*
*/
function buildInvoke(contract: Contract, functionAbi: FunctionAbi): AsyncContractFunction {
return async function (...args: Array<any>): Promise<any> {
return contract.invoke(functionAbi.name, args);
};
}

/**
* Adds call/invoke methods to the contract
*
*/
function buildDefault(contract: Contract, functionAbi: FunctionAbi): AsyncContractFunction {
if (functionAbi.stateMutability === 'view') {
return buildCall(contract, functionAbi);
}
return buildInvoke(contract, functionAbi);
}

/**
* Adds populate for methods to the contract
*
*/
function buildPopulate(contract: Contract, functionAbi: FunctionAbi): ContractFunction {
return function (...args: Array<any>): any {
return contract.populate(functionAbi.name, args);
};
}

/**
* Adds estimateFee for methods to the contract
*
*/
function buildEstimate(contract: Contract, functionAbi: FunctionAbi): ContractFunction {
return function (...args: Array<any>): any {
return contract.estimate(functionAbi.name, args);
};
}

export class Contract {
export class Contract implements ContractInterface {
address: string;

providerOrAccount: Provider | Account;
Expand Down Expand Up @@ -170,22 +190,18 @@ export class Contract {
* Saves the address of the contract deployed on network that will be used for interaction
*
* @param address - address of the contract
* @returns Contract
*/
public attach(address: string): Contract {
public attach(address: string): void {
this.address = address;
return this;
}

/**
* Attaches to new Provider or Account
*
* @param providerOrAccount - new Provider or Account to attach to
* @returns Contract
*/
public connect(providerOrAccount: Provider | Account): Contract {
public connect(providerOrAccount: Provider | Account) {
this.providerOrAccount = providerOrAccount;
return this;
}

/**
Expand Down Expand Up @@ -496,7 +512,7 @@ export class Contract {
}, [] as Result);
}

public invoke(method: string, args: Array<any>) {
public invoke(method: string, args: Array<any>): Promise<AddTransactionResponse> {
// ensure contract is connected
assert(this.address !== null, 'contract isnt connected to an address');
// validate method and args
Expand Down Expand Up @@ -534,7 +550,7 @@ export class Contract {
});
}

public async call(method: string, args: Array<any>) {
public async call(method: string, args: Array<any>): Promise<Result> {
// ensure contract is connected
assert(this.address !== null, 'contract isnt connected to an address');

Expand Down
2 changes: 2 additions & 0 deletions src/contract/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './default';
export * from './interface';
74 changes: 74 additions & 0 deletions src/contract/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Account } from '../account';
import { Provider } from '../provider';
import {
AddTransactionResponse,
AsyncContractFunction,
ContractFunction,
Invocation,
Result,
} from '../types';

export abstract class ContractInterface {
public abstract address: string;

public abstract providerOrAccount: Provider | Account;

readonly functions!: { [name: string]: AsyncContractFunction };

readonly callStatic!: { [name: string]: AsyncContractFunction };

readonly populateTransaction!: { [name: string]: ContractFunction };

readonly estimateFee!: { [name: string]: ContractFunction };

readonly [key: string]: AsyncContractFunction | any;

/**
* Saves the address of the contract deployed on network that will be used for interaction
*
* @param address - address of the contract
*/
public abstract attach(address: string): void;

/**
* Attaches to new Provider or Account
*
* @param providerOrAccount - new Provider or Account to attach to
*/
public abstract connect(providerOrAccount: Provider | Account): void;

/**
* Calls a method on a contract
*
* @param method name of the method
* @param args Array of the arguments for the call
* @returns Result of the call as an array with key value pars
*/
public abstract call(method: string, args: Array<any>): Promise<Result>;

/**
* Invokes a method on a contract
*
* @param method name of the method
* @param args Array of the arguments for the invoke
* @returns Add Transaction Response
*/
public abstract invoke(method: string, args: Array<any>): Promise<AddTransactionResponse>;

/**
* Calls a method on a contract
*
* @param method name of the method
* @param args Array of the arguments for the call
*/
public abstract estimate(method: string, args: Array<any>): Promise<any>;

/**
* Calls a method on a contract
*
* @param method name of the method
* @param args Array of the arguments for the call
* @returns Invocation objet
*/
public abstract populate(method: string, args: Array<any>): Invocation;
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Main
*/
export * from './types';
export * from './contract';
export * from './types';
export * from './provider';
export * from './account';
export * from './signer';
Expand Down
5 changes: 5 additions & 0 deletions src/types/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type AsyncContractFunction<T = any> = (...args: Array<any>) => Promise<T>;
export type ContractFunction = (...args: Array<any>) => any;
export interface Result extends Array<any> {
[key: string]: any;
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './lib';
export * from './api';
export * from './signer';
export * from './contract';
7 changes: 7 additions & 0 deletions src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ export type CompressedCompiledContract = Omit<CompiledContract, 'program'> & {
program: CompressedProgram;
};

export type Struct = {
type: 'struct';
[k: string]: BigNumberish;
};
export type Args = {
[inputName: string]: BigNumberish | BigNumberish[] | ParsedStruct | ParsedStruct[];
};
export type ParsedStruct = {
[key: string]: BigNumberish | ParsedStruct;
};

0 comments on commit 1ef9d2f

Please sign in to comment.