Skip to content

Commit

Permalink
Print child token address as part of deposit commands (#1616)
Browse files Browse the repository at this point in the history
* fix deposit_erc20 send with channel

* Extract child token address and print it in the bridge deposit command

* Filter nil exit event ids

* Remove Event interface

---------

Co-authored-by: Igor Crevar <[email protected]>
  • Loading branch information
Stefan-Ethernal and igorcrevar authored Jun 14, 2023
1 parent a0e359a commit 902bd37
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 57 deletions.
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)
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.

0 comments on commit 902bd37

Please sign in to comment.