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

[goreleaser] Adding RFQ to the Explorer [goreleaser] #2474

Merged
merged 21 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
7 changes: 6 additions & 1 deletion services/explorer/backfill/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type ChainBackfiller struct {
messageBusParser *parser.MessageBusParser
// cctpParser is the parser to use to parse cctp events.
cctpParser *parser.CCTPParser
// rfqParser is the parser to use to parse rfq events.
rfqParser *parser.RFQParser
// Fetcher is the Fetcher to use to fetch logs.
Fetcher fetcher.ScribeFetcher
// chainConfig is the chain config for the chain.
Expand All @@ -44,13 +46,14 @@ const (
)

// NewChainBackfiller creates a new backfiller for a chain.
func NewChainBackfiller(consumerDB db.ConsumerDB, bridgeParser *parser.BridgeParser, swapParsers map[common.Address]*parser.SwapParser, messageBusParser *parser.MessageBusParser, cctpParser *parser.CCTPParser, fetcher fetcher.ScribeFetcher, chainConfig indexerconfig.ChainConfig) *ChainBackfiller {
func NewChainBackfiller(consumerDB db.ConsumerDB, bridgeParser *parser.BridgeParser, swapParsers map[common.Address]*parser.SwapParser, messageBusParser *parser.MessageBusParser, cctpParser *parser.CCTPParser, rfqParser *parser.RFQParser, fetcher fetcher.ScribeFetcher, chainConfig indexerconfig.ChainConfig) *ChainBackfiller {
return &ChainBackfiller{
consumerDB: consumerDB,
bridgeParser: bridgeParser,
swapParsers: swapParsers,
messageBusParser: messageBusParser,
cctpParser: cctpParser,
rfqParser: rfqParser,
Fetcher: fetcher,
chainConfig: chainConfig,
}
Expand Down Expand Up @@ -132,6 +135,8 @@ func (c *ChainBackfiller) makeEventParser(contract indexerconfig.ContractConfig)
eventParser = c.swapParsers[common.HexToAddress(contract.Address)]
case indexerconfig.CCTPContractType:
eventParser = c.cctpParser
case indexerconfig.RFQContractType:
eventParser = c.rfqParser
}
return eventParser, nil
}
Expand Down
149 changes: 142 additions & 7 deletions services/explorer/backfill/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import (
gosql "database/sql"
"fmt"
scribeTypes "github.com/synapsecns/sanguine/services/scribe/types"
"math/big"

scribeTypes "github.com/synapsecns/sanguine/services/scribe/types"

"github.com/synapsecns/sanguine/ethergo/mocks"
"github.com/synapsecns/sanguine/services/explorer/consumer/fetcher/tokenprice"
"github.com/synapsecns/sanguine/services/explorer/consumer/parser/tokendata"
Expand All @@ -26,6 +27,7 @@
"github.com/synapsecns/sanguine/services/explorer/testutil/testcontracts"
bridgeTypes "github.com/synapsecns/sanguine/services/explorer/types/bridge"
cctpTypes "github.com/synapsecns/sanguine/services/explorer/types/cctp"
rfqTypes "github.com/synapsecns/sanguine/services/explorer/types/fastbridge"
Defi-Moses marked this conversation as resolved.
Show resolved Hide resolved
swapTypes "github.com/synapsecns/sanguine/services/explorer/types/swap"
)

Expand All @@ -47,6 +49,7 @@
metaSwapContract, metaSwapRef := b.testDeployManager.GetTestMetaSwap(b.GetTestContext(), b.testBackend)
messageBusContract, messageBusRef := b.testDeployManager.GetTestMessageBusUpgradeable(b.GetTestContext(), b.testBackend)
cctpContract, cctpRef := b.testDeployManager.GetTestCCTP(b.GetTestContext(), b.testBackend)
rfqContract, rfqRef := b.testDeployManager.GetTestRFQ(b.GetTestContext(), b.testBackend)
testDeployManagerB := testcontracts.NewDeployManager(b.T())

swapContractB, swapRefB := testDeployManagerB.GetTestSwapFlashLoan(b.GetTestContext(), b.testBackend)
Expand Down Expand Up @@ -93,14 +96,21 @@
StartBlock: 0,
}

// RFQ config
contractRFQ := indexerConfig.ContractConfig{
ContractType: "rfq",
Address: rfqContract.Address().String(),
StartBlock: 0,
}

