Skip to content

Commit

Permalink
Remove IsMintable native token flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Nov 27, 2023
1 parent 66a2c4f commit 797e037
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 577 deletions.
30 changes: 1 addition & 29 deletions command/bridge/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const (
exitHelperName = "ExitHelper"
rootERC20PredicateName = "RootERC20Predicate"
childERC20MintablePredicateName = "ChildERC20MintablePredicate"
rootERC20Name = "RootERC20"
erc20TemplateName = "ERC20Template"
rootERC721PredicateName = "RootERC721Predicate"
childERC721MintablePredicateName = "ChildERC721MintablePredicate"
Expand Down Expand Up @@ -78,9 +77,6 @@ var (
rootchainConfig *polybft.RootchainConfig, addr types.Address) {
rootchainConfig.ChildMintableERC20PredicateAddress = addr
},
rootERC20Name: func(rootchainConfig *polybft.RootchainConfig, addr types.Address) {
rootchainConfig.RootNativeERC20Address = addr
},
erc20TemplateName: func(rootchainConfig *polybft.RootchainConfig, addr types.Address) {
rootchainConfig.ChildERC20Address = addr
},
Expand Down Expand Up @@ -133,7 +129,7 @@ var (
NewChildERC20Predicate: contracts.ChildERC20PredicateContract,
NewChildTokenTemplate: contracts.ChildERC20Contract,
// map root native token address should be non-zero only if native token is non-mintable on a childchain
NewNativeTokenRoot: config.RootNativeERC20Address,
NewNativeTokenRoot: types.ZeroAddress,
}

return initContract(fmt, relayer, inputParams,
Expand Down Expand Up @@ -247,13 +243,6 @@ func GetCommand() *cobra.Command {
"the JSON RPC rootchain IP address",
)

cmd.Flags().StringVar(
&params.rootERC20TokenAddr,
erc20AddrFlag,
"",
"existing root chain root native token address",
)

cmd.Flags().BoolVar(
&params.isTestMode,
helper.TestModeFlag,
Expand Down Expand Up @@ -419,21 +408,6 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
StakeManagerAddress: types.StringToAddress(params.stakeManagerAddr),
}

tokenContracts := []*contractInfo{}

// deploy root ERC20 token only if non-mintable native token flavor is used on a child chain
if params.rootERC20TokenAddr != "" {
// use existing root chain ERC20 token
if err := populateExistingTokenAddr(client.Eth(),
params.rootERC20TokenAddr, rootERC20Name, rootchainConfig); err != nil {
return deploymentResultInfo{RootchainCfg: nil, CommandResults: nil}, err
}
} else {
// deploy MockERC20 as a root chain root native token
tokenContracts = append(tokenContracts,
&contractInfo{name: rootERC20Name, artifact: contractsapi.RootERC20})
}

allContracts := []*contractInfo{
{
name: stateSenderName,
Expand Down Expand Up @@ -515,8 +489,6 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
},
}

allContracts = append(tokenContracts, allContracts...)

g, ctx := errgroup.WithContext(cmdCtx)
results := make(map[string]*deployContractResult, len(allContracts))
resultsLock := sync.Mutex{}
Expand Down
7 changes: 3 additions & 4 deletions command/bridge/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ func TestDeployContracts_NoPanics(t *testing.T) {
params.proxyContractsAdmin = "0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed"
consensusCfg = polybft.PolyBFTConfig{
NativeTokenConfig: &polybft.TokenConfig{
Name: "Test",
Symbol: "TST",
Decimals: 18,
IsMintable: false,
Name: "Test",
Symbol: "TST",
Decimals: 18,
},
}

Expand Down
7 changes: 0 additions & 7 deletions command/bridge/deploy/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import (
const (
deployerKeyFlag = "deployer-key"
jsonRPCFlag = "json-rpc"
erc20AddrFlag = "erc20-token"
)

type deployParams struct {
genesisPath string
deployerKey string
jsonRPCAddress string
stakeTokenAddr string
rootERC20TokenAddr string
stakeManagerAddr string
proxyContractsAdmin string
isTestMode bool
Expand All @@ -42,11 +40,6 @@ func (ip *deployParams) validateFlags() error {
return errors.New("native token configuration is undefined")
}

// when using mintable native token, child native token on root chain gets mapped automatically
if consensusCfg.NativeTokenConfig.IsMintable && ip.rootERC20TokenAddr != "" {
return errors.New("if child chain native token is mintable, root native token must not pre-exist on root chain")
}

if params.stakeTokenAddr == "" {
return errors.New("stake token address is not provided")
}
Expand Down
4 changes: 1 addition & 3 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ var (
errBaseFeeEMZero = errors.New("base fee elasticity multiplier must be greater than 0")
errBaseFeeZero = errors.New("base fee must be greater than 0")
errRewardWalletNotDefined = errors.New("reward wallet address must be defined")
errRewardTokenOnNonMintable = errors.New("a custom reward token must be defined when " +
"native ERC20 token is non-mintable")
errRewardWalletZero = errors.New("reward wallet address must not be zero address")
errRewardWalletZero = errors.New("reward wallet address must not be zero address")
)

type genesisParams struct {
Expand Down
18 changes: 5 additions & 13 deletions command/genesis/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ func Test_extractNativeTokenMetadata(t *testing.T) {
name: "valid config",
rawConfig: "MyToken:MTK:9:0x123456789",
expectedCfg: &polybft.TokenConfig{
Name: "MyToken",
Symbol: "MTK",
Decimals: 9,
IsMintable: true,
Owner: types.StringToAddress("0x123456789"),
Name: "MyToken",
Symbol: "MTK",
Decimals: 9,
Owner: types.StringToAddress("0x123456789"),
},
expectErr: false,
},
Expand Down Expand Up @@ -185,13 +184,6 @@ func Test_validateRewardWallet(t *testing.T) {
isNativeERC20Mintable: true,
expectValidateErr: nil,
},
{
name: "valid reward wallet: native ERC20 mintable",
rewardWallet: types.StringToAddress("1").String() + ":0",
epochReward: 0,
isNativeERC20Mintable: false,
expectValidateErr: errRewardTokenOnNonMintable,
},
}
for _, c := range cases {
c := c
Expand All @@ -201,7 +193,7 @@ func Test_validateRewardWallet(t *testing.T) {
p := &genesisParams{
rewardWallet: c.rewardWallet,
epochReward: c.epochReward,
nativeTokenConfig: &polybft.TokenConfig{IsMintable: c.isNativeERC20Mintable},
nativeTokenConfig: &polybft.TokenConfig{},
}
err := p.validateRewardWalletAndToken()
require.ErrorIs(t, err, c.expectValidateErr)
Expand Down
34 changes: 4 additions & 30 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ const (

var (
errNoGenesisValidators = errors.New("genesis validators aren't provided")
errNoPremineAllowed = errors.New("native token is not mintable, so no premine is allowed " +
"except for zero address and reward wallet if native token is used as reward token")
)

type contractInfo struct {
Expand All @@ -81,16 +79,6 @@ func (p *genesisParams) generateChainConfig(o command.OutputFormatter) error {
return fmt.Errorf("invalid reward wallet configuration provided '%s' : %w", p.rewardWallet, err)
}

if !p.nativeTokenConfig.IsMintable {
// validate premine map, no premine is allowed if token is not mintable,
// except for the reward wallet (if native token is used as reward token) and zero address
for a := range premineBalances {
if a != types.ZeroAddress && (p.rewardTokenCode != "" || a != walletPremineInfo.Address) {
return errNoPremineAllowed
}
}
}

var (
rewardTokenByteCode []byte
rewardTokenAddr = contracts.NativeERC20TokenContract
Expand Down Expand Up @@ -339,20 +327,10 @@ func (p *genesisParams) deployContracts(
artifact: contractsapi.RootERC20,
address: contracts.ERC20Contract,
},
}

if !params.nativeTokenConfig.IsMintable {
genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.NativeERC20,
address: contracts.NativeERC20TokenContractV1,
})
} else {
genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.NativeERC20,
address: contracts.NativeERC20TokenContractV1,
})
{
artifact: contractsapi.NativeERC20,
address: contracts.NativeERC20TokenContractV1,
},
}

if len(params.bridgeAllowListAdmin) != 0 || len(params.bridgeBlockListAdmin) != 0 {
Expand Down Expand Up @@ -540,10 +518,6 @@ func (p *genesisParams) validateRewardWalletAndToken() error {
return errRewardWalletNotDefined
}

if !p.nativeTokenConfig.IsMintable && p.rewardTokenCode == "" {
return errRewardTokenOnNonMintable
}

premineInfo, err := helper.ParsePremineInfo(p.rewardWallet)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/contracts_initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func getInitERC20PredicateInput(config *BridgeConfig, childChainMintable bool) (
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC20Predicate: config.RootERC20PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC20Contract,
NewNativeTokenRootAddress: config.RootNativeERC20Addr,
NewNativeTokenRootAddress: types.ZeroAddress,
}
}

Expand All @@ -133,7 +133,7 @@ func getInitERC20PredicateACLInput(config *BridgeConfig, owner types.Address,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC20Predicate: config.RootERC20PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC20Contract,
NewNativeTokenRootAddress: config.RootNativeERC20Addr,
NewNativeTokenRootAddress: types.ZeroAddress,
NewUseAllowList: useAllowList,
NewUseBlockList: useBlockList,
NewOwner: owner,
Expand Down
1 change: 0 additions & 1 deletion consensus/polybft/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func createTestBridgeConfig() *BridgeConfig {
ExitHelperAddr: types.StringToAddress("3"),
RootERC20PredicateAddr: types.StringToAddress("4"),
ChildMintableERC20PredicateAddr: types.StringToAddress("5"),
RootNativeERC20Addr: types.StringToAddress("6"),
RootERC721PredicateAddr: types.StringToAddress("8"),
ChildMintableERC721PredicateAddr: types.StringToAddress("9"),
RootERC1155PredicateAddr: types.StringToAddress("11"),
Expand Down
56 changes: 17 additions & 39 deletions consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,47 +343,25 @@ func GenesisPostHookFactory(config *chain.Chain, engineName string) func(txn *st
}
}

if polyBFTConfig.NativeTokenConfig.IsMintable {
// initialize NativeERC20Mintable SC
params := &contractsapi.InitializeNativeERC20Fn{
Predicate_: contracts.ChildERC20PredicateContract,
Owner_: polyBFTConfig.NativeTokenConfig.Owner,
RootToken_: types.ZeroAddress, // in case native mintable token is used, it is always root token
Name_: polyBFTConfig.NativeTokenConfig.Name,
Symbol_: polyBFTConfig.NativeTokenConfig.Symbol,
Decimals_: polyBFTConfig.NativeTokenConfig.Decimals,
TokenSupply_: initialTotalSupply,
}

input, err := params.EncodeAbi()
if err != nil {
return err
}

if err = callContract(contracts.SystemCaller,
contracts.NativeERC20TokenContract, input, "NativeERC20Mintable", transition); err != nil {
return err
}
} else {
// initialize NativeERC20 SC
params := &contractsapi.InitializeNativeERC20Fn{
Name_: polyBFTConfig.NativeTokenConfig.Name,
Symbol_: polyBFTConfig.NativeTokenConfig.Symbol,
Decimals_: polyBFTConfig.NativeTokenConfig.Decimals,
RootToken_: polyBFTConfig.Bridge.RootNativeERC20Addr,
Predicate_: contracts.ChildERC20PredicateContract,
TokenSupply_: initialTotalSupply,
}
// initialize NativeERC20 SC
params := &contractsapi.InitializeNativeERC20Fn{
Predicate_: contracts.ChildERC20PredicateContract,
Owner_: polyBFTConfig.NativeTokenConfig.Owner,
RootToken_: types.ZeroAddress, // in case native mintable token is used, it is always root token
Name_: polyBFTConfig.NativeTokenConfig.Name,
Symbol_: polyBFTConfig.NativeTokenConfig.Symbol,
Decimals_: polyBFTConfig.NativeTokenConfig.Decimals,
TokenSupply_: initialTotalSupply,
}

input, err := params.EncodeAbi()
if err != nil {
return err
}
input, err := params.EncodeAbi()
if err != nil {
return err
}

if err = callContract(contracts.SystemCaller,
contracts.NativeERC20TokenContract, input, "NativeERC20", transition); err != nil {
return err
}
if err = callContract(contracts.SystemCaller,
contracts.NativeERC20TokenContract, input, "NativeERC20", transition); err != nil {
return err
}
}

Expand Down
32 changes: 13 additions & 19 deletions consensus/polybft/polybft_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ const (

var (
DefaultTokenConfig = &TokenConfig{
Name: defaultNativeTokenName,
Symbol: defaultNativeTokenSymbol,
Decimals: defaultNativeTokenDecimals,
Owner: types.ZeroAddress,
IsMintable: true,
Name: defaultNativeTokenName,
Symbol: defaultNativeTokenSymbol,
Decimals: defaultNativeTokenDecimals,
Owner: types.ZeroAddress,
}

errInvalidTokenParams = errors.New("native token params were not submitted in proper format " +
"(<name:symbol:decimals count:mintable flag:[mintable token owner address]>)")
"(<name:symbol:decimals count:token owner address>)")
)

// PolyBFTConfig is the configuration file for the Polybft consensus protocol.
Expand Down Expand Up @@ -122,7 +121,6 @@ type BridgeConfig struct {
ExitHelperAddr types.Address `json:"exitHelperAddress"`
RootERC20PredicateAddr types.Address `json:"erc20PredicateAddress"`
ChildMintableERC20PredicateAddr types.Address `json:"erc20ChildMintablePredicateAddress"`
RootNativeERC20Addr types.Address `json:"nativeERC20Address"`
RootERC721PredicateAddr types.Address `json:"erc721PredicateAddress"`
ChildMintableERC721PredicateAddr types.Address `json:"erc721ChildMintablePredicateAddress"`
RootERC1155PredicateAddr types.Address `json:"erc1155PredicateAddress"`
Expand Down Expand Up @@ -156,7 +154,6 @@ type RootchainConfig struct {
ExitHelperAddress types.Address
RootERC20PredicateAddress types.Address
ChildMintableERC20PredicateAddress types.Address
RootNativeERC20Address types.Address
ChildERC20Address types.Address
RootERC721PredicateAddress types.Address
ChildMintableERC721PredicateAddress types.Address
Expand All @@ -179,7 +176,6 @@ func (r *RootchainConfig) ToBridgeConfig() *BridgeConfig {
ExitHelperAddr: r.ExitHelperAddress,
RootERC20PredicateAddr: r.RootERC20PredicateAddress,
ChildMintableERC20PredicateAddr: r.ChildMintableERC20PredicateAddress,
RootNativeERC20Addr: r.RootNativeERC20Address,
RootERC721PredicateAddr: r.RootERC721PredicateAddress,
ChildMintableERC721PredicateAddr: r.ChildMintableERC721PredicateAddress,
RootERC1155PredicateAddr: r.RootERC1155PredicateAddress,
Expand All @@ -196,11 +192,10 @@ func (r *RootchainConfig) ToBridgeConfig() *BridgeConfig {

// TokenConfig is the configuration of native token used by edge network
type TokenConfig struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals uint8 `json:"decimals"`
IsMintable bool `json:"isMintable"`
Owner types.Address `json:"owner"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals uint8 `json:"decimals"`
Owner types.Address `json:"owner"`
}

func ParseRawTokenConfig(rawConfig string) (*TokenConfig, error) {
Expand Down Expand Up @@ -235,11 +230,10 @@ func ParseRawTokenConfig(rawConfig string) (*TokenConfig, error) {
owner := types.StringToAddress(strings.TrimSpace(params[3]))

return &TokenConfig{
Name: name,
Symbol: symbol,
Decimals: uint8(decimals),
IsMintable: true, // native token on blade is always mintable
Owner: owner,
Name: name,
Symbol: symbol,
Decimals: uint8(decimals),
Owner: owner,
}, nil
}

Expand Down
Loading

0 comments on commit 797e037

Please sign in to comment.