Skip to content

Commit

Permalink
Deploy token templates to the same addresses on both chains
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Jun 15, 2023
1 parent a02e313 commit d2c30df
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 114 deletions.
2 changes: 1 addition & 1 deletion command/bridge/withdraw/erc1155/withdraw_erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetCommand() *cobra.Command {
withdrawCmd.Flags().StringVar(
&wp.TokenAddr,
common.ChildTokenFlag,
contracts.ChildERC1155Contract.String(),
"",
"ERC 1155 child chain token address",
)

Expand Down
2 changes: 1 addition & 1 deletion command/bridge/withdraw/erc721/withdraw_erc721.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func GetCommand() *cobra.Command {
withdrawCmd.Flags().StringVar(
&wp.TokenAddr,
common.ChildTokenFlag,
contracts.ChildERC721Contract.String(),
"",
"ERC 721 child chain token address",
)

Expand Down
15 changes: 0 additions & 15 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,6 @@ func (p *genesisParams) deployContracts(rewardTokenByteCode []byte,
artifact: contractsapi.StateReceiver,
address: contracts.StateReceiverContract,
},
{
// ChildERC20 token contract
artifact: contractsapi.ChildERC20,
address: contracts.ChildERC20Contract,
},
{
// ChildERC721 token contract
artifact: contractsapi.ChildERC721,
address: contracts.ChildERC721Contract,
},
{
// ChildERC1155 contract
artifact: contractsapi.ChildERC1155,
address: contracts.ChildERC1155Contract,
},
{
// BLS contract
artifact: contractsapi.BLS,
Expand Down
31 changes: 21 additions & 10 deletions command/rootchain/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math/big"

"github.com/spf13/cobra"
"github.com/umbracle/ethgo"
Expand Down Expand Up @@ -54,6 +55,9 @@ var (
// consensusCfg contains consensus protocol configuration parameters
consensusCfg polybft.PolyBFTConfig

// chainConfig contains chain configuration, written in the genesis file
chainConfig *chain.Chain

// metadataPopulatorMap maps rootchain contract names to callback
// which populates appropriate field in the RootchainMetadata
metadataPopulatorMap = map[string]func(*polybft.RootchainConfig, types.Address){
Expand Down Expand Up @@ -329,13 +333,6 @@ func runCommand(cmd *cobra.Command, _ []string) {
Message: fmt.Sprintf("%s started... Rootchain JSON RPC address %s.", contractsDeploymentTitle, params.jsonRPCAddress),
})

chainConfig, err := chain.ImportFromFile(params.genesisPath)
if err != nil {
outputter.SetError(fmt.Errorf("failed to read chain configuration: %w", err))

return
}

client, err := jsonrpc.NewClient(params.jsonRPCAddress)
if err != nil {
outputter.SetError(fmt.Errorf("failed to initialize JSON RPC client for provided IP address: %s: %w",
Expand Down Expand Up @@ -391,6 +388,20 @@ func runCommand(cmd *cobra.Command, _ []string) {
}
consensusCfg.SupernetID = supernetID

// deploy token templates contracts to the genesis
chainConfig.Genesis.Alloc[rootchainCfg.ERC20TemplateAddress] = &chain.GenesisAccount{
Balance: big.NewInt(0),
Code: contractsapi.ERC20TokenTemplate.DeployedBytecode,
}
chainConfig.Genesis.Alloc[rootchainCfg.ERC721TemplateAddress] = &chain.GenesisAccount{
Balance: big.NewInt(0),
Code: contractsapi.ERC721TokenTemplate.DeployedBytecode,
}
chainConfig.Genesis.Alloc[rootchainCfg.ERC1155TemplateAddress] = &chain.GenesisAccount{
Balance: big.NewInt(0),
Code: contractsapi.ERC1155TokenTemplate.DeployedBytecode,
}

// write updated consensus configuration
chainConfig.Params.Engine[polybft.ConsensusName] = consensusCfg

Expand Down Expand Up @@ -511,7 +522,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
},
{
name: erc20TemplateName,
artifact: contractsapi.ChildERC20,
artifact: contractsapi.ERC20TokenTemplate,
},
{
name: rootERC721PredicateName,
Expand All @@ -523,7 +534,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
},
{
name: erc721TemplateName,
artifact: contractsapi.ChildERC721,
artifact: contractsapi.ERC721TokenTemplate,
},
{
name: rootERC1155PredicateName,
Expand All @@ -535,7 +546,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
},
{
name: erc1155TemplateName,
artifact: contractsapi.ChildERC1155,
artifact: contractsapi.ERC1155TokenTemplate,
},
{
name: customSupernetManagerName,
Expand Down
8 changes: 6 additions & 2 deletions command/rootchain/deploy/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
)

Expand Down Expand Up @@ -35,8 +36,11 @@ func (ip *deployParams) validateFlags() error {
return fmt.Errorf("provided genesis path '%s' is invalid. Error: %w ", ip.genesisPath, err)
}

consensusCfg, err = polybft.LoadPolyBFTConfig(ip.genesisPath)
if err != nil {
if chainConfig, err = chain.ImportFromFile(ip.genesisPath); err != nil {
return fmt.Errorf("failed to load chain config %s: %w", ip.genesisPath, err)
}

if consensusCfg, err = polybft.GetPolyBFTConfig(chainConfig); err != nil {
return err
}

Expand Down
24 changes: 12 additions & 12 deletions consensus/polybft/contracts_initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func getInitERC20PredicateInput(config *BridgeConfig, childChainMintable bool) (
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewChildERC20Predicate: config.ChildMintableERC20PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC20Contract,
NewChildTokenTemplate: config.ERC20TemplateAddr,
}
} else {
params = &contractsapi.InitializeChildERC20PredicateFn{
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC20Predicate: config.RootERC20PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC20Contract,
NewChildTokenTemplate: config.ERC20TemplateAddr,
NewNativeTokenRootAddress: config.RootNativeERC20Addr,
}
}
Expand All @@ -92,7 +92,7 @@ func getInitERC20PredicateACLInput(config *BridgeConfig, owner types.Address,
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewChildERC20Predicate: config.ChildMintableERC20PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC20Contract,
NewChildTokenTemplate: config.ERC20TemplateAddr,
NewUseAllowList: owner != contracts.SystemCaller,
NewUseBlockList: owner != contracts.SystemCaller,
NewOwner: owner,
Expand All @@ -102,7 +102,7 @@ func getInitERC20PredicateACLInput(config *BridgeConfig, owner types.Address,
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC20Predicate: config.RootERC20PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC20Contract,
NewChildTokenTemplate: config.ERC20TemplateAddr,
NewNativeTokenRootAddress: config.RootNativeERC20Addr,
NewUseAllowList: owner != contracts.SystemCaller,
NewUseBlockList: owner != contracts.SystemCaller,
Expand All @@ -121,14 +121,14 @@ func getInitERC721PredicateInput(config *BridgeConfig, childOriginatedTokens boo
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewChildERC721Predicate: config.ChildMintableERC721PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC20Contract,
NewChildTokenTemplate: config.ERC721TemplateAddr,
}
} else {
params = &contractsapi.InitializeChildERC721PredicateFn{
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC721Predicate: config.RootERC721PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC721Contract,
NewChildTokenTemplate: config.ERC721TemplateAddr,
}
}

Expand All @@ -145,7 +145,7 @@ func getInitERC721PredicateACLInput(config *BridgeConfig, owner types.Address,
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewChildERC721Predicate: config.ChildMintableERC721PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC721Contract,
NewChildTokenTemplate: config.ERC721TemplateAddr,
NewUseAllowList: owner != contracts.SystemCaller,
NewUseBlockList: owner != contracts.SystemCaller,
NewOwner: owner,
Expand All @@ -155,7 +155,7 @@ func getInitERC721PredicateACLInput(config *BridgeConfig, owner types.Address,
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC721Predicate: config.RootERC721PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC721Contract,
NewChildTokenTemplate: config.ERC721TemplateAddr,
NewUseAllowList: owner != contracts.SystemCaller,
NewUseBlockList: owner != contracts.SystemCaller,
NewOwner: owner,
Expand All @@ -173,14 +173,14 @@ func getInitERC1155PredicateInput(config *BridgeConfig, childChainMintable bool)
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewChildERC1155Predicate: config.ChildMintableERC1155PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC1155Contract,
NewChildTokenTemplate: config.ERC1155TemplateAddr,
}
} else {
params = &contractsapi.InitializeChildERC1155PredicateFn{
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC1155Predicate: config.RootERC1155PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC1155Contract,
NewChildTokenTemplate: config.ERC1155TemplateAddr,
}
}

Expand All @@ -197,7 +197,7 @@ func getInitERC1155PredicateACLInput(config *BridgeConfig, owner types.Address,
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewChildERC1155Predicate: config.ChildMintableERC1155PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC1155Contract,
NewChildTokenTemplate: config.ERC1155TemplateAddr,
NewUseAllowList: owner != contracts.SystemCaller,
NewUseBlockList: owner != contracts.SystemCaller,
NewOwner: owner,
Expand All @@ -207,7 +207,7 @@ func getInitERC1155PredicateACLInput(config *BridgeConfig, owner types.Address,
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC1155Predicate: config.RootERC1155PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC1155Contract,
NewChildTokenTemplate: config.ERC1155TemplateAddr,
NewUseAllowList: owner != contracts.SystemCaller,
NewUseBlockList: owner != contracts.SystemCaller,
NewOwner: owner,
Expand Down
6 changes: 3 additions & 3 deletions consensus/polybft/contractsapi/artifacts-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
},
{
"child/ChildERC20.sol",
"ChildERC20",
"ERC20TokenTemplate",
},
{
"child/ChildERC20Predicate.sol",
Expand All @@ -67,7 +67,7 @@ func main() {
},
{
"child/ChildERC721.sol",
"ChildERC721",
"ERC721TokenTemplate",
},
{
"child/ChildERC721Predicate.sol",
Expand All @@ -87,7 +87,7 @@ func main() {
},
{
"child/ChildERC1155.sol",
"ChildERC1155",
"ERC1155TokenTemplate",
},
{
"child/ChildERC1155Predicate.sol",
Expand Down
8 changes: 4 additions & 4 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ func main() {
},
},
{
"ChildERC1155",
gensc.ChildERC1155,
"ERC1155TokenTemplate",
gensc.ERC1155TokenTemplate,
false,
[]string{
"initialize",
Expand Down Expand Up @@ -327,8 +327,8 @@ func main() {
[]string{},
},
{
"ChildERC721",
gensc.ChildERC721,
"ERC721TokenTemplate",
gensc.ERC721TokenTemplate,
false,
[]string{
"initialize",
Expand Down
Loading

0 comments on commit d2c30df

Please sign in to comment.