-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: deploy fee contract for transaction processing (#807)
Co-authored-by: todd <81545601+todd-woko@users.noreply.github.com>
- Loading branch information
1 parent
6a2f0a1
commit 08db739
Showing
17 changed files
with
5,303 additions
and
509 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package contract | ||
|
||
import ( | ||
"math/big" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
func DeployBridgeFeeContract( | ||
ctx sdk.Context, | ||
evmKeeper EvmKeeper, | ||
bridgeFeeQuoteKeeper BridgeFeeQuoteKeeper, | ||
bridgeFeeOracleKeeper BridgeFeeOracleKeeper, | ||
bridgeDenoms []BridgeDenoms, | ||
evmModuleAddress, owner, defaultOracleAddress common.Address, | ||
) error { | ||
if err := deployBridgeProxy( | ||
ctx, | ||
evmKeeper, | ||
GetBridgeFeeQuote().ABI, | ||
GetBridgeFeeQuote().Bin, | ||
common.HexToAddress(BridgeFeeAddress), | ||
evmModuleAddress, | ||
); err != nil { | ||
return err | ||
} | ||
if err := deployBridgeProxy( | ||
ctx, | ||
evmKeeper, | ||
GetBridgeFeeOracle().ABI, | ||
GetBridgeFeeOracle().Bin, | ||
common.HexToAddress(BridgeFeeOracleAddress), | ||
evmModuleAddress, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
if err := initBridgeFeeOracle(ctx, bridgeFeeOracleKeeper, owner, defaultOracleAddress); err != nil { | ||
return err | ||
} | ||
return initBridgeFeeQuote(ctx, bridgeFeeQuoteKeeper, bridgeDenoms, owner) | ||
} | ||
|
||
func deployBridgeProxy( | ||
ctx sdk.Context, | ||
evmKeeper EvmKeeper, | ||
logicABI abi.ABI, | ||
logicBin []byte, | ||
proxyAddress, evmModuleAddress common.Address, | ||
) error { | ||
logicContract, err := evmKeeper.DeployContract(ctx, evmModuleAddress, logicABI, logicBin) | ||
if err != nil { | ||
return err | ||
} | ||
if err = evmKeeper.CreateContractWithCode(ctx, proxyAddress, GetBridgeProxy().Code); err != nil { | ||
return err | ||
} | ||
if _, err = evmKeeper.ApplyContract(ctx, evmModuleAddress, proxyAddress, nil, GetBridgeProxy().ABI, "init", logicContract); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func initBridgeFeeOracle( | ||
ctx sdk.Context, | ||
bridgeFeeOracleKeeper BridgeFeeOracleKeeper, | ||
owner, defaultOracleAddress common.Address, | ||
) error { | ||
if _, err := bridgeFeeOracleKeeper.Initialize(ctx); err != nil { | ||
return err | ||
} | ||
role, err := bridgeFeeOracleKeeper.GetQuoteRole(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err = bridgeFeeOracleKeeper.GrantRole(ctx, role, common.HexToAddress(BridgeFeeAddress)); err != nil { | ||
return err | ||
} | ||
ownerRole, err := bridgeFeeOracleKeeper.GetOwnerRole(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err = bridgeFeeOracleKeeper.GrantRole(ctx, ownerRole, owner); err != nil { | ||
return err | ||
} | ||
upgradeRole, err := bridgeFeeOracleKeeper.GetUpgradeRole(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err = bridgeFeeOracleKeeper.GrantRole(ctx, upgradeRole, owner); err != nil { | ||
return err | ||
} | ||
if _, err = bridgeFeeOracleKeeper.SetDefaultOracle(ctx, defaultOracleAddress); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func initBridgeFeeQuote( | ||
ctx sdk.Context, | ||
bridgeFeeQuoteKeeper BridgeFeeQuoteKeeper, | ||
bridgeDenoms []BridgeDenoms, | ||
owner common.Address, | ||
) error { | ||
if _, err := bridgeFeeQuoteKeeper.Initialize(ctx, common.HexToAddress(BridgeFeeOracleAddress), big.NewInt(DefaultMaxQuoteIndex)); err != nil { | ||
return err | ||
} | ||
ownerRole, err := bridgeFeeQuoteKeeper.GetOwnerRole(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err = bridgeFeeQuoteKeeper.GrantRole(ctx, ownerRole, owner); err != nil { | ||
return err | ||
} | ||
upgradeRole, err := bridgeFeeQuoteKeeper.GetUpgradeRole(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err = bridgeFeeQuoteKeeper.GrantRole(ctx, upgradeRole, owner); err != nil { | ||
return err | ||
} | ||
for _, bridgeDenom := range bridgeDenoms { | ||
if _, err = bridgeFeeQuoteKeeper.RegisterChain(ctx, bridgeDenom.ChainName, bridgeDenom.Denoms...); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package contract | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/evmos/ethermint/x/evm/types" | ||
) | ||
|
||
type BridgeFeeOracleKeeper struct { | ||
Caller | ||
abi abi.ABI | ||
from common.Address | ||
contract common.Address | ||
} | ||
|
||
func NewBridgeFeeOracleKeeper(caller Caller, contract string) BridgeFeeOracleKeeper { | ||
return BridgeFeeOracleKeeper{ | ||
Caller: caller, | ||
abi: GetBridgeFeeOracle().ABI, | ||
from: common.BytesToAddress(authtypes.NewModuleAddress(types.ModuleName).Bytes()), | ||
contract: common.HexToAddress(contract), | ||
} | ||
} | ||
|
||
func (k BridgeFeeOracleKeeper) Initialize(ctx context.Context) (*types.MsgEthereumTxResponse, error) { | ||
return k.Caller.ApplyContract(ctx, k.from, k.contract, nil, k.abi, "initialize", common.HexToAddress(CrosschainAddress)) | ||
} | ||
|
||
func (k BridgeFeeOracleKeeper) GetOwnerRole(ctx context.Context) (common.Hash, error) { | ||
var res struct{ Role common.Hash } | ||
if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.from, k.contract, k.abi, "OWNER_ROLE", &res); err != nil { | ||
return common.Hash{}, err | ||
} | ||
return res.Role, nil | ||
} | ||
|
||
func (k BridgeFeeOracleKeeper) GetUpgradeRole(ctx context.Context) (common.Hash, error) { | ||
var res struct{ Role common.Hash } | ||
if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.from, k.contract, k.abi, "UPGRADE_ROLE", &res); err != nil { | ||
return common.Hash{}, err | ||
} | ||
return res.Role, nil | ||
} | ||
|
||
func (k BridgeFeeOracleKeeper) GetQuoteRole(ctx context.Context) (common.Hash, error) { | ||
var res struct{ Role common.Hash } | ||
if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.from, k.contract, k.abi, "QUOTE_ROLE", &res); err != nil { | ||
return common.Hash{}, err | ||
} | ||
return res.Role, nil | ||
} | ||
|
||
func (k BridgeFeeOracleKeeper) GrantRole(ctx context.Context, role common.Hash, account common.Address) (*types.MsgEthereumTxResponse, error) { | ||
return k.ApplyContract(ctx, k.from, k.contract, nil, k.abi, "grantRole", role, account) | ||
} | ||
|
||
func (k BridgeFeeOracleKeeper) SetDefaultOracle(ctx context.Context, oracle common.Address) (*types.MsgEthereumTxResponse, error) { | ||
return k.ApplyContract(ctx, k.from, k.contract, nil, k.abi, "setDefaultOracle", oracle) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.