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

fix: use single on-chain bridge fee #522

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions bridge/standard/pkg/gwcontract/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type GatewayTransactor interface {
_recipient common.Address,
_amount *big.Int,
_counterpartyIdx *big.Int,
_finalizationFee *big.Int,
) (*types.Transaction, error)
}

Expand Down Expand Up @@ -71,6 +72,7 @@ func (g *Gateway[EventType]) FinalizeTransfer(
recipient common.Address,
amount *big.Int,
counterpartyIdx *big.Int,
finalizationFee *big.Int,
) error {
switch settled, err := g.store.IsSettled(ctx, counterpartyIdx); {
case err != nil:
Expand All @@ -92,6 +94,7 @@ func (g *Gateway[EventType]) FinalizeTransfer(
recipient,
amount,
counterpartyIdx,
finalizationFee,
)
if err != nil {
g.logger.Error(
Expand Down
32 changes: 20 additions & 12 deletions bridge/standard/pkg/gwcontract/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (t *testGatewayTransactor) FinalizeTransfer(
recipient common.Address,
amount *big.Int,
counterpartyIdx *big.Int,
finalizationFee *big.Int,
) (*types.Transaction, error) {
newNonce := t.nonce
t.nonce++
Expand Down Expand Up @@ -143,22 +144,25 @@ func TestGateway(t *testing.T) {

transfers := []*l1gateway.L1gatewayTransferInitiated{
{
Sender: common.HexToAddress("0x1234"),
Recipient: common.HexToAddress("0x5678"),
Amount: big.NewInt(100),
TransferIdx: big.NewInt(1),
Sender: common.HexToAddress("0x1234"),
Recipient: common.HexToAddress("0x5678"),
Amount: big.NewInt(100),
TransferIdx: big.NewInt(1),
CounterpartyFinalizationFee: big.NewInt(10),
},
{
Sender: common.HexToAddress("0x5678"),
Recipient: common.HexToAddress("0x1234"),
Amount: big.NewInt(200),
TransferIdx: big.NewInt(2),
Sender: common.HexToAddress("0x5678"),
Recipient: common.HexToAddress("0x1234"),
Amount: big.NewInt(200),
TransferIdx: big.NewInt(2),
CounterpartyFinalizationFee: big.NewInt(20),
},
{
Sender: common.HexToAddress("0x1234"),
Recipient: common.HexToAddress("0x5678"),
Amount: big.NewInt(300),
TransferIdx: big.NewInt(3),
Sender: common.HexToAddress("0x1234"),
Recipient: common.HexToAddress("0x5678"),
Amount: big.NewInt(300),
TransferIdx: big.NewInt(3),
CounterpartyFinalizationFee: big.NewInt(30),
},
}

Expand Down Expand Up @@ -208,6 +212,7 @@ func TestGateway(t *testing.T) {
transfer.Recipient,
transfer.Amount,
transfer.TransferIdx,
transfer.CounterpartyFinalizationFee,
); err != nil {
t.Fatal(err)
}
Expand All @@ -229,6 +234,7 @@ func TestGateway(t *testing.T) {
transfers[0].Recipient,
transfers[0].Amount,
transfers[0].TransferIdx,
transfers[0].CounterpartyFinalizationFee,
); err != nil {
t.Fatal(err)
}
Expand All @@ -241,6 +247,7 @@ func TestGateway(t *testing.T) {
common.HexToAddress("0x1234"),
big.NewInt(100),
big.NewInt(4),
big.NewInt(10),
); err == nil {
t.Fatal("expected error")
}
Expand All @@ -254,6 +261,7 @@ func publishTransfer(
event := brABI.Events["TransferInitiated"]
buf, err := event.Inputs.NonIndexed().Pack(
transfer.Amount,
transfer.CounterpartyFinalizationFee,
)
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions bridge/standard/pkg/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (

type L1Gateway interface {
Subscribe(ctx context.Context) (<-chan *l1gateway.L1gatewayTransferInitiated, <-chan error)
FinalizeTransfer(ctx context.Context, recipient common.Address, amount *big.Int, transferIdx *big.Int) error
FinalizeTransfer(ctx context.Context, recipient common.Address, amount *big.Int, transferIdx *big.Int, finalizationFee *big.Int) error
}

type SettlementGateway interface {
Subscribe(ctx context.Context) (<-chan *settlementgateway.SettlementgatewayTransferInitiated, <-chan error)
FinalizeTransfer(ctx context.Context, recipient common.Address, amount *big.Int, transferIdx *big.Int) error
FinalizeTransfer(ctx context.Context, recipient common.Address, amount *big.Int, transferIdx *big.Int, finalizationFee *big.Int) error
}

type Relayer struct {
Expand Down Expand Up @@ -72,6 +72,7 @@ func (r *Relayer) Start(ctx context.Context) <-chan struct{} {
upd.Recipient,
upd.Amount,
upd.TransferIdx,
upd.CounterpartyFinalizationFee,
)
if err != nil {
r.logger.Error(
Expand Down Expand Up @@ -104,6 +105,7 @@ func (r *Relayer) Start(ctx context.Context) <-chan struct{} {
upd.Recipient,
upd.Amount,
upd.TransferIdx,
upd.CounterpartyFinalizationFee,
)
if err != nil {
r.logger.Error(
Expand Down
67 changes: 38 additions & 29 deletions bridge/standard/pkg/relayer/relayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
)

type Transfer struct {
Recipient common.Address
Amount *big.Int
TransferIdx *big.Int
Recipient common.Address
Amount *big.Int
TransferIdx *big.Int
FinalizationFee *big.Int
}

type testL1Gateway struct {
Expand All @@ -33,12 +34,13 @@ func (t *testL1Gateway) Subscribe(ctx context.Context) (<-chan *l1gateway.L1gate
return t.initiated, t.err
}

func (t *testL1Gateway) FinalizeTransfer(ctx context.Context, recipient common.Address, amount *big.Int, transferIdx *big.Int) error {
func (t *testL1Gateway) FinalizeTransfer(ctx context.Context, recipient common.Address, amount *big.Int, transferIdx *big.Int, finalizationFee *big.Int) error {
t.mu.Lock()
t.finalized = append(t.finalized, Transfer{
Recipient: recipient,
Amount: amount,
TransferIdx: transferIdx,
Recipient: recipient,
Amount: amount,
TransferIdx: transferIdx,
FinalizationFee: finalizationFee,
})
t.mu.Unlock()
return nil
Expand All @@ -55,12 +57,13 @@ func (t *testSettlementGateway) Subscribe(ctx context.Context) (<-chan *settleme
return t.initiated, t.err
}

func (t *testSettlementGateway) FinalizeTransfer(ctx context.Context, recipient common.Address, amount *big.Int, transferIdx *big.Int) error {
func (t *testSettlementGateway) FinalizeTransfer(ctx context.Context, recipient common.Address, amount *big.Int, transferIdx *big.Int, finalizationFee *big.Int) error {
t.mu.Lock()
t.finalized = append(t.finalized, Transfer{
Recipient: recipient,
Amount: amount,
TransferIdx: transferIdx,
Recipient: recipient,
Amount: amount,
TransferIdx: transferIdx,
FinalizationFee: finalizationFee,
})
t.mu.Unlock()
return nil
Expand All @@ -83,32 +86,37 @@ func TestRelayer(t *testing.T) {

expTransfers := []Transfer{
{
Recipient: common.HexToAddress("0x1234"),
Amount: big.NewInt(100),
TransferIdx: big.NewInt(1),
Recipient: common.HexToAddress("0x1234"),
Amount: big.NewInt(100),
TransferIdx: big.NewInt(1),
FinalizationFee: big.NewInt(10),
},
{
Recipient: common.HexToAddress("0x5678"),
Amount: big.NewInt(200),
TransferIdx: big.NewInt(2),
Recipient: common.HexToAddress("0x5678"),
Amount: big.NewInt(200),
TransferIdx: big.NewInt(2),
FinalizationFee: big.NewInt(20),
},
{
Recipient: common.HexToAddress("0x9abc"),
Amount: big.NewInt(300),
TransferIdx: big.NewInt(3),
Recipient: common.HexToAddress("0x9abc"),
Amount: big.NewInt(300),
TransferIdx: big.NewInt(3),
FinalizationFee: big.NewInt(30),
},
{
Recipient: common.HexToAddress("0xdef0"),
Amount: big.NewInt(400),
TransferIdx: big.NewInt(4),
Recipient: common.HexToAddress("0xdef0"),
Amount: big.NewInt(400),
TransferIdx: big.NewInt(4),
FinalizationFee: big.NewInt(40),
},
}

for _, transfer := range expTransfers {
l1Gateway.initiated <- &l1gateway.L1gatewayTransferInitiated{
Recipient: transfer.Recipient,
Amount: transfer.Amount,
TransferIdx: transfer.TransferIdx,
Recipient: transfer.Recipient,
Amount: transfer.Amount,
TransferIdx: transfer.TransferIdx,
CounterpartyFinalizationFee: transfer.FinalizationFee,
}
}

Expand All @@ -135,9 +143,10 @@ func TestRelayer(t *testing.T) {

for _, transfer := range expTransfers {
settlementGateway.initiated <- &settlementgateway.SettlementgatewayTransferInitiated{
Recipient: transfer.Recipient,
Amount: transfer.Amount,
TransferIdx: transfer.TransferIdx,
Recipient: transfer.Recipient,
Amount: transfer.Amount,
TransferIdx: transfer.TransferIdx,
CounterpartyFinalizationFee: transfer.FinalizationFee,
}
}

Expand Down
Loading
Loading