Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print child token address as part of deposit commands #1616

Merged
Show file tree
Hide file tree
Changes from 3 commits
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
56 changes: 49 additions & 7 deletions command/bridge/common/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
cmdHelper "github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
)

type TokenType int
Expand Down Expand Up @@ -160,21 +161,58 @@ func ExtractExitEventID(receipt *ethgo.Receipt) (*big.Int, error) {
return nil, errors.New("failed to find exit event log")
}

// ExtractChildTokenAddr extracts predicted deterministic child token address
func ExtractChildTokenAddr(receipt *ethgo.Receipt, childChainMintable bool) (*types.Address, error) {
var (
l1TokenMapped contractsapi.TokenMappedEvent
l2TokenMapped contractsapi.L2MintableTokenMappedEvent
)

for _, log := range receipt.Logs {
if childChainMintable {
doesMatch, err := l2TokenMapped.ParseLog(log)
if err != nil {
return nil, err
}

if !doesMatch {
continue
}

return &l2TokenMapped.ChildToken, nil
} else {
doesMatch, err := l1TokenMapped.ParseLog(log)
if err != nil {
return nil, err
}

if !doesMatch {
continue
}

return &l1TokenMapped.ChildToken, nil
}
}

return nil, nil
}

type BridgeTxResult struct {
Sender string `json:"sender"`
Receivers []string `json:"receivers"`
ExitEventIDs []*big.Int `json:"exitEventIDs"`
Amounts []string `json:"amounts"`
TokenIDs []string `json:"tokenIds"`
BlockNumbers []uint64 `json:"blockNumbers"`
Sender string `json:"sender"`
Receivers []string `json:"receivers"`
ExitEventIDs []*big.Int `json:"exitEventIDs"`
Amounts []string `json:"amounts"`
TokenIDs []string `json:"tokenIds"`
BlockNumbers []uint64 `json:"blockNumbers"`
ChildTokenAddr *types.Address `json:"childTokenAddr"`

Title string `json:"title"`
}

func (r *BridgeTxResult) GetOutput() string {
var buffer bytes.Buffer

vals := make([]string, 0, 5)
vals := make([]string, 0, 7)
goran-ethernal marked this conversation as resolved.
Show resolved Hide resolved
vals = append(vals, fmt.Sprintf("Sender|%s", r.Sender))
vals = append(vals, fmt.Sprintf("Receivers|%s", strings.Join(r.Receivers, ", ")))

Expand Down Expand Up @@ -214,6 +252,10 @@ func (r *BridgeTxResult) GetOutput() string {
vals = append(vals, fmt.Sprintf("Inclusion Block Numbers|%s", buf.String()))
}

if r.ChildTokenAddr != nil {
vals = append(vals, fmt.Sprintf("Child Token Address|%s", (*r.ChildTokenAddr).String()))
}

_, _ = buffer.WriteString(fmt.Sprintf("\n[%s]\n", r.Title))
_, _ = buffer.WriteString(cmdHelper.FormatKV(vals))
_, _ = buffer.WriteString("\n")
Expand Down
10 changes: 10 additions & 0 deletions command/bridge/deposit/erc1155/deposit_erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ func runCommand(cmd *cobra.Command, _ []string) {
res.ExitEventIDs = []*big.Int{exitEventID}
}

// populate child token address if a token is mapped alongside with deposit
childToken, err := common.ExtractChildTokenAddr(receipt, dp.ChildChainMintable)
if err != nil {
outputter.SetError(fmt.Errorf("failed to extract child token address: %w", err))

return
}

res.ChildTokenAddr = childToken

outputter.SetCommandResult(res)
}

Expand Down
59 changes: 40 additions & 19 deletions command/bridge/deposit/erc20/deposit_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,14 @@ func runCommand(cmd *cobra.Command, _ []string) {
return
}

type bridgeTxData struct {
exitEventID *big.Int
blockNumber uint64
childTokenAddr *types.Address
}

g, ctx := errgroup.WithContext(cmd.Context())
exitEventIDsCh := make(chan *big.Int, len(dp.Receivers))
blockNumbersCh := make(chan uint64, len(dp.Receivers))
bridgeTxCh := make(chan bridgeTxData, len(dp.Receivers))
exitEventIDs := make([]*big.Int, 0, len(dp.Receivers))
blockNumbers := make([]uint64, 0, len(dp.Receivers))

