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: using band oracle as data source #193

Merged
merged 2 commits into from
Sep 24, 2024
Merged
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
80 changes: 80 additions & 0 deletions docs/band_price_source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Integrate mesh security using band oracle price source.

## Prerequisites

- Setup mesh-security consumer and provider chains as [integration_consumer](./integration_consumer.md), [integration_provider](./integration_provider.md)
- Choose a price source for 2 chains in [band laozi testnet](https://laozi-testnet6.cosmoscan.io/data-sources), [band mainnet](https://www.cosmoscan.io/data-sources)

You can request 4 symbols on [oracle script id: 360](https://laozi-testnet6.cosmoscan.io/oracle-script/360)

- BTC, ETH, USDT, BAND

## Band Price Feeder

Band oracle price feeder is design base on cw-band. More details could be found in: <https://github.com/bandprotocol/cw-band/tree/main/docs>

The contract needs some information to instantiate, which is:

```
trading_pair: {
base_asset: String,
quote_asset: String,
} -> Consumer chain denom is base_asset, provider chain denom is quote_asset
client_id: String -> Arbitary string for your request
oracle_script_id: Uint64, -> The data source's id you choose
ask_count: Uint64, -> The number of validator you want to ask (Recommend: 4 on testnet)
min_count: Uint64, -> The minimum number of validator need to answer to aggregate result (Recommend: 3 on testnet)
fee_limit: Vec<Coin>, -> Data source fee that you willing to pay (Recommend: 250000uband, which is [{"denom": "uband", "amount":"250000"}])
prepare_gas: Uint64, -> Gas for running prepare phrase (Recommend: 100000)
execute_gas: Uint64, -> Gas for running execute phrase (Recommend: 500000)
minimum_sources: u8, -> The minimum available sources to determine price is aggregated from at least minimum sources (for data integrity) 1 should be ok for testing
price_info_ttl_in_secs: u64, -> The price only can live in an amount of time, after that, they should be update (Recommend: 60)
```

## Configure relayer

1. Create config for consumer chain and band chain as usual.
2. Create path, clients, connections
3. Deploy band price feeder on consumer chain
4. Setup channel using port wasm on consumer chain, port oracle on band chain and version bandchain-1
5. Using contract address as price source in converter contract
Deploy contract:

```bash
meshconsumerd tx wasm store ./tests/testdata/mesh_band_price_feed.wasm.gz --node $node --from $wallet --home=$home --chain-id $chainid --keyring-backend test --gas 300000 -y

band_code_id=$(meshconsumerd q wasm list-code --node $node --output json | jq -r '.code_infos[-1].code_id')
client_id=$(yq e '.paths.{$PATH_NAME}.src.client-id' $relayer_home/config/config.yaml)
oracle_script_id=445

init_band_price_feed=$(cat <<EOF
{
"trading_pair": {
"base_asset": "OSMO",
"quote_asset": "ATOM"
},
"client_id": "$client_id",
"oracle_script_id": "$oracle_script_id",
"ask_count": "1",
"min_count": "1",
"fee_limit": [{"denom": "uband", "amount":"10000"}],
"prepare_gas": "40000",
"execute_gas": "300000",
"minimum_sources": 2,
"price_info_ttl_in_secs": 60
}
EOF
)
meshconsumerd tx wasm instantiate $band_code_id "$init_band_price_feed" --node $node --label contract-pricefeed --admin $wallet --from $wallet --home=$home --chain-id $chainid --keyring-backend test -y --gas 300000
sleep 7

price_feed=$(meshconsumerd q wasm list-contract-by-code $band_code_id --node $node --output json | jq -r '.contracts[0]' )
echo "price feed contract: $price_feed"

```

Create channel

```bash
rly tx channel {$PATH_NAME} --src-port wasm.$price_feed --dst-port oracle --order unordered --version bandchain-1 --home $relayer_home --override
```
47 changes: 26 additions & 21 deletions scripts/mesh/test_mvp.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
# !/bin/bash
killall meshd || true
killall meshconsumerd || true
killall meshproviderd || true
killall rly || true
# deploy chain 1
./scripts/mesh/testibc/chain1.sh
# deploy chain 2
./scripts/mesh/testibc/chain2.sh
sleep 7

./scripts/mesh/testibc/rly_band.sh

./scripts/mesh/testibc/instantiate.sh
# run relayer
./scripts/mesh/testibc/rly.sh

home1node1=$HOME/.meshd/chain1/node1
home2node1=$HOME/.meshd/chain2/node1
node1=tcp://127.0.0.1:26657
node2=tcp://127.0.0.1:26667

virtual_staking=$(meshd q wasm list-contract-by-code 2 --output json | jq -r '.contracts[0]' )
converter=$(meshd q wasm list-contract-by-code 3 --output json | jq -r '.contracts[0]' )
vault=$(meshd q wasm list-contract-by-code 1 --output json --node $node2 | jq -r '.contracts[0]' )
native_staking=$(meshd q wasm list-contract-by-code 3 --output json --node $node2 | jq -r '.contracts[0]' )
ext_staking=$(meshd q wasm list-contract-by-code 4 --output json --node $node2 | jq -r '.contracts[0]' )
test1_provider_addr=$(meshd keys show test1 --keyring-backend test --home=$home2node1 --address)
meshd tx meshsecurity submit-proposal set-virtual-staking-max-cap $virtual_staking 100000000stake --title "a title" --summary "a summary" --from test1 --keyring-backend test --home=$home1node1 --chain-id chain-1 -y --deposit 10000000stake
virtual_staking=$(meshconsumerd q wasm list-contract-by-code 2 --output json --node $node1| jq -r '.contracts[0]' )
converter=$(meshconsumerd q wasm list-contract-by-code 3 --output json --node $node1 | jq -r '.contracts[0]' )
vault=$(meshproviderd q wasm list-contract-by-code 1 --output json --node $node2 | jq -r '.contracts[0]' )
native_staking=$(meshproviderd q wasm list-contract-by-code 3 --output json --node $node2 | jq -r '.contracts[0]' )
ext_staking=$(meshproviderd q wasm list-contract-by-code 4 --output json --node $node2 | jq -r '.contracts[0]' )
test1_provider_addr=$(meshproviderd keys show test1 --keyring-backend test --home=$home2node1 --address)
meshconsumerd tx meshsecurity submit-proposal set-virtual-staking-max-cap $virtual_staking 100000000stake --title "a title" --summary "a summary" --from test1 --keyring-backend test --home=$home1node1 --node $node1 --chain-id chain-1 -y --deposit 10000000stake

sleep 7

meshd tx gov vote 1 yes --from val1 --keyring-backend test --home=$home1node1 --chain-id chain-1 -y
meshconsumerd tx gov vote 1 yes --from val1 --keyring-backend test --node $node1 --home=$home1node1 --chain-id chain-1 -y

sleep 5

gov_addr=$(meshd q auth --node $node2 module-account gov -o json | jq ".account.base_account.address")
gov_addr=$(meshproviderd q auth --node $node2 module-account gov -o json | jq ".account.base_account.address")

echo "gov addr: $gov_addr"
proposal=$(cat <<EOF
Expand All @@ -52,16 +57,16 @@ EOF
)
echo $proposal
echo $proposal > ./scripts/mesh/update_params.json
meshd tx gov submit-proposal ./scripts/mesh/update_params.json --from test1 --keyring-backend test --home=$home2node1 --node $node2 --chain-id chain-2 -y
meshproviderd tx gov submit-proposal ./scripts/mesh/update_params.json --from test1 --keyring-backend test --home=$home2node1 --node $node2 --chain-id chain-2 -y

sleep 7

meshd tx gov vote 1 yes --from val1 --keyring-backend test --home=$home2node1 --chain-id chain-2 --node $node2 -y
meshproviderd tx gov vote 1 yes --from val1 --keyring-backend test --home=$home2node1 --chain-id chain-2 --node $node2 -y

sleep 30

# stake from provider(chain2) '{"bond":{}}'=======bond============
meshd tx wasm execute $vault '{"bond":{"amount":{"amount": "10000", "denom":"stake"}}}' --from test1 --home=$home2node1 --chain-id chain-2 --keyring-backend test --node $node2 --fees 1stake -y --gas 15406929
meshproviderd tx wasm execute $vault '{"bond":{"amount":{"amount": "10000", "denom":"stake"}}}' --from test1 --home=$home2node1 --chain-id chain-2 --keyring-backend test --node $node2 --fees 1stake -y --gas 15406929

sleep 7

Expand All @@ -74,10 +79,10 @@ account_query=$(cat <<EOF
EOF
)

meshd q wasm state smart $vault "$account_query" --node $node2
meshproviderd q wasm state smart $vault "$account_query" --node $node2
# ===========stake local=============================meshvaloper1w7f3xx7e75p4l7qdym5msqem9rd4dyc4u6ypev
# '{"stake_local":{"amount": {"denom":"stake", "amount":"190000000"}, "msg":"eyJ2YWxpZGF0b3IiOiAiY29zbW9zdmFsb3BlcjF5OHNzMzR6Y2RncTlnM3l6MDNyZjZrZ3J1ajZ6cGFneGh6ZXFmbSJ9"}}'
val1_provider_addr=$(meshd q staking validators --output json --node $node2 | jq -r '.validators[0].operator_address')
val1_provider_addr=$(meshproviderd q staking validators --output json --node $node2 | jq -r '.validators[0].operator_address')
stake_msg=$(cat <<EOF
{"validator": "$val1_provider_addr"}
EOF
Expand All @@ -91,18 +96,18 @@ stake_local_msg=$(cat <<EOF
"denom":"stake",
"amount":"9000"
},
"msg":"$encode_msg"
"msg":"$(echo -n $encode_msg| tr -d '[:space:]')"
}
}
EOF
)
meshd tx wasm execute $vault "$stake_local_msg" --from test1 --home=$home2node1 --chain-id chain-2 --keyring-backend test --node $node2 --fees 1stake -y --gas 15406929
meshproviderd tx wasm execute $vault "$stake_local_msg" --from test1 --home=$home2node1 --chain-id chain-2 --keyring-backend test --node $node2 --fees 1stake -y --gas 15406929

