Skip to content

Commit

Permalink
Remove actor codes and update to ethers v6 (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
navFooh authored Jan 15, 2024
1 parent e7cfaf8 commit ab12f71
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 346 deletions.
359 changes: 248 additions & 111 deletions packages/filecoin-actor-utils/package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions packages/filecoin-actor-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@
"dependencies": {
"@glif/filecoin-address": "^2.0.43",
"@ipld/dag-cbor": "^7.0.3",
"base32-decode": "^1.0.0",
"ethers": "^5.7.2",
"ethers": "^6.10.0",
"lodash.clonedeep": "^4.5.0",
"uint8arrays": "^3.1.0"
},
"devDependencies": {
"@types/lodash.clonedeep": "^4.5.7",
"@types/lodash.clonedeep": "^4.5.9",
"@typescript-eslint/eslint-plugin": "^5.35.1",
"@typescript-eslint/parser": "^5.35.1",
"eslint": "^8.22.0",
Expand Down
28 changes: 1 addition & 27 deletions packages/filecoin-actor-utils/src/data.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
import networkActorCodeMapJSON from './data/actor-codes.json'
import actorDescriptorMapJSON from './data/actor-descriptors.json'
import {
NetworkActorCodeMap,
ActorDescriptorMap,
NetworkActorCodeMapInv
} from './types'

/**
* Returns the actor code using networkActorCodeMap[networkName][actorName]
*/
export const networkActorCodeMap: NetworkActorCodeMap = networkActorCodeMapJSON

/**
* Returns the actor name using networkActorCodeMapInv[networkName][actorCode]
*/
export const networkActorCodeMapInv: NetworkActorCodeMapInv =
Object.fromEntries(
Object.entries(networkActorCodeMap).map(([networkName, actorCodeMap]) => [
networkName,
Object.fromEntries(
Object.entries(actorCodeMap).map(([actorName, actorCode]) => [
actorCode,
actorName
])
)
])
)
import { ActorDescriptorMap } from './types'

/**
* Returns the actor descriptors with MethodNum converted to number from string.
Expand Down
38 changes: 0 additions & 38 deletions packages/filecoin-actor-utils/src/data/actor-codes.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/filecoin-actor-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './types'
export * from './utils/abi'
export * from './utils/code'
export * from './utils/logs'
export * from './utils/method'
export * from './utils/params'
Expand Down
17 changes: 0 additions & 17 deletions packages/filecoin-actor-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
export type ActorName = string
export type ActorCode = string
export type MethodNum = number
export type NetworkName = string
export type PropName = string

export type ActorCodeMap = {
[actorName: ActorName]: ActorCode
}

export type ActorCodeMapInv = {
[actorCode: ActorCode]: ActorName
}

export type NetworkActorCodeMap = {
[networkName: NetworkName]: ActorCodeMap
}

export type NetworkActorCodeMapInv = {
[networkName: NetworkName]: ActorCodeMapInv
}

export enum Type {
Bool = 'boolean',
Number = 'number',
Expand Down
8 changes: 6 additions & 2 deletions packages/filecoin-actor-utils/src/utils/abi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fragment, JsonFragment, ParamType } from '@ethersproject/abi'
import { Fragment, JsonFragment, ParamType } from 'ethers'
import { fromString, toString } from 'uint8arrays'
import { decode } from '@ipld/dag-cbor'
import { DataType, Type } from '../types'
Expand All @@ -24,7 +24,7 @@ export const cborToHex = (base64: string): string => {
*/
export const abiParamsToDataType = (
name: string,
params: ParamType[]
params: ReadonlyArray<ParamType>
): DataType => ({
Type: Type.Object,
Name: name,
Expand All @@ -41,12 +41,16 @@ export const abiParamsToDataType = (
export const abiParamToDataType = (param: ParamType): DataType => {
switch (param.baseType) {
case 'array':
if (!param.arrayChildren)
throw new Error('ParamType array is missing arrayChildren')
return {
Type: Type.Array,
Name: param.type,
Contains: abiParamToDataType(param.arrayChildren)
}
case 'tuple':
if (!param.components)
throw new Error('ParamType tuple is missing components')
return {
Type: Type.Object,
Name: param.type,
Expand Down
53 changes: 0 additions & 53 deletions packages/filecoin-actor-utils/src/utils/code.ts

This file was deleted.

9 changes: 4 additions & 5 deletions packages/filecoin-actor-utils/src/utils/generic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import cloneDeep from 'lodash.clonedeep'
import { Address } from '@glif/filecoin-address'
import { BigNumber } from 'ethers'
import { toString as BytesToString } from 'uint8arrays'
import { DataType, Type } from '../types'

Expand All @@ -26,8 +25,8 @@ export const describeDataType = (dataType: DataType, value: any) => {
return

case Type.Number:
BigNumber.isBigNumber(value)
? describeBigNumber(dataType, value)
typeof value === 'bigint'
? describeBigInt(dataType, value)
: describeBaseValue(dataType, value)
return

Expand Down Expand Up @@ -68,11 +67,11 @@ export const describeBaseValue = (
}

/**
* Adds a BigNumber value to a descriptor as a string
* Adds a BigInt value to a descriptor as a string
* @param dataType the descriptor to add the value to
* @param value the value to add to the descriptor
*/
export const describeBigNumber = (dataType: DataType, value: BigNumber) => {
export const describeBigInt = (dataType: DataType, value: bigint) => {
dataType.Value = value.toString()
}

Expand Down
9 changes: 5 additions & 4 deletions packages/filecoin-actor-utils/src/utils/logs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from 'ethers'
import { Interface } from 'ethers'
import { DataType, Type } from '../types'
import { ABI, abiParamsToDataType } from './abi'
import { describeDataType } from './generic'
Expand Down Expand Up @@ -34,14 +34,15 @@ export const describeFEVMLogs = (
// Sort logs by logIndex
const sorted = logs.sort((a, b) => Number(a.logIndex) - Number(b.logIndex))

// Parse logs to array of decribed DataTypes
const iface = new ethers.utils.Interface(abi)
// Parse logs to array of described DataTypes
const iface = new Interface(abi)
const parsed = sorted.map(({ data, topics, logIndex }) => {
const log = iface.parseLog({ data, topics })
if (!log) throw new Error('Failed to parse log')

// Convert ABI inputs to descriptor
const name = `${Number(logIndex)}: ${log.name}`
const dataType = abiParamsToDataType(name, log.eventFragment.inputs)
const dataType = abiParamsToDataType(name, log.fragment.inputs)

// Supplement the descriptor with log data
describeDataType(dataType, log.args)
Expand Down
8 changes: 5 additions & 3 deletions packages/filecoin-actor-utils/src/utils/method.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from 'ethers'
import { Interface } from 'ethers'
import { actorDescriptorMap } from '../data'
import { ABI, cborToHex } from './abi'

Expand All @@ -23,9 +23,11 @@ export const getMethodName = (
export const getFEVMMethodName = (params: string, abi: ABI): string | null => {
if (!params || !abi) return null
try {
const iface = new ethers.utils.Interface(abi)
const iface = new Interface(abi)
const data = cborToHex(params)
return iface.parseTransaction({ data }).name
const tx = iface.parseTransaction({ data })
if (!tx) throw new Error('Failed to parse transaction')
return tx.name
} catch {
return null
}
Expand Down
7 changes: 4 additions & 3 deletions packages/filecoin-actor-utils/src/utils/params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cloneDeep from 'lodash.clonedeep'
import { ethers } from 'ethers'
import { Interface } from 'ethers'
import { actorDescriptorMap } from '../data'
import { ActorName, DataType, MethodNum } from '../types'
import { ABI, cborToHex, abiParamsToDataType } from './abi'
Expand Down Expand Up @@ -65,12 +65,13 @@ export const describeFEVMTxParams = (
if (!params) return null

// Parse transaction from params
const iface = new ethers.utils.Interface(abi)
const iface = new Interface(abi)
const data = cborToHex(params)
const tx = iface.parseTransaction({ data })
const { inputs } = tx.functionFragment
if (!tx) throw new Error('Failed to parse transaction')

// Return null for empty ABI inputs
const { inputs } = tx.fragment
if (!inputs.length) return null

// Convert ABI inputs to descriptor
Expand Down
7 changes: 4 additions & 3 deletions packages/filecoin-actor-utils/src/utils/return.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from 'ethers'
import { Interface } from 'ethers'
import cloneDeep from 'lodash.clonedeep'
import { actorDescriptorMap } from '../data'
import { ActorName, DataType, MethodNum } from '../types'
Expand Down Expand Up @@ -67,12 +67,13 @@ export const describeFEVMTxReturn = (
if (!params || !returnVal) return null

// Parse transaction from params
const iface = new ethers.utils.Interface(abi)
const iface = new Interface(abi)
const data = cborToHex(params)
const tx = iface.parseTransaction({ data })
const { outputs } = tx.functionFragment
if (!tx) throw new Error('Failed to parse transaction')

// Return null for empty ABI outputs
const { outputs } = tx.fragment
if (!outputs?.length) return null

// Decode return value
Expand Down
34 changes: 1 addition & 33 deletions packages/filecoin-actor-utils/src/utils/state.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
import cloneDeep from 'lodash.clonedeep'
import { actorDescriptorMap } from '../data'
import {
ActorName,
DataTypeMap,
LotusActorState,
NetworkName,
Type
} from '../types'
import { getActorName } from './code'
import { ActorName, DataTypeMap, Type } from '../types'
import { describeObject } from './generic'

/**
* Returns a descriptor with values for the provided Lotus actor state and network name
* @param lotusActorState the Lotus actor state as returned from StateReadState
* @param networkName the network in which to search for the actor name
* @returns the described actor state or null when the state is null
*/
export const describeLotusActorState = (
lotusActorState: LotusActorState,
networkName: NetworkName
): DataTypeMap | null => {
// Return null for falsy actor state
if (!lotusActorState?.State) return null

// Retrieve the actor name from the code
const actorCode = lotusActorState.Code['/']
const actorName = getActorName(actorCode, networkName)
if (!actorName)
throw new Error(
`Failed to resolve actor name for code: ${actorCode}, in network: ${networkName}`
)

// Return the described actor state
return describeActorState(actorName, lotusActorState.State)
}

/**
* Returns a descriptor with values based on the provided actor name and state
* @param actorName the name of the actor
Expand Down
Loading

0 comments on commit ab12f71

Please sign in to comment.