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(rfq): move chain utils to new util pkg #2982

Merged
merged 5 commits into from
Aug 6, 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
15 changes: 6 additions & 9 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: Go Workflows
on:
pull_request:
branches-ignore:
- 'gh-pages'
- 'fe-release'
- 'gh-pages'
- 'fe-release'
push:
branches-ignore:
- 'gh-pages'
Expand Down Expand Up @@ -77,8 +77,8 @@ jobs:

test:
name: Go Coverage
runs-on: ${{ (matrix.package == 'agents' || matrix.package == 'core' || matrix.package == 'tools' || contains(matrix.package, 'explorer') || contains(matrix.package, 'contrib')) && 'ubuntu-latest' || 'namespace-profile-default-coverage-runner' }}
if: ${{ !contains(matrix.package, 'agents') && needs.changes.outputs.package_count_deps > 0 && (github.event_name != 'push' || github.ref == format('refs/heads/{0}', github.event.repository.default_branch)) }}
runs-on: ${{ (matrix.package == 'core' || matrix.package == 'tools' || contains(matrix.package, 'explorer') || contains(matrix.package, 'contrib')) && 'ubuntu-latest' || 'namespace-profile-default-coverage-runner' }}
if: ${{ needs.changes.outputs.package_count_deps > 0 && (github.event_name != 'push' || github.ref == format('refs/heads/{0}', github.event.repository.default_branch)) }}
needs: changes
strategy:
fail-fast: false
Expand Down Expand Up @@ -109,7 +109,7 @@ jobs:

- name: Go modules cache
uses: actions/cache@v4
if: ${{ !contains(runner.name, 'nsc') }}
if: ${{ !contains(runner.name, 'nsc') }}
with:
# see https://github.com/mvdan/github-actions-golang
# also: https://glebbahmutov.com/blog/do-not-let-npm-cache-snowball/ w/ go build (workaround now is having a cache that just gets expired at night)
Expand Down Expand Up @@ -143,7 +143,6 @@ jobs:
sleep 1
done


- name: Install gotestsum
uses: jaxxstorm/[email protected]
with: # Grab the latest version
Expand All @@ -156,7 +155,6 @@ jobs:
run: |
echo "::set-output name=flag::$(echo "${{ matrix.package }}" | rev | cut -d/ -f1 | rev)"


- name: Precompile Tests
working-directory: ${{ matrix.package }}
run: go test -run=nope ./...
Expand Down Expand Up @@ -232,9 +230,8 @@ jobs:
run: echo "::set-output name=package_name::$(echo ${{ matrix.package }} | sed 's/\//-/g')"
shell: bash


- name: Upload logs to GitHub
# if: failure()
# if: failure()
uses: actions/upload-artifact@v4
with:
name: ${{ steps.replace.outputs.package_name }}
Expand Down
10 changes: 5 additions & 5 deletions services/rfq/e2e/rfq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"github.com/synapsecns/sanguine/services/rfq/contracts/fastbridge"
"github.com/synapsecns/sanguine/services/rfq/guard/guarddb"
guardService "github.com/synapsecns/sanguine/services/rfq/guard/service"
"github.com/synapsecns/sanguine/services/rfq/relayer/chain"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb"
"github.com/synapsecns/sanguine/services/rfq/relayer/service"
"github.com/synapsecns/sanguine/services/rfq/testutil"
"github.com/synapsecns/sanguine/services/rfq/util"
)

