feat(xc_admin_frontend): parse entropy and executor abis (#1481)
* parse entropy and executor abis * correct import * move parse to xc admin frontend * undo change * fix deps * add comment * comment * revert changes and then some minor change * fix unknown by typecast
This commit is contained in:
parent
644b54676c
commit
e46821d423
|
@ -22,6 +22,7 @@ import { usePythContext } from '../../contexts/PythContext'
|
|||
import { getMappingCluster, isPubkey } from './utils'
|
||||
import { PythCluster } from '@pythnetwork/client'
|
||||
import { lamportsToSol } from '../../utils/lamportsToSol'
|
||||
import { parseEvmExecuteCallData } from '../../utils/parseEvmExecuteCallData'
|
||||
|
||||
const GovernanceInstructionView = ({
|
||||
instruction,
|
||||
|
@ -408,12 +409,7 @@ export const WormholeInstructionView = ({
|
|||
<CopyText text={'0x' + governanceAction.callAddress} />
|
||||
</div>
|
||||
<div>Value: {governanceAction.value.toString()}</div>
|
||||
<div>
|
||||
Call Data:{' '}
|
||||
<CopyText
|
||||
text={'0x' + governanceAction.calldata.toString('hex')}
|
||||
/>
|
||||
</div>
|
||||
<EvmExecuteCallData calldata={governanceAction.calldata} />
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
|
@ -421,3 +417,33 @@ export const WormholeInstructionView = ({
|
|||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function EvmExecuteCallData({ calldata }: { calldata: Buffer }) {
|
||||
const callDataHex = calldata.toString('hex')
|
||||
const parsedData = parseEvmExecuteCallData(callDataHex)
|
||||
if (parsedData === undefined)
|
||||
return (
|
||||
<div>
|
||||
Call Data:
|
||||
<CopyText text={callDataHex} />
|
||||
</div>
|
||||
)
|
||||
|
||||
return (
|
||||
<>
|
||||
<div>Call Method: {parsedData.method}</div>
|
||||
<div>Call Params: </div>
|
||||
<div className="mx-4">
|
||||
{parsedData.inputs.length > 0 ? (
|
||||
parsedData.inputs.map(([key, value]) => (
|
||||
<div key={key}>
|
||||
{key}: {value}
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<div>No params</div>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
|
|
@ -35,8 +35,8 @@
|
|||
"react-hot-toast": "^2.4.0",
|
||||
"typescript": "4.9.4",
|
||||
"use-debounce": "^9.0.2",
|
||||
"xc_admin_common": "*",
|
||||
"message_buffer": "*"
|
||||
"web3": "^4.8.0",
|
||||
"xc_admin_common": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@svgr/webpack": "^6.3.1",
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
import Web3 from 'web3'
|
||||
|
||||
// Note: Currently, the ABI only contains the functions which take in primitives as inputs.
|
||||
// Though it is possible for EVM functions to accept structs as one of the inputs.
|
||||
// We don't support that right now. There is no requirement for that now or in the
|
||||
// foreseeable future. Adding it now will add unnecessary complexity.
|
||||
// It will be added when needed.
|
||||
const ABI = [
|
||||
{
|
||||
inputs: [],
|
||||
name: 'acceptOwnership',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [],
|
||||
name: 'acceptAdmin',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: 'newImplementation',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'upgradeTo',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
]
|
||||
|
||||
type Input = {
|
||||
internalType: string
|
||||
name: string
|
||||
type: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the call data for an EVM contract call only if the call data matches one of the entry in ABI.
|
||||
* If there is a match, the method name and inputs are returned. Else, undefined is returned.
|
||||
* @param callData The call data in hex format.
|
||||
* @returns The parsed call data or undefined.
|
||||
*/
|
||||
export function parseEvmExecuteCallData(callData: string):
|
||||
| {
|
||||
method: string
|
||||
inputs: [string, string][]
|
||||
}
|
||||
| undefined {
|
||||
for (const abi of ABI) {
|
||||
const web3 = new Web3()
|
||||
const methodSignature = web3.eth.abi
|
||||
.encodeFunctionSignature(abi)
|
||||
.replace('0x', '')
|
||||
|
||||
if (!callData.includes(methodSignature)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const inputs: Input[] = abi.inputs
|
||||
|
||||
const decodedParams = web3.eth.abi.decodeParameters(
|
||||
inputs.map((input) => ({
|
||||
type: input.type,
|
||||
name: input.name,
|
||||
})),
|
||||
callData.replace(methodSignature, '')
|
||||
)
|
||||
return {
|
||||
method: abi.name,
|
||||
inputs: inputs.map((input) => [
|
||||
input.name,
|
||||
decodedParams[input.name] as string,
|
||||
]),
|
||||
}
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue