Skip to content

Commit

Permalink
Merge pull request #4 from hyle-team/feature/native-bridge
Browse files Browse the repository at this point in the history
Feature/native bridge
  • Loading branch information
slbmax authored Sep 17, 2024
2 parents e053c42 + bc5d1fb commit ddf37b6
Show file tree
Hide file tree
Showing 36 changed files with 557 additions and 336 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ build-run: build run
clear-db:
KV_VIPER_FILE=$(VIPER_FILE) signer migrate down

migrate-db:
KV_VIPER_FILE=$(VIPER_FILE) signer migrate up

test:
KV_VIPER_FILE=$(VIPER_FILE) go test -count=1 $(TESTING_PACKAGES)
171 changes: 80 additions & 91 deletions contracts/bridge.go

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion docs/static/service.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"depositor": {
"type": "string"
},
"amount": {
"depositAmount": {
"type": "string"
},
"depositToken": {
Expand All @@ -140,6 +140,12 @@
},
"withdrawalToken": {
"type": "string"
},
"isWrapped": {
"type": "boolean"
},
"withdrawalAmount": {
"type": "string"
}
}
},
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ require (
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
github.com/cosmos/gogoproto v1.5.0
github.com/ethereum/go-ethereum v1.10.26
github.com/hyle-team/bridgeless-core v0.0.0-20240814124421-34bfbc7e857c
github.com/hyle-team/bridgeless-core v0.0.0-20240911163215-e9a1ef12c6da
github.com/lib/pq v1.10.9
github.com/rabbitmq/amqp091-go v1.10.0
github.com/spf13/cast v1.6.0
gitlab.com/distributed_lab/figure/v3 v3.1.4
Expand Down Expand Up @@ -100,7 +101,6 @@ require (
github.com/klauspost/compress v1.17.0 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
Expand Down Expand Up @@ -168,7 +168,8 @@ replace (
github.com/cosmos/iavl => github.com/cosmos/iavl v0.19.6

github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
//github.com/hyle-team/bridgeless-core => /Users/slbmax/go/src/github.com/hyle-team/bridgeless-core

github.com/tendermint/tendermint => github.com/cometbft/cometbft v0.34.29
github.com/tidwall/btree => github.com/tidwall/btree v1.5.0
nhooyr.io/websocket => github.com/coder/websocket v1.8.7
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWH
github.com/cockroachdb/apd/v2 v2.0.2 h1:weh8u7Cneje73dDh+2tEVLUvyBc89iwepWCD8b8034E=
github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw=
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM=
github.com/coder/websocket v1.8.7 h1:jiep6gmlfP/yq2w1gBoubJEXL9gf8x3bp6lzzX8nJxE=
github.com/coder/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
github.com/coinbase/rosetta-sdk-go v0.7.9 h1:lqllBjMnazTjIqYrOGv8h8jxjg9+hJazIGZr9ZvoCcA=
github.com/coinbase/rosetta-sdk-go v0.7.9/go.mod h1:0/knutI7XGVqXmmH4OQD8OckFrbQ8yMsUZTG7FXCR2M=
github.com/cometbft/cometbft v0.34.29 h1:Q4FqMevP9du2pOgryZJHpDV2eA6jg/kMYxBj9ZTY6VQ=
Expand Down Expand Up @@ -1782,8 +1784,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/huin/goupnp v1.0.3 h1:N8No57ls+MnjlB+JPiCVSOyy/ot7MJTqlo7rn+NYSqQ=
github.com/huin/goupnp v1.0.3/go.mod h1:ZxNlw5WqJj6wSsRK5+YfflQGXYfccj5VgQsMNixHM7Y=
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
github.com/hyle-team/bridgeless-core v0.0.0-20240814124421-34bfbc7e857c h1:fJsuw9PxH3FByVaS6BK78VeqCXW5ioZkToiOL9OJNjo=
github.com/hyle-team/bridgeless-core v0.0.0-20240814124421-34bfbc7e857c/go.mod h1:uOnrp+nR2tpLf8ehAo5oIa2/tK/MDn6OhfBOcjJ0weY=
github.com/hyle-team/bridgeless-core v0.0.0-20240911163215-e9a1ef12c6da h1:r7V5qDnd+6AIaMSx463+mq8vhMrY8p2NxQWzPgHBGew=
github.com/hyle-team/bridgeless-core v0.0.0-20240911163215-e9a1ef12c6da/go.mod h1:E416XZ8/nZ4Te3gzqYFcasErEvv8JHXpR9nMBMdxLI8=
github.com/hyle-team/cosmos-sdk v0.46.20 h1:2pd+6xOEf3c9CgIG2be/Rl3X3wAuTwL+hJT5v+33Db4=
github.com/hyle-team/cosmos-sdk v0.46.20/go.mod h1:nIfy+qNyHSoV2b901hNglQNVvHGK/9vrnMlgJU6SsIM=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
Expand Down Expand Up @@ -3347,8 +3349,6 @@ modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
modernc.org/z v1.5.1/go.mod h1:eWFB510QWW5Th9YGZT81s+LwvaAs3Q2yr4sP0rmLkv8=
moul.io/http2curl/v2 v2.3.0/go.mod h1:RW4hyBjTWSYDOxapodpNEtX0g5Eb16sxklBqmd2RHcE=
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
9 changes: 9 additions & 0 deletions internal/assets/migrations/002_is_wrapped.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +migrate Up

ALTER TABLE deposits
ADD COLUMN is_wrapped_token boolean DEFAULT false;

-- +migrate Down

ALTER TABLE deposits
DROP COLUMN is_wrapped_token;
15 changes: 15 additions & 0 deletions internal/assets/migrations/003_amount.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- +migrate Up

ALTER TABLE deposits
RENAME COLUMN amount TO deposit_amount;

ALTER TABLE deposits
ADD COLUMN withdrawal_amount TEXT;

-- +migrate Down

ALTER TABLE deposits
RENAME COLUMN deposit_amount to amount;

ALTER TABLE deposits
DROP COLUMN withdrawal_amount;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

import (
"crypto/tls"
"github.com/hyle-team/bridgeless-signer/internal/connectors/core"
"github.com/hyle-team/bridgeless-signer/internal/bridge/core"
pkgTypes "github.com/hyle-team/bridgeless-signer/pkg/types"
"github.com/pkg/errors"
"gitlab.com/distributed_lab/figure/v3"
Expand Down
53 changes: 8 additions & 45 deletions internal/connectors/core/main.go → internal/bridge/core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
txclient "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/ethereum/go-ethereum/common"
"github.com/hyle-team/bridgeless-signer/pkg/tokens"
pkgTypes "github.com/hyle-team/bridgeless-signer/pkg/types"
"github.com/pkg/errors"
"google.golang.org/grpc"
"strings"

txclient "github.com/cosmos/cosmos-sdk/types/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
coretypes "github.com/hyle-team/bridgeless-core/types"
bridgetypes "github.com/hyle-team/bridgeless-core/x/bridge/types"
"github.com/hyle-team/bridgeless-signer/internal/bridge/types"
pkgTypes "github.com/hyle-team/bridgeless-signer/pkg/types"
"github.com/pkg/errors"
"google.golang.org/grpc"
)

var _ tokens.TokenPairer = &Connector{}
var _ types.Bridger = &Connector{}

type ConnectorSettings struct {
Account pkgTypes.Account `fig:"account_private_key,required"`
Expand All @@ -36,7 +33,7 @@ type Connector struct {
transactor txclient.ServiceClient
txConfiger sdkclient.TxConfig
auther authtypes.QueryClient
bridger bridgetypes.QueryClient
querier bridgetypes.QueryClient

settings ConnectorSettings
}
Expand All @@ -46,45 +43,11 @@ func NewConnector(conn *grpc.ClientConn, settings ConnectorSettings) *Connector
transactor: txclient.NewServiceClient(conn),
txConfiger: authtx.NewTxConfig(codec.NewProtoCodec(codectypes.NewInterfaceRegistry()), []signing.SignMode{signing.SignMode_SIGN_MODE_DIRECT}),
auther: authtypes.NewQueryClient(conn),
bridger: bridgetypes.NewQueryClient(conn),
querier: bridgetypes.NewQueryClient(conn),
settings: settings,
}
}

func (c *Connector) GetDestinationTokenAddress(
srcChainId string,
srcTokenAddr common.Address,
dstChainId string,
) (common.Address, error) {
req := bridgetypes.QueryGetTokenPair{
SrcChain: srcChainId,
SrcAddress: strings.ToLower(srcTokenAddr.String()),
DstChain: dstChainId,
}

resp, err := c.bridger.GetTokenPair(context.Background(), &req)
if err != nil {
if errors.Is(bridgetypes.ErrTokenPairNotFound.GRPCStatus().Err(), err) {
return common.Address{}, tokens.ErrPairNotFound
}

return common.Address{}, errors.Wrap(err, "failed to get token pair")
}

return common.HexToAddress(resp.Info.Address), nil

}

func (c *Connector) SubmitDeposits(depositTxs ...bridgetypes.Transaction) error {
if len(depositTxs) == 0 {
return nil
}

msg := bridgetypes.NewMsgSubmitTransactions(c.settings.Account.CosmosAddress(), depositTxs...)

return c.submitMsgs(msg)
}

func (c *Connector) submitMsgs(msgs ...sdk.Msg) error {
if len(msgs) == 0 {
return nil
Expand Down
13 changes: 13 additions & 0 deletions internal/bridge/core/submit_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core

import bridgetypes "github.com/hyle-team/bridgeless-core/x/bridge/types"

func (c *Connector) SubmitDeposits(depositTxs ...bridgetypes.Transaction) error {
if len(depositTxs) == 0 {
return nil
}

msg := bridgetypes.NewMsgSubmitTransactions(c.settings.Account.CosmosAddress(), depositTxs...)

return c.submitMsgs(msg)
}
25 changes: 25 additions & 0 deletions internal/bridge/core/token_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core

import (
"context"
bridgetypes "github.com/hyle-team/bridgeless-core/x/bridge/types"
"github.com/pkg/errors"
)

func (c *Connector) GetTokenInfo(chainId string, addr string) (bridgetypes.TokenInfo, error) {
req := bridgetypes.QueryGetTokenInfo{
Chain: chainId,
Address: addr,
}

resp, err := c.querier.GetTokenInfo(context.Background(), &req)
if err != nil {
if errors.Is(err, bridgetypes.ErrTokenInfoNotFound.GRPCStatus().Err()) {
return bridgetypes.TokenInfo{}, bridgetypes.ErrTokenInfoNotFound
}

return bridgetypes.TokenInfo{}, errors.Wrap(err, "failed to get token info")
}

return resp.Info, nil
}
34 changes: 34 additions & 0 deletions internal/bridge/core/token_pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package core

import (
"context"
"github.com/ethereum/go-ethereum/common"
bridgetypes "github.com/hyle-team/bridgeless-core/x/bridge/types"
"github.com/hyle-team/bridgeless-signer/internal/bridge/types"
"github.com/pkg/errors"
"strings"
)

func (c *Connector) GetDestinationTokenInfo(
srcChainId string,
srcTokenAddr common.Address,
dstChainId string,
) (bridgetypes.TokenInfo, error) {
req := bridgetypes.QueryGetTokenPair{
SrcChain: srcChainId,
SrcAddress: strings.ToLower(srcTokenAddr.String()),
DstChain: dstChainId,
}

resp, err := c.querier.GetTokenPair(context.Background(), &req)
if err != nil {
if errors.Is(err, bridgetypes.ErrTokenPairNotFound.GRPCStatus().Err()) {
return bridgetypes.TokenInfo{}, types.ErrPairNotFound
}

return bridgetypes.TokenInfo{}, errors.Wrap(err, "failed to get token pair")
}

return resp.Info, nil

}
File renamed without changes.
50 changes: 26 additions & 24 deletions internal/bridge/processor/get_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package processor

import (
"fmt"
"github.com/hyle-team/bridgeless-signer/internal/bridge/proxy/btc"
"github.com/ethereum/go-ethereum/common"
bridgeTypes "github.com/hyle-team/bridgeless-signer/internal/bridge/types"
"github.com/hyle-team/bridgeless-signer/pkg/tokens"
"github.com/pkg/errors"
)

Expand All @@ -28,7 +27,6 @@ func (p *Processor) ProcessGetDepositRequest(req bridgeTypes.GetDepositRequest)
errors.Is(err, bridgeTypes.ErrInvalidScriptPubKey) {
reprocessable = false
}

return nil, reprocessable, errors.Wrap(err, "failed to get deposit data")
}

Expand All @@ -43,31 +41,35 @@ func (p *Processor) ProcessGetDepositRequest(req bridgeTypes.GetDepositRequest)
return data, false, errors.Wrap(bridgeTypes.ErrInvalidReceiverAddress, depositData.DestinationAddress)
}

switch dstProxy.Type() {
case bridgeTypes.ChainTypeBitcoin:
if depositData.Amount.Int64() < btc.MinSatoshisPerOutput {
return data, false, bridgeTypes.ErrInvalidDepositedAmount
srcTokenInfo, err := p.core.GetTokenInfo(depositData.ChainId, depositData.TokenAddress.String())
if err != nil {
reprocessable = true
if errors.Is(err, bridgeTypes.ErrTokenInfoNotFound) {
reprocessable = false
}
case bridgeTypes.ChainTypeEVM:
depositData.DestinationTokenAddress, err = p.tokenPairer.GetDestinationTokenAddress(
depositData.ChainId,
depositData.TokenAddress,
depositData.DestinationChainId,
)
if err != nil {
reprocessable = true
if errors.Is(err, tokens.ErrSourceTokenNotSupported) ||
errors.Is(err, tokens.ErrDestinationTokenNotSupported) ||
errors.Is(err, tokens.ErrPairNotFound) {
reprocessable = false
}

return nil, reprocessable, errors.Wrap(err, "failed to get destination token address")
return nil, reprocessable, errors.Wrap(err, "failed to get source token info")
}
dstTokenInfo, err := p.core.GetDestinationTokenInfo(
depositData.ChainId,
depositData.TokenAddress,
depositData.DestinationChainId,
)
if err != nil {
reprocessable = true
if errors.Is(err, bridgeTypes.ErrPairNotFound) {
reprocessable = false
}
default:
return data, false, errors.Wrap(err, fmt.Sprintf("invalid chain type: %v", dstProxy.Type()))
return nil, reprocessable, errors.Wrap(err, "failed to get destination token info")
}

depositData.WithdrawalAmount = transformAmount(depositData.DepositAmount, srcTokenInfo.Decimals, dstTokenInfo.Decimals)
if !dstProxy.WithdrawalAmountValid(depositData.WithdrawalAmount) {
return nil, false, bridgeTypes.ErrInvalidDepositedAmount
}

depositData.DestinationTokenAddress = common.HexToAddress(dstTokenInfo.Address)
depositData.IsWrappedToken = dstTokenInfo.IsWrapped

if err = p.db.New().SetDepositData(*depositData); err != nil {
return nil, true, errors.Wrap(err, "failed to save deposit data")
}
Expand Down
Loading

0 comments on commit ddf37b6

Please sign in to comment.