Skip to content

Commit

Permalink
fix: 🐛 decodeTxinput in ethers
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 🧨 decodeTxInput change to decodeTxinputToEvmInput
  • Loading branch information
waynewyang committed Dec 10, 2023
1 parent 021ffc1 commit 0e11bd4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
9 changes: 6 additions & 3 deletions src/evm/implements/ether/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
import { EtherUnits } from "web3-utils"
import { Result } from "@unipackage/utils"
import {
EvmDecodeOutPut,
EvmInput,
EvmOutput,
EvmTransactionOptions,
Expand Down Expand Up @@ -205,7 +204,7 @@ export class EthersEvm implements IEVM {
* @param txInput - The transaction input data.
* @returns A promise that resolves to the decoded output.
*/
decodeTxInput(txInput: string): EvmOutput<EvmDecodeOutPut> {
decodeTxInputToEvmInput(txInput: string): EvmOutput<EvmInput> {
if (!this.contract) {
return {
ok: false,
Expand All @@ -216,11 +215,15 @@ export class EthersEvm implements IEVM {
const abi = this.contract.interface.parseTransaction({
data: txInput,
})
const params = abi?.args
return {
ok: true,
data: {
method: abi!.name,
params: abi!.args.toArray(),
params:
params instanceof EthersResult
? params.toArray()
: params,
},
}
} catch (error) {
Expand Down
3 changes: 1 addition & 2 deletions src/evm/implements/web3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { SignTransactionResult as Web3Signature } from "web3-eth-accounts"
import { Result } from "@unipackage/utils"
import type { Contract } from "web3-eth-contract"
import {
EvmDecodeOutPut,
EvmInput,
EvmOutput,
EvmTransactionOptions,
Expand Down Expand Up @@ -240,7 +239,7 @@ export class Web3Evm implements IEVM {
* @param txInput - The transaction input.
* @returns A promise resolving to EvmOutput object with the decoded transaction input.
*/
decodeTxInput(txInput: string): EvmOutput<EvmDecodeOutPut> {
decodeTxInputToEvmInput(txInput: string): EvmOutput<EvmInput> {
if (!this.web3Object) {
return {
ok: false,
Expand Down
10 changes: 1 addition & 9 deletions src/evm/interface/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ export const defaultTransactionOptions: EvmTransactionOptions = {
confirmations: 0,
}

/**
* Represents the output of decoding EVM input.
*/
export interface EvmDecodeOutPut {
method: string
params?: any
}

export enum EvmType {
Web3,
Ethers,
Expand Down Expand Up @@ -90,7 +82,7 @@ export interface IEVM {
* @param txInput - The transaction input data.
* @returns A promise that resolves to the decoded output.
*/
decodeTxInput(txInput: string): EvmOutput<EvmDecodeOutPut>
decodeTxInputToEvmInput(txInput: string): EvmOutput<EvmInput>

/**
* Encode EVM input to transaction input data.
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export {
EvmType,
EvmInput,
EvmOutput,
EvmDecodeOutPut,
EvmTransactionOptions,
IEVM,
isEvmTransactionOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/shared/types/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
*/
export interface InputParams {
method: string // The name of the method to be invoked.
params?: any[] // Optional parameters for the method.
params?: any // Optional parameters for the method.
}
10 changes: 6 additions & 4 deletions test/evm/decode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ describe("Encoding and decoding test", () => {
//@ts-ignore
describe("decodeTxInput Test", () => {
it("web3 correct test", () => {
const web3Decode = web3Datasets.decodeTxInput(txInput)
const web3Decode = web3Datasets.decodeTxInputToEvmInput(txInput)
assert.deepStrictEqual(web3Decode, evmInput)
})

it("ethers correct test", () => {
const ethersDecode = ethersDatasets.decodeTxInput(txInput)
const ethersDecode = ethersDatasets.decodeTxInputToEvmInput(txInput)
assert.deepStrictEqual(ethersDecode, evmInput)
})

it("web3 error test", () => {
const web3Decode = web3Datasets.decodeTxInput(incorrectTxInput)
const web3Decode =
web3Datasets.decodeTxInputToEvmInput(incorrectTxInput)

const expected = {
ok: false,
Expand All @@ -69,7 +70,8 @@ describe("Encoding and decoding test", () => {
})

it("ethers error test", () => {
const decode = ethersDatasets.decodeTxInput(incorrectTxInput)
const decode =
ethersDatasets.decodeTxInputToEvmInput(incorrectTxInput)
assert.deepStrictEqual(decode.ok, false)
})
})
Expand Down

0 comments on commit 0e11bd4

Please sign in to comment.