CronCats / croncat-rs

A Rust version of the CronCat agent.
GNU Affero General Public License v3.0
19 stars 4 forks source link

Update `check_queries` for the latest queries changes #110

Closed nlipartiia-hacken closed 1 year ago

nlipartiia-hacken commented 1 year ago

Works with contracts from the main branch.

nlipartiia-hacken commented 1 year ago

Simple task with one query and without transforms

#!/bin/bash
set -e

CHAIN_ID="elgafar-1"
BINARY="starsd"

RPC="https://rpc.elgafar-1.stargaze-apis.com:443"
NODE="--node $RPC"
TXFLAG="--node $RPC --chain-id $CHAIN_ID --gas-prices 0.04ustars --gas auto --gas-adjustment 1.3 --broadcast-mode block"

if [ -z "$1" ]
then
    echo "Must provide contract address"
    exit 1
elif [ -z "$2" ]
then
    echo "Must provide user address"
    exit 1
else
    CONTRACT="$1"
    USER="$2"
fi

TASK='{
  "create_task": {
    "task": {
      "interval": "Immediate",
      "boundary": null,
      "cw20_coins": [],
      "stop_on_fail": false,
      "actions": [
        {
          "msg": {
            "bank": {
              "send": {
                "amount": [
                  {
                    "amount": "1",
                    "denom": "ustars"
                  }
                ],
                "to_address": "stars1nhdn38xsa8way7vckwc7hp7rxmzqqslweezz7g"
              }
            }
          }
        },
        {
          "msg": {
            "bank": {
              "send": {
                "amount": [
                  {
                    "amount": "1",
                    "denom": "ustars"
                  }
                ],
                "to_address": "stars1uwcy0vetsuh2huzl6t8akfqdl2zuxughqpgasp"
              }
            }
          }
        }
      ],
      "queries": [
        {
          "has_balance_gte": {
            "address": "stars16lc43z4fjf3a5qmp6ctdhc79axe4j9m57rztm9",
            "required_balance": {
              "native": [
                {
                  "denom": "ustars",
                  "amount": "10"
                }
              ]
            }
          }
        }
      ],
      "transforms": []
    }
  }
}'
$BINARY tx wasm execute $CONTRACT "$TASK" --amount 300000ustars --from $USER $TXFLAG -y
nlipartiia-hacken commented 1 year ago

A task with queries and transforms

#!/bin/bash
set -e

CHAIN_ID="elgafar-1"
BINARY="starsd"

RPC="https://rpc.elgafar-1.stargaze-apis.com:443"
NODE="--node $RPC"
TXFLAG="--node $RPC --chain-id $CHAIN_ID --gas-prices 0.04ustars --gas auto --gas-adjustment 1.3 --broadcast-mode block"

if [ -z "$1" ]
then
    echo "Must provide CronCat address"
    exit 1
elif [ -z "$2" ]
then
    echo "Must provide admin address"
    exit 1
else
    CONTRACT="$1"
    ADMIN="$2"
fi

# Download and deploy external contracts
curl -o artifacts/cw4_stake.wasm -LO "https://github.com/CosmWasm/cw-plus/releases/latest/download/cw4_stake.wasm"
CW20_ID=$($BINARY tx wasm store "./artifacts/cw20_base.wasm" -y --from $ADMIN $TXFLAG --output json | jq -r '.logs[0].events[-1].attributes[-1].value')
CW4_ID=$($BINARY tx wasm store "./artifacts/cw4_stake.wasm" -y --from $ADMIN $TXFLAG --output json | jq -r '.logs[0].events[-1].attributes[-1].value')

# Init CW20
$BINARY tx wasm instantiate $CW20_ID '{"name":"CronCatCoin","symbol":"cron","decimals":6,"initial_balances":[{"address":"'$ADMIN'","amount":"5000"}]}' --from $ADMIN --label "cron_coin" $TXFLAG -y --no-admin > /dev/null
CW20_ADDR=$($BINARY query wasm list-contract-by-code $CW20_ID $NODE --output json | jq -r '.contracts[-1]')