type IntegrationSuite struct {
Expand Down Expand Up @@ -288,7 +288,7 @@ func (i *IntegrationSuite) TestETHtoETH() {

// let's figure out the amount of ETH we need
for _, quote := range allQuotes {
if common.HexToAddress(quote.DestTokenAddr) == chain.EthAddress {
if common.HexToAddress(quote.DestTokenAddr) == util.EthAddress {
destAmountBigInt, _ := new(big.Int).SetString(quote.DestAmount, 10)
if destAmountBigInt.Cmp(realWantAmount) > 0 {
// we found our quote!
Expand All @@ -308,9 +308,9 @@ func (i *IntegrationSuite) TestETHtoETH() {
tx, err := originFastBridge.Bridge(auth.TransactOpts, fastbridge.IFastBridgeBridgeParams{
DstChainId: uint32(i.destBackend.GetChainID()),
To: i.userWallet.Address(),
OriginToken: chain.EthAddress,
OriginToken: util.EthAddress,
SendChainGas: true,
DestToken: chain.EthAddress,
DestToken: util.EthAddress,
OriginAmount: realWantAmount,
DestAmount: new(big.Int).Sub(realWantAmount, big.NewInt(1e17)),
Deadline: new(big.Int).SetInt64(time.Now().Add(time.Hour * 24).Unix()),
Expand Down Expand Up @@ -353,7 +353,7 @@ func (i *IntegrationSuite) TestETHtoETH() {

// let's figure out the amount of ETH we need
for _, quote := range allQuotes {
if common.HexToAddress(quote.DestTokenAddr) == chain.EthAddress && quote.DestChainID == originBackendChainID {
if common.HexToAddress(quote.DestTokenAddr) == util.EthAddress && quote.DestChainID == originBackendChainID {
// we should now have some ETH on the origin chain since we claimed
// this should be offered up as inventory
destAmountBigInt, _ := new(big.Int).SetString(quote.DestAmount, 10)
Expand Down
52 changes: 33 additions & 19 deletions services/rfq/e2e/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package e2e_test

import (
"context"
"fmt"
"math/big"
"net/http"
"slices"
"strconv"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"

Expand All @@ -17,6 +19,7 @@ import (
"github.com/phayes/freeport"
"github.com/synapsecns/sanguine/core"
"github.com/synapsecns/sanguine/core/dbcommon"
"github.com/synapsecns/sanguine/core/retry"
"github.com/synapsecns/sanguine/core/testsuite"
"github.com/synapsecns/sanguine/ethergo/backends"
"github.com/synapsecns/sanguine/ethergo/backends/anvil"
Expand All @@ -34,11 +37,11 @@ import (
"github.com/synapsecns/sanguine/services/rfq/guard/guardconfig"
guardConnect "github.com/synapsecns/sanguine/services/rfq/guard/guarddb/connect"
guardService "github.com/synapsecns/sanguine/services/rfq/guard/service"
"github.com/synapsecns/sanguine/services/rfq/relayer/chain"
"github.com/synapsecns/sanguine/services/rfq/relayer/relconfig"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb/connect"
"github.com/synapsecns/sanguine/services/rfq/relayer/service"
"github.com/synapsecns/sanguine/services/rfq/testutil"
"github.com/synapsecns/sanguine/services/rfq/util"
)

func (i *IntegrationSuite) setupQuoterAPI() {
Expand Down Expand Up @@ -208,21 +211,32 @@ func addressToBytes32(addr common.Address) [32]byte {

// Approve checks if the token is approved and approves it if not.
func (i *IntegrationSuite) Approve(backend backends.SimulatedTestBackend, token contracts.DeployedContract, user wallet.Wallet) {
erc20, err := ierc20.NewIERC20(token.Address(), backend)
i.NoError(err)
err := retry.WithBackoff(i.GetTestContext(), func(_ context.Context) (err error) {
erc20, err := ierc20.NewIERC20(token.Address(), backend)
if err != nil {
return fmt.Errorf("could not get token at %s: %w", token.Address().String(), err)
}
Comment on lines +216 to +218
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Ensure error messages are consistent with project standards


_, fastBridge := i.manager.GetFastBridge(i.GetTestContext(), backend)
_, fastBridge := i.manager.GetFastBridge(i.GetTestContext(), backend)

allowance, err := erc20.Allowance(&bind.CallOpts{Context: i.GetTestContext()}, user.Address(), fastBridge.Address())
i.NoError(err)
allowance, err := erc20.Allowance(&bind.CallOpts{Context: i.GetTestContext()}, user.Address(), fastBridge.Address())
if err != nil {
return fmt.Errorf("could not get allowance: %w", err)
}

// TODO: can also use in mem cache
if allowance.Cmp(big.NewInt(0)) == 0 {
txOpts := backend.GetTxContext(i.GetTestContext(), user.AddressPtr())
tx, err := erc20.Approve(txOpts.TransactOpts, fastBridge.Address(), core.CopyBigInt(abi.MaxUint256))
i.NoError(err)
backend.WaitForConfirmation(i.GetTestContext(), tx)
}
// TODO: can also use in mem cache
if allowance.Cmp(big.NewInt(0)) == 0 {
txOpts := backend.GetTxContext(i.GetTestContext(), user.AddressPtr())
tx, err := erc20.Approve(txOpts.TransactOpts, fastBridge.Address(), core.CopyBigInt(abi.MaxUint256))
if err != nil {
return fmt.Errorf("could not approve: %w", err)
}
backend.WaitForConfirmation(i.GetTestContext(), tx)
}

return nil
}, retry.WithMaxTotalTime(15*time.Second))
i.NoError(err)
}

func (i *IntegrationSuite) getRelayerConfig() relconfig.Config {
Expand All @@ -241,7 +255,7 @@ func (i *IntegrationSuite) getRelayerConfig() relconfig.Config {
Confirmations: 0,
Tokens: map[string]relconfig.TokenConfig{
"ETH": {
Address: chain.EthAddress.String(),
Address: util.EthAddress.String(),
PriceUSD: 2000,
Decimals: 18,
},
Expand All @@ -254,7 +268,7 @@ func (i *IntegrationSuite) getRelayerConfig() relconfig.Config {
Confirmations: 0,
Tokens: map[string]relconfig.TokenConfig{
"ETH": {
Address: chain.EthAddress.String(),
Address: util.EthAddress.String(),
PriceUSD: 2000,
Decimals: 18,
},
Expand Down Expand Up @@ -379,11 +393,11 @@ func (i *IntegrationSuite) setupRelayer() {
}

// Add ETH as quotable token from origin to destination
cfg.QuotableTokens[fmt.Sprintf("%d-%s", originBackendChainID, chain.EthAddress)] = []string{
fmt.Sprintf("%d-%s", destBackendChainID, chain.EthAddress),
cfg.QuotableTokens[fmt.Sprintf("%d-%s", originBackendChainID, util.EthAddress)] = []string{
fmt.Sprintf("%d-%s", destBackendChainID, util.EthAddress),
}
cfg.QuotableTokens[fmt.Sprintf("%d-%s", destBackendChainID, chain.EthAddress)] = []string{
fmt.Sprintf("%d-%s", originBackendChainID, chain.EthAddress),
cfg.QuotableTokens[fmt.Sprintf("%d-%s", destBackendChainID, util.EthAddress)] = []string{
fmt.Sprintf("%d-%s", originBackendChainID, util.EthAddress),
}

var err error
Expand Down
3 changes: 2 additions & 1 deletion services/rfq/relayer/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/synapsecns/sanguine/services/rfq/contracts/fastbridge"
"github.com/synapsecns/sanguine/services/rfq/relayer/relconfig"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb"
"github.com/synapsecns/sanguine/services/rfq/util"
)

// Chain is a chain helper for relayer.
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c Chain) SubmitRelay(ctx context.Context, request reldb.QuoteRequest) (uin
var err error

// Check to see if ETH should be sent to destination
if IsGasToken(request.Transaction.DestToken) {
if util.IsGasToken(request.Transaction.DestToken) {
gasAmount = request.Transaction.DestAmount
} else if request.Transaction.SendChainGas {
gasAmount, err = c.Bridge.ChainGasAmount(&bind.CallOpts{Context: ctx})
Expand Down
14 changes: 7 additions & 7 deletions services/rfq/relayer/inventory/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"github.com/synapsecns/sanguine/ethergo/client"
"github.com/synapsecns/sanguine/ethergo/submitter"
"github.com/synapsecns/sanguine/services/rfq/contracts/ierc20"
"github.com/synapsecns/sanguine/services/rfq/relayer/chain"
"github.com/synapsecns/sanguine/services/rfq/relayer/relconfig"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb"
"github.com/synapsecns/sanguine/services/rfq/util"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -95,7 +95,7 @@ func (i *inventoryManagerImpl) GetCommittableBalance(ctx context.Context, chainI
balance := committableBalances[chainID][token]
// the gas token may not be registered in the inventory tokens map,
// but it is always tracked in gasBalances.
if balance == nil && token == chain.EthAddress {
if balance == nil && token == util.EthAddress {
gasBalance, ok := i.gasBalances[chainID]
if !ok || gasBalance == nil {
return nil, ErrUnsupportedChain
Expand Down Expand Up @@ -311,7 +311,7 @@ func (i *inventoryManagerImpl) ApproveAllTokens(ctx context.Context) error {
// approve RFQ contract.
// Note: in the case where submitter hasn't finished from last boot,
// this will double submit approvals unfortunately.
if address != chain.EthAddress && token.Allowances[contractRFQ].Cmp(big.NewInt(0)) == 0 {
if address != util.EthAddress && token.Allowances[contractRFQ].Cmp(big.NewInt(0)) == 0 {
tokenAddr := address // capture func literal
contractAddr, err := i.cfg.GetRFQAddress(chainID)
if err != nil {
Expand All @@ -324,7 +324,7 @@ func (i *inventoryManagerImpl) ApproveAllTokens(ctx context.Context) error {
}

// approve SynapseCCTP contract
if address != chain.EthAddress && token.Allowances[contractSynapseCCTP].Cmp(big.NewInt(0)) == 0 {
if address != util.EthAddress && token.Allowances[contractSynapseCCTP].Cmp(big.NewInt(0)) == 0 {
tokenAddr := address // capture func literal
contractAddr, err := i.cfg.GetSynapseCCTPAddress(chainID)
if err != nil {
Expand All @@ -337,7 +337,7 @@ func (i *inventoryManagerImpl) ApproveAllTokens(ctx context.Context) error {
}

// approve TokenMessenger contract
if address != chain.EthAddress && token.Allowances[contractTokenMessenger].Cmp(big.NewInt(0)) == 0 {
if address != util.EthAddress && token.Allowances[contractTokenMessenger].Cmp(big.NewInt(0)) == 0 {
tokenAddr := address // capture func literal
contractAddr, err := i.cfg.GetTokenMessengerAddress(chainID)
if err != nil {
Expand Down Expand Up @@ -409,7 +409,7 @@ func (i *inventoryManagerImpl) HasSufficientGas(parentCtx context.Context, chain
span.SetAttributes(attribute.String("gas_value", gasValue.String()))
}

gasBalance, err := i.GetCommittableBalance(ctx, chainID, chain.EthAddress)
gasBalance, err := i.GetCommittableBalance(ctx, chainID, util.EthAddress)
if err != nil {
return false, fmt.Errorf("error getting committable gas on origin: %w", err)
}
Expand Down Expand Up @@ -541,7 +541,7 @@ func (i *inventoryManagerImpl) initializeTokens(parentCtx context.Context, cfg r

var token common.Address
if rtoken.IsGasToken {
token = chain.EthAddress
token = util.EthAddress
} else {
token = common.HexToAddress(tokenCfg.Address)
}
Expand Down
4 changes: 2 additions & 2 deletions services/rfq/relayer/quoter/quoter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (

"github.com/ipfs/go-log"
"github.com/synapsecns/sanguine/core/metrics"
"github.com/synapsecns/sanguine/services/rfq/relayer/chain"
"github.com/synapsecns/sanguine/services/rfq/relayer/pricer"
"github.com/synapsecns/sanguine/services/rfq/relayer/relconfig"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb"
"github.com/synapsecns/sanguine/services/rfq/util"
"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"

Expand Down Expand Up @@ -539,7 +539,7 @@ func (m *Manager) getOriginAmount(parentCtx context.Context, origin, dest int, a

// deductGasCost deducts the gas cost from the quote amount, if necessary.
func (m *Manager) deductGasCost(parentCtx context.Context, quoteAmount *big.Int, address common.Address, dest int) (quoteAmountAdj *big.Int, err error) {
if !chain.IsGasToken(address) {
if !util.IsGasToken(address) {
return quoteAmount, nil
}

Expand Down
16 changes: 8 additions & 8 deletions services/rfq/relayer/quoter/quoter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
fetcherMocks "github.com/synapsecns/sanguine/ethergo/submitter/mocks"
"github.com/synapsecns/sanguine/services/rfq/api/model"
"github.com/synapsecns/sanguine/services/rfq/contracts/fastbridge"
"github.com/synapsecns/sanguine/services/rfq/relayer/chain"
inventoryMocks "github.com/synapsecns/sanguine/services/rfq/relayer/inventory/mocks"
"github.com/synapsecns/sanguine/services/rfq/relayer/pricer"
priceMocks "github.com/synapsecns/sanguine/services/rfq/relayer/pricer/mocks"
"github.com/synapsecns/sanguine/services/rfq/relayer/quoter"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb"
"github.com/synapsecns/sanguine/services/rfq/util"
)

func (s *QuoterSuite) TestGenerateQuotes() {
Expand All @@ -43,7 +43,7 @@ func (s *QuoterSuite) TestGenerateQuotes() {
func (s *QuoterSuite) TestGenerateQuotesForNativeToken() {
// Generate quotes for ETH on the destination chain.
balance, _ := new(big.Int).SetString("1000000000000000000", 10) // 1 ETH
quotes, err := s.manager.GenerateQuotes(s.GetTestContext(), int(s.destinationEth), chain.EthAddress, balance)
quotes, err := s.manager.GenerateQuotes(s.GetTestContext(), int(s.destinationEth), util.EthAddress, balance)
s.Require().NoError(err)

minGasToken, err := s.config.GetMinGasToken(int(s.destination))
Expand All @@ -54,9 +54,9 @@ func (s *QuoterSuite) TestGenerateQuotesForNativeToken() {
expectedQuotes := []model.PutQuoteRequest{
{
OriginChainID: int(s.origin),
OriginTokenAddr: chain.EthAddress.String(),
OriginTokenAddr: util.EthAddress.String(),
DestChainID: int(s.destinationEth),
DestTokenAddr: chain.EthAddress.String(),
DestTokenAddr: util.EthAddress.String(),
DestAmount: expectedQuoteAmount.String(),
MaxOriginAmount: expectedQuoteAmount.String(),
FixedFee: "150000000000000000", // (500k gas + 1m gas) * 100 gwei
Expand All @@ -68,7 +68,7 @@ func (s *QuoterSuite) TestGenerateQuotesForNativeToken() {
s.config.BaseChainConfig.MinGasToken = "100000000000000000" // 0.1 ETH
s.manager.SetConfig(s.config)

quotes, err = s.manager.GenerateQuotes(s.GetTestContext(), int(s.destinationEth), chain.EthAddress, balance)
quotes, err = s.manager.GenerateQuotes(s.GetTestContext(), int(s.destinationEth), util.EthAddress, balance)
s.Require().NoError(err)

minGasToken, err = s.config.GetMinGasToken(int(s.destination))
Expand All @@ -79,9 +79,9 @@ func (s *QuoterSuite) TestGenerateQuotesForNativeToken() {
expectedQuotes = []model.PutQuoteRequest{
{
OriginChainID: int(s.origin),
OriginTokenAddr: chain.EthAddress.String(),
OriginTokenAddr: util.EthAddress.String(),
DestChainID: int(s.destinationEth),
DestTokenAddr: chain.EthAddress.String(),
DestTokenAddr: util.EthAddress.String(),
DestAmount: expectedQuoteAmount.String(),
MaxOriginAmount: expectedQuoteAmount.String(),
FixedFee: "150000000000000000", // (500k gas + 1m gas) * 100 gwei
Expand All @@ -93,7 +93,7 @@ func (s *QuoterSuite) TestGenerateQuotesForNativeToken() {
s.config.BaseChainConfig.MinGasToken = "1000000000000000001" // 0.1 ETH
s.manager.SetConfig(s.config)

quotes, err = s.manager.GenerateQuotes(s.GetTestContext(), int(s.destinationEth), chain.EthAddress, balance)
quotes, err = s.manager.GenerateQuotes(s.GetTestContext(), int(s.destinationEth), util.EthAddress, balance)
s.NoError(err)
s.Equal(quotes[0].DestAmount, "0")
s.Equal(quotes[0].MaxOriginAmount, "0")
Expand Down
Loading
Loading