-
Notifications
You must be signed in to change notification settings - Fork 33
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(rfq-relayer): gas estimation for zaps #3413
Merged
dwasse
merged 21 commits into
feat/arb-call-fee-pricer
from
feat/arb-call-fee-pricer-encode
Dec 3, 2024
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
603e42f
WIP: use QuoteData instead of QuoteRequest
dwasse cd70e74
WIP: impl conversion
dwasse d1796a1
Feat: use QuoteRequest instead of QuoteData for encoding
dwasse f557248
WIP: another encoding impl
dwasse fdeff59
WIP: add bridgetransactionv2
dwasse 1589b83
WIP: regenerate with running test
dwasse 6ae2731
Working zap e2e
dwasse 3858f68
Cleanup: encoding
dwasse cc1cd2e
Cleanup: move to new encoding file
dwasse 5c5c557
ABIgen from harness instead of lib
dwasse e0f3e55
Cleanup: lint
dwasse 78a3293
Fix: CI build
dwasse bde24d6
Fix: deploy harness
dwasse 20a2e82
Fix: rfq test
dwasse c59425c
Feat: use gofakeit to mock
dwasse e8db2fe
Cleanup: test structure
dwasse 3783b2c
Replace: EncodeQuoteRequest -> EncodeBridgeTx
dwasse a65d861
Feat: add Decode func and add to parity test
dwasse 89e8bb7
Fix: eth address mocks
dwasse 1e05972
Cleanup: lint
dwasse 8d8491a
Cleanup: lint
dwasse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4,004 changes: 4,004 additions & 0 deletions
4,004
services/rfq/contracts/bridgetransactionv2/bridgetransactionv2.abigen.go
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
services/rfq/contracts/bridgetransactionv2/bridgetransactionv2.contractinfo.json
Large diffs are not rendered by default.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
services/rfq/contracts/bridgetransactionv2/bridgetransactionv2.metadata.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Package bridgetransactionv2 is the bridge transaction contract. | ||
package bridgetransactionv2 | ||
|
||
//go:generate go run github.com/synapsecns/sanguine/tools/abigen generate --sol ../../../../packages/contracts-rfq/flattened/BridgeTransactionV2Harness.sol --pkg bridgetransactionv2 --sol-version 0.8.24 --filename bridgetransactionv2 --evm-version istanbul |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package bridgetransactionv2 | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
) | ||
|
||
// BridgeTransactionV2Ref is a bound fast bridge contract that returns the address of the contract. | ||
// | ||
//nolint:golint | ||
type BridgeTransactionV2Ref struct { | ||
*BridgeTransactionV2Harness | ||
address common.Address | ||
} | ||
|
||
// Address gets the ocntract address. | ||
func (f *BridgeTransactionV2Ref) Address() common.Address { | ||
return f.address | ||
} | ||
|
||
// NewBridgeTransactionV2Ref creates a new fast bridge mock contract with a ref. | ||
func NewBridgeTransactionV2Ref(address common.Address, backend bind.ContractBackend) (*BridgeTransactionV2Ref, error) { | ||
bridgetransactionv2, err := NewBridgeTransactionV2Harness(address, backend) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &BridgeTransactionV2Ref{ | ||
BridgeTransactionV2Harness: bridgetransactionv2, | ||
address: address, | ||
}, nil | ||
} | ||
|
||
var _ vm.ContractRef = &BridgeTransactionV2Ref{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package chain | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/synapsecns/sanguine/services/rfq/contracts/fastbridgev2" | ||
) | ||
|
||
const ( | ||
// Field sizes in bytes. | ||
sizeVersion = 2 | ||
sizeChainID = 4 | ||
sizeAddress = 20 | ||
sizeUint256 = 32 | ||
|
||
// Field offsets in bytes. | ||
offsetVersion = 0 | ||
offsetOriginChainID = offsetVersion + sizeVersion | ||
offsetDestChainID = offsetOriginChainID + sizeChainID | ||
offsetOriginSender = offsetDestChainID + sizeChainID | ||
offsetDestRecipient = offsetOriginSender + sizeAddress | ||
offsetOriginToken = offsetDestRecipient + sizeAddress | ||
offsetDestToken = offsetOriginToken + sizeAddress | ||
offsetOriginAmount = offsetDestToken + sizeAddress | ||
offsetDestAmount = offsetOriginAmount + sizeUint256 | ||
offsetOriginFeeAmount = offsetDestAmount + sizeUint256 | ||
offsetDeadline = offsetOriginFeeAmount + sizeUint256 | ||
offsetNonce = offsetDeadline + sizeUint256 | ||
offsetExclusivityRelayer = offsetNonce + sizeUint256 | ||
offsetExclusivityEndTime = offsetExclusivityRelayer + sizeAddress | ||
offsetZapNative = offsetExclusivityEndTime + sizeUint256 | ||
offsetZapData = offsetZapNative + sizeUint256 | ||
) | ||
|
||
// Helper function to properly encode uint256. | ||
func padUint256(b *big.Int) []byte { | ||
// Convert big.Int to bytes | ||
bytes := b.Bytes() | ||
// Create 32-byte array (initialized to zeros) | ||
result := make([]byte, 32) | ||
// Copy bytes to right side of array (left-pad with zeros) | ||
copy(result[32-len(bytes):], bytes) | ||
return result | ||
} | ||
|
||
// EncodeBridgeTx encodes a bridge transaction into a byte array. | ||
func EncodeBridgeTx(tx fastbridgev2.IFastBridgeV2BridgeTransactionV2) ([]byte, error) { | ||
// Initialize with total size including ZapData | ||
result := make([]byte, offsetZapData+len(tx.ZapData)) | ||
|
||
// Version | ||
result[offsetVersion] = 0 | ||
result[offsetVersion+1] = 2 | ||
|
||
// Chain IDs | ||
binary.BigEndian.PutUint32(result[offsetOriginChainID:offsetOriginChainID+sizeChainID], tx.OriginChainId) | ||
binary.BigEndian.PutUint32(result[offsetDestChainID:offsetDestChainID+sizeChainID], tx.DestChainId) | ||
|
||
// Addresses | ||
copy(result[offsetOriginSender:offsetOriginSender+sizeAddress], tx.OriginSender.Bytes()) | ||
copy(result[offsetDestRecipient:offsetDestRecipient+sizeAddress], tx.DestRecipient.Bytes()) | ||
copy(result[offsetOriginToken:offsetOriginToken+sizeAddress], tx.OriginToken.Bytes()) | ||
copy(result[offsetDestToken:offsetDestToken+sizeAddress], tx.DestToken.Bytes()) | ||
|
||
// uint256 values | ||
copy(result[offsetOriginAmount:offsetOriginAmount+sizeUint256], padUint256(tx.OriginAmount)) | ||
copy(result[offsetDestAmount:offsetDestAmount+sizeUint256], padUint256(tx.DestAmount)) | ||
copy(result[offsetOriginFeeAmount:offsetOriginFeeAmount+sizeUint256], padUint256(tx.OriginFeeAmount)) | ||
copy(result[offsetDeadline:offsetDeadline+sizeUint256], padUint256(tx.Deadline)) | ||
copy(result[offsetNonce:offsetNonce+sizeUint256], padUint256(tx.Nonce)) | ||
|
||
// Exclusivity address | ||
copy(result[offsetExclusivityRelayer:offsetExclusivityRelayer+sizeAddress], tx.ExclusivityRelayer.Bytes()) | ||
|
||
// More uint256 values | ||
copy(result[offsetExclusivityEndTime:offsetExclusivityEndTime+sizeUint256], padUint256(tx.ExclusivityEndTime)) | ||
copy(result[offsetZapNative:offsetZapNative+sizeUint256], padUint256(tx.ZapNative)) | ||
|
||
// Replace append with copy for ZapData | ||
copy(result[offsetZapData:], tx.ZapData) | ||
|
||
return result, nil | ||
} | ||
|
||
// DecodeBridgeTx decodes a byte array into a bridge transaction. | ||
func DecodeBridgeTx(data []byte) (fastbridgev2.IFastBridgeV2BridgeTransactionV2, error) { | ||
if len(data) < offsetZapData { | ||
return fastbridgev2.IFastBridgeV2BridgeTransactionV2{}, fmt.Errorf("data too short: got %d bytes, need at least %d", len(data), offsetZapData) | ||
} | ||
|
||
tx := fastbridgev2.IFastBridgeV2BridgeTransactionV2{ | ||
OriginChainId: binary.BigEndian.Uint32(data[offsetOriginChainID:offsetDestChainID]), | ||
DestChainId: binary.BigEndian.Uint32(data[offsetDestChainID:offsetOriginSender]), | ||
OriginSender: common.BytesToAddress(data[offsetOriginSender:offsetDestRecipient]), | ||
DestRecipient: common.BytesToAddress(data[offsetDestRecipient:offsetOriginToken]), | ||
OriginToken: common.BytesToAddress(data[offsetOriginToken:offsetDestToken]), | ||
DestToken: common.BytesToAddress(data[offsetDestToken:offsetOriginAmount]), | ||
OriginAmount: new(big.Int).SetBytes(data[offsetOriginAmount:offsetDestAmount]), | ||
DestAmount: new(big.Int).SetBytes(data[offsetDestAmount:offsetOriginFeeAmount]), | ||
OriginFeeAmount: new(big.Int).SetBytes(data[offsetOriginFeeAmount:offsetDeadline]), | ||
Deadline: new(big.Int).SetBytes(data[offsetDeadline:offsetNonce]), | ||
Nonce: new(big.Int).SetBytes(data[offsetNonce:offsetExclusivityRelayer]), | ||
ExclusivityRelayer: common.BytesToAddress(data[offsetExclusivityRelayer:offsetExclusivityEndTime]), | ||
ExclusivityEndTime: new(big.Int).SetBytes(data[offsetExclusivityEndTime:offsetZapNative]), | ||
ZapNative: new(big.Int).SetBytes(data[offsetZapNative:offsetZapData]), | ||
ZapData: data[offsetZapData:], | ||
} | ||
|
||
return tx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix potential data race on
fastBridgeV2ABI
during concurrent access.The package-level variable
fastBridgeV2ABI
is accessed without synchronization, which can lead to data races ifgetZapGasEstimate
is called concurrently. Usesync.Once
or a mutex to ensure thread-safe initialization.Apply this diff to fix the issue: