Skip to content

Commit

Permalink
refactor: check balance with polling
Browse files Browse the repository at this point in the history
  • Loading branch information
rabi-siddique committed Jul 24, 2024
1 parent fa0c775 commit 4d478f7
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 36 deletions.
47 changes: 47 additions & 0 deletions .github/actions/check-balance-with-polling/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: 'Check Token Balance With Polling'

inputs:
url:
description: 'API endpoint for fetching a balance for a wallet'
required: true
address:
description: 'Wallet address to check the balance'
required: true
denom:
description: 'Denom whose amount has to be extracted from the JSON response'
required: true
file:
description: 'Name of the file to place extracted amount of denom'
required: true
comparison_type:
description: 'Type of comparison to perform ("greater" or "lesser")'
required: true
default: 'greater'
polling_seconds:
description: 'Polling time in seconds'
required: true

runs:
using: 'composite'
steps:
- name: Check token balance and validate
shell: bash
run: |
timeout ${{ inputs.polling_seconds }} bash -c "\
OLD_BALANCE=\$(cat ${{ inputs.file }})
NEW_BALANCE=\$(curl -s ${{ inputs.url }}/${{ inputs.address }} | jq -r --arg denom '${{ inputs.denom }}' '([.balances[] | select(.denom == \$denom) | .amount | tonumber] | if length == 0 then 0 else .[] end)')
COMPARISON_OP='-gt'
if [[ ${{ inputs.comparison_type }} == 'lesser' ]]; then
COMPARISON_OP='-lt'
fi
while true; do
if [[ \$COMPARISON_OP == '-gt' && \$NEW_BALANCE -gt \$OLD_BALANCE ]] || [[ \$COMPARISON_OP == '-lt' && \$NEW_BALANCE -lt \$OLD_BALANCE ]]; then
break
fi
echo \"Old Balance: \$OLD_BALANCE\"
echo \"New Balance: \$NEW_BALANCE\"
sleep 1 # consider using a different input or a fixed short delay for polling
NEW_BALANCE=\$(curl -s ${{ inputs.url }}/${{ inputs.address }} | jq -r --arg denom '${{ inputs.denom }}' '([.balances[] | select(.denom == \$denom) | .amount | tonumber] | if length == 0 then 0 else .[] end)')
done"
2 changes: 1 addition & 1 deletion .github/actions/store-balances/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ runs:
shell: bash
run: |
curl "${{ inputs.url }}/${{ inputs.address }}" |
jq -r --arg denom "${{ inputs.denom }}" '.balances[] | select(.denom == $denom) | .amount | tonumber' > "${{ inputs.file }}"
jq -r --arg denom "${{ inputs.denom }}" '([.balances[] | select(.denom == $denom) | .amount | tonumber] | if length == 0 then 0 else .[] end)' > "${{ inputs.file }}"
84 changes: 49 additions & 35 deletions .github/workflows/emerynet-localchain-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:

env:
EMERYNET_BALANCE_FILE: emerynet_balance.txt
LOCAL_BALANCE_FILE: local_balance.txt
EMERYNET_CHAIN_ID: agoric-emerynet-8
LOCAL_CHAIN_ID: agoric-local
WALLET_ADDRESS_EMERYNET: agoric10emrzln03exuc9uv98mjwmp95735mjm6k2n9xm
Expand Down Expand Up @@ -60,11 +61,19 @@ jobs:
echo 'waiting for relayer to start...'
done"
- name: Store Emerynet Wallet Balance in a file
- name: Store Local Wallet Balance in a file
uses: ./.github/actions/store-balances
with:
url: https://emerynet.api.agoric.net/cosmos/bank/v1beta1/balances
url: ${{ env.LOCAL_API_URL }}
address: ${{ env.WALLET_ADDRESS_LOCAL }}
denom: 'ibc/49C630713B2AB60653F76C0C58D43C2A64956803B4D422CACB6DD4AD016ED846'
file: ${{ env.LOCAL_BALANCE_FILE }}

- name: Store Emerynet Wallet Balance in a file
uses: ./.github/actions/store-balances
with:
url: ${{ env.EMERYNET_API_URL }}
address: ${{ env.WALLET_ADDRESS_EMERYNET }}
denom: ubld
file: ${{ env.EMERYNET_BALANCE_FILE }}

