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

feat: transfer liquidity handler #358

Open
wants to merge 8 commits into
base: adding-frost-peers
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func Run() error {
log.Info().Str("domain", config.String()).Msgf("Registering EVM domain")

bridgeAddress := common.HexToAddress(config.Bridge)
frostAddress := common.HexToAddress(config.FrostKeygen)
adminAddress := common.HexToAddress(config.Admin)
gasPricer := gas.NewLondonGasPriceClient(client, &gas.GasPricerOpts{
UpperLimitFeePerGas: config.MaxGasPrice,
GasPriceFactor: config.GasMultiplier,
Expand Down Expand Up @@ -224,8 +224,8 @@ func Run() error {
l := log.With().Str("chain", fmt.Sprintf("%v", config.GeneralChainConfig.Name)).Uint8("domainID", *config.GeneralChainConfig.Id)
eventHandlers = append(eventHandlers, hubEventHandlers.NewDepositEventHandler(depositListener, depositHandler, bridgeAddress, *config.GeneralChainConfig.Id, msgChan))
eventHandlers = append(eventHandlers, hubEventHandlers.NewKeygenEventHandler(l, tssListener, coordinator, host, communication, keyshareStore, bridgeAddress, networkTopology.Threshold))
eventHandlers = append(eventHandlers, hubEventHandlers.NewFrostKeygenEventHandler(l, tssListener, coordinator, host, communication, frostKeyshareStore, frostAddress, networkTopology.Threshold))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRefreshEventHandler(l, topologyProvider, topologyStore, tssListener, coordinator, host, communication, connectionGate, keyshareStore, frostKeyshareStore, bridgeAddress))
eventHandlers = append(eventHandlers, hubEventHandlers.NewFrostKeygenEventHandler(l, tssListener, coordinator, host, communication, frostKeyshareStore, adminAddress, networkTopology.Threshold))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRefreshEventHandler(l, topologyProvider, topologyStore, tssListener, coordinator, host, communication, connectionGate, keyshareStore, bridgeAddress))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRetryEventHandler(l, tssListener, depositHandler, propStore, bridgeAddress, *config.GeneralChainConfig.Id, config.BlockConfirmations, msgChan))
evmListener := listener.NewEVMListener(client, eventHandlers, blockstore, sygmaMetrics, *config.GeneralChainConfig.Id, config.BlockRetryInterval, config.BlockConfirmations, config.BlockInterval)
executor := executor.NewExecutor(host, communication, coordinator, bridgeContract, keyshareStore, exitLock, config.GasLimit.Uint64())
Expand Down
3 changes: 3 additions & 0 deletions chains/btc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type RawResource struct {
FeeAmount string
Tweak string
Script string
PublicKey []byte
}

type Resource struct {
Expand All @@ -30,6 +31,7 @@ type Resource struct {
ResourceID [32]byte
Tweak string
Script []byte
PublicKey string
}

type RawBtcConfig struct {
Expand Down Expand Up @@ -136,6 +138,7 @@ func NewBtcConfig(chainConfig map[string]interface{}) (*BtcConfig, error) {
Script: scriptBytes,
Tweak: r.Tweak,
FeeAmount: feeAmount,
PublicKey: hex.EncodeToString(r.PublicKey),
}
}

Expand Down
19 changes: 19 additions & 0 deletions chains/btc/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func (s *NewBtcConfigTestSuite) Test_ValidConfig() {
feeAddress, _ := btcutil.DecodeAddress("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt", &chaincfg.TestNet3Params)
expectedScript, _ := hex.DecodeString("51206a698882348433b57d549d6344f74500fcd13ad8d2200cdf89f8e39e5cafa7d5")

expectedPublicKey := "36c62696a3869cb46b2e6c93be73d039e6ab341853d824efadecd3dcee332c1a"
publicKeyBytes, _ := hex.DecodeString(expectedPublicKey)
rawConfig := map[string]interface{}{
"id": 1,
"endpoint": "ws://domain.com",
Expand All @@ -112,6 +114,14 @@ func (s *NewBtcConfigTestSuite) Test_ValidConfig() {
Script: "51206a698882348433b57d549d6344f74500fcd13ad8d2200cdf89f8e39e5cafa7d5",
Tweak: "tweak",
},
config.RawResource{
Address: "tb1qln69zuhdunc9stwfh6t7adexxrcr04ppy6thgm",
FeeAmount: "10000000",
ResourceID: "0x0000000000000000000000000000000000000000000000000000000000000300",
Script: "51206a698882348433b57d549d6344f74500fcd13ad8d2200cdf89f8e39e5cafa7d5",
Tweak: "tweak",
PublicKey: publicKeyBytes,
},
},
}

Expand Down Expand Up @@ -141,6 +151,15 @@ func (s *NewBtcConfigTestSuite) Test_ValidConfig() {
Script: expectedScript,
Tweak: "tweak",
FeeAmount: big.NewInt(10000000),
PublicKey: "",
},
{
Address: expectedAddress,
ResourceID: expectedResource,
Script: expectedScript,
Tweak: "tweak",
FeeAmount: big.NewInt(10000000),
PublicKey: expectedPublicKey,
},
},
})
Expand Down
1 change: 1 addition & 0 deletions chains/btc/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (e *Executor) executeResourceProps(props []*BtcTransferProposal, resource c
}
signing, err := signing.NewSigning(
i,
resource.PublicKey,
signingHash,
resource.Tweak,
messageID,
Expand Down
14 changes: 6 additions & 8 deletions chains/btc/listener/deposit-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ func TestRunErc20HandlerTestSuite(t *testing.T) {

func (s *Erc20HandlerTestSuite) Test_Erc20HandleEvent() {
deposit := &listener.Deposit{
SenderAddress: "senderAddress",
ResourceID: [32]byte{0},
Amount: big.NewInt(100),
Data: "0x1c3A03D04c026b1f4B4208D2ce053c5686E6FB8d_1",
ResourceID: [32]byte{0},
Amount: big.NewInt(100),
Data: "0x1c3A03D04c026b1f4B4208D2ce053c5686E6FB8d_1",
}

sourceID := uint8(1)
Expand Down Expand Up @@ -70,10 +69,9 @@ func (s *Erc20HandlerTestSuite) Test_Erc20HandleEvent() {
func (s *Erc20HandlerTestSuite) Test_Erc20HandleEvent_InvalidDestinationDomainID() {

deposit := &listener.Deposit{
SenderAddress: "senderAddress",
ResourceID: [32]byte{0},
Amount: big.NewInt(100),
Data: "0x1c3A03D04c026b1f4B4208D2ce053c5686E6FB8d_InvalidDestinationDomainID",
ResourceID: [32]byte{0},
Amount: big.NewInt(100),
Data: "0x1c3A03D04c026b1f4B4208D2ce053c5686E6FB8d_InvalidDestinationDomainID",
}

sourceID := uint8(1)
Expand Down
30 changes: 2 additions & 28 deletions chains/btc/listener/event-handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
package listener

import (
"crypto/sha256"
"encoding/binary"
"math/big"

"github.com/ChainSafe/sygma-relayer/chains"
"github.com/ChainSafe/sygma-relayer/chains/btc/config"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
Expand Down Expand Up @@ -82,11 +81,8 @@ func (eh *FungibleTransferEventHandler) HandleEvents(blockNumber *big.Int) error
if !isDeposit {
return nil
}
nonce, err := eh.CalculateNonce(blockNumber, evt.Hash)
if err != nil {
return err
}

nonce := chains.CalculateNonce(blockNumber, evt.Hash)
m, err := eh.depositHandler.HandleDeposit(eh.domainID, nonce, d.ResourceID, d.Amount, d.Data, blockNumber)
if err != nil {
return err
Expand Down Expand Up @@ -122,25 +118,3 @@ func (eh *FungibleTransferEventHandler) FetchEvents(startBlock *big.Int) ([]btcj
}
return block.Tx, nil
}

func (eh *FungibleTransferEventHandler) CalculateNonce(blockNumber *big.Int, transactionHash string) (uint64, error) {
// Convert blockNumber to string
blockNumberStr := blockNumber.String()

// Concatenate blockNumberStr and transactionHash with a separator
concatenatedStr := blockNumberStr + "-" + transactionHash

// Calculate SHA-256 hash of the concatenated string
hash := sha256.New()
hash.Write([]byte(concatenatedStr))
hashBytes := hash.Sum(nil)

// XOR fold the hash to get a 64-bit value
var result uint64
for i := 0; i < 4; i++ {
part := binary.BigEndian.Uint64(hashBytes[i*8 : (i+1)*8])
result ^= part
}

return result, nil
}
8 changes: 0 additions & 8 deletions chains/btc/listener/event-handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ func (s *DepositHandlerTestSuite) Test_FetchDepositFails_GetBlockVerboseTxError(
s.NotNil(err)
}

func (s *DepositHandlerTestSuite) Test_CalculateNonce() {
blockNumber := big.NewInt(850000)
nonce, err := s.fungibleTransferEventHandler.CalculateNonce(blockNumber, "a3f1e4d8b3c5e2a1f6d3c7e4b8a9f3e2c1d4a6b7c8e3f1d2c4b5a6e7")
fmt.Println(nonce)
s.Equal(nonce, uint64(12849897320021645821))
s.Nil(err)
}

func (s *DepositHandlerTestSuite) Test_HandleDepositFails_ExecutionContinue() {
blockNumber := big.NewInt(100)
data2 := map[string]any{
Expand Down
156 changes: 156 additions & 0 deletions chains/evm/calls/consts/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package consts

const AdminABI = `
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "previousOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnershipTransferred",
"type": "event"
},
{
"anonymous": false,
"inputs": [],
"name": "StartedFROSTKeygen",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "string",
"name": "publicKey",
"type": "string"
}
],
"name": "StartedFROSTRefresh",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint8",
"name": "domainID",
"type": "uint8"
},
{
"indexed": false,
"internalType": "bytes32",
"name": "resourceID",
"type": "bytes32"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes",
"name": "destinationAddress",
"type": "bytes"
}
],
"name": "TransferLiquidity",
"type": "event"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "renounceOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "startFROSTKeygen",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "publicKey",
"type": "string"
}
],
"name": "startFROSTRefresh",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint8",
"name": "domainID",
"type": "uint8"
},
{
"internalType": "bytes32",
"name": "resourceID",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "destinationAddress",
"type": "bytes"
}
],
"name": "transferLiquidity",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "transferOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
`
13 changes: 13 additions & 0 deletions chains/evm/calls/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package events

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
Expand All @@ -23,8 +25,19 @@ const (
FeeChangedSig EventSig = "FeeChanged(uint256)"
RetrySig EventSig = "Retry(string)"
FeeHandlerChanged EventSig = "FeeHandlerChanged(address)"
TransferLiquiditySig EventSig = "TransferLiquidity(uint8,bytes32,uint256,bytes)"
)

// TransferLiquidity holds params required to transfer
// liqudity from one address to another via the MPC key
type TransferLiquidity struct {
DomainID uint8
ResourceID [32]byte
Amount *big.Int
DestinationAddress []byte
TransactionHash string
}

// Refresh struct holds key refresh event data
type Refresh struct {
// SHA1 hash of topology file
Expand Down
Loading
Loading