Skip to content

Commit

Permalink
add basic e2e tests for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
tuantran1702 committed Mar 27, 2024
1 parent 5d22450 commit 63a4a73
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ dependency-graph.png
*.out
*.synctex.gz
contract_tests/*
.eved
scripts/wasm/*.wasm
27 changes: 27 additions & 0 deletions scripts/wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Cosmwasm contracts e2e tests

## Guideline

Run local node

```bash
bash scripts/run-node.sh
```

Download the cw20_base wasm file

```bash
bash scripts/wasm/get_contract.sh
```

Then test upload code

```bash
bash scripts/wasm/upload_code.sh
```

After upload, we can test instantiate the cw20_code

```bash
bash scripts/wasm/instantiate_cw20.sh
```
13 changes: 13 additions & 0 deletions scripts/wasm/get_contract.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

VERSION="v0.16.0"
CONTRACTS="cw20_base"

for CONTRACT in $CONTRACTS; do
# check if already exists wasm
if [ -f ./scripts/wasm/$CONTRACT.wasm ]; then
echo "Wasm file already exists for $CONTRACT"
continue
fi
curl -s -L -o ./scripts/wasm/$CONTRACT.wasm https://github.com/CosmWasm/cw-plus/releases/download/$VERSION/$CONTRACT.wasm
done
42 changes: 42 additions & 0 deletions scripts/wasm/gov_upload_code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash
BINARY=build/eved

CHAIN_ID=local-eve
VAL_KEY=test0
VOTER=
VAL=$($BINARY keys show -a $VAL_KEY --keyring-backend test)
CONTRACT=cw20_base
PROPOSAL=1
DENOM=${2:-"ueve"}
HOME=mytestnet

echo "submit wasm store proposal..."
$BINARY tx wasm submit-proposal wasm-store scripts/wasm/$CONTRACT.wasm --title "Add $CONTRACT" \
--summary "Let's upload this contract 3" \
--from $VAL_KEY --keyring-backend test --chain-id $CHAIN_ID -y \
--gas auto --gas-adjustment 1.3 > /dev/null


echo "deposit ueve to proposal..."
sleep 5
# $BINARY query gov proposal $PROPOSAL
$BINARY tx gov deposit $PROPOSAL 40000000000000000000$DENOM --from $VAL_KEY --keyring-backend test \
--chain-id $CHAIN_ID -y -b sync --gas auto --gas-adjustment 1.3 --home $HOME > /dev/null

echo "process to self vote..."
sleep 5
$BINARY tx gov vote $PROPOSAL yes --from $VAL_KEY --keyring-backend test \
--chain-id $CHAIN_ID -y -b sync --gas auto --gas-adjustment 1.3 --home $HOME > /dev/null

echo "Waiting for voting periods to finish..."
COUNTER=0
while ((COUNTER < 12)); do
# Capture output of $BINARY command and extract "status" using jq
status=$($BINARY q gov proposal 1 --output=json | jq '.status')
sleep 5
echo "Current proposal status: $status"
# Increment COUNTER using arithmetic expansion
((COUNTER++))
done

$BINARY query wasm list-code
79 changes: 79 additions & 0 deletions scripts/wasm/instantiate_cw20.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!bin/bash

# this is cw20 code from install_contracts.sh, true if this is the first proposal
CODE=1
CHAIN_ID=local-eve
BINARY=build/eved
HOME=mytestnet
$BINARY keys add demo --keyring-backend test --home $HOME
VAL_KEY=test0
VAL=$($BINARY keys show -a $VAL_KEY --keyring-backend test --home $HOME)
DEMO=$($BINARY keys show -a demo --keyring-backend test --home $HOME)


# Init params in json
INIT=$(cat <<EOF
{
"name": "My first token",
"symbol": "FRST",
"decimals": 6,
"initial_balances": [{
"address": "$VAL",
"amount": "123456789000"
}]
}
EOF
)

$BINARY tx wasm instantiate $CODE "$INIT" --label "First Coin" --no-admin --from $VAL_KEY \
--keyring-backend test --chain-id $CHAIN_ID -y --gas auto --gas-adjustment 1.3 --home $HOME
# wait the chain to process the tx
echo "Waiting for tx to be processed..."
sleep 5
# query the contract address, include this in case of multiple contract instantiation, default getting the first contract
CONTRACT_POSITION=$1
if [ -z "$CONTRACT_POSITION" ]; then
CONTRACT_POSITION=0
fi
CONTRACT=$($BINARY q wasm list-contracts-by-creator $VAL --output json | jq -r ".contract_addresses[$CONTRACT_POSITION]" )

if [ -z "$CONTRACT" ]; then
echo "No contract found"
exit 1
fi

QUERY=$(cat <<EOF
{ "balance": { "address": "$VAL" }}
EOF
)
QUERY_DEMO=$(cat <<EOF
{ "balance": { "address": "$DEMO" }}
EOF
)

# check initial balance
echo "Validator Balance:"
$BINARY query wasm contract-state smart $CONTRACT "$QUERY"
echo "Demo Balance:"
$BINARY query wasm contract-state smart $CONTRACT "$QUERY_DEMO"

# send some tokens
TRANSFER=$(cat <<EOF
{
"transfer": {
"recipient": "$DEMO",
"amount": "987654321"
}
}
EOF
)
$BINARY tx wasm execute $CONTRACT "$TRANSFER" --from $VAL_KEY --keyring-backend test \
--chain-id $CHAIN_ID -y --gas auto --gas-adjustment 1.3 --home $HOME
# wait the chain to process the tx
echo "Waiting for tx to be processed..."
sleep 5
# check final balance
echo "Validator Balance:"
$BINARY query wasm contract-state smart $CONTRACT "$QUERY"
echo "Demo Balance:"
$BINARY query wasm contract-state smart $CONTRACT "$QUERY_DEMO"
22 changes: 22 additions & 0 deletions scripts/wasm/upload_code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!bin/bash

BINARY=build/eved

CHAIN_ID=local-eve

CONTRACT=cw20_base
HOME=mytestnet
DENOM="ueve"
KEY="test0"

KEYRING="test"
TEST0_ADDRESS=$($BINARY keys show $KEY -a --keyring-backend $KEYRING --home $HOME)

$BINARY tx wasm store scripts/wasm/$CONTRACT.wasm \
--from test0 --keyring-backend $KEYRING --chain-id $CHAIN_ID \
--gas auto --gas-adjustment 1.3 --home $HOME -y


# $BINARY query wasm list-contract-by-code $PROPOSAL
# $BINARY query wasm list-contracts-by-creator $TEST0_ADDRESS
$BINARY query wasm list-code

0 comments on commit 63a4a73

Please sign in to comment.