Documentation
¶
Overview ¶
Package bridgeevm detects cross-chain bridge events from EVM transaction logs.
A Detector is built once per chain. Pass any *types.Log to Detect; if the log matches a known bridge event, Detect returns a Result describing the bridge, the leg type (source or destination), and the extracted correlation ID that links the leg to its counterpart on the other chain.
Bridge configurations are embedded JSON, covering Across, Stargate, CCTP, deBridge, USDT0, and 1inch across Ethereum, Polygon, Arbitrum, Base, Optimism, and BSC. See the README for the full coverage matrix.
Index ¶
Examples ¶
Constants ¶
const ( ChainArbitrum = "arbitrum" ChainBase = "base" ChainBSC = "bsc" ChainEthereum = "ethereum" ChainOptimism = "optimism" ChainPolygon = "polygon" )
Supported chain identifiers for use with New.
New accepts any string and returns a usable Detector with zero subscriptions for unrecognized chains; passing one of these constants is the recommended way to avoid typos and to discover the supported set from godoc.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Detector ¶
type Detector struct {
// contains filtered or unexported fields
}
func New ¶
New loads the embedded bridge configs for the given chain (case-insensitive) and returns a Detector with an O(1) address+topic lookup. Pass one of the Chain* constants for the supported set; any other string returns a usable Detector with no subscriptions, and Detect will always return ok=false.
Example ¶
ExampleNew shows how to construct a Detector for a specific chain. New is cheap and safe to call once at process start.
package main
import (
"fmt"
"github.com/miradorlabs/bridgeevm"
)
func main() {
d, err := bridgeevm.New("ethereum")
if err != nil {
panic(err)
}
fmt.Println(d.ChainName(), d.Len() > 0)
}
Output: ethereum true
func (*Detector) Detect ¶
Detect returns the bridge details for log if it matches a known bridge.
The boolean indicates whether a configured bridge matched the log's (address, topic[0]) pair. The error is non-nil only when a bridge matched but correlation extraction failed (malformed log data); in that case the boolean is true and Result is the zero value. Callers that just want to know "is this a bridge?" can ignore the error.
Example ¶
ExampleDetector_Detect shows the common path: hand any *types.Log to Detect and read the bridge name, leg, and correlation ID off the result.
package main
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/miradorlabs/bridgeevm"
)
func main() {
d, _ := bridgeevm.New("arbitrum")
log := &types.Log{
Address: common.HexToAddress("0xe35e9842fceaCA96570B734083f4a58e8F7C5f2A"),
Topics: []common.Hash{
common.HexToHash("0x32ed1a409ef04c7b0227189c3a103dc5ac10e775a15b785dcc510201f7c25ad3"),
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000002105"),
common.HexToHash("0x6f1b280c20fb309b3653566de6f876cd100c6d26bc9722649865147ce22480e7"),
common.HexToHash("0x0000000000000000000000006892ca799bbb10cb13e3cc2a7b587365ba2f3597"),
},
}
result, ok, err := d.Detect(log)
if err != nil {
// A bridge matched but its data was malformed; the boolean is true.
panic(err)
}
if !ok {
fmt.Println("not a bridge log")
return
}
fmt.Printf("%s %s leg, correlation %s\n",
result.BridgeName, result.LegType, result.CorrelationID)
}
Output: across source leg, correlation 50254707460338143966371593114785553611174598192372265612265721490268521529575
Example (UnknownLog) ¶
ExampleDetector_Detect_unknownLog shows that Detect returns ok=false (with no error) for the common case of a log that does not match any configured bridge.
package main
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/miradorlabs/bridgeevm"
)
func main() {
d, _ := bridgeevm.New("ethereum")
log := &types.Log{
Address: common.HexToAddress("0x1234567890123456789012345678901234567890"),
Topics: []common.Hash{common.HexToHash("0xdeadbeef")},
}
_, ok, err := d.Detect(log)
fmt.Println(ok, errors.Is(err, nil))
}
Output: false true
func (*Detector) Len ¶
Len returns the number of (address, topic) subscriptions configured for this chain. A Detector with Len() == 0 will always return ok=false from Detect; callers can use this to detect chains that have no embedded coverage.
func (*Detector) Subscriptions ¶ added in v0.2.0
func (d *Detector) Subscriptions() []Subscription
Subscriptions returns every (address, topic) pair this Detector watches. Order is deterministic across calls, sorted by address bytes then topic bytes, so callers can build stable FilterQuery payloads. The returned slice is freshly allocated on each call; callers may mutate it.
type Subscription ¶ added in v0.2.0
Subscription is a (contract, event-topic) pair the Detector matches against. Consumers building an eth_subscribeFilterLogs FilterQuery iterate Subscriptions and pass the deduped addresses and topics into the query.