Skip to content

Commit

Permalink
Extract child token address and print it in the bridge deposit command
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Jun 13, 2023
1 parent 0060315 commit 84c5490
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 51 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
41 changes: 28 additions & 13 deletions command/bridge/deposit/erc20/deposit_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

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

g, ctx := errgroup.WithContext(cmd.Context())
Expand Down Expand Up @@ -200,18 +201,25 @@ func runCommand(cmd *cobra.Command, _ []string) {
return fmt.Errorf("receiver: %s, amount: %s", receiver, amount)
}

var exitEventId *big.Int
var exitEventID *big.Int

if dp.ChildChainMintable {
if exitEventId, err = common.ExtractExitEventID(receipt); 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)
}

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

return nil
Expand All @@ -227,19 +235,26 @@ func runCommand(cmd *cobra.Command, _ []string) {

close(bridgeTxCh)

var childToken *types.Address

for x := range bridgeTxCh {
exitEventIDs = append(exitEventIDs, x.exitEventId)
exitEventIDs = append(exitEventIDs, x.exitEventID)
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 {
// 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

0 comments on commit 84c5490

Please sign in to comment.