Skip to content

Commit b0bbaf9

Browse files
yihuangsrdtrk
andauthored
chore(precompiles)!: use pure abi in abi.json (cosmos#758)
* Problem: precompile abi json is not pure abi Closes: cosmos#757 Solution: - cleanup the non-abi stuff from abi.json. - cleanup the abi parsing function. * Update CHANGELOG.md * fix lint * lint: ignore --------- Co-authored-by: srdtrk <[email protected]> Co-authored-by: srdtrk <[email protected]>
1 parent 1e4f7a7 commit b0bbaf9

File tree

24 files changed

+4209
-4339
lines changed

24 files changed

+4209
-4339
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### IMPROVEMENTS
88

9+
- [\#758](https://github.com/cosmos/evm/pull/758) Cleanup precompiles abi.json.
10+
911
### FEATURES
1012

1113
### BUG FIXES

contracts/utils/utils.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,6 @@ func ConvertHardhatBytesToCompiledContract(bz []byte) (evmtypes.CompiledContract
8282
return compiledContract, nil
8383
}
8484

85-
// ConvertPrecompileHardhatBytesToCompiledContract is a helper method to convert the embedded bytes from a
86-
// Hardhat JSON file into an instance of the CompiledContract type.
87-
//
88-
// NOTE: The precompile implementations have to special property that the binary data is empty.
89-
func ConvertPrecompileHardhatBytesToCompiledContract(bz []byte) (evmtypes.CompiledContract, error) {
90-
compiledContract, err := convertHardhatBzToCompiledContract(bz)
91-
if err != nil {
92-
return evmtypes.CompiledContract{}, err
93-
}
94-
95-
if len(compiledContract.Bin) != 0 {
96-
return evmtypes.CompiledContract{}, errors.New("expected binary data to be empty for precompile contract")
97-
}
98-
99-
return compiledContract, nil
100-
}
101-
10285
// convertHardhatBzToCompiledContract is a helper method to convert the embedded bytes from a
10386
// Hardhat JSON file into an instance of the CompiledContract type.
10487
func convertHardhatBzToCompiledContract(bz []byte) (evmtypes.CompiledContract, error) {

precompiles/bank/abi.json

Lines changed: 77 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,77 @@
1-
{
2-
"_format": "hh-sol-artifact-1",
3-
"contractName": "IBank",
4-
"sourceName": "solidity/precompiles/bank/IBank.sol",
5-
"abi": [
6-
{
7-
"inputs": [
8-
{
9-
"internalType": "address",
10-
"name": "account",
11-
"type": "address"
12-
}
13-
],
14-
"name": "balances",
15-
"outputs": [
16-
{
17-
"components": [
18-
{
19-
"internalType": "address",
20-
"name": "contractAddress",
21-
"type": "address"
22-
},
23-
{
24-
"internalType": "uint256",
25-
"name": "amount",
26-
"type": "uint256"
27-
}
28-
],
29-
"internalType": "struct Balance[]",
30-
"name": "balances",
31-
"type": "tuple[]"
32-
}
33-
],
34-
"stateMutability": "view",
35-
"type": "function"
36-
},
37-
{
38-
"inputs": [
39-
{
40-
"internalType": "address",
41-
"name": "erc20Address",
42-
"type": "address"
43-
}
44-
],
45-
"name": "supplyOf",
46-
"outputs": [
47-
{
48-
"internalType": "uint256",
49-
"name": "totalSupply",
50-
"type": "uint256"
51-
}
52-
],
53-
"stateMutability": "view",
54-
"type": "function"
55-
},
56-
{
57-
"inputs": [],
58-
"name": "totalSupply",
59-
"outputs": [
60-
{
61-
"components": [
62-
{
63-
"internalType": "address",
64-
"name": "contractAddress",
65-
"type": "address"
66-
},
67-
{
68-
"internalType": "uint256",
69-
"name": "amount",
70-
"type": "uint256"
71-
}
72-
],
73-
"internalType": "struct Balance[]",
74-
"name": "totalSupply",
75-
"type": "tuple[]"
76-
}
77-
],
78-
"stateMutability": "view",
79-
"type": "function"
80-
}
81-
],
82-
"bytecode": "0x",
83-
"deployedBytecode": "0x",
84-
"linkReferences": {},
85-
"deployedLinkReferences": {}
86-
}
1+
[
2+
{
3+
"inputs": [
4+
{
5+
"internalType": "address",
6+
"name": "account",
7+
"type": "address"
8+
}
9+
],
10+
"name": "balances",
11+
"outputs": [
12+
{
13+
"components": [
14+
{
15+
"internalType": "address",
16+
"name": "contractAddress",
17+
"type": "address"
18+
},
19+
{
20+
"internalType": "uint256",
21+
"name": "amount",
22+
"type": "uint256"
23+
}
24+
],
25+
"internalType": "struct Balance[]",
26+
"name": "balances",
27+
"type": "tuple[]"
28+
}
29+
],
30+
"stateMutability": "view",
31+
"type": "function"
32+
},
33+
{
34+
"inputs": [
35+
{
36+
"internalType": "address",
37+
"name": "erc20Address",
38+
"type": "address"
39+
}
40+
],
41+
"name": "supplyOf",
42+
"outputs": [
43+
{
44+
"internalType": "uint256",
45+
"name": "totalSupply",
46+
"type": "uint256"
47+
}
48+
],
49+
"stateMutability": "view",
50+
"type": "function"
51+
},
52+
{
53+
"inputs": [],
54+
"name": "totalSupply",
55+
"outputs": [
56+
{
57+
"components": [
58+
{
59+
"internalType": "address",
60+
"name": "contractAddress",
61+
"type": "address"
62+
},
63+
{
64+
"internalType": "uint256",
65+
"name": "amount",
66+
"type": "uint256"
67+
}
68+
],
69+
"internalType": "struct Balance[]",
70+
"name": "totalSupply",
71+
"type": "tuple[]"
72+
}
73+
],
74+
"stateMutability": "view",
75+
"type": "function"
76+
}
77+
]

precompiles/bank/bank.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
package bank
77

88
import (
9-
"embed"
9+
"bytes"
1010
"fmt"
1111

1212
"github.com/ethereum/go-ethereum/accounts/abi"
1313
"github.com/ethereum/go-ethereum/common"
1414
"github.com/ethereum/go-ethereum/core/vm"
1515

16+
_ "embed"
17+
1618
cmn "github.com/cosmos/evm/precompiles/common"
1719
evmtypes "github.com/cosmos/evm/x/vm/types"
1820

@@ -38,13 +40,13 @@ var (
3840
// Embed abi json file to the executable binary. Needed when importing as dependency.
3941
//
4042
//go:embed abi.json
41-
f embed.FS
43+
f []byte
4244
ABI abi.ABI
4345
)
4446

4547
func init() {
4648
var err error
47-
ABI, err = cmn.LoadABI(f, "abi.json")
49+
ABI, err = abi.JSON(bytes.NewReader(f))
4850
if err != nil {
4951
panic(err)
5052
}

precompiles/bech32/abi.json

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,45 @@
1-
{
2-
"_format": "hh-sol-artifact-1",
3-
"contractName": "Bech32I",
4-
"sourceName": "solidity/precompiles/bech32/Bech32I.sol",
5-
"abi": [
6-
{
7-
"inputs": [
8-
{
9-
"internalType": "string",
10-
"name": "bech32Address",
11-
"type": "string"
12-
}
13-
],
14-
"name": "bech32ToHex",
15-
"outputs": [
16-
{
17-
"internalType": "address",
18-
"name": "addr",
19-
"type": "address"
20-
}
21-
],
22-
"stateMutability": "nonpayable",
23-
"type": "function"
24-
},
25-
{
26-
"inputs": [
27-
{
28-
"internalType": "address",
29-
"name": "addr",
30-
"type": "address"
31-
},
32-
{
33-
"internalType": "string",
34-
"name": "prefix",
35-
"type": "string"
36-
}
37-
],
38-
"name": "hexToBech32",
39-
"outputs": [
40-
{
41-
"internalType": "string",
42-
"name": "bech32Address",
43-
"type": "string"
44-
}
45-
],
46-
"stateMutability": "nonpayable",
47-
"type": "function"
48-
}
49-
],
50-
"bytecode": "0x",
51-
"deployedBytecode": "0x",
52-
"linkReferences": {},
53-
"deployedLinkReferences": {}
54-
}
1+
[
2+
{
3+
"inputs": [
4+
{
5+
"internalType": "string",
6+
"name": "bech32Address",
7+
"type": "string"
8+
}
9+
],
10+
"name": "bech32ToHex",
11+
"outputs": [
12+
{
13+
"internalType": "address",
14+
"name": "addr",
15+
"type": "address"
16+
}
17+
],
18+
"stateMutability": "nonpayable",
19+
"type": "function"
20+
},
21+
{
22+
"inputs": [
23+
{
24+
"internalType": "address",
25+
"name": "addr",
26+
"type": "address"
27+
},
28+
{
29+
"internalType": "string",
30+
"name": "prefix",
31+
"type": "string"
32+
}
33+
],
34+
"name": "hexToBech32",
35+
"outputs": [
36+
{
37+
"internalType": "string",
38+
"name": "bech32Address",
39+
"type": "string"
40+
}
41+
],
42+
"stateMutability": "nonpayable",
43+
"type": "function"
44+
}
45+
]

precompiles/bech32/bech32.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package bech32
22

33
import (
4-
"embed"
4+
"bytes"
55
"fmt"
66

77
"github.com/ethereum/go-ethereum/accounts/abi"
88
"github.com/ethereum/go-ethereum/common"
99
"github.com/ethereum/go-ethereum/core/vm"
1010

11-
cmn "github.com/cosmos/evm/precompiles/common"
11+
_ "embed"
12+
1213
evmtypes "github.com/cosmos/evm/x/vm/types"
1314
)
1415

@@ -18,13 +19,13 @@ var (
1819
// Embed abi json file to the executable binary. Needed when importing as dependency.
1920
//
2021
//go:embed abi.json
21-
f embed.FS
22+
f []byte
2223
ABI abi.ABI
2324
)
2425

2526
func init() {
2627
var err error
27-
ABI, err = cmn.LoadABI(f, "abi.json")
28+
ABI, err = abi.JSON(bytes.NewReader(f))
2829
if err != nil {
2930
panic(err)
3031
}

precompiles/callbacks/abi.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package callbacks
22

33
import (
4-
"embed"
4+
"bytes"
55

66
"github.com/ethereum/go-ethereum/accounts/abi"
77

8-
cmn "github.com/cosmos/evm/precompiles/common"
8+
_ "embed"
99
)
1010

1111
// Embed abi json file to the executable binary. Needed when importing as dependency.
12-
//
13-
//go:embed abi.json
14-
var f embed.FS
12+
var (
13+
//go:embed abi.json
14+
f []byte
15+
ABI abi.ABI
16+
)
1517

16-
func LoadABI() (*abi.ABI, error) {
17-
newABI, err := cmn.LoadABI(f, "abi.json")
18+
func init() {
19+
var err error
20+
ABI, err = abi.JSON(bytes.NewReader(f))
1821
if err != nil {
19-
return nil, err
22+
panic(err)
2023
}
21-
22-
return &newABI, nil
2324
}

0 commit comments

Comments
 (0)