diff --git a/.github/actions/check-balance-with-polling/action.yml b/.github/actions/check-balance-with-polling/action.yml new file mode 100644 index 0000000..53a1be2 --- /dev/null +++ b/.github/actions/check-balance-with-polling/action.yml @@ -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" diff --git a/.github/actions/store-balances/action.yml b/.github/actions/store-balances/action.yml index c490c75..6afb531 100644 --- a/.github/actions/store-balances/action.yml +++ b/.github/actions/store-balances/action.yml @@ -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 }}" diff --git a/.github/workflows/emerynet-localchain-test.yml b/.github/workflows/emerynet-localchain-test.yml index 59f3c93..105ff68 100644 --- a/.github/workflows/emerynet-localchain-test.yml +++ b/.github/workflows/emerynet-localchain-test.yml @@ -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 @@ -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 }} @@ -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 }} @@ -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