Skip to content

Commit

Permalink
fix: decode contructor contract code cor-17
Browse files Browse the repository at this point in the history
  • Loading branch information
tiendn committed Jul 27, 2023
1 parent 2e8b514 commit b714b15
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 79 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"abi-decoder": "^2.4.0",
"axios": "^0.27.2",
"bignumber.js": "^9.1.0",
"canoe-solidity": "^0.1.0",
"chart.js": "^3.9.1",
"chartjs-adapter-dayjs": "^1.0.0",
"clsx": "^1.2.1",
Expand Down
33 changes: 21 additions & 12 deletions views/accounts/tabs/ContractCodeTab/ContructorArgs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { decodeConstructorArgs } from 'canoe-solidity'
import AutoLanguageView from 'components/CodeView/AutoView'
import { useMemo } from 'react'
import Web3 from 'web3'

interface DecodeArgItem {
name: string
Expand All @@ -18,17 +19,25 @@ const getData = (type, data) => {
}

const ContractConstructorArguments = ({ abi, constructorArgument }: Props) => {
if (!abi || !constructorArgument) return
const decodeArgs = decodeConstructorArgs(JSON.parse(abi), constructorArgument)
const code = `
${constructorArgument}
${decodeArgs.map(
(d: DecodeArgItem, index: number) => `Arg [${index}] (${d.type}): ${getData(d.type, d.data)} ${
d.name ? `(${d.name})` : ''
}
`
)}`.replaceAll(',', '')
const code = useMemo(() => {
const abiJson = JSON.parse(abi)
const contractConstructor = abiJson.find(item => item.type === 'constructor')
const decodeArgsInput = contractConstructor.inputs
const decodeArgsType: string[] = decodeArgsInput.map(item => item.type)
const web3 = new Web3()
const decodeArgs = web3.eth.abi.decodeParameters(decodeArgsType, constructorArgument)
const result = `${constructorArgument}
${decodeArgsType.map(
(d: string, index: number) => `Arg [${index}] (${d}): ${getData(d, decodeArgs[index])} (${
decodeArgsInput[index].name
})
`
)}`.replaceAll(',', '')
return result
}, [abi, constructorArgument])

if (!abi || !constructorArgument) return ''
return (
<div className="margin-bottom-xl">
<AutoLanguageView code={code} filename="Constructor Arguments" />
Expand Down
69 changes: 3 additions & 66 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3117,13 +3117,6 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/bn.js@^4.11.3":
version "4.11.6"
resolved "https://registry.yarnpkg.com/@types/bn.js/-/bn.js-4.11.6.tgz#c306c70d9358aaea33cd4eda092a742b9505967c"
integrity sha512-pqr857jrp2kPuO9uRjZ3PwnJTjoQy+fcdxvBTvHm6dkmEL9q+hDD/2j/0ELOBPtPnS8LjCX0gI9nbl8lVkadpg==
dependencies:
"@types/node" "*"

"@types/bn.js@^5.1.0":
version "5.1.1"
resolved "https://registry.yarnpkg.com/@types/bn.js/-/bn.js-5.1.1.tgz#b51e1b55920a4ca26e9285ff79936bbdec910682"
Expand Down Expand Up @@ -4041,7 +4034,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.6.tgz#53344adb14617a13f6e8dd2ce28905d1c0ba3215"
integrity sha512-XWwnNNFCuuSQ0m3r3C4LE3EiORltHd9M05pq6FOlVeiophzRbMo50Sbz1ehl8K3Z+jw9+vmgnXefY1hz8X+2wA==

bn.js@^4.11.0, bn.js@^4.11.6, bn.js@^4.11.8, bn.js@^4.11.9:
bn.js@^4.11.6, bn.js@^4.11.9:
version "4.12.0"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88"
integrity sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==
Expand Down Expand Up @@ -4295,15 +4288,6 @@ caniuse-lite@^1.0.30001406, caniuse-lite@^1.0.30001449:
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001451.tgz#2e197c698fc1373d63e1406d6607ea4617c613f1"
integrity sha512-XY7UbUpGRatZzoRft//5xOa69/1iGJRBlrieH6QYrkKLIFn3m7OVEJ81dSrKoy2BnKsdbX5cLrOispZNYo9v2w==

canoe-solidity@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/canoe-solidity/-/canoe-solidity-0.1.0.tgz#8082030569e4623626bb4adc6bbe50245e4b4002"
integrity sha512-lfkV5U4ABKGO/QdK0DonJS7UTr5qoZJVnfnqwVdYRqxS4fNns12g0XJRBZV4Zgc3VcR840SpV68x8WvrTy/Srw==
dependencies:
ethereumjs-abi "^0.6.5"
ethereumjs-util "^5.2.0"
underscore "^1.9.1"

cardinal@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/cardinal/-/cardinal-2.1.1.tgz#7cc1055d822d212954d07b085dea251cc7bc5505"
Expand Down Expand Up @@ -5352,7 +5336,7 @@ electron-to-chromium@^1.4.284:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.293.tgz#eaea755ad37a0f1759874a8597b33be62def2781"
integrity sha512-h7vBlhC83NsgC9UO3LOZx91xgstIrHk5iqMbZgnEArL5rHTM6HfsUZhnwb3oRnNetXM1741kB9SO7x9jLshz5A==

[email protected], elliptic@^6.4.0, elliptic@^6.5.2, elliptic@^6.5.4:
[email protected], elliptic@^6.4.0, elliptic@^6.5.4:
version "6.5.4"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb"
integrity sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ==
Expand Down Expand Up @@ -5879,40 +5863,6 @@ ethereum-cryptography@^0.1.3:
secp256k1 "^4.0.1"
setimmediate "^1.0.5"

ethereumjs-abi@^0.6.5:
version "0.6.8"
resolved "https://registry.yarnpkg.com/ethereumjs-abi/-/ethereumjs-abi-0.6.8.tgz#71bc152db099f70e62f108b7cdfca1b362c6fcae"
integrity sha512-Tx0r/iXI6r+lRsdvkFDlut0N08jWMnKRZ6Gkq+Nmw75lZe4e6o3EkSnkaBP5NF6+m5PTGAr9JP43N3LyeoglsA==
dependencies:
bn.js "^4.11.8"
ethereumjs-util "^6.0.0"

ethereumjs-util@^5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/ethereumjs-util/-/ethereumjs-util-5.2.1.tgz#a833f0e5fca7e5b361384dc76301a721f537bf65"
integrity sha512-v3kT+7zdyCm1HIqWlLNrHGqHGLpGYIhjeHxQjnDXjLT2FyGJDsd3LWMYUo7pAFRrk86CR3nUJfhC81CCoJNNGQ==
dependencies:
bn.js "^4.11.0"
create-hash "^1.1.2"
elliptic "^6.5.2"
ethereum-cryptography "^0.1.3"
ethjs-util "^0.1.3"
rlp "^2.0.0"
safe-buffer "^5.1.1"

ethereumjs-util@^6.0.0:
version "6.2.1"
resolved "https://registry.yarnpkg.com/ethereumjs-util/-/ethereumjs-util-6.2.1.tgz#fcb4e4dd5ceacb9d2305426ab1a5cd93e3163b69"
integrity sha512-W2Ktez4L01Vexijrm5EB6w7dg4n/TgpoYU4avuT5T3Vmnw/eCRtiBrJfQYS/DCSvDIOLn2k57GcHdeBcgVxAqw==
dependencies:
"@types/bn.js" "^4.11.3"
bn.js "^4.11.0"
create-hash "^1.1.2"
elliptic "^6.5.2"
ethereum-cryptography "^0.1.3"
ethjs-util "0.1.6"
rlp "^2.2.3"

ethereumjs-util@^7.1.0, ethereumjs-util@^7.1.1, ethereumjs-util@^7.1.2, ethereumjs-util@^7.1.5:
version "7.1.5"
resolved "https://registry.yarnpkg.com/ethereumjs-util/-/ethereumjs-util-7.1.5.tgz#9ecf04861e4fbbeed7465ece5f23317ad1129181"
Expand Down Expand Up @@ -5968,14 +5918,6 @@ [email protected]:
bn.js "4.11.6"
number-to-bn "1.7.0"

[email protected], ethjs-util@^0.1.3:
version "0.1.6"
resolved "https://registry.yarnpkg.com/ethjs-util/-/ethjs-util-0.1.6.tgz#f308b62f185f9fe6237132fb2a9818866a5cd536"
integrity sha512-CUnVOQq7gSpDHZVVrQW8ExxUETWrnrvXYvYz55wOU8Uj4VCgw56XC2B/fVqQN+f7gmrnRHSLVnFAwsCuNwji8w==
dependencies:
is-hex-prefixed "1.0.0"
strip-hex-prefix "1.0.0"

[email protected]:
version "6.4.7"
resolved "https://registry.yarnpkg.com/eventemitter2/-/eventemitter2-6.4.7.tgz#a7f6c4d7abf28a14c1ef3442f21cb306a054271d"
Expand Down Expand Up @@ -10912,7 +10854,7 @@ ripemd160@^2.0.0, ripemd160@^2.0.1, ripemd160@^2.0.2:
hash-base "^3.0.0"
inherits "^2.0.1"

rlp@^2.0.0, rlp@^2.2.3, rlp@^2.2.4:
rlp@^2.2.4:
version "2.2.7"
resolved "https://registry.yarnpkg.com/rlp/-/rlp-2.2.7.tgz#33f31c4afac81124ac4b283e2bd4d9720b30beaf"
integrity sha512-d5gdPmgQ0Z+AklL2NVXr/IoSjNZFfTVvQWzL/AM2AOcSzYP2xjlb0AC8YyCLc41MSNf6P6QVtjgPdmVtzb+4lQ==
Expand Down Expand Up @@ -12022,11 +11964,6 @@ unbox-primitive@^1.0.2:
has-symbols "^1.0.3"
which-boxed-primitive "^1.0.2"

underscore@^1.9.1:
version "1.13.6"
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.13.6.tgz#04786a1f589dc6c09f761fc5f45b89e935136441"
integrity sha512-+A5Sja4HP1M08MaXya7p5LvjuM7K6q/2EaC0+iovj/wOcMsTzMvDFbasi/oSapiwOlt252IqsKqPjCl7huKS0A==

unicode-canonical-property-names-ecmascript@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.0.tgz#301acdc525631670d39f6146e0e77ff6bbdebddc"
Expand Down

0 comments on commit b714b15

Please sign in to comment.