Skip to content

Commit

Permalink
Implement basic deposit handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Oct 20, 2023
1 parent 73ef8a5 commit 7279b6f
Show file tree
Hide file tree
Showing 5 changed files with 460 additions and 1 deletion.
210 changes: 210 additions & 0 deletions chains/evm/abi/router.go
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"
}
]`
33 changes: 33 additions & 0 deletions chains/evm/listener/events/events.go
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
}
86 changes: 86 additions & 0 deletions chains/evm/listener/events/handlers/deposit.go
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
}
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,32 @@ module github.com/sygmaprotocol/spectre-node
go 1.19

require (
github.com/ethereum/go-ethereum v1.13.4
github.com/kelseyhightower/envconfig v1.4.0
github.com/rs/zerolog v1.31.0
github.com/stretchr/testify v1.8.4
github.com/sygmaprotocol/sygma-core v0.0.0-20231019093207-256532b316d8
)

require (
github.com/bits-and-blooms/bitset v1.7.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/c-kzg-4844 v0.3.1 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading

0 comments on commit 7279b6f

Please sign in to comment.