sleep 7

# =========extenal============================meshvaloper1f7twgcq4ypzg7y24wuywy06xmdet8pc4hsl6ty
# '{"stake_remote":{"contract":"cosmos1zwv6feuzhy6a9wekh96cd57lsarmqlwxdypdsplw6zhfncqw6ftqp82y57", "amount": {"denom":"stake", "amount":"100000000"}, "msg":"eyJ2YWxpZGF0b3IiOiAiY29zbW9zdmFsb3BlcjE0cjNhbmRuM3FyaDJ6NzN1eTNhNjZ2YzgyazU1ZHE1ZG04MmFxeSJ9"}}'
val1_consumer_addr=$(meshd q staking validators --output json | jq -r '.validators[0].operator_address')
val1_consumer_addr=$(meshconsumerd q staking validators --output json --node $node1| jq -r '.validators[0].operator_address')
stake_msg=$(cat <<EOF
{"validator": "$val1_consumer_addr"}
EOF
Expand All @@ -117,13 +122,13 @@ stake_remote_msg=$(cat <<EOF
"denom":"stake",
"amount":"1000"
},
"msg":"$encode_msg"
"msg":"$(echo -n $encode_msg| tr -d '[:space:]')"
}
}
EOF
)

meshd tx wasm execute $vault "$stake_remote_msg" --from test1 --home=$home2node1 --chain-id chain-2 --keyring-backend test --node $node2 --fees 1stake -y --gas 15406929
meshproviderd tx wasm execute $vault "$stake_remote_msg" --from test1 --home=$home2node1 --chain-id chain-2 --keyring-backend test --node $node2 --fees 1stake -y --gas 15406929

