Skip to content

Commit

Permalink
Add contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Jul 18, 2023
1 parent ea64172 commit cd7faa3
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 39 deletions.
6 changes: 3 additions & 3 deletions command/bridge/deposit/erc1155/deposit_erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ func runCommand(cmd *cobra.Command, _ []string) {
return
}

receivers := make([]ethgo.Address, len(dp.Receivers))
receivers := make([]types.Address, len(dp.Receivers))
for i, receiverRaw := range dp.Receivers {
receivers[i] = ethgo.Address(types.StringToAddress(receiverRaw))
receivers[i] = types.StringToAddress(receiverRaw)
}

// deposit tokens
Expand Down Expand Up @@ -244,7 +244,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

// createDepositTxn encodes parameters for deposit function on rootchain predicate contract
func createDepositTxn(sender ethgo.Address, receivers []ethgo.Address,
func createDepositTxn(sender ethgo.Address, receivers []types.Address,
amounts, tokenIDs []*big.Int) (*ethgo.Transaction, error) {
depositBatchFn := &contractsapi.DepositBatchRootERC1155PredicateFn{
RootToken: types.StringToAddress(dp.TokenAddr),
Expand Down
6 changes: 3 additions & 3 deletions command/bridge/deposit/erc721/deposit_erc721.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
return
}

receivers := make([]ethgo.Address, len(dp.Receivers))
receivers := make([]types.Address, len(dp.Receivers))
tokenIDs := make([]*big.Int, len(dp.Receivers))

for i, tokenIDRaw := range dp.TokenIDs {
Expand All @@ -105,7 +105,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
return
}

receivers[i] = ethgo.Address(types.StringToAddress(dp.Receivers[i]))
receivers[i] = types.StringToAddress(dp.Receivers[i])
tokenIDs[i] = tokenID
}

Expand Down Expand Up @@ -218,7 +218,7 @@ func runCommand(cmd *cobra.Command, _ []string) {

// createDepositTxn encodes parameters for deposit fnction on rootchain predicate contract
func createDepositTxn(sender ethgo.Address,
receivers []ethgo.Address, tokenIDs []*big.Int) (*ethgo.Transaction, error) {
receivers []types.Address, tokenIDs []*big.Int) (*ethgo.Transaction, error) {
depositToRoot := &contractsapi.DepositBatchRootERC721PredicateFn{
RootToken: types.StringToAddress(dp.TokenAddr),
Receivers: receivers,
Expand Down
6 changes: 3 additions & 3 deletions command/bridge/withdraw/erc1155/withdraw_erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ func runCommand(cmd *cobra.Command, _ []string) {
return
}

receivers := make([]ethgo.Address, len(wp.Receivers))
receivers := make([]types.Address, len(wp.Receivers))
amounts := make([]*big.Int, len(wp.Receivers))
TokenIDs := make([]*big.Int, len(wp.Receivers))

for i, receiverRaw := range wp.Receivers {
receivers[i] = ethgo.Address(types.StringToAddress(receiverRaw))
receivers[i] = types.StringToAddress(receiverRaw)
amountRaw := wp.Amounts[i]
tokenIDRaw := wp.TokenIDs[i]

Expand Down Expand Up @@ -171,7 +171,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

// createWithdrawTxn encodes parameters for withdraw function on child chain predicate contract
func createWithdrawTxn(receivers []ethgo.Address, amounts, TokenIDs []*big.Int) (*ethgo.Transaction, error) {
func createWithdrawTxn(receivers []types.Address, amounts, TokenIDs []*big.Int) (*ethgo.Transaction, error) {
withdrawFn := &contractsapi.WithdrawBatchChildERC1155PredicateFn{
ChildToken: types.StringToAddress(wp.TokenAddr),
Receivers: receivers,
Expand Down
6 changes: 3 additions & 3 deletions command/bridge/withdraw/erc721/withdraw_erc721.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func run(cmd *cobra.Command, _ []string) {
return
}

receivers := make([]ethgo.Address, len(wp.Receivers))
receivers := make([]types.Address, len(wp.Receivers))
tokenIDs := make([]*big.Int, len(wp.Receivers))

for i, tokenIDRaw := range wp.TokenIDs {
Expand All @@ -101,7 +101,7 @@ func run(cmd *cobra.Command, _ []string) {
return
}

receivers[i] = ethgo.Address(types.StringToAddress(wp.Receivers[i]))
receivers[i] = types.StringToAddress(wp.Receivers[i])
tokenIDs[i] = tokenID
}

Expand Down Expand Up @@ -150,7 +150,7 @@ func run(cmd *cobra.Command, _ []string) {
}

// createWithdrawTxn encodes parameters for withdraw function on child chain predicate contract
func createWithdrawTxn(receivers []ethgo.Address, tokenIDs []*big.Int) (*ethgo.Transaction, error) {
func createWithdrawTxn(receivers []types.Address, tokenIDs []*big.Int) (*ethgo.Transaction, error) {
withdrawToFn := &contractsapi.WithdrawBatchChildERC721PredicateFn{
ChildToken: types.StringToAddress(wp.TokenAddr),
Receivers: receivers,
Expand Down
16 changes: 16 additions & 0 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,22 @@ func (p *genesisParams) deployContracts(
artifact: contractsapi.RewardPool,
address: contracts.RewardPoolContract,
},
{
artifact: contractsapi.NetworkParams,
address: contracts.NetworkParamsContract,
},
{
artifact: contractsapi.ForkParams,
address: contracts.ForkParamsContract,
},
{
artifact: contractsapi.ChildGovernor,
address: contracts.ChildGovernorContract,
},
{
artifact: contractsapi.ChildTimelock,
address: contracts.ChildTimelockContract,
},
}

if !params.nativeTokenConfig.IsMintable {
Expand Down
6 changes: 3 additions & 3 deletions command/rootchain/whitelist/whitelist_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return nil
}

func stringSliceToAddressSlice(addrs []string) []ethgo.Address {
res := make([]ethgo.Address, len(addrs))
func stringSliceToAddressSlice(addrs []string) []types.Address {
res := make([]types.Address, len(addrs))
for indx, addr := range addrs {
res[indx] = ethgo.Address(types.StringToAddress(addr))
res[indx] = types.StringToAddress(addr)
}

return res
Expand Down
99 changes: 94 additions & 5 deletions consensus/polybft/contracts_initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func initValidatorSet(polyBFTConfig PolyBFTConfig, transition *state.Transition)
NewStateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootChainManager: polyBFTConfig.Bridge.CustomSupernetManagerAddr,
NewEpochSize: new(big.Int).SetUint64(polyBFTConfig.EpochSize),
NewNetworkParams: contracts.NetworkParamsContract,
InitialValidators: initialValidators,
}

Expand All @@ -45,10 +45,10 @@ func initValidatorSet(polyBFTConfig PolyBFTConfig, transition *state.Transition)
// initRewardPool initializes RewardPool SC
func initRewardPool(polybftConfig PolyBFTConfig, transition *state.Transition) error {
initFn := &contractsapi.InitializeRewardPoolFn{
NewRewardToken: polybftConfig.RewardConfig.TokenAddress,
NewRewardWallet: polybftConfig.RewardConfig.WalletAddress,
NewValidatorSet: contracts.ValidatorSetContract,
NewBaseReward: new(big.Int).SetUint64(polybftConfig.EpochReward),
NewRewardToken: polybftConfig.RewardConfig.TokenAddress,
NewRewardWallet: polybftConfig.RewardConfig.WalletAddress,
NewValidatorSet: contracts.ValidatorSetContract,
NetworkParamsAddr: contracts.NetworkParamsContract,
}

input, err := initFn.EncodeAbi()
Expand Down Expand Up @@ -276,3 +276,92 @@ func callContract(from, to types.Address, input []byte, contractName string, tra
func isNativeRewardToken(cfg PolyBFTConfig) bool {
return cfg.RewardConfig.TokenAddress == contracts.NativeERC20TokenContract
}

// initNetworkParamsContract initializes NetworkParams contract on child chain
func initNetworkParamsContract(cfg PolyBFTConfig, owner types.Address, transition *state.Transition) error {
initFn := &contractsapi.InitializeNetworkParamsFn{
InitParams: &contractsapi.InitParams{
NewOwner: owner,
NewCheckpointBlockInterval: new(big.Int).SetUint64(cfg.CheckpointInterval),
NewEpochSize: new(big.Int).SetUint64(cfg.EpochSize),
NewEpochReward: new(big.Int).SetUint64(cfg.EpochReward),
NewMinValidatorSetSize: new(big.Int).SetUint64(cfg.MinValidatorSetSize),
NewMaxValidatorSetSize: new(big.Int).SetUint64(cfg.MaxValidatorSetSize),
NewWithdrawalWaitPeriod: new(big.Int).SetUint64(cfg.WithdrawalWaitPeriod),
NewBlockTime: new(big.Int).SetUint64(uint64(cfg.BlockTime.Duration)),
NewBlockTimeDrift: new(big.Int).SetUint64(cfg.BlockTimeDrift),
NewVotingDelay: new(big.Int).Set(cfg.GovernanceConfig.VotingDelay),
NewVotingPeriod: new(big.Int).Set(cfg.GovernanceConfig.VotingPeriod),
NewProposalThreshold: new(big.Int).Set(cfg.GovernanceConfig.ProposalThreshold),
},
}

input, err := initFn.EncodeAbi()
if err != nil {
return fmt.Errorf("NetworkParams.initialize params encoding failed: %w", err)
}

return callContract(contracts.SystemCaller,
contracts.NetworkParamsContract, input, "ValidatorSet.initialize", transition)
}

// initForkParamsContract initializes ForkParams contract on child chain
func initForkParamsContract(owner types.Address, transition *state.Transition) error {
initFn := &contractsapi.InitializeForkParamsFn{
NewOwner: owner,
}

input, err := initFn.EncodeAbi()
if err != nil {
return fmt.Errorf("ForkParams.initialize params encoding failed: %w", err)
}

return callContract(contracts.SystemCaller,
contracts.ForkParamsContract, input, "ForkParams.initialize", transition)
}

// initChildTimelock initializes ChildTimelock contract on child chain
func initChildTimelock(cfg PolyBFTConfig, transition *state.Transition) error {
addresses := make([]types.Address, len(cfg.InitialValidatorSet))
for i := 0; i < len(cfg.InitialValidatorSet); i++ {
addresses[i] = cfg.InitialValidatorSet[i].Address
}

initFn := &contractsapi.InitializeChildTimelockFn{
Admin: cfg.GovernanceConfig.GovernorAdmin,
Proposers: addresses,
Executors: addresses,
MinDelay: bigZero, // for now
}

input, err := initFn.EncodeAbi()
if err != nil {
return fmt.Errorf("ChildTimelock.initialize params encoding failed: %w", err)
}

return callContract(contracts.SystemCaller,
contracts.ChildTimelockContract, input, "ChildTimelock.initialize", transition)
}

// initChildGovernor initializes ChildGovernor contract on child chain
func initChildGovernor(cfg PolyBFTConfig, transition *state.Transition) error {
addresses := make([]types.Address, len(cfg.InitialValidatorSet))
for i := 0; i < len(cfg.InitialValidatorSet); i++ {
addresses[i] = cfg.InitialValidatorSet[i].Address
}

initFn := &contractsapi.InitializeChildGovernorFn{
Token_: contracts.ValidatorSetContract,
Timelock_: contracts.ChildTimelockContract,
NetworkParams_: contracts.NetworkParamsContract,
QuorumNumerator_: new(big.Int).SetUint64(cfg.GovernanceConfig.ProposalQuorumPercentage),
}

input, err := initFn.EncodeAbi()
if err != nil {
return fmt.Errorf("ChildGovernor.initialize params encoding failed: %w", err)
}

return callContract(contracts.SystemCaller,
contracts.ChildGovernorContract, input, "ChildGovernor.initialize", transition)
}
16 changes: 16 additions & 0 deletions consensus/polybft/contractsapi/artifacts-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ func main() {
"child/EIP1559Burn.sol",
"EIP1559Burn",
},
{
"child/NetworkParams.sol",
"NetworkParams",
},
{
"child/ForkParams.sol",
"ForkParams",
},
{
"child/governance/ChildGovernor.sol",
"ChildGovernor",
},
{
"child/governance/ChildTimelock.sol",
"ChildTimelock",
},
}

for _, v := range readContracts {
Expand Down
44 changes: 41 additions & 3 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,42 @@ func main() {
},
[]string{},
},
{
"NetworkParams",
gensc.NetworkParams,
false,
[]string{
"initialize",
},
[]string{},
},
{
"ForkParams",
gensc.ForkParams,
false,
[]string{
"initialize",
},
[]string{},
},
{
"ChildGovernor",
gensc.ChildGovernor,
false,
[]string{
"initialize",
},
[]string{},
},
{
"ChildTimelock",
gensc.ChildTimelock,
false,
[]string{
"initialize",
},
[]string{},
},
}

generatedData := &generatedData{}
Expand Down Expand Up @@ -512,10 +548,12 @@ func generateType(generatedData *generatedData, name string, obj *abi.Type, res

typ = "[" + strconv.Itoa(elem.Size()) + "]" + nestedType
} else if elem.Kind() == abi.KindAddress {
// for address use the native `types.Address` type instead of `ethgo.Address`. Note that
// this only works for simple types and not for []address inputs. This is good enough since
// there are no kinds like that in our smart contracts.
// for address use the native `types.Address` type instead of `ethgo.Address`
typ = "types.Address"
} else if (elem.Kind() == abi.KindArray || elem.Kind() == abi.KindSlice) &&
elem.Elem().Kind() == abi.KindAddress {
// for address slice or arrays use the native `types.Address` type instead of `ethgo.Address`
typ = "[]types.Address"
} else {
// for the rest of the types use the go type returned by abi
typ = elem.GoType().String()
Expand Down
Loading

0 comments on commit cd7faa3

Please sign in to comment.