-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
460 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
package abi | ||
|
||
const RouterABI = `[ | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "uint8", | ||
"name": "domainID", | ||
"type": "uint8" | ||
} | ||
], | ||
"stateMutability": "nonpayable", | ||
"type": "constructor" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "DepositToCurrentDomain", | ||
"type": "error" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "NonceDecrementsNotAllowed", | ||
"type": "error" | ||
}, | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": false, | ||
"internalType": "uint8", | ||
"name": "destinationDomainID", | ||
"type": "uint8" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "uint8", | ||
"name": "securityModel", | ||
"type": "uint8" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "bytes32", | ||
"name": "resourceID", | ||
"type": "bytes32" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "uint64", | ||
"name": "depositNonce", | ||
"type": "uint64" | ||
}, | ||
{ | ||
"indexed": true, | ||
"internalType": "address", | ||
"name": "user", | ||
"type": "address" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "bytes", | ||
"name": "data", | ||
"type": "bytes" | ||
} | ||
], | ||
"name": "Deposit", | ||
"type": "event" | ||
}, | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": true, | ||
"internalType": "address", | ||
"name": "previousOwner", | ||
"type": "address" | ||
}, | ||
{ | ||
"indexed": true, | ||
"internalType": "address", | ||
"name": "newOwner", | ||
"type": "address" | ||
} | ||
], | ||
"name": "OwnershipTransferred", | ||
"type": "event" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "uint8", | ||
"name": "", | ||
"type": "uint8" | ||
} | ||
], | ||
"name": "_depositCounts", | ||
"outputs": [ | ||
{ | ||
"internalType": "uint64", | ||
"name": "", | ||
"type": "uint64" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "_domainID", | ||
"outputs": [ | ||
{ | ||
"internalType": "uint8", | ||
"name": "", | ||
"type": "uint8" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "uint8", | ||
"name": "destinationDomainID", | ||
"type": "uint8" | ||
}, | ||
{ | ||
"internalType": "uint8", | ||
"name": "securityModel", | ||
"type": "uint8" | ||
}, | ||
{ | ||
"internalType": "bytes32", | ||
"name": "resourceID", | ||
"type": "bytes32" | ||
}, | ||
{ | ||
"internalType": "bytes", | ||
"name": "depositData", | ||
"type": "bytes" | ||
}, | ||
{ | ||
"internalType": "bytes", | ||
"name": "feeData", | ||
"type": "bytes" | ||
} | ||
], | ||
"name": "deposit", | ||
"outputs": [ | ||
{ | ||
"internalType": "uint64", | ||
"name": "depositNonce", | ||
"type": "uint64" | ||
} | ||
], | ||
"stateMutability": "payable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "owner", | ||
"outputs": [ | ||
{ | ||
"internalType": "address", | ||
"name": "", | ||
"type": "address" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "renounceOwnership", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "uint256", | ||
"name": "", | ||
"type": "uint256" | ||
} | ||
], | ||
"name": "transferHashes", | ||
"outputs": [ | ||
{ | ||
"internalType": "bytes32", | ||
"name": "", | ||
"type": "bytes32" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "address", | ||
"name": "newOwner", | ||
"type": "address" | ||
} | ||
], | ||
"name": "transferOwnership", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
} | ||
]` |
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,33 @@ | ||
package events | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
type EventSig string | ||
|
||
func (es EventSig) GetTopic() common.Hash { | ||
return crypto.Keccak256Hash([]byte(es)) | ||
} | ||
|
||
const ( | ||
DepositSig EventSig = "Deposit(uint8,uint8,bytes32,uint64,address,bytes)" | ||
) | ||
|
||
// Deposit struct holds event data raised by Deposit event on-chain | ||
type Deposit struct { | ||
// ID of chain deposit will be bridged to | ||
DestinationDomainID uint8 | ||
// SecurityModel is used to distringuish between block header oracles | ||
// on the destination network that verify this deposit | ||
SecurityModel uint8 | ||
// ResourceID used to find address of handler to be used for deposit | ||
ResourceID [32]byte | ||
// Nonce of deposit | ||
DepositNonce uint64 | ||
// Address of sender (msg.sender: user) | ||
SenderAddress common.Address | ||
// Additional data to be passed to specified handler | ||
Data []byte | ||
} |
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,86 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
|
||
ethereumABI "github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/rs/zerolog/log" | ||
"github.com/sygmaprotocol/spectre-node/chains/evm/abi" | ||
"github.com/sygmaprotocol/spectre-node/chains/evm/listener/events" | ||
"github.com/sygmaprotocol/sygma-core/relayer/message" | ||
) | ||
|
||
type EventFetcher interface { | ||
FetchEventLogs(ctx context.Context, contractAddress common.Address, event string, startBlock *big.Int, endBlock *big.Int) ([]types.Log, error) | ||
} | ||
|
||
type DepositEventHandler struct { | ||
msgChan chan []*message.Message | ||
|
||
eventFetcher EventFetcher | ||
|
||
routerABI ethereumABI.ABI | ||
routerAddress common.Address | ||
} | ||
|
||
func NewDepositEventHandler(msgChan chan []*message.Message, eventFetcher EventFetcher, routerAddress common.Address) *DepositEventHandler { | ||
routerABI, _ := ethereumABI.JSON(strings.NewReader(abi.RouterABI)) | ||
return &DepositEventHandler{ | ||
eventFetcher: eventFetcher, | ||
routerAddress: routerAddress, | ||
routerABI: routerABI, | ||
msgChan: msgChan, | ||
} | ||
} | ||
|
||
// HandleEvents fetches deposit events and if deposits exists, submits a block root message | ||
// to the destination network of the deposit | ||
func (h *DepositEventHandler) HandleEvents(startBlock *big.Int, endBlock *big.Int) error { | ||
deposits, err := h.fetchDeposits(startBlock, endBlock) | ||
if err != nil { | ||
return fmt.Errorf("unable to fetch deposit events because of: %+v", err) | ||
} | ||
if len(deposits) == 0 { | ||
return nil | ||
} | ||
|
||
h.msgChan <- make([]*message.Message, 0) | ||
return nil | ||
} | ||
|
||
func (h *DepositEventHandler) fetchDeposits(startBlock *big.Int, endBlock *big.Int) ([]*events.Deposit, error) { | ||
logs, err := h.eventFetcher.FetchEventLogs(context.Background(), h.routerAddress, string(events.DepositSig), startBlock, endBlock) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
deposits := make([]*events.Deposit, 0) | ||
for _, dl := range logs { | ||
d, err := h.unpackDeposit(dl.Data) | ||
if err != nil { | ||
log.Error().Msgf("Failed unpacking deposit event log: %v", err) | ||
continue | ||
} | ||
d.SenderAddress = common.BytesToAddress(dl.Topics[1].Bytes()) | ||
|
||
log.Debug().Msgf("Found deposit log in block: %d, TxHash: %s, contractAddress: %s, sender: %s", dl.BlockNumber, dl.TxHash, dl.Address, d.SenderAddress) | ||
deposits = append(deposits, d) | ||
} | ||
|
||
return deposits, nil | ||
} | ||
|
||
func (h *DepositEventHandler) unpackDeposit(data []byte) (*events.Deposit, error) { | ||
var d events.Deposit | ||
err := h.routerABI.UnpackIntoInterface(&d, "Deposit", data) | ||
if err != nil { | ||
return &events.Deposit{}, err | ||
} | ||
|
||
return &d, 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.