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: add thorchain support #1219

Merged
merged 40 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ffe6544
initial thorchain addition, configs setup, still need genesis update
misko9 Jul 19, 2024
9116cec
wip: thorchain genesis
misko9 Jul 23, 2024
ceba377
thorchain running
misko9 Jul 23, 2024
277335b
thorchain and bifrost working together
misko9 Jul 25, 2024
3e6981b
add savers, swaps, and saver eject example case for gaia
misko9 Jul 26, 2024
49eafc3
arb wip, all other sim tests passing
misko9 Jul 31, 2024
c0febe8
arb code in, needs testing with >1 pool
misko9 Jul 31, 2024
cbc6d2d
add ethereum to thorchain test and start cleaning up
misko9 Aug 2, 2024
50a6017
clean up savers/arb features
misko9 Aug 2, 2024
699ebf5
clean up swap feature
misko9 Aug 2, 2024
de5eb93
cleanup of saver eject and ragnarok features
misko9 Aug 2, 2024
2fcd796
fix: second+ saver eject now works
misko9 Aug 5, 2024
ac8aac9
Add back arbing and eth->gaia swap
misko9 Aug 5, 2024
cfc4091
wip, add utxo support, SendFundsWithNote() still needed
misko9 Aug 7, 2024
e1c5671
hardfork wip
agouin Aug 8, 2024
de8797a
start with genesis contents
agouin Aug 8, 2024
a166e88
progress further
agouin Aug 8, 2024
95520c5
wip: removed non-BTC chains temporarily, BTC's SendFundsWithNote succ…
misko9 Aug 8, 2024
69977a6
thorchain<->btc dual lp working
misko9 Aug 8, 2024
3fa9983
btc and bch looking good
misko9 Aug 9, 2024
ea0a2a0
ltc working, clean up logging, fix coins funded on each chain, and fi…
misko9 Aug 9, 2024
34f5287
utxo chains fully operational
misko9 Aug 10, 2024
4346aae
fix ether type
misko9 Aug 11, 2024
e39bd9a
add some protections around utxo node wallet usage
misko9 Aug 12, 2024
ad4a0a7
send utxo change back to sender instead of a change address
misko9 Aug 12, 2024
5daf638
Run tests in parallel
misko9 Aug 13, 2024
2dbbf78
clean up utxo test
misko9 Aug 13, 2024
f08f494
fmt/lint
misko9 Aug 13, 2024
7508c67
More cleanup
misko9 Aug 14, 2024
d1641df
increase time for bifrost to initialize
misko9 Aug 14, 2024
bcab349
Merge branch 'andrew/thorchain_hardfork' into steve/thorchain
misko9 Aug 14, 2024
a8b23da
Set bifrost envs at runtime
misko9 Aug 14, 2024
e8ae3a0
change wg to eg
misko9 Aug 15, 2024
7d72058
minor fmt
misko9 Aug 15, 2024
0545c5f
Merge branch 'main' into steve/thorchain
misko9 Aug 15, 2024
2a52c9c
add back mainnet-genesis.json
misko9 Aug 15, 2024
7e3f0d0
remove mainnet-genesis.json
misko9 Aug 21, 2024
70ae530
Merge branch 'main' into steve/thorchain
misko9 Aug 21, 2024
9ec32dd
lint
Reecepbcups Aug 21, 2024
ea0b733
fix: nil gRPC queries
Reecepbcups Aug 22, 2024
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
Prev Previous commit
Next Next commit
thorchain and bifrost working together
  • Loading branch information
misko9 committed Jul 25, 2024
commit 277335b7476de6276845a226315d272066e43fbf
203 changes: 203 additions & 0 deletions chain/thorchain/api_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package thorchain

// Api queries for thorchain
// Copied from thorchain's simulation test, may be replaced in the future for queries via chain binary

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"

cosmossdk_io_math "cosmossdk.io/math"

"github.com/strangelove-ventures/interchaintest/v8/chain/thorchain/common"
)

func (c *Thorchain) ApiGetBalances(addr string) (common.Coins, error) {
url := fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", c.GetAPIAddress(), addr)
var balances struct {
Balances []struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
} `json:"balances"`
}
err := get(url, &balances)
if err != nil {
return nil, err
}

// convert to common.Coins
coins := make(common.Coins, 0, len(balances.Balances))
for _, balance := range balances.Balances {
var amount uint64
amount, err = strconv.ParseUint(balance.Amount, 10, 64)
if err != nil {
return nil, err
}
var asset common.Asset
asset, err = common.NewAsset(strings.ToUpper(balance.Denom))
if err != nil {
return nil, err
}
coins = append(coins, common.NewCoin(asset, cosmossdk_io_math.NewUint(amount)))
}

return coins, nil
}

func (c *Thorchain) ApiGetInboundAddress(chain string) (address string, router *string, err error) {
url := fmt.Sprintf("%s/thorchain/inbound_addresses", c.GetAPIAddress())
var inboundAddresses []InboundAddress
err = get(url, &inboundAddresses)
if err != nil {
return "", nil, err
}

// find address for chain
for _, inboundAddress := range inboundAddresses {
if *inboundAddress.Chain == string(chain) {
if inboundAddress.Router != nil {
router = new(string)
*router = *inboundAddress.Router
}
return *inboundAddress.Address, router, nil
}
}

return "", nil, fmt.Errorf("no inbound address found for chain %s", chain)
}