// Create the chain configs
chainConfigs := []indexerConfig.ChainConfig{
{
ChainID: uint32(testChainID.Uint64()),
RPCURL: gofakeit.URL(),
FetchBlockIncrement: 2,
MaxGoroutines: 2,
Contracts: []indexerConfig.ContractConfig{contractConfigBridge, contractConfigSwap1, contractConfigSwap2, contractMessageBus, contractConfigMetaSwap, contractCCTP},
Contracts: []indexerConfig.ContractConfig{contractConfigBridge, contractConfigSwap1, contractConfigSwap2, contractMessageBus, contractConfigMetaSwap, contractCCTP, contractRFQ},
},
}
chainConfigsV1 := []indexerConfig.ChainConfig{
Expand Down Expand Up @@ -318,6 +328,23 @@
requestFulfilledLog, err := b.storeTestLog(requestFulfilledTx, uint32(testChainID.Uint64()), 6)
Nil(b.T(), err)

// Store every rfq event
var transactionID [32]byte
transactionIDBytes := common.Hex2Bytes(mocks.NewMockHash(b.T()).String()) // Replace "your_hex_value_here" with an actual hex value if needed
copy(transactionID[:], transactionIDBytes)

bridgeRequestedTx, err := rfqRef.TestBridge(transactOpts.TransactOpts, uint32(testChainID.Uint64()), common.BigToAddress(big.NewInt(gofakeit.Int64())), common.BigToAddress(big.NewInt(gofakeit.Int64())), common.BigToAddress(big.NewInt(gofakeit.Int64())), common.BigToAddress(big.NewInt(gofakeit.Int64())), big.NewInt(gofakeit.Int64()), big.NewInt(gofakeit.Int64()), gofakeit.Bool())
Nil(b.T(), err)
b.storeEthTx(bridgeRequestedTx, testChainID, big.NewInt(int64(5)), 5)
bridgeRequestedLog, err := b.storeTestLog(bridgeRequestedTx, uint32(testChainID.Uint64()), 5)
Nil(b.T(), err)

bridgeRelayedTx, err := rfqRef.TestRelay(transactOpts.TransactOpts, transactionID, common.BigToAddress(big.NewInt(gofakeit.Int64())), common.BigToAddress(big.NewInt(gofakeit.Int64())), uint32(testChainID.Uint64()), common.BigToAddress(big.NewInt(gofakeit.Int64())), common.BigToAddress(big.NewInt(gofakeit.Int64())), big.NewInt(gofakeit.Int64()), big.NewInt(gofakeit.Int64()), big.NewInt(gofakeit.Int64()))
Nil(b.T(), err)
b.storeEthTx(bridgeRelayedTx, testChainID, big.NewInt(int64(5)), 5)
bridgeRelayedLog, err := b.storeTestLog(bridgeRelayedTx, uint32(testChainID.Uint64()), 5)
Nil(b.T(), err)

// Go through each contract and save the end height in scribe
for i := range chainConfigs[0].Contracts {
// the last block store per contract
Expand Down Expand Up @@ -364,12 +391,18 @@
msp, err := parser.NewSwapParser(b.db, metaSwapContract.Address(), true, b.consumerFetcher, msr, tokenDataService, tokenPriceService)
Nil(b.T(), err)

// msr is the meta swap ref for getting token data
// cr is the cctp ref for getting token data
cr, err := fetcher.NewCCTPFetcher(cctpRef.Address(), b.testBackend)
Nil(b.T(), err)
cp, err := parser.NewCCTPParser(b.db, cctpRef.Address(), b.consumerFetcher, cr, tokenDataService, tokenPriceService, false)
Nil(b.T(), err)

// pr is the rfq ref for getting token data
pr, err := fetcher.NewRFQFetcher(rfqRef.Address(), b.testBackend)
Nil(b.T(), err)
pp, err := parser.NewRFQParser(b.db, rfqRef.Address(), b.consumerFetcher, pr, tokenDataService, tokenPriceService, false)
Nil(b.T(), err)

spMap := map[common.Address]*parser.SwapParser{}
spMap[swapContractA.Address()] = spA
spMap[swapContractB.Address()] = spB
Expand All @@ -381,8 +414,9 @@
Nil(b.T(), err)

// Test the first chain in the config file
chainBackfiller := backfill.NewChainBackfiller(b.db, bp, spMap, mbp, cp, f, chainConfigs[0])
chainBackfillerV1 := backfill.NewChainBackfiller(b.db, bpv1, spMap, mbp, cp, f, chainConfigsV1[0])
//@moses this may break??

Check failure on line 417 in services/explorer/backfill/chain_test.go

View workflow job for this annotation

GitHub Actions / Lint (services/explorer)

commentFormatting: put a space between `//` and comment text (gocritic)
Defi-Moses marked this conversation as resolved.
Show resolved Hide resolved
chainBackfiller := backfill.NewChainBackfiller(b.db, bp, spMap, mbp, cp, pp, f, chainConfigs[0])
chainBackfillerV1 := backfill.NewChainBackfiller(b.db, bpv1, spMap, mbp, cp, pp, f, chainConfigsV1[0])

// Backfill the blocks
var count int64
Expand All @@ -395,7 +429,7 @@

bridgeEvents := b.db.UNSAFE_DB().WithContext(b.GetTestContext()).Find(&sql.BridgeEvent{}).Count(&count)
Nil(b.T(), bridgeEvents.Error)
Equal(b.T(), int64(12), count) // 10 + 2 cctp events
Equal(b.T(), int64(14), count) // 10 + 2 cctp events + 2 rfq events

messageEvents := b.db.UNSAFE_DB().WithContext(b.GetTestContext()).Find(&sql.MessageBusEvent{}).Count(&count)
Nil(b.T(), messageEvents.Error)
Expand All @@ -405,12 +439,22 @@
Nil(b.T(), cctpEvents.Error)
Equal(b.T(), int64(2), count)

rfqEvents := b.db.UNSAFE_DB().WithContext(b.GetTestContext()).Find(&sql.RFQEvent{}).Count(&count)
Nil(b.T(), rfqEvents.Error)
Equal(b.T(), int64(2), count)

// Test cctp parity
err = b.sendCircleTokenParity(requestSentLog, cp)
Nil(b.T(), err)
err = b.receiveCircleTokenParity(requestFulfilledLog, cp)
Nil(b.T(), err)

// Test rfq parity
err = b.rfqRequestedTokenParity(bridgeRequestedLog, pp)
Nil(b.T(), err)
err = b.rfqRelayedTokenParity(bridgeRelayedLog, pp)
Nil(b.T(), err)

// Test bridge parity
err = b.depositParity(depositLog, bp, uint32(testChainID.Uint64()), false)
Nil(b.T(), err)
Expand Down Expand Up @@ -487,7 +531,7 @@
bridgeEvents = b.db.UNSAFE_DB().WithContext(b.GetTestContext()).Find(&sql.BridgeEvent{}).Count(&count)

Nil(b.T(), bridgeEvents.Error)
Equal(b.T(), int64(18), count) // 16 + 2 cctp events
Equal(b.T(), int64(20), count) // 16 + 2 cctp events + 2 rfq events

lastBlockStored, err := b.db.GetLastStoredBlock(b.GetTestContext(), uint32(testChainID.Uint64()), chainConfigsV1[0].Contracts[0].Address)

Expand Down Expand Up @@ -597,6 +641,97 @@
return nil
}

// @moses to fix
//
//nolint:dupl
func (b *BackfillSuite) rfqRequestedTokenParity(log *types.Log, parser *parser.RFQParser) error {
// parse the log
parsedLog, err := parser.Filterer.ParseBridgeRequested(*log)
if err != nil {
return fmt.Errorf("error parsing log: %w", err)
}
var count int64
requester := gosql.NullString{
String: parsedLog.Sender.String(),
Valid: true,
}
// Hot fix for chainGas
var chainGas uint8
if parsedLog.SendChainGas {
chainGas = 1
} else {
chainGas = 0
}
// Assuming other relevant fields from the parsed log
// and constructing the query accordingly
events := b.db.UNSAFE_DB().WithContext(b.GetTestContext()).Model(&sql.RFQEvent{}).
Where(&sql.RFQEvent{
ContractAddress: log.Address.String(),
BlockNumber: log.BlockNumber,
TxHash: log.TxHash.String(),
EventType: rfqTypes.BridgeRequestedEvent.Int(),
TransactionID: common.Bytes2Hex(parsedLog.TransactionId[:]),
DestinationChainID: big.NewInt(int64(parsedLog.DestChainId)),
OriginToken: parsedLog.OriginToken.String(),
DestinationToken: parsedLog.DestToken.String(),
OriginAmount: parsedLog.OriginAmount,
DestinationAmount: parsedLog.DestAmount,
ChainGas: chainGas,
Sender: requester,
// Add other fields from parsedLog as needed
}).Count(&count)
query := b.db.UNSAFE_DB().Dialector.Explain(events.Statement.SQL.String(), events.Statement.Vars...)
fmt.Printf("send query: %v\n", query)
if events.Error != nil {
return fmt.Errorf("error querying for event: %w", events.Error)
}
Equal(b.T(), int64(1), count)
return nil
}

//nolint:dupl
func (b *BackfillSuite) rfqRelayedTokenParity(log *types.Log, parser *parser.RFQParser) error {
// parse the log
parsedLog, err := parser.Filterer.ParseBridgeRelayed(*log)
if err != nil {
return fmt.Errorf("error parsing log: %w", err)
}
var count int64
relayer := gosql.NullString{
String: parsedLog.Relayer.String(),
Valid: true,
}
recipient := gosql.NullString{
String: parsedLog.To.String(),
Valid: true,
}

// Assuming other relevant fields from the parsed log
// and constructing the query accordingly
events := b.db.UNSAFE_DB().WithContext(b.GetTestContext()).Model(&sql.RFQEvent{}).
Where(&sql.RFQEvent{
ContractAddress: log.Address.String(),
BlockNumber: log.BlockNumber,
TxHash: log.TxHash.String(),
EventType: rfqTypes.BridgeRelayedEvent.Int(),
TransactionID: common.Bytes2Hex(parsedLog.TransactionId[:]),
Relayer: relayer,
Recipient: recipient,
OriginChainID: big.NewInt(int64(parsedLog.OriginChainId)),
OriginToken: parsedLog.OriginToken.String(),
DestinationToken: parsedLog.DestToken.String(),
OriginAmount: parsedLog.OriginAmount,
DestinationAmount: parsedLog.DestAmount,
ChainGasAmount: parsedLog.ChainGasAmount,
// Add other fields from parsedLog as needed
}).Count(&count)
if events.Error != nil {
return fmt.Errorf("error querying for event: %w", events.Error)
}
Equal(b.T(), int64(1), count)
return nil
}

//nolint:dupl
func (b *BackfillSuite) depositParity(log *types.Log, parser *parser.BridgeParser, chainID uint32, useV1 bool) error {
// parse the log
Expand Down
11 changes: 8 additions & 3 deletions services/explorer/config/indexer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package indexerconfig

import (
"fmt"
"github.com/richardwilkes/toolbox/collection"
"github.com/synapsecns/sanguine/services/explorer/config"
"os"
"path/filepath"

"github.com/richardwilkes/toolbox/collection"
"github.com/synapsecns/sanguine/services/explorer/config"

"github.com/ethereum/go-ethereum/common"
"github.com/jftuga/ellipsis"
"gopkg.in/yaml.v2"
Expand All @@ -27,10 +28,12 @@ const (
MetaSwapContractType
// CCTPContractType is the ContractType for the cctp contract.
CCTPContractType
// RFQContractType is the ContractType for the rfq contract.
RFQContractType
)

func (c ContractType) String() string {
return [...]string{"bridge", "swap", "messagebus", "metaswap", "cctp"}[c]
return [...]string{"bridge", "swap", "messagebus", "metaswap", "cctp", "rfq"}[c]
}

// ContractTypeFromString converts a string (intended to be from parsed config) into the ContractType type.
Expand All @@ -46,6 +49,8 @@ func ContractTypeFromString(s string) (ContractType, error) {
return MetaSwapContractType, nil
case "cctp":
return CCTPContractType, nil
case "rfq":
return RFQContractType, nil
default:
return -1, fmt.Errorf("unknown contract type: %s", s)
}
Expand Down
9 changes: 6 additions & 3 deletions services/explorer/config/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package serverconfig

import (
"fmt"
"github.com/richardwilkes/toolbox/collection"
"github.com/synapsecns/sanguine/services/explorer/config"
"os"
"path/filepath"

"github.com/richardwilkes/toolbox/collection"
"github.com/synapsecns/sanguine/services/explorer/config"

"github.com/jftuga/ellipsis"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -56,6 +57,8 @@ type ContractsConfig struct {
CCTP string `yaml:"cctp"`
// Bridge is the URL of the Scribe server.
Bridge string `yaml:"bridge"`
// RFQ is the address of the rfq contract
RFQ string `yaml:"rfq"`
}

// IsValid makes sure the config is valid.
Expand Down Expand Up @@ -108,7 +111,7 @@ func (c *ChainConfig) IsValid() error {

// IsValid checks if the entered ContractsConfig is valid.
func (c ContractsConfig) IsValid() error {
if c.CCTP == "" && c.Bridge == "" {
if c.CCTP == "" && c.Bridge == "" && c.RFQ == "" {
return fmt.Errorf("one contract must be specified on each contract config")
}
return nil
Expand Down
Loading
Loading