Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

chore: Fix Module Accounts for Precompile Contracts #653

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions cosmos/precompile/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ package auth_test

import (
"context"
"fmt"
"math/big"
"testing"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"pkg.berachain.dev/polaris/cosmos/runtime/config"
erc20types "pkg.berachain.dev/polaris/cosmos/x/erc20/types"

generated "pkg.berachain.dev/polaris/contracts/bindings/cosmos/precompile"
cosmlib "pkg.berachain.dev/polaris/cosmos/lib"
Expand All @@ -48,9 +55,50 @@ var _ = Describe("Address Precompile", func() {
var contract *auth.Contract

BeforeEach(func() {
config.SetupCosmosConfig()
contract = utils.MustGetAs[*auth.Contract](auth.NewPrecompileContract())
})

It("print module addrs", func() {
authAddr := authtypes.NewModuleAddress(authtypes.ModuleName)
fmt.Println(authtypes.ModuleName)
fmt.Println("bech32", authAddr.String())
fmt.Println("hex ", cosmlib.AccAddressToEthAddress(authAddr).Hex())
fmt.Println()

stakingAddr := authtypes.NewModuleAddress(stakingtypes.ModuleName)
fmt.Println(stakingtypes.ModuleName)
fmt.Println("bech32", stakingAddr.String())
fmt.Println("hex ", cosmlib.AccAddressToEthAddress(stakingAddr).Hex())
fmt.Println()

bankAddr := authtypes.NewModuleAddress(banktypes.ModuleName)
fmt.Println(banktypes.ModuleName)
fmt.Println("bech32", bankAddr.String())
fmt.Println("hex ", cosmlib.AccAddressToEthAddress(bankAddr).Hex())
fmt.Println()

govAddr := authtypes.NewModuleAddress(govtypes.ModuleName)
fmt.Println(govtypes.ModuleName)
fmt.Println("bech32", govAddr.String())
fmt.Println("hex ", cosmlib.AccAddressToEthAddress(govAddr).Hex())
fmt.Println()

distrAddr := authtypes.NewModuleAddress(distrtypes.ModuleName)
fmt.Println(distrtypes.ModuleName)
fmt.Println("bech32", distrAddr.String())
fmt.Println("hex ", cosmlib.AccAddressToEthAddress(distrAddr).Hex())
fmt.Println()

erc20Addr := authtypes.NewModuleAddress(erc20types.ModuleName)
fmt.Println(erc20types.ModuleName)
fmt.Println("bech32", erc20Addr.String())
fmt.Println("hex ", cosmlib.AccAddressToEthAddress(erc20Addr).Hex())
fmt.Println()

panic("see logs")
})

It("should have static registry key", func() {
Expect(contract.RegistryKey()).To(Equal(
cosmlib.AccAddressToEthAddress(authtypes.NewModuleAddress(authtypes.ModuleName))),
Expand Down
4 changes: 3 additions & 1 deletion cosmos/precompile/distribution/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

generated "pkg.berachain.dev/polaris/contracts/bindings/cosmos/precompile"
cosmlib "pkg.berachain.dev/polaris/cosmos/lib"
"pkg.berachain.dev/polaris/cosmos/precompile"
"pkg.berachain.dev/polaris/cosmos/x/evm/plugins/precompile/log"
"pkg.berachain.dev/polaris/eth/common"
Expand All @@ -50,7 +52,7 @@ func NewPrecompileContract(
return &Contract{
BaseContract: ethprecompile.NewBaseContract(
generated.DistributionModuleMetaData.ABI,
common.BytesToAddress([]byte{0x69}),
cosmlib.AccAddressToEthAddress(authtypes.NewModuleAddress(distributiontypes.ModuleName)),
),
msgServer: m,
querier: q,
Expand Down
8 changes: 4 additions & 4 deletions cosmos/precompile/erc20/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

cpbindings "pkg.berachain.dev/polaris/contracts/bindings/cosmos/precompile"
pbindings "pkg.berachain.dev/polaris/contracts/bindings/polaris"
Expand Down Expand Up @@ -53,10 +54,9 @@ func NewPrecompileContract(bk cosmlib.BankKeeper, em ERC20Module) ethprecompile.
return &Contract{
BaseContract: ethprecompile.NewBaseContract(
cpbindings.ERC20ModuleMetaData.ABI,
// cosmlib.AccAddressToEthAddress(
// authtypes.NewModuleAddress(erc20types.ModuleName),
// ),
common.HexToAddress("0x696969"), // TODO: module addresses are broken
cosmlib.AccAddressToEthAddress(
authtypes.NewModuleAddress(erc20types.ModuleName),
),
),
bk: bk,
em: em,
Expand Down
12 changes: 12 additions & 0 deletions cosmos/runtime/localnode/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
# TITLE.


PRECOMPILES[0]="polar1mx5e3jkxvzf8frl7cl8m69264ctn0shlz6zxjx" # staking
PRECOMPILES[1]="polar1gwqac243g2z3vryqsev6acq965f9ttwhjgk226" # bank
PRECOMPILES[2]="polar10d07y265gmmuvt4z0w9aw880jnsr700j33u8zp" # governance
PRECOMPILES[3]="polar1hh6fc0pcsggzlsqhl7mxzyyvvw5rd5r9vff2ea" # auth
PRECOMPILES[4]="polar1jv65s3grqf6v6jl3dp4t6c9t9rk99cd8vvt7ad" # distribution
PRECOMPILES[5]="polar1glht96kr2rseywuvhhay894qw7ekuc4qphuca5" # erc20

KEYS[0]="dev0"
KEYS[1]="dev1"
KEYS[2]="dev2"
Expand Down Expand Up @@ -99,6 +106,11 @@ if [[ $overwrite == "y" || $overwrite == "Y" ]]; then
# 0xfffdbb37105441e14b0ee6330d855d8504ff39e705c3afa8f859ac9865f99306
./bin/polard genesis add-genesis-account polar1yrene6g2zwjttemf0c65fscg8w8c55w5vhc9hd 69000000000000000000000000abera --keyring-backend $KEYRING --home "$HOMEDIR"

## Fund all precompile addresses with 1abera (https://ethereum.stackexchange.com/questions/68056/puppeth-precompile-addresses)
for PRECOMPILE in "${PRECOMPILES[@]}"; do
./bin/polard genesis add-genesis-account $PRECOMPILE 1abera --keyring-backend $KEYRING --home "$HOMEDIR"
done

# Sign genesis transaction
./bin/polard genesis gentx ${KEYS[0]} 1000000000000000000000abera --keyring-backend $KEYRING --chain-id $CHAINID --home "$HOMEDIR"
## In case you want to create multiple validators at genesis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

bindings "pkg.berachain.dev/polaris/contracts/bindings/cosmos/precompile"
tbindings "pkg.berachain.dev/polaris/contracts/bindings/testing"
Expand Down Expand Up @@ -54,12 +57,14 @@ var _ = SynchronizedBeforeSuite(func() []byte {
tf = integration.NewTestFixture(GinkgoT())
// Setup the governance precompile.
precompile, _ = bindings.NewDistributionModule(
common.HexToAddress("0x69"),
cosmlib.AccAddressToEthAddress(authtypes.NewModuleAddress(distributiontypes.ModuleName)),
tf.EthClient,
)
// Setup the staking precompile.
stakingPrecompile, _ = bindings.NewStakingModule(
common.HexToAddress("0xd9A998CaC66092748FfEc7cFBD155Aae1737C2fF"), tf.EthClient)
cosmlib.AccAddressToEthAddress(authtypes.NewModuleAddress(stakingtypes.ModuleName)),
tf.EthClient,
)
// Set the validator address.
validator = common.Address(tf.Network.Validators[0].Address.Bytes())
return nil
Expand Down
14 changes: 8 additions & 6 deletions cosmos/testing/integration/precompile/erc20/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

bindings "pkg.berachain.dev/polaris/contracts/bindings/cosmos/precompile"
pbindings "pkg.berachain.dev/polaris/contracts/bindings/polaris"
tbindings "pkg.berachain.dev/polaris/contracts/bindings/testing"
cosmlib "pkg.berachain.dev/polaris/cosmos/lib"
"pkg.berachain.dev/polaris/cosmos/testing/integration"
erc20types "pkg.berachain.dev/polaris/cosmos/x/erc20/types"
"pkg.berachain.dev/polaris/eth/common"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -47,17 +49,17 @@ var (
tf *integration.TestFixture
erc20Precompile *bindings.ERC20Module
bankPrecompile *bindings.BankModule
erc20ModuleAddress = common.HexToAddress("0x696969")
// cosmlib.AccAddressToEthAddress(
// authtypes.NewModuleAddress(erc20types.ModuleName),
// ).
erc20ModuleAddress = cosmlib.AccAddressToEthAddress(
authtypes.NewModuleAddress(erc20types.ModuleName),
)
)

var _ = SynchronizedBeforeSuite(func() []byte {
// Setup the network and clients here.
tf = integration.NewTestFixture(GinkgoT())
bankPrecompile, _ = bindings.NewBankModule(
common.HexToAddress("0x4381dC2aB14285160c808659aEe005D51255adD7"), tf.EthClient,
cosmlib.AccAddressToEthAddress(authtypes.NewModuleAddress(banktypes.ModuleName)),
tf.EthClient,
)
erc20Precompile, _ = bindings.NewERC20Module(erc20ModuleAddress, tf.EthClient)
return nil
Expand Down
5 changes: 2 additions & 3 deletions docs/web/pages/docs/precompile-cosmos-overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ A Stateful Precompile is a precompiled contract that can alter chain state.
| Precompile Contract | Contract Address | Solidity Interface | Associated Module |
| :----------------------------- | :------------------------------------------- | :------------------------------------------------------------------------------------------------------------------ | :---------------------------------------------------------------------------- |
| Staking Precompile | `0xd9A998CaC66092748FfEc7cFBD155Aae1737C2fF` | [staking.sol](https://github.com/berachain/polaris/blob/main/contracts/src/cosmos/precompile/staking.sol) | [Staking Module](https://docs.cosmos.network/v0.47/modules/staking) |
| Multicall Precompile | Coming soon | [multicall.sol]() | [Ethereum Multicall](https://github.com/mds1/multicall) |
| Bank Precompile | `0x4381dC2aB14285160c808659aEe005D51255adD7` | [bank.sol](https://github.com/berachain/polaris/blob/main/contracts/src/cosmos/precompile/bank.sol) | [Bank Module](https://docs.cosmos.network/v0.47/modules/bank) |
| IBC Precompile | Coming soon | [ibc.sol]() | [IBC](https://ibc.cosmos.network/) |
| Governance Module Precompile | `0x7b5Fe22B5446f7C62Ea27B8BD71CeF94e03f3dF2` | [governance.sol](https://github.com/berachain/polaris/blob/main/contracts/src/cosmos/precompile/governance.sol) | [Governance Module](https://docs.cosmos.network/v0.47/modules/gov) |
| Auth Module Precompile | `0xBDF49C3C3882102fc017FFb661108c63a836D065` | [address.sol](https://github.com/berachain/polaris/blob/main/contracts/src/cosmos/precompile/auth.sol) | [Auth Module](https://docs.cosmos.network/main/modules/auth) |
| Distribution Module Precompile | `0x0000000000000000000000000000000000000069` | [distribution.sol](https://github.com/berachain/polaris/blob/main/contracts/src/cosmos/precompile/distribution.sol) | [Distribution Module](https://docs.cosmos.network/v0.47/modules/distribution) |
| ERC20 Module Precompile | `0x0000000000000000000000000000000000696969` | [erc20.sol](https://github.com/berachain/polaris/blob/main/contracts/src/cosmos/precompile/erc20.sol) | [ERC20 Module](./erc20-middleware) |
| Distribution Module Precompile | `0x93354845030274cD4bf1686Abd60AB28EC52e1a7` | [distribution.sol](https://github.com/berachain/polaris/blob/main/contracts/src/cosmos/precompile/distribution.sol) | [Distribution Module](https://docs.cosmos.network/v0.47/modules/distribution) |
| ERC20 Module Precompile | `0x47EeB2eac350E1923b8CBDfA4396A077b36E62a0` | [erc20.sol](https://github.com/berachain/polaris/blob/main/contracts/src/cosmos/precompile/erc20.sol) | [ERC20 Module](./erc20-middleware) |

## Stateful Precompile ABI Generation

Expand Down