Expand Down Expand Up @@ -101,32 +110,38 @@ jobs:
sleep 5
- name: Check Token Balance on Emerynet after transfer
run: |
NEW_EMERYNET_BALANCE=$(curl https://emerynet.api.agoric.net/cosmos/bank/v1beta1/balances/$WALLET_ADDRESS_EMERYNET |
jq -r '.balances[] | select(.denom == "ubld") | .amount | tonumber')
ORIGINAL_EMERYNET_BALANCE=$(cat $EMERYNET_BALANCE_FILE)
BALANCE_DIFFERENCE=$((ORIGINAL_EMERYNET_BALANCE - NEW_EMERYNET_BALANCE))
if [ "$BALANCE_DIFFERENCE" -ne 100 ]; then
echo "Token transfer discrepancy detected: Expected 100, found $BALANCE_DIFFERENCE."
exit 1
else
echo "Token transfer validated: Balance difference is exactly 100ubld."
fi
uses: ./.github/actions/check-balance-with-polling
with:
url: ${{ env.EMERYNET_API_URL }}
address: ${{ env.WALLET_ADDRESS_EMERYNET }}
denom: ubld
file: ${{ env.EMERYNET_BALANCE_FILE }}
comparison_type: lesser
polling_seconds: 120

- name: Check Token Balance on Local Chain after transfer
uses: ./.github/actions/check-balances
uses: ./.github/actions/check-balance-with-polling
with:
container: ${{ env.LOCAL_CHAIN_ID }}
url: ${{ env.LOCAL_API_URL }}
address: ${{ env.WALLET_ADDRESS_LOCAL }}
expected_amount: ${{ env.TRANSFER_AMOUNT_FROM_EMERYNET }}
expected_denom: 'ibc/49C630713B2AB60653F76C0C58D43C2A64956803B4D422CACB6DD4AD016ED846'
expected_length: '2'
denom: 'ibc/49C630713B2AB60653F76C0C58D43C2A64956803B4D422CACB6DD4AD016ED846'
file: ${{ env.LOCAL_BALANCE_FILE }}
comparison_type: greater
polling_seconds: 120

- name: Store Emerynet Wallet Balance in a file
- name: Store Local Wallet Balance in a file
uses: ./.github/actions/store-balances
with:
url: https://emerynet.api.agoric.net/cosmos/bank/v1beta1/balances
url: ${{ env.LOCAL_API_URL }}
address: ${{ env.WALLET_ADDRESS_LOCAL }}
denom: 'ibc/49C630713B2AB60653F76C0C58D43C2A64956803B4D422CACB6DD4AD016ED846'
file: ${{ env.LOCAL_BALANCE_FILE }}

- name: Store Emerynet Wallet Balance in a file
uses: ./.github/actions/store-balances
with:
url: ${{ env.EMERYNET_API_URL }}
address: ${{ env.WALLET_ADDRESS_EMERYNET }}
denom: ubld
file: ${{ env.EMERYNET_BALANCE_FILE }}

Expand All @@ -137,22 +152,21 @@ jobs:
sleep 5
- name: Check Token Balance on Local Chain after transfer
uses: ./.github/actions/check-balances
uses: ./.github/actions/check-balance-with-polling
with:
container: ${{ env.LOCAL_CHAIN_ID }}
url: ${{ env.LOCAL_API_URL }}
address: ${{ env.WALLET_ADDRESS_LOCAL }}
expected_amount: '50'
expected_denom: 'ibc/49C630713B2AB60653F76C0C58D43C2A64956803B4D422CACB6DD4AD016ED846'
expected_length: '2'
denom: 'ibc/49C630713B2AB60653F76C0C58D43C2A64956803B4D422CACB6DD4AD016ED846'
file: ${{ env.LOCAL_BALANCE_FILE }}
comparison_type: lesser
polling_seconds: 120

- name: Check Token Balance on Emerynet after transfer
run: |
timeout 120 bash -c "\
OLD_BALANCE=\$(cat \"\$EMERYNET_BALANCE_FILE\")
NEW_BALANCE=\$(curl -s https://emerynet.api.agoric.net/cosmos/bank/v1beta1/balances/\"\$WALLET_ADDRESS_EMERYNET\" | jq -r '.balances[] | select(.denom == \"ubld\") | .amount')
until [ \"\$NEW_BALANCE\" -gt \"\$OLD_BALANCE\" ]; do
NEW_BALANCE=\$(curl -s https://emerynet.api.agoric.net/cosmos/bank/v1beta1/balances/\"\$WALLET_ADDRESS_EMERYNET\" | jq -r '.balances[] | select(.denom == \"ubld\") | .amount')
echo \"Old Balance: \$OLD_BALANCE\"
echo \"New Balance: \$NEW_BALANCE\"
done
"
uses: ./.github/actions/check-balance-with-polling
with:
url: ${{ env.EMERYNET_API_URL }}
address: ${{ env.WALLET_ADDRESS_EMERYNET }}
denom: ubld
file: ${{ env.EMERYNET_BALANCE_FILE }}
comparison_type: greater
polling_seconds: 120

0 comments on commit 4d478f7

Please sign in to comment.