# Init CW4
$BINARY tx wasm instantiate $CW4_ID '{"admin":"'$ADMIN'","denom":{"cw20":"'$CW20_ADDR'"},"tokens_per_weight":"1","min_bond":"1","unbonding_period":{"height":1}}' --from $ADMIN --label "cw4_group" $TXFLAG -y --no-admin > /dev/null
CW4_ADDR=$($BINARY query wasm list-contract-by-code $CW4_ID $NODE --output json | jq -r '.contracts[-1]')

# Stake 500 tokens for admin inside cw4 contract
BOND_MSG=$(printf '{"bond":{}}' | base64)
ADMIN_CW4_STAKE='{"send": {"contract": "'$CW4_ADDR'", "amount": "500", "msg": "'$BOND_MSG'"}}'
$BINARY tx wasm execute $CW20_ADDR "$ADMIN_CW4_STAKE" --from $ADMIN $TXFLAG -y

# Placeholder BASE64 messages
TRANSFER_CW20_B64=$(printf '{"transfer":{"recipient":"","amount":"1"}}' | base64)
QUERY_CW4_ADMIN_B64=$(printf '{"admin":{}}' | base64)
QUERY_CW4_STAKED_B64=$(printf '{"staked":{"address":""}}' | base64)

# Refill cw20 before task creation
CW20_SEND='{"send":{"contract":"'$CONTRACT'","amount":"1000","msg":""}}'
$BINARY tx wasm execute $CW20_ADDR "$CW20_SEND" --from $ADMIN $TXFLAG -y

INSERTABLE_ROLL='{
  "create_task": {
    "task": {
      "interval": "Once",
      "boundary": null,
      "stop_on_fail": false,
      "actions": [
        {
          "msg": {
            "wasm": {
              "execute": {
                "contract_addr": "'$CW20_ADDR'",
                "msg": "'$TRANSFER_CW20_B64'",
                "funds": []
              }
            }
          },
          "gas_limit": 200000
        }
      ],
      "queries": [
        {
          "smart_query": {
            "contract_addr": "'$CW4_ADDR'",
            "msg": "'$QUERY_CW4_ADMIN_B64'",
            "path_to_query_value": [
              {
                "key": "admin"
              }
            ],
            "queries": [
              {
                "contract_addr": "'$CW4_ADDR'",
                "msg": "'$QUERY_CW4_STAKED_B64'",
                "path_to_msg_value": [
                  {
                    "key": "staked"
                  },
                  {
                    "key": "address"
                  }
                ],
                "path_to_query_value": [
                  {
                    "key": "stake"
                  }
                ]
              }
            ],
            "ordering": "unit_above",
            "value": "'$(printf '"400"' | base64)'"
          }
        },
        {
          "query": {
            "contract_addr": "'$CW4_ADDR'",
            "msg": "'$QUERY_CW4_ADMIN_B64'"
          }
        }
      ],
      "transforms": [
        {
          "action_idx": 0,
          "query_idx": 0,
          "action_path": [
            {
              "key": "transfer"
            },
            {
              "key": "amount"
            }
          ],
          "query_response_path": []
        },
        {
          "action_idx": 0,
          "query_idx": 1,
          "action_path": [
            {
              "key": "transfer"
            },
            {
              "key": "recipient"
            }
          ],
          "query_response_path": [
            {
              "key": "admin"
            }
          ]
        }
      ],
      "cw20_coins": [
        {
          "address": "'$CW20_ADDR'",
          "amount": "1000"
        }
      ]
    }
  }
}'

# Create a task
$BINARY tx wasm execute $CONTRACT "$INSERTABLE_ROLL" --amount 1000000ustars --from $ADMIN $TXFLAG -y > /dev/null
TrevorJTClarke commented 1 year ago

Thanks for the scripts @NinoLipartiia - this actually doesnt match how the new beta-split stuff works, so i will close.