Skip to content

Commit

Permalink
imp(scripts): Adjust contract compilation util to include abi.json fi…
Browse files Browse the repository at this point in the history
…les (evmos#2650)

* adjust script to include abi.json files too

* rename vesting interface file to match interface name

* adjust utils and precompile ABI loading for Hardhat setup

* clean up after compiling contracts with all target

* recompile abis

* add changelog entry

* run black formatter

* address some more linters

---------

Co-authored-by: Tom <[email protected]>
  • Loading branch information
MalteHerrmann and GAtom22 authored Jul 2, 2024
1 parent 7609f4d commit cd4647d
Show file tree
Hide file tree
Showing 26 changed files with 3,790 additions and 3,684 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (ci) [#2639](https://github.com/evmos/evmos/pull/2639) Ignore Swagger JSON in CheckOV linter.
- (app) [#2644](https://github.com/evmos/evmos/pull/2644) Split full chain configuration template into EVM and MemIAVL templates.
- (ante) [#2648](https://github.com/evmos/evmos/pull/2648) Decouple EVM mono decorator utilities from concrete decorator implementation.
- (contracts) [#2650](https://github.com/evmos/evmos/pull/2650) Adjust contract compilation util to include `abi.json` files.

## [v18.1.0](https://github.com/evmos/evmos/releases/tag/v18.1.0) - 2024-05-31

Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,9 @@ release:
### Compile Solidity Contracts ###
###############################################################################

# Clean up the contracts directory, install the necessary dependencies
# and then compile the solidity contracts found in the Evmos repository.
contracts-all: contracts-clean contracts-compile
# Install the necessary dependencies, compile the solidity contracts found in the
# Evmos repository and then clean up the contracts data.
contracts-all: contracts-compile contracts-clean

# Clean smart contract compilation artifacts, dependencies and cache files
contracts-clean:
Expand Down
36 changes: 34 additions & 2 deletions contracts/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ func loadCompiledBytesFromJSONFile(jsonFile string) ([]byte, error) {
// ConvertHardhatBytesToCompiledContract is a helper method to convert the embedded bytes from a
// Hardhat JSON file into an instance of the CompiledContract type.
func ConvertHardhatBytesToCompiledContract(bz []byte) (evmtypes.CompiledContract, error) {
compiledContract, err := convertHardhatBzToCompiledContract(bz)
if err != nil {
return evmtypes.CompiledContract{}, err
}

if len(compiledContract.Bin) == 0 {
return evmtypes.CompiledContract{}, errors.New("got empty binary data for contract")
}

return compiledContract, nil
}

// ConvertPrecompileHardhatBytesToCompiledContract is a helper method to convert the embedded bytes from a
// Hardhat JSON file into an instance of the CompiledContract type.
//
// NOTE: The precompile implementations have to special property that the binary data is empty.
func ConvertPrecompileHardhatBytesToCompiledContract(bz []byte) (evmtypes.CompiledContract, error) {
compiledContract, err := convertHardhatBzToCompiledContract(bz)
if err != nil {
return evmtypes.CompiledContract{}, err
}

if len(compiledContract.Bin) != 0 {
return evmtypes.CompiledContract{}, errors.New("expected binary data to be empty for precompile contract")
}

return compiledContract, nil
}

// convertHardhatBzToCompiledContract is a helper method to convert the embedded bytes from a
// Hardhat JSON file into an instance of the CompiledContract type.
func convertHardhatBzToCompiledContract(bz []byte) (evmtypes.CompiledContract, error) {
var hardhatContract evmtypes.HardhatCompiledContract
err := json.Unmarshal(bz, &hardhatContract)
if err != nil {
Expand All @@ -84,8 +116,8 @@ func ConvertHardhatBytesToCompiledContract(bz []byte) (evmtypes.CompiledContract
return evmtypes.CompiledContract{}, err
}

if len(compiledContract.Bin) == 0 {
return evmtypes.CompiledContract{}, errors.New("got empty binary data for contract")
if len(compiledContract.ABI.Methods) == 0 {
return evmtypes.CompiledContract{}, errors.New("got empty array of methods for ABI")
}

return compiledContract, nil
Expand Down
163 changes: 86 additions & 77 deletions precompiles/bank/abi.json
Original file line number Diff line number Diff line change
@@ -1,77 +1,86 @@
[
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balances",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "contractAddress",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"internalType": "struct Balance[]",
"name": "balances",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "erc20Address",
"type": "address"
}
],
"name": "supplyOf",
"outputs": [
{
"internalType": "uint256",
"name": "totalSupply",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "contractAddress",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"internalType": "struct Balance[]",
"name": "totalSupply",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
}
]
{
"_format": "hh-sol-artifact-1",
"contractName": "IBank",
"sourceName": "solidity/precompiles/bank/IBank.sol",
"abi": [
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balances",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "contractAddress",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"internalType": "struct Balance[]",
"name": "balances",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "erc20Address",
"type": "address"
}
],
"name": "supplyOf",
"outputs": [
{
"internalType": "uint256",
"name": "totalSupply",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "contractAddress",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"internalType": "struct Balance[]",
"name": "totalSupply",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
}
],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}
99 changes: 54 additions & 45 deletions precompiles/bech32/abi.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
[
{
"inputs": [
{
"internalType": "string",
"name": "bech32Address",
"type": "string"
}
],
"name": "bech32ToHex",
"outputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
},
{
"internalType": "string",
"name": "prefix",
"type": "string"
}
],
"name": "hexToBech32",
"outputs": [
{
"internalType": "string",
"name": "bech32Address",
"type": "string"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
{
"_format": "hh-sol-artifact-1",
"contractName": "Bech32I",
"sourceName": "solidity/precompiles/bech32/Bech32I.sol",
"abi": [
{
"inputs": [
{
"internalType": "string",
"name": "bech32Address",
"type": "string"
}
],
"name": "bech32ToHex",
"outputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
},
{
"internalType": "string",
"name": "prefix",
"type": "string"
}
],
"name": "hexToBech32",
"outputs": [
{
"internalType": "string",
"name": "bech32Address",
"type": "string"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}
7 changes: 4 additions & 3 deletions precompiles/common/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package common

import (
"bytes"
"embed"
"fmt"
"math/big"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/common/math"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
contractutils "github.com/evmos/evmos/v18/contracts/utils"
)

// MakeTopic converts a filter query argument into a filter topic.
Expand Down Expand Up @@ -138,9 +138,10 @@ func LoadABI(fs embed.FS, path string) (abi.ABI, error) {
return abi.ABI{}, fmt.Errorf("error loading the ABI %s", err)
}

newAbi, err := abi.JSON(bytes.NewReader(abiBz))
contract, err := contractutils.ConvertPrecompileHardhatBytesToCompiledContract(abiBz)
if err != nil {
return abi.ABI{}, fmt.Errorf(ErrInvalidABI, err)
}
return newAbi, nil

return contract.ABI, nil
}
Loading

0 comments on commit cd4647d

Please sign in to comment.