Skip to content

Commit

Permalink
feat: 🎸 split I into I and IEngine and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
waynewyang committed Dec 13, 2023
1 parent 988fddf commit 801f4ff
Show file tree
Hide file tree
Showing 19 changed files with 398 additions and 83 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/buildAndTest-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: ["main"]
jobs:
buildAndTest:
runs-on: macos-latest
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
Expand All @@ -21,20 +21,22 @@ jobs:
PROOF_SUBMITTER: ${{ vars.PROOF_SUBMITTER }}
LOTUS_API_ENDPOINT: ${{ vars.LOTUS_API_ENDPOINT }}
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Checkout repository
uses: actions/checkout@v2

- name: Install dependencies
run: |
npm install
- name: Build
run: |
npm run build
- name: Test
run: |
npm run test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
dist-test
yarn.lock
.env
5 changes: 3 additions & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loader" : "ts-node/esm",
"spec": "test/**/*.test.ts",
"exclude": ["node_modules"]
"spec": "dist-test/**/*.test.js",
"exclude": ["node_modules"],
"project": "./tsconfig.test.json"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "rm -rf ./dist;npx tsc",
"test": "mocha test/**/*.test.ts",
"build:test": "rm -rf ./dist-test;tsc -p tsconfig.test.json;",
"test": "npm run build:test; mocha ",
"readme": "npx readme-md-generator -p ./templates/readme.md",
"commit": "git-cz",
"release": "npm run build; git branch --show-current | grep -q '^main$' && release-it",
Expand Down
186 changes: 186 additions & 0 deletions src/evm/abstract/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import { JsonFragment, Contract as EtherContract, ethers } from "ethers"
import { Contract, Web3, AbiFunctionFragment, Numbers } from "web3"
import { SignTransactionResult as Web3Signature } from "web3-eth-accounts"
import { EtherUnits } from "web3-utils"
import { Result } from "@unipackage/utils"
import {
EvmInput,
EvmOutput,
EvmTransactionOptions,
EvmType,
IEVMEngine,
} from "../interface"
/**
* Represents the Ethereum Virtual Machine (EVM) Engine interface.
*/
export abstract class AbstractEvm {
private engine: IEVMEngine

constructor(engine: IEVMEngine) {
this.engine = engine
}

/**
* Call a function on the EVM contract.
*
* @param input - The input parameters for the function call.
* @returns A promise that resolves to the output of the function call.
*/
async call(input: EvmInput): Promise<EvmOutput<any>> {
return await this.engine.call(input)
}

/**
* Decode the transaction input on the EVM.
*
* @param txInput - The transaction input data.
* @returns A promise that resolves to the decoded output.
*/
decodeTxInputToEvmInput(txInput: string): EvmOutput<EvmInput> {
return this.engine.decodeTxInputToEvmInput(txInput)
}

/**
* Encode EVM input to transaction input data.
*
* @param input - The input parameters for the EVM operation.
* @returns A promise that resolves to the encoded transaction input.
*/
encodeEvmInputToTxinput(input: EvmInput): EvmOutput<string> {
return this.engine.encodeEvmInputToTxinput(input)
}

/**
* Encode function signature using the ABI.
*
* @param abi - The ABI function fragment.
* @returns The encoded function signature or an error result.
*/
encodeFunctionSignatureByAbi(
abi: AbiFunctionFragment | JsonFragment
): Result<string> {
return this.engine.encodeFunctionSignatureByAbi(abi)
}

/**
* Encode function signature using the function name.
*
* @param name - The name of the function.
* @returns A promise that resolves to the encoded function signature or an error result.
*/
encodeFunctionSignatureByFuntionName(name: string): EvmOutput<string> {
return this.engine.encodeFunctionSignatureByFuntionName(name)
}

/**
* Generates a value in Wei based on the provided number and unit.
*
* @param number - The number to convert to Wei.
* @param unit - The unit to convert the number to (e.g., Ether, Gwei).
* @returns The generated value in Wei as a string or bigint.
*/
generateWei(number: Numbers, unit: EtherUnits): string | bigint {
return this.engine.generateWei(number, unit)
}

/**
* Get the EVM contract.
*
* @returns The EVM contract object or null if not initialized.
*/
getContract(): Contract<AbiFunctionFragment[]> | EtherContract | null {
return this.engine.getContract()
}

/**
* Get the EVM contract address.
*
* @returns The EVM contract address or null if not initialized.
*/
getContractAddress(): string {
return this.engine.getContractAddress()
}

/**
* Get the EVM contract abi.
*
* @returns The EVM contract abi or null if not initialized.
*/
getContractABI(): AbiFunctionFragment[] | JsonFragment[] {
return this.engine.getContractABI()
}

/**
* Get the EVM type.
*
* @returns The EVM type,Web3 or Ethers.
*/
getEvmType(): EvmType {
return this.engine.getEvmType()
}

/**
* Get the EVM provider url.
*
* @returns The EVM provider url or undefined not initialized.
*/
getProviderUrl(): string | undefined {
return this.engine.getProviderUrl()
}

/**
* Get the Ether Provider
*
* @returns Ether Provider object or null if not initialized.
*/
getEtherProvider(): ethers.AbstractProvider | null {
return this.engine.getEtherProvider()
}

/**
* Get the Web3 object.
*
* @returns The Web3 object or null if not initialized.
*/
getWeb3(): Web3 | null {
return this.engine.getWeb3()
}

/**
* Send a transaction to the EVM contract.
*
* @param input - The input parameters for the transaction.
* @param options - The transaction options.
* @returns A promise that resolves to the transaction result.
*/
async send(
input: EvmInput,
options: EvmTransactionOptions
): Promise<EvmOutput<any>> {
return await this.engine.send(input, options)
}

/**
* Send a signed transaction to the EVM.
*
* @param signed - The signed transaction data.
* @returns A promise that resolves to the transaction result.
*/
async sendSigned(signed: Web3Signature | string): Promise<EvmOutput<any>> {
return await this.engine.sendSigned(signed)
}

/**
* Sign a transaction on the EVM.
*
* @param input - The input parameters for the transaction.
* @param options - The transaction options.
* @returns A promise that resolves to the signed transaction or an error result.
*/
async sign(
input: EvmInput,
options: EvmTransactionOptions
): Promise<EvmOutput<Web3Signature | string>> {
return await this.engine.sign(input, options)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
EvmOutput,
EvmTransactionOptions,
EvmType,
IEVM,
IEVMEngine,
defaultTransactionOptions,
} from "../../interface"

Expand All @@ -52,7 +52,7 @@ declare global {
/**
* Class representing a EthersEvm instance.
*/
export class EthersEvm implements IEVM {
export class EthersEvmEngine implements IEVMEngine {
private provider: ethers.JsonRpcProvider | ethers.BrowserProvider | null =
null
private contract: Contract | null = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
EvmOutput,
EvmTransactionOptions,
EvmType,
IEVMEngine,
} from "../../interface"
import { IEVM, defaultTransactionOptions } from "../../interface"
import {
Expand All @@ -55,7 +56,7 @@ declare global {
/**
* Class representing a Web3Evm instance.
*/
export class Web3Evm implements IEVM {
export class Web3EvmEngine implements IEVMEngine {
private web3Object: Web3 | null = null
private contractObject: Contract<AbiFunctionFragment[]> | undefined
private contractAddress: string
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions src/evm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AbstractEvm } from "./abstract"
import { IEVMEngine } from "./interface"

/**
* Represents the Evm class.
*/
export class Evm extends AbstractEvm {
constructor(engine: IEVMEngine) {
super(engine)
}
/*
Add common method
*/
}
13 changes: 11 additions & 2 deletions src/evm/interface/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export interface EvmTransactionOptions {
}

/**
* Represents the Ethereum Virtual Machine (EVM) interface.
* Represents the Ethereum Virtual Machine (EVM) Engine interface.
*/
export interface IEVM {
export interface IEVMEngine {
/**
* Call a function on the EVM contract.
*
Expand Down Expand Up @@ -206,6 +206,15 @@ export interface IEVM {
): Promise<EvmOutput<Web3Signature | string>>
}

/**
* Represents the Ethereum Virtual Machine (EVM) interface.
*/
export interface IEVM extends IEVMEngine {
/*
Add common method
*/
}

/**
* Check if an object is of type EvmTransactionOptions.
*
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

export {
IRPC,
IRPCEngine,
RPCEngineConfig,
RPCOptions,
RPCRequest,
Expand All @@ -29,8 +30,10 @@ export {
isRPCOptions,
DefaultOptions,
} from "./rpc/interface"
export { FilecoinRPC } from "./rpc/implements/filecoin"
export { FilecoinRPCEngine } from "./rpc/engine/filecoin"
export { withRequestMethod } from "./rpc/withMethod"
export { Rpc } from "./rpc/"
export { AbstractRpc } from "./rpc/abstract"

export { withMethods } from "./shared/withMethods"
export { InputParams } from "./shared/types/params"
Expand All @@ -41,13 +44,16 @@ export {
EvmInput,
EvmOutput,
EvmTransactionOptions,
IEVMEngine,
IEVM,
isEvmTransactionOptions,
} from "./evm/interface"
export { Web3Evm } from "./evm/implements/web3"
export { EthersEvm } from "./evm/implements/ether"
export { Web3EvmEngine } from "./evm/engine/web3"
export { EthersEvmEngine } from "./evm/engine/ether"
export {
getEncodedParamsFromTxinput,
getFunctionSignatureFromTxinput,
} from "./evm/implements/web3/tools"
} from "./evm/engine/web3/tools"
export { withCallMethod, withSendMethod } from "./evm/withMethod"
export { Evm } from "./evm/"
export { AbstractEvm } from "./evm/abstract"
Loading

0 comments on commit 801f4ff

Please sign in to comment.