func (c *Thorchain) ApiGetRouterAddress(chain string) (string, error) {
url := fmt.Sprintf("%s/thorchain/inbound_addresses", c.GetAPIAddress())
var inboundAddresses []InboundAddress
err := get(url, &inboundAddresses)
if err != nil {
return "", err
}

// find address for chain
for _, inboundAddress := range inboundAddresses {
if *inboundAddress.Chain == chain {
return *inboundAddress.Router, nil
}
}

return "", fmt.Errorf("no inbound address found for chain %s", chain)
}

func (c *Thorchain) ApiGetLiquidityProviders(asset common.Asset) ([]LiquidityProvider, error) {
url := fmt.Sprintf("%s/thorchain/pool/%s/liquidity_providers", c.GetAPIAddress(), asset.String())
var liquidityProviders []LiquidityProvider
err := get(url, &liquidityProviders)
return liquidityProviders, err
}

func (c *Thorchain) ApiGetSavers(asset common.Asset) ([]Saver, error) {
url := fmt.Sprintf("%s/thorchain/pool/%s/savers", c.GetAPIAddress(), asset.GetLayer1Asset().String())
var savers []Saver
err := get(url, &savers)
return savers, err
}

func (c *Thorchain) ApiGetPools() ([]Pool, error) {
url := fmt.Sprintf("%s/thorchain/pools", c.GetAPIAddress())
var pools []Pool
err := get(url, &pools)
return pools, err
}

func (c *Thorchain) ApiGetPool(asset common.Asset) (Pool, error) {
url := fmt.Sprintf("%s/thorchain/pool/%s", c.GetAPIAddress(), asset.String())
var pool Pool
err := get(url, &pool)
return pool, err
}

func (c *Thorchain) ApiGetSwapQuote(from, to common.Asset, amount cosmossdk_io_math.Uint) (QuoteSwapResponse, error) {
baseURL := fmt.Sprintf("%s/thorchain/quote/swap", c.GetAPIAddress())
parsedURL, err := url.Parse(baseURL)
if err != nil {
return QuoteSwapResponse{}, err
}
params := url.Values{}
params.Add("from_asset", from.String())
params.Add("to_asset", to.String())
params.Add("amount", amount.String())
parsedURL.RawQuery = params.Encode()
url := parsedURL.String()

var quote QuoteSwapResponse
err = get(url, &quote)
return quote, err
}

func (c *Thorchain) ApiGetSaverDepositQuote(asset common.Asset, amount cosmossdk_io_math.Uint) (QuoteSaverDepositResponse, error) {
baseURL := fmt.Sprintf("%s/thorchain/quote/saver/deposit", c.GetAPIAddress())
parsedURL, err := url.Parse(baseURL)
if err != nil {
return QuoteSaverDepositResponse{}, err
}
params := url.Values{}
params.Add("asset", asset.String())
params.Add("amount", amount.String())
parsedURL.RawQuery = params.Encode()
url := parsedURL.String()

var quote QuoteSaverDepositResponse
err = get(url, &quote)
return quote, err
}

func (c *Thorchain) GetTxStages(txid string) (TxStagesResponse, error) {
url := fmt.Sprintf("%s/thorchain/tx/stages/%s", c.GetAPIAddress(), txid)
var stages TxStagesResponse
err := get(url, &stages)
return stages, err
}

func (c *Thorchain) GetTxDetails(txid string) (TxDetailsResponse, error) {
url := fmt.Sprintf("%s/thorchain/tx/details/%s", c.GetAPIAddress(), txid)
var details TxDetailsResponse
err := get(url, &details)
return details, err
}

func (c *Thorchain) GetMimirs() (map[string]int64, error) {
url := fmt.Sprintf("%s/thorchain/mimir", c.GetAPIAddress())
var mimirs map[string]int64
err := get(url, &mimirs)
return mimirs, err
}

////////////////////////////////////////////////////////////////////////////////////////
// Internal
////////////////////////////////////////////////////////////////////////////////////////

func get(url string, target interface{}) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

// extract error if the request failed
type ErrorResponse struct {
Error string `json:"error"`
}
buf, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
errResp := ErrorResponse{}
err = json.Unmarshal(buf, &errResp)
if err == nil && errResp.Error != "" {
return fmt.Errorf(errResp.Error)
}

// decode response
return json.Unmarshal(buf, target)
}
37 changes: 37 additions & 0 deletions chain/thorchain/common/chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package common

import (
"errors"
"strings"
)

type Chain string

// Chains represent a slice of Chain
type Chains []Chain

// Valid validates chain format, should consist only of uppercase letters
func (c Chain) Valid() error {
if len(c) < 3 {
return errors.New("chain id len is less than 3")
}
if len(c) > 10 {
return errors.New("chain id len is more than 10")
}
for _, ch := range string(c) {
if ch < 'A' || ch > 'Z' {
return errors.New("chain id can consist only of uppercase letters")
}
}
return nil
}

// NewChain create a new Chain and default the siging_algo to Secp256k1
func NewChain(chainID string) (Chain, error) {
chain := Chain(strings.ToUpper(chainID))
if err := chain.Valid(); err != nil {
return chain, err
}
return chain, nil
}

101 changes: 101 additions & 0 deletions chain/thorchain/common/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading