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

refactor: use protocol contracts V2 with TON deposits #3439

Merged
merged 8 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [3450](https://github.com/zeta-chain/node/pull/3450) - SOL withdraw and call integration
* [3541](https://github.com/zeta-chain/node/pull/3541) - implement `MsgUpdateZRC20Name` to update the name or symbol of a ZRC20 token
* [3520](https://github.com/zeta-chain/node/pull/3520) - SPL withdraw and call integration
* [3439](https://github.com/zeta-chain/node/pull/3439) - use protocol contracts V2 with TON deposits

### Refactor

Expand Down
5 changes: 1 addition & 4 deletions e2e/runner/ton.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,9 @@ func (r *E2ERunner) TONDepositAndCall(
}

filter := func(cctx *cctypes.CrossChainTx) bool {
memo := zevmRecipient.Bytes()
memo = append(memo, callData...)

return cctx.InboundParams.SenderChainId == chain.ChainId &&
cctx.InboundParams.Sender == sender.GetAddress().ToRaw() &&
cctx.RelayedMessage == hex.EncodeToString(memo)
cctx.RelayedMessage == hex.EncodeToString(callData)
}

// Wait for cctx
Expand Down
10 changes: 2 additions & 8 deletions pkg/contracts/ton/gateway_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Deposit struct {

// Memo casts deposit to memo bytes
func (d Deposit) Memo() []byte {
return d.Recipient.Bytes()
return []byte{}
}

// AsBody casts struct as internal message body.
Expand All @@ -77,13 +77,7 @@ type DepositAndCall struct {

// Memo casts deposit to call to memo bytes
func (d DepositAndCall) Memo() []byte {
recipient := d.Recipient.Bytes()
out := make([]byte, 0, len(recipient)+len(d.CallData))

out = append(out, recipient...)
out = append(out, d.CallData...)

return out
return d.CallData
}

// AsBody casts struct to internal message body.
Expand Down
65 changes: 42 additions & 23 deletions zetaclient/chains/ton/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@
return nil
}

// inboundData represents extract data from a TON inbound deposit
type inboundData struct {
sender string
amount math.Uint
receiver string
message []byte
isContractCall bool
}

// Sends PostVoteInbound to zetacore
func (ob *Observer) voteInbound(ctx context.Context, tx *toncontracts.Transaction) (string, error) {
// noop
Expand All @@ -193,44 +202,52 @@
return "", errors.Wrapf(err, "unable to get block header %s", tx.BlockID.String())
}

sender, amount, memo, err := extractInboundData(tx)
inboundData, err := extractInboundData(tx)
if err != nil {
return "", err
}

seqno := blockHeader.MinRefMcSeqno

return ob.voteDeposit(ctx, tx, sender, amount, memo, seqno)
return ob.voteDeposit(ctx, tx, inboundData, seqno)
}

// extractInboundData parses Gateway tx into deposit (TON sender, amount, memo)
func extractInboundData(tx *toncontracts.Transaction) (string, math.Uint, []byte, error) {
func extractInboundData(tx *toncontracts.Transaction) (inboundData, error) {
switch tx.Operation {
case toncontracts.OpDeposit:
d, err := tx.Deposit()
if err != nil {
return "", math.NewUint(0), nil, err
return inboundData{}, err

Check warning on line 221 in zetaclient/chains/ton/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/inbound.go#L221

Added line #L221 was not covered by tests
}

return d.Sender.ToRaw(), d.Amount, d.Memo(), nil
return inboundData{
sender: d.Sender.ToRaw(),
amount: d.Amount,
receiver: d.Recipient.Hex(),
message: d.Memo(),
isContractCall: false,
}, nil
case toncontracts.OpDepositAndCall:
d, err := tx.DepositAndCall()
if err != nil {
return "", math.NewUint(0), nil, err
return inboundData{}, err

Check warning on line 233 in zetaclient/chains/ton/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/inbound.go#L233

Added line #L233 was not covered by tests
}

return d.Sender.ToRaw(), d.Amount, d.Memo(), nil
return inboundData{
sender: d.Sender.ToRaw(),
amount: d.Amount,
receiver: d.Recipient.Hex(),
message: d.Memo(),
isContractCall: true,
}, nil
default:
return "", math.NewUint(0), nil, fmt.Errorf("unknown operation %d", tx.Operation)
return inboundData{}, fmt.Errorf("unknown operation %d", tx.Operation)

Check warning on line 243 in zetaclient/chains/ton/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/inbound.go#L243

Added line #L243 was not covered by tests
}
}

func (ob *Observer) voteDeposit(
ctx context.Context,
tx *toncontracts.Transaction,
sender string,
amount math.Uint,
memo []byte,
inboundData inboundData,
seqno uint32,
) (string, error) {
const (
Expand All @@ -246,25 +263,27 @@
inboundHash = liteapi.TransactionHashToString(tx.Lt, ton.Bits256(tx.Hash()))
)

// TODO: use protocol contract v2 for deposit
// https://github.com/zeta-chain/node/issues/2967

msg := zetacore.GetInboundVoteMessage(
sender,
// create the inbound message
msg := types.NewMsgVoteInbound(
operatorAddress.String(),
inboundData.sender,
ob.Chain().ChainId,
sender,
sender,
inboundData.sender,
inboundData.receiver,
ob.ZetacoreClient().Chain().ChainId,
amount,
hex.EncodeToString(memo),
inboundData.amount,
hex.EncodeToString(inboundData.message),
inboundHash,
uint64(seqno),
gasLimit,
coinType,
asset,
operatorAddress.String(),
eventIndex,
types.ProtocolContractVersion_V2,
false, // not used
types.InboundStatus_SUCCESS,
types.ConfirmationMode_SAFE,
types.WithCrossChainCall(inboundData.isContractCall),
)

return ob.PostVoteInbound(ctx, msg, retryGasLimit)
Expand Down
20 changes: 8 additions & 12 deletions zetaclient/chains/ton/observer/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package observer

import (
"encoding/hex"
"fmt"
"testing"

"github.com/pkg/errors"
Expand Down Expand Up @@ -163,7 +162,9 @@ func TestInbound(t *testing.T) {

assert.Equal(t, "", cctx.Asset)
assert.Equal(t, deposit.Amount.Uint64(), cctx.Amount.Uint64())
assert.Equal(t, hex.EncodeToString(deposit.Recipient.Bytes()), cctx.Message)
assert.Equal(t, "", cctx.Message)
assert.Equal(t, deposit.Recipient.Hex(), cctx.Receiver)
assert.False(t, cctx.IsCrossChainCall)

// Check hash & block height
expectedHash := liteapi.TransactionHashToString(depositTX.Lt, txHash(depositTX))
Expand Down Expand Up @@ -225,13 +226,9 @@ func TestInbound(t *testing.T) {

assert.Equal(t, "", cctx.Asset)
assert.Equal(t, depositAndCall.Amount.Uint64(), cctx.Amount.Uint64())

expectedMessage := hex.EncodeToString(append(
depositAndCall.Recipient.Bytes(),
[]byte(callData)...,
))

assert.Equal(t, expectedMessage, cctx.Message)
assert.Equal(t, hex.EncodeToString([]byte(callData)), cctx.Message)
assert.Equal(t, depositAndCall.Recipient.Hex(), cctx.Receiver)
assert.True(t, cctx.IsCrossChainCall)

// Check hash & block height
expectedHash := liteapi.TransactionHashToString(depositAndCallTX.Lt, txHash(depositAndCallTX))
Expand Down Expand Up @@ -436,9 +433,8 @@ func TestInboundTracker(t *testing.T) {
vote := ts.votesBag[0]
assert.Equal(t, deposit.Amount, vote.Amount)
assert.Equal(t, deposit.Sender.ToRaw(), vote.Sender)

// zevm recipient bytes == memo bytes
assert.Equal(t, fmt.Sprintf("%x", deposit.Recipient), vote.Message)
assert.Equal(t, "", vote.Message)
assert.Equal(t, deposit.Recipient.Hex(), vote.Receiver)
}

func txHash(tx ton.Transaction) ton.Bits256 {
Expand Down
Loading