Expand Down Expand Up @@ -196,15 +201,25 @@ func runCommand(cmd *cobra.Command, _ []string) {
return fmt.Errorf("receiver: %s, amount: %s", receiver, amount)
}

blockNumbersCh <- receipt.BlockNumber
var exitEventID *big.Int

if dp.ChildChainMintable {
exitEventID, err := common.ExtractExitEventID(receipt)
if err != nil {
if exitEventID, err = common.ExtractExitEventID(receipt); err != nil {
return fmt.Errorf("failed to extract exit event: %w", err)
}
}

// populate child token address if a token is mapped alongside with deposit
childToken, err := common.ExtractChildTokenAddr(receipt, dp.ChildChainMintable)
if err != nil {
return fmt.Errorf("failed to extract child token address: %w", err)
}

exitEventIDsCh <- exitEventID
// send aggregated data to channel if everything went ok
bridgeTxCh <- bridgeTxData{
blockNumber: receipt.BlockNumber,
exitEventID: exitEventID,
childTokenAddr: childToken,
}

return nil
Expand All @@ -218,25 +233,31 @@ func runCommand(cmd *cobra.Command, _ []string) {
return
}

close(exitEventIDsCh)
close(blockNumbersCh)
close(bridgeTxCh)

for exitEventID := range exitEventIDsCh {
exitEventIDs = append(exitEventIDs, exitEventID)
}
var childToken *types.Address

for x := range bridgeTxCh {
if x.exitEventID != nil {
exitEventIDs = append(exitEventIDs, x.exitEventID)
}

for blockNum := range blockNumbersCh {
blockNumbers = append(blockNumbers, blockNum)
blockNumbers = append(blockNumbers, x.blockNumber)

if x.childTokenAddr != nil {
childToken = x.childTokenAddr
}
}

outputter.SetCommandResult(
&common.BridgeTxResult{
Sender: depositorAddr.String(),
Receivers: dp.Receivers,
Amounts: dp.Amounts,
ExitEventIDs: exitEventIDs,
BlockNumbers: blockNumbers,
Title: "DEPOSIT ERC 20",
Sender: depositorAddr.String(),
Receivers: dp.Receivers,
Amounts: dp.Amounts,
ExitEventIDs: exitEventIDs,
ChildTokenAddr: childToken,
BlockNumbers: blockNumbers,
Title: "DEPOSIT ERC 20",
})
}

Expand Down
10 changes: 10 additions & 0 deletions command/bridge/deposit/erc721/deposit_erc721.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ func runCommand(cmd *cobra.Command, _ []string) {
res.ExitEventIDs = []*big.Int{exitEventID}
}

// populate child token address if a token is mapped alongside with deposit
childToken, err := common.ExtractChildTokenAddr(receipt, dp.ChildChainMintable)
if err != nil {
outputter.SetError(fmt.Errorf("failed to extract child token address: %w", err))

return
}

res.ChildTokenAddr = childToken

outputter.SetCommandResult(res)
}

Expand Down
8 changes: 6 additions & 2 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ func main() {
"initialize",
"depositTo",
},
[]string{},
[]string{
"TokenMapped",
},
},
{
"ChildMintableERC20Predicate",
Expand Down Expand Up @@ -243,7 +245,9 @@ func main() {
[]string{
"initialize",
},
[]string{},
[]string{
"L2MintableTokenMapped",
},
},
{
"ChildERC1155",
Expand Down
42 changes: 42 additions & 0 deletions consensus/polybft/contractsapi/contractsapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 29 additions & 29 deletions consensus/polybft/contractsapi/gen_sc_data.go

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions consensus/polybft/contractsapi/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package contractsapi

import (
"github.com/0xPolygon/polygon-edge/types"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/abi"
)

Expand All @@ -13,6 +14,16 @@ type StateTransactionInput interface {
DecodeAbi(b []byte) error
}

// Event is an abstraction for Ethereum events
type Event interface {
Stefan-Ethernal marked this conversation as resolved.
Show resolved Hide resolved
// Sig returns hash signature of given event
Sig() ethgo.Hash
// Encode encodes given inputs into the ABI
Encode(inputs interface{}) ([]byte, error)
// ParseLog parses provided log and populates Event's instance fields
ParseLog(log *ethgo.Log) (bool, error)
}

var (
// stateSyncABIType is a specific case where we need to encode state sync event as a tuple of tuple
stateSyncABIType = abi.MustNewType(
Expand Down