# Wait a while for relaying tx to consumer chain
sleep 20
Expand All @@ -138,4 +143,4 @@ stake_query=$(cat <<EOF
}
EOF
)
meshd q wasm state smart $ext_staking "$stake_query" --node $node2
meshproviderd q wasm state smart $ext_staking "$stake_query" --node $node2
38 changes: 19 additions & 19 deletions scripts/mesh/testibc/chain1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ home2=$HOME/.meshd/chain1/node2
chainid=chain-1

# init chain
meshd init mesh-1 --chain-id $chainid --home=$home1
meshd init mesh-2 --chain-id $chainid --home=$home2
meshconsumerd init mesh-1 --chain-id $chainid --home=$home1
meshconsumerd init mesh-2 --chain-id $chainid --home=$home2

# keys add
meshd keys add val1 --keyring-backend test --home=$home1
meshd keys add val2 --keyring-backend test --home=$home2
meshd keys add test1 --keyring-backend test --home=$home1
meshconsumerd keys add val1 --keyring-backend test --home=$home1
meshconsumerd keys add val2 --keyring-backend test --home=$home2
meshconsumerd keys add test1 --keyring-backend test --home=$home1

# Change parameter token denominations to stake
cat $home1/config/genesis.json | jq '.app_state["staking"]["params"]["bond_denom"]="stake"' > $home1/config/tmp_genesis.json && mv $home1/config/tmp_genesis.json $home1/config/genesis.json
Expand All @@ -26,25 +26,25 @@ cat $home1/config/genesis.json | jq '.app_state["meshsecurity"]["params"]["epoch
cat $home1/config/genesis.json | jq '.app_state["mint"]["params"]["mint_denom"]="stake"' > $home1/config/tmp_genesis.json && mv $home1/config/tmp_genesis.json $home1/config/genesis.json

# Allocate genesis accounts (cosmos formatted addresses)
val1=$(meshd keys show val1 --keyring-backend test --home=$home1 -a)
test1=$(meshd keys show test1 --keyring-backend test --home=$home1 -a)
val2=$(meshd keys show val2 --keyring-backend test --home=$home2 -a)
meshd add-genesis-account $val1 1000000000000stake --keyring-backend test --home=$home1
meshd add-genesis-account $val2 1000000000000stake --keyring-backend test --home=$home1
meshd add-genesis-account $test1 1000000000stake --keyring-backend test --home=$home1
val1=$(meshconsumerd keys show val1 --keyring-backend test --home=$home1 -a)
test1=$(meshconsumerd keys show test1 --keyring-backend test --home=$home1 -a)
val2=$(meshconsumerd keys show val2 --keyring-backend test --home=$home2 -a)
meshconsumerd add-genesis-account $val1 1000000000000stake --keyring-backend test --home=$home1
meshconsumerd add-genesis-account $val2 1000000000000stake --keyring-backend test --home=$home1
meshconsumerd add-genesis-account $test1 1000000000stake --keyring-backend test --home=$home1
cp $home1/config/genesis.json $home2/config/genesis.json

# Sign genesis transaction
meshd gentx val1 900000000000stake --keyring-backend test --chain-id $chainid --home=$home1
meshd gentx val2 100000000000stake --keyring-backend test --chain-id $chainid --home=$home2
meshconsumerd gentx val1 900000000000stake --keyring-backend test --chain-id $chainid --home=$home1
meshconsumerd gentx val2 100000000000stake --keyring-backend test --chain-id $chainid --home=$home2
cp $home2/config/gentx/*.json $home1/config/gentx/

# Collect genesis tx
meshd collect-gentxs --home=$home1
meshconsumerd collect-gentxs --home=$home1
cp $home1/config/genesis.json $home2/config/genesis.json

# Run this to ensure everything worked and that the genesis file is setup correctly
meshd validate-genesis --home=$home1
meshconsumerd validate-genesis --home=$home1

# change app.toml values
VALIDATOR1_APP_TOML=$home1/config/app.toml
Expand All @@ -67,11 +67,11 @@ sed -i -E 's|allow_duplicate_ip = false|allow_duplicate_ip = true|g' $VALIDATOR2
sed -i -E 's|prometheus = false|prometheus = true|g' $VALIDATOR2_CONFIG
sed -i -E 's|prometheus_listen_addr = ":26660"|prometheus_listen_addr = ":26630"|g' $VALIDATOR2_CONFIG

node1=$(meshd tendermint show-node-id --home=$home1)
node2=$(meshd tendermint show-node-id --home=$home2)
node1=$(meshconsumerd tendermint show-node-id --home=$home1)
node2=$(meshconsumerd tendermint show-node-id --home=$home2)
sed -i -E "s|persistent_peers = \"\"|persistent_peers = \"$node1@localhost:26656,$node2@localhost:26656\"|g" $home1/config/config.toml
sed -i -E "s|persistent_peers = \"\"|persistent_peers = \"$node1@localhost:26656,$node2@localhost:26656\"|g" $home2/config/config.toml

# Start the node (remove the --pruning=nothing flag if historical queries are not needed)
screen -S mesh1-node1 -t mesh1-node1 -d -m meshd start --home=$home1
screen -S mesh1-node2 -t mesh1-node2 -d -m meshd start --home=$home2
screen -S mesh1-node1 -t mesh1-node1 -d -m meshconsumerd start --home=$home1
screen -S mesh1-node2 -t mesh1-node2 -d -m meshconsumerd start --home=$home2
38 changes: 19 additions & 19 deletions scripts/mesh/testibc/chain2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ home2=$HOME/.meshd/chain2/node2
chainid=chain-2

# init chain
meshd init mesh-2 --chain-id $chainid --home=$home1
meshd init mesh-2 --chain-id $chainid --home=$home2
meshproviderd init mesh-2 --chain-id $chainid --home=$home1
meshproviderd init mesh-2 --chain-id $chainid --home=$home2

# keys add
meshd keys add val1 --keyring-backend test --home=$home1
meshd keys add val2 --keyring-backend test --home=$home2
meshd keys add test1 --keyring-backend test --home=$home1
meshproviderd keys add val1 --keyring-backend test --home=$home1
meshproviderd keys add val2 --keyring-backend test --home=$home2
meshproviderd keys add test1 --keyring-backend test --home=$home1

# Change parameter token denominations to stake
cat $home1/config/genesis.json | jq '.app_state["staking"]["params"]["bond_denom"]="stake"' > $home1/config/tmp_genesis.json && mv $home1/config/tmp_genesis.json $home1/config/genesis.json
Expand All @@ -25,24 +25,24 @@ cat $home1/config/genesis.json | jq '.app_state["slashing"]["params"]["signed_bl
cat $home1/config/genesis.json | jq '.app_state["mint"]["params"]["mint_denom"]="stake"' > $home1/config/tmp_genesis.json && mv $home1/config/tmp_genesis.json $home1/config/genesis.json

# Allocate genesis accounts (cosmos formatted addresses)
val1=$(meshd keys show val1 --keyring-backend test --home=$home1 -a)
test1=$(meshd keys show test1 --keyring-backend test --home=$home1 -a)
val2=$(meshd keys show val2 --keyring-backend test --home=$home2 -a)
meshd add-genesis-account $val1 1000000000000stake --keyring-backend test --home=$home1
meshd add-genesis-account $val2 1000000000000stake --keyring-backend test --home=$home1
meshd add-genesis-account $test1 1000000000stake --keyring-backend test --home=$home1
val1=$(meshproviderd keys show val1 --keyring-backend test --home=$home1 -a)
test1=$(meshproviderd keys show test1 --keyring-backend test --home=$home1 -a)
val2=$(meshproviderd keys show val2 --keyring-backend test --home=$home2 -a)
meshproviderd add-genesis-account $val1 1000000000000stake --keyring-backend test --home=$home1
meshproviderd add-genesis-account $val2 1000000000000stake --keyring-backend test --home=$home1
meshproviderd add-genesis-account $test1 1000000000stake --keyring-backend test --home=$home1
cp $home1/config/genesis.json $home2/config/genesis.json

# Sign genesis transactions
meshd gentx val1 900000000000stake --keyring-backend test --chain-id $chainid --home $home1
meshd gentx val2 100000000000stake --keyring-backend test --chain-id $chainid --home $home2
meshproviderd gentx val1 900000000000stake --keyring-backend test --chain-id $chainid --home $home1
meshproviderd gentx val2 100000000000stake --keyring-backend test --chain-id $chainid --home $home2
cp $home2/config/gentx/*.json $home1/config/gentx/

# Collect genesis tx
meshd collect-gentxs --home $home1
meshproviderd collect-gentxs --home $home1

# Run this to ensure everything worked and that the genesis file is setup correctly
meshd validate-genesis --home $home1
meshproviderd validate-genesis --home $home1
cp $home1/config/genesis.json $home2/config/genesis.json

# change app.toml values
Expand Down Expand Up @@ -71,11 +71,11 @@ sed -i -E 's|tcp://0.0.0.0:26656|tcp://0.0.0.0:26663|g' $VALIDATOR2_CONFIG
sed -i -E 's|allow_duplicate_ip = false|allow_duplicate_ip = true|g' $VALIDATOR2_CONFIG

# peers
NODE1=$(meshd tendermint show-node-id --home=$home1)
NODE2=$(meshd tendermint show-node-id --home=$home2)
NODE1=$(meshproviderd tendermint show-node-id --home=$home1)
NODE2=$(meshproviderd tendermint show-node-id --home=$home2)
sed -i -E "s|persistent_peers = \"\"|persistent_peers = \"$NODE1@localhost:26666,$NODE2@localhost:26666\"|g" $home1/config/config.toml
sed -i -E "s|persistent_peers = \"\"|persistent_peers = \"$NODE1@localhost:26666,$NODE2@localhost:26666\"|g" $home2/config/config.toml

# start
screen -S mesh2-node1 -t mesh2-node1 -d -m meshd start --home=$home1
screen -S mesh2-node2 -t mesh2-node2 -d -m meshd start --home=$home2
screen -S mesh2-node1 -t mesh2-node1 -d -m meshproviderd start --home=$home1
screen -S mesh2-node2 -t mesh2-node2 -d -m meshproviderd start --home=$home2
4 changes: 2 additions & 2 deletions scripts/mesh/testibc/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ chains:
consumer:
type: cosmos
value:
key-directory: /Users/phamminh/.relayer/keys/chain-1/
key-directory: [Your directory/keys/chain-1]
key: key1
chain-id: chain-1
rpc-addr: http://localhost:26657
Expand All @@ -32,7 +32,7 @@ chains:
provider:
type: cosmos
value:
key-directory: /Users/phamminh/.relayer/keys/chain-2/
key-directory: [Your directory/keys/chain-2]
key: key2
chain-id: chain-2
rpc-addr: http://localhost:26667
Expand Down
Loading
Loading