From b98bc7c661a611786959fcbb8d9ac8b1d0f8d1eb Mon Sep 17 00:00:00 2001 From: "shiqi.zheng@algorand.com" Date: Thu, 23 Sep 2021 10:53:52 -0400 Subject: [PATCH 01/10] axfer tests --- test/scripts/e2e_subs/app-assets.sh | 222 ++++++++++++++++ .../e2e_subs/tealprogs/assets-escrow.teal | 243 ++++++++++++++++++ 2 files changed, 465 insertions(+) create mode 100755 test/scripts/e2e_subs/app-assets.sh create mode 100644 test/scripts/e2e_subs/tealprogs/assets-escrow.teal diff --git a/test/scripts/e2e_subs/app-assets.sh b/test/scripts/e2e_subs/app-assets.sh new file mode 100755 index 0000000000..aa44bcc433 --- /dev/null +++ b/test/scripts/e2e_subs/app-assets.sh @@ -0,0 +1,222 @@ +#!/bin/bash + +filename=$(basename "$0") +scriptname="${filename%.*}" +date "+${scriptname} start %Y%m%d_%H%M%S" + + +my_dir="$(dirname "$0")" +source "$my_dir/rest.sh" "$@" +function rest() { + curl -q -s -H "Authorization: Bearer $PUB_TOKEN" "$NET$1" +} + +set -e +set -x +set -o pipefail +export SHELLOPTS + +WALLET=$1 + +TEAL=test/scripts/e2e_subs/tealprogs + +gcmd="goal -w ${WALLET}" + +ACCOUNT=$(${gcmd} account list|awk '{ print $3 }') +# Create a smaller account so rewards won't change balances. +SMALL=$(${gcmd} account new | awk '{ print $6 }') +# Under one algo receives no rewards +${gcmd} clerk send -a 1000000 -f "$ACCOUNT" -t "$SMALL" + +function balance { + acct=$1; shift + goal account balance -a "$acct" | awk '{print $1}' +} + +[ "$(balance "$ACCOUNT")" = 999998999000 ] +[ "$(balance "$SMALL")" = 1000000 ] + +function held_assets { + acct=$1; + goal account info -a "$acct" | awk '/Held Assets:/,/Created Apps:/' | grep "ID*" +} + +function asset_bal { + acct=$1; + goal account info -a "$acct" | awk '/Held Assets:/,/Created Apps:/' | grep "ID*" | awk -F'[, ]' '{print $7}' +} + +function asset_ids { + acct=$1; + goal account info -a "$acct" | awk '/Held Assets:/,/Created Apps:/' | grep "ID*" | awk -F'[, ]' '{print $2}' +} +# +function assets { + acct=$1; + goal account info -a "$acct" | awk '/Held Assets:/,/Created Apps:/' | grep "ID*" | awk -F'[, ]' '{print $4}' +} + +APPID=$(${gcmd} app create --creator "${SMALL}" --approval-prog=${TEAL}/assets-escrow.teal --global-byteslices 4 --global-ints 0 --local-byteslices 0 --local-ints 1 --clear-prog=${TEAL}/approve-all.teal | grep Created | awk '{ print $6 }') +[ "$(balance "$SMALL")" = 999000 ] # 1000 fee + +function appl { + method=$1; shift + ${gcmd} app call --app-id="$APPID" --app-arg="str:$method" "$@" +} + +function app-txid { + # When app (call or optin) submits, this is how the txid is + # printed. Not in appl() because appl is also used with -o to + # create tx + grep -o -E 'txid [A-Z0-9]{52}' | cut -c 6- | head -1 +} + +function asset-id { + grep -o -E 'index [A-Z0-9]+'| cut -c 7- +} + +APPACCT=$(python -c "import algosdk.encoding as e; print(e.encode_address(e.checksum(b'appID'+($APPID).to_bytes(8, 'big'))))") +[ "$(balance "$SMALL")" = 999000 ] # 1000 fee + +function asset-create { + amount=$1; shift + ${gcmd} asset create --creator "$SMALL" --total "$amount" --decimals 0 "$@" +} + +function asset-deposit { + amount=$1;shift + ID=$1; shift + ${gcmd} asset send -f "$SMALL" -t "$APPACCT" -a "$amount" --assetid "$ID" "$@" +} + +function asset-optin { + ${gcmd} asset send -a 0 "$@" +} + +function clawback_addr { + grep -o -E 'Clawback address: [A-Z0-9]{58}' | awk '{print $3}' +} + +T=$TEMPDIR + +function sign { + ${gcmd} clerk sign -i "$T/$1.tx" -o "$T/$1.stx" +} + +TXID=$(${gcmd} app optin --app-id "$APPID" --from "${SMALL}" | app-txid) +# Rest succeeds, no stray inner-txn array +[ "$(rest "/v2/transactions/pending/$TXID" | jq '.["inner-txn"]')" == null ] +[ "$(balance "$SMALL")" = 998000 ] # 1000 fee + +ASSETID=$(asset-create 1000000 --name "e2e" --unitname "e" | asset-id) +[ "$(balance "$SMALL")" = 997000 ] # 1000 fee + +${gcmd} clerk send -a 1000000 -f "$ACCOUNT" -t "$APPACCT" +appl "optin():void" --app-arg="int:$ASSETID" --foreign-asset="$ASSETID" --from="$SMALL" +[ "$(balance "$APPACCT")" = 999000 ] # 1000 fee +[ "$(balance "$SMALL")" = 996000 ] + +appl "deposit():void" -o "$T/deposit.tx" --from="$SMALL" +asset-deposit 1000 $ASSETID -o "$T/axfer1.tx" +cat "$T/deposit.tx" "$T/axfer1.tx" | ${gcmd} clerk group -i - -o "$T/group.tx" +sign group +${gcmd} clerk rawsend -f "$T/group.stx" + +[ "$(asset_ids "$SMALL")" = $ASSETID ] # asset ID +[ "$(asset_bal "$SMALL")" = 999000 ] # asset balance +[ "$(asset_ids "$APPACCT")" = $ASSETID ] +[ "$(asset_bal "$APPACCT")" = 1000 ] +[ "$(balance "$SMALL")" = 994000 ] # 2 fees +[ "$(balance "$APPACCT")" = 999000 ] + + +# Withdraw 100 in app. Confirm that inner txn is visible to transaction API. +TXID=$(appl "withdraw(uint64):void" --foreign-asset="$ASSETID" --app-arg="int:$ASSETID" --app-arg="int:100" --from="$SMALL" | app-txid) +[ "$(rest "/v2/transactions/pending/$TXID" \ + | jq '.["inner-txns"][0].txn.txn.aamt')" = 100 ] +[ "$(rest "/v2/transactions/pending/$TXID?format=msgpack" | msgpacktool -d \ + | jq '.["inner-txns"][0].txn.txn.type')" = '"axfer"' ] +# Now confirm it's in blocks API (this time in our internal form) +ROUND=$(rest "/v2/transactions/pending/$TXID" | jq '.["confirmed-round"]') +rest "/v2/blocks/$ROUND" | jq .block.txns[0].dt.itx + +[ "$(asset_bal "$SMALL")" = 999100 ] # 100 asset withdrawn +[ "$(asset_bal "$APPACCT")" = 900 ] # 100 asset withdrawn +[ "$(balance "$SMALL")" = 993000 ] # 1 fee +[ "$(balance "$APPACCT")" = 998000 ] # fee paid by app + +appl "withdraw(uint64):void" --foreign-asset="$ASSETID" --app-arg="int:$ASSETID" --app-arg="int:100" --fee 2000 --from="$SMALL" +[ "$(asset_bal "$SMALL")" = 999200 ] # 100 asset withdrawn +[ "$(balance "$SMALL")" = 991000 ] # 2000 fee +[ "$(asset_bal "$APPACCT")" = 800 ] # 100 asset withdrawn +[ "$(balance "$APPACCT")" = 998000 ] # fee credit used + +# Try to withdraw too much +appl "withdraw(uint64):void" --foreign-asset="$ASSETID" --app-arg="int:$ASSETID" --app-arg="int:1000" --from="$SMALL" && exit 1 +[ "$(asset_bal "$SMALL")" = 999200 ] # no change +[ "$(asset_bal "$APPACCT")" = 800 ] # no change +[ "$(balance "$SMALL")" = 991000 ] +[ "$(balance "$APPACCT")" = 998000 ] + +# Show that it works AT exact asset balance +appl "withdraw(uint64):void" --foreign-asset="$ASSETID" --app-arg="int:$ASSETID" --app-arg="int:800" --from="$SMALL" +[ "$(asset_bal "$SMALL")" = 1000000 ] +[ "$(asset_bal "$APPACCT")" = 0 ] +[ "$(balance "$SMALL")" = 990000 ] +[ "$(balance "$APPACCT")" = 997000 ] + + +# set appacct as clawback address on asset +${gcmd} asset config --manager $SMALL --assetid $ASSETID --new-clawback $APPACCT +cb_addr=$(${gcmd} asset info --assetid $ASSETID | clawback_addr) +[ "$cb_addr" = "$APPACCT" ] #app is set as clawback address + +# app can transfer asset between accounts +USER=$(${gcmd} account new | awk '{ print $6 }') #new account +${gcmd} clerk send -a 1000000 -f "$ACCOUNT" -t "$USER" #fund account +asset-optin -f "$USER" -t "$USER" --assetid "$ASSETID" #opt in to asset +# transfer asset from $SMALL to $USER +appl "transfer(uint64):void" --app-arg="int:$ASSETID" --app-arg="int:1000" --app-arg="addr:$USER" --foreign-asset="$ASSETID" --from="$SMALL" --app-account="$USER" +[ $(assets "$USER") = "e2e" ] +[ $(asset_bal "$USER") = 1000 ] +[ $(asset_bal "$SMALL") = 999000 ] +# transfer asset from $USER to $SMALL +appl "transfer(uint64):void" --app-arg="int:$ASSETID" --app-arg="int:100" --app-arg="addr:$SMALL" --foreign-asset="$ASSETID" --from="$USER" --app-account="$SMALL" +[ $(asset_bal "$USER") = 900 ] +[ $(asset_bal "$SMALL") = 999100 ] +# $USER does not have clawback auth +${gcmd} asset send -f "$SMALL" -t "$USER" -a "$amount" --assetid "$ASSETID" --clawback "$USER" && exit 1 +#rekey $USER to "$APPACCT" +${gcmd} clerk send --from "$USER" --to "$USER" -a 0 --rekey-to "$APPACCT" + +# opt in more assets +ASSETID2=$(asset-create 1000000 --name "alpha" --unitname "a" | asset-id) +appl "optin():void" --app-arg="int:$ASSETID2" --foreign-asset="$ASSETID2" --from="$SMALL" +ASSETID3=$(asset-create 1000000 --name "beta" --unitname "b" | asset-id) +appl "optin():void" --app-arg="int:$ASSETID3" --foreign-asset="$ASSETID3" --from="$SMALL" + +IDs=" +$ASSETID \n +$ASSETID2 \n +$ASSETID3 \n +" +[[ "$(held_assets "$APPACCT")" = $IDs ]] # account has 3 assets + +# opt out of assets +appl "close(uint64):void" --app-arg="int:$ASSETID2" --foreign-asset="$ASSETID2" --from="$SMALL" +IDs=" +$ASSETID \n +$ASSETID3 \n +" +[[ "$(held_assets "$APPACCT")" = "$IDs" ]] # account has 2 assets +appl "close(uint64):void" --app-arg="int:$ASSETID" --foreign-asset="$ASSETID" --from="$SMALL" +appl "close(uint64):void" --app-arg="int:$ASSETID3" --foreign-asset="$ASSETID3" --from="$SMALL" +[[ "$(held_assets "$APPACCT")" = "" ]] # account has no assets + + +#app creates asset +#mint asset +#app afrz + + +date "+${scriptname} OK %Y%m%d_%H%M%S" diff --git a/test/scripts/e2e_subs/tealprogs/assets-escrow.teal b/test/scripts/e2e_subs/tealprogs/assets-escrow.teal new file mode 100644 index 0000000000..63826e6c5f --- /dev/null +++ b/test/scripts/e2e_subs/tealprogs/assets-escrow.teal @@ -0,0 +1,243 @@ +#pragma version 5 + // This application creates assets and pays out an asset for some algo. It + // also accepts assets, and holds them until the requester asks for them back. + // Depositors invoke the app with "deposit" as arg[0], and make a pay + // transaction to the app account in the following txn slot. + // The app records the deposit in the user's local state (so + // the user must be opted-in), though they may do so in their + // initial call. + + // To withdraw, users call with "withdraw" in arg[0], and a + // big-endian integer amount in arg[1]. If the withdrawal + // amount is less than the amount deposited, the app pays out + // the request, and decrements the user's balance. + + // ApplicationID is zero in inital creation txn + txn ApplicationID + bz handle_createapp + + // Handle possible OnCompletion type. We don't have to + // worry about handling ClearState, because the + // ClearStateProgram will execute in that case, not the + // ApprovalProgram. + + txn OnCompletion + int NoOp + == + bnz handle_noop + + txn OnCompletion + int OptIn + == + bnz handle_optin + + txn OnCompletion + int CloseOut + == + bnz handle_closeout + + txn OnCompletion + int UpdateApplication + == + bnz handle_updateapp + + txn OnCompletion + int DeleteApplication + == + bnz handle_deleteapp + // Unexpected OnCompletion value. Should be unreachable. + err + +handle_createapp: + int 1 + return + +handle_optin: + // Let anyone optin with a single txn, with no arguments. If + // it's not a single txn, fall through to handle_noop, so that + // a deposit can be made while opting in. + // We should standardize a behaviour like this in ABI. + global GroupSize + int 1 + == + bz handle_noop + int 1 + return + +handle_noop: + txn ApplicationArgs 0 + byte "optin():void" + == + bz not_optin + byte "optin" + callsub debug + + itxn_begin + int axfer + itxn_field TypeEnum + + int 0 + itxn_field AssetAmount + + txn ApplicationArgs 1 + btoi + itxn_field XferAsset + + global CurrentApplicationAddress + itxn_field AssetReceiver + itxn_submit + + int 1 + return +not_optin: + txn ApplicationArgs 0 + byte "deposit():void" + == + bz not_deposit + + byte "deposit" + callsub debug + + // Handle a deposit. Next txn slot must axfer our app account + txn GroupIndex + int 1 + + + dup + dup + + gtxns TypeEnum + int axfer + == + assert + + gtxns AssetReceiver + global CurrentApplicationAddress + == + assert + + gtxns AssetAmount + + // Track the amount this sender deposited in their local state + int 0 + byte "balance" + dup2 + app_local_get + uncover 3 // pull up the Amount + + + app_local_put + + int 1 + return +not_deposit: + txn ApplicationArgs 0 + byte "withdraw(uint64):void" + == + bz not_withdraw + + // Handle withdraw. + + int 0 + byte "balance" + dup2 + app_local_get + + // Subtract the request and replace. Rejects on underflow + txn ApplicationArgs 2 + btoi + - + app_local_put + + itxn_begin + int axfer + itxn_field TypeEnum + + txn ApplicationArgs 1 + btoi + itxn_field XferAsset + + txn ApplicationArgs 2 + btoi + itxn_field AssetAmount + + txn Sender + itxn_field AssetReceiver + itxn_submit + + int 1 + return +not_withdraw: + txn ApplicationArgs 0 + byte "close(uint64):void" + == + bz not_close + + // Handle close. + itxn_begin + int axfer + itxn_field TypeEnum + + txn ApplicationArgs 1 + btoi + itxn_field XferAsset + + int 0 + itxn_field AssetAmount + + txn Sender + itxn_field AssetReceiver + + txn Sender + itxn_field AssetCloseTo + itxn_submit + + int 1 + return +not_close: + txn ApplicationArgs 0 + byte "transfer(uint64):void" + == + bz not_transfer + + // Handle transfer. + itxn_begin + int axfer + itxn_field TypeEnum + + txn ApplicationArgs 1 + btoi + itxn_field XferAsset + + txn ApplicationArgs 2 + btoi + itxn_field AssetAmount + + txn Sender + itxn_field AssetSender + + txn ApplicationArgs 3 + itxn_field AssetReceiver + + itxn_submit + + int 1 + return + +not_transfer: + // Unknown call "method" + err + +handle_closeout: + int 1 + return + +handle_updateapp: +handle_deleteapp: + txn Sender + global CreatorAddress + == + return +debug: + byte "debug" + swap + app_global_put + retsub From 595caa5e5186cb35d89167efa0c208d0f682ba6c Mon Sep 17 00:00:00 2001 From: "shiqi.zheng@algorand.com" Date: Thu, 23 Sep 2021 19:44:23 -0400 Subject: [PATCH 02/10] tests for acfg, afrz --- test/scripts/e2e_subs/app-assets.sh | 127 +++++++++++----- .../e2e_subs/tealprogs/assets-escrow.teal | 137 ++++++++++++++---- 2 files changed, 200 insertions(+), 64 deletions(-) diff --git a/test/scripts/e2e_subs/app-assets.sh b/test/scripts/e2e_subs/app-assets.sh index aa44bcc433..b1cdf3685e 100755 --- a/test/scripts/e2e_subs/app-assets.sh +++ b/test/scripts/e2e_subs/app-assets.sh @@ -36,9 +36,14 @@ function balance { [ "$(balance "$ACCOUNT")" = 999998999000 ] [ "$(balance "$SMALL")" = 1000000 ] -function held_assets { +function created_assets { acct=$1; - goal account info -a "$acct" | awk '/Held Assets:/,/Created Apps:/' | grep "ID*" + goal account info -a "$acct" | awk '/Created Assets:/,/Held Assets:/' | grep "ID*" | awk -F'[, ]' '{print $4}' +} + +function created_supply { + acct=$1; + goal account info -a "$acct" | awk '/Created Assets:/,/Held Assets:/' | grep "ID*" | awk -F'[, ]' '{print $7}' } function asset_bal { @@ -97,6 +102,11 @@ function clawback_addr { grep -o -E 'Clawback address: [A-Z0-9]{58}' | awk '{print $3}' } +function payin { + amount=$1; shift + ${gcmd} clerk send -f "$SMALL" -t "$APPACCT" -a "$amount" "$@" +} + T=$TEMPDIR function sign { @@ -112,7 +122,7 @@ ASSETID=$(asset-create 1000000 --name "e2e" --unitname "e" | asset-id) [ "$(balance "$SMALL")" = 997000 ] # 1000 fee ${gcmd} clerk send -a 1000000 -f "$ACCOUNT" -t "$APPACCT" -appl "optin():void" --app-arg="int:$ASSETID" --foreign-asset="$ASSETID" --from="$SMALL" +appl "optin():void" --foreign-asset="$ASSETID" --from="$SMALL" [ "$(balance "$APPACCT")" = 999000 ] # 1000 fee [ "$(balance "$SMALL")" = 996000 ] @@ -129,9 +139,8 @@ ${gcmd} clerk rawsend -f "$T/group.stx" [ "$(balance "$SMALL")" = 994000 ] # 2 fees [ "$(balance "$APPACCT")" = 999000 ] - # Withdraw 100 in app. Confirm that inner txn is visible to transaction API. -TXID=$(appl "withdraw(uint64):void" --foreign-asset="$ASSETID" --app-arg="int:$ASSETID" --app-arg="int:100" --from="$SMALL" | app-txid) +TXID=$(appl "withdraw(uint64):void" --app-arg="int:100" --foreign-asset="$ASSETID" --from="$SMALL" | app-txid) [ "$(rest "/v2/transactions/pending/$TXID" \ | jq '.["inner-txns"][0].txn.txn.aamt')" = 100 ] [ "$(rest "/v2/transactions/pending/$TXID?format=msgpack" | msgpacktool -d \ @@ -145,78 +154,120 @@ rest "/v2/blocks/$ROUND" | jq .block.txns[0].dt.itx [ "$(balance "$SMALL")" = 993000 ] # 1 fee [ "$(balance "$APPACCT")" = 998000 ] # fee paid by app -appl "withdraw(uint64):void" --foreign-asset="$ASSETID" --app-arg="int:$ASSETID" --app-arg="int:100" --fee 2000 --from="$SMALL" +appl "withdraw(uint64):void" --app-arg="int:100" --foreign-asset="$ASSETID" --fee 2000 --from="$SMALL" [ "$(asset_bal "$SMALL")" = 999200 ] # 100 asset withdrawn [ "$(balance "$SMALL")" = 991000 ] # 2000 fee [ "$(asset_bal "$APPACCT")" = 800 ] # 100 asset withdrawn [ "$(balance "$APPACCT")" = 998000 ] # fee credit used # Try to withdraw too much -appl "withdraw(uint64):void" --foreign-asset="$ASSETID" --app-arg="int:$ASSETID" --app-arg="int:1000" --from="$SMALL" && exit 1 +appl "withdraw(uint64):void" --app-arg="int:1000" --foreign-asset="$ASSETID" --from="$SMALL" && exit 1 [ "$(asset_bal "$SMALL")" = 999200 ] # no change [ "$(asset_bal "$APPACCT")" = 800 ] # no change [ "$(balance "$SMALL")" = 991000 ] [ "$(balance "$APPACCT")" = 998000 ] # Show that it works AT exact asset balance -appl "withdraw(uint64):void" --foreign-asset="$ASSETID" --app-arg="int:$ASSETID" --app-arg="int:800" --from="$SMALL" +appl "withdraw(uint64):void" --app-arg="int:800" --foreign-asset="$ASSETID" --from="$SMALL" [ "$(asset_bal "$SMALL")" = 1000000 ] [ "$(asset_bal "$APPACCT")" = 0 ] [ "$(balance "$SMALL")" = 990000 ] [ "$(balance "$APPACCT")" = 997000 ] - -# set appacct as clawback address on asset -${gcmd} asset config --manager $SMALL --assetid $ASSETID --new-clawback $APPACCT -cb_addr=$(${gcmd} asset info --assetid $ASSETID | clawback_addr) -[ "$cb_addr" = "$APPACCT" ] #app is set as clawback address - -# app can transfer asset between accounts USER=$(${gcmd} account new | awk '{ print $6 }') #new account ${gcmd} clerk send -a 1000000 -f "$ACCOUNT" -t "$USER" #fund account asset-optin -f "$USER" -t "$USER" --assetid "$ASSETID" #opt in to asset -# transfer asset from $SMALL to $USER -appl "transfer(uint64):void" --app-arg="int:$ASSETID" --app-arg="int:1000" --app-arg="addr:$USER" --foreign-asset="$ASSETID" --from="$SMALL" --app-account="$USER" -[ $(assets "$USER") = "e2e" ] +# SET $USER as clawback address +${gcmd} asset config --manager $SMALL --assetid $ASSETID --new-clawback $USER +cb_addr=$(${gcmd} asset info --assetid $ASSETID | clawback_addr) +[ "$cb_addr" = "$USER" ] +${gcmd} asset send -f "$SMALL" -t "$USER" -a "1000" --assetid "$ASSETID" --clawback "$USER" [ $(asset_bal "$USER") = 1000 ] [ $(asset_bal "$SMALL") = 999000 ] -# transfer asset from $USER to $SMALL -appl "transfer(uint64):void" --app-arg="int:$ASSETID" --app-arg="int:100" --app-arg="addr:$SMALL" --foreign-asset="$ASSETID" --from="$USER" --app-account="$SMALL" -[ $(asset_bal "$USER") = 900 ] -[ $(asset_bal "$SMALL") = 999100 ] -# $USER does not have clawback auth -${gcmd} asset send -f "$SMALL" -t "$USER" -a "$amount" --assetid "$ASSETID" --clawback "$USER" && exit 1 -#rekey $USER to "$APPACCT" +# rekey $USER to "$APPACCT" ${gcmd} clerk send --from "$USER" --to "$USER" -a 0 --rekey-to "$APPACCT" +# $USER should still have clawback auth. should have been authorized by "$APPACCT" +${gcmd} asset send -f "$SMALL" -t "$USER" -a "1000" --assetid "$ASSETID" --clawback "$USER" && exit 1 + +USER2=$(${gcmd} account new | awk '{ print $6 }') #new account +${gcmd} clerk send -a 1000000 -f "$ACCOUNT" -t "$USER2" #fund account +asset-optin -f "$USER2" -t "$USER2" --assetid "$ASSETID" #opt in to asset +# set $APPACCT as clawback address on asset +${gcmd} asset config --manager $SMALL --assetid $ASSETID --new-clawback $APPACCT +cb_addr=$(${gcmd} asset info --assetid $ASSETID | clawback_addr) +[ "$cb_addr" = "$APPACCT" ] #app is set as clawback address +# transfer asset from $SMALL to $USER +appl "transfer(uint64):void" --app-arg="int:1000" --foreign-asset="$ASSETID" --from="$SMALL" --app-account="$USER2" +[ $(asset_bal "$USER2") = 1000 ] +[ $(asset_bal "$SMALL") = 998000 ] +# transfer asset from $USER to $SMALL +appl "transfer(uint64):void" --app-arg="int:100" --foreign-asset="$ASSETID" --from="$USER2" --app-account="$SMALL" +[ $(asset_bal "$USER2") = 900 ] +[ $(asset_bal "$SMALL") = 998100 ] # opt in more assets ASSETID2=$(asset-create 1000000 --name "alpha" --unitname "a" | asset-id) -appl "optin():void" --app-arg="int:$ASSETID2" --foreign-asset="$ASSETID2" --from="$SMALL" +appl "optin():void" --foreign-asset="$ASSETID2" --from="$SMALL" ASSETID3=$(asset-create 1000000 --name "beta" --unitname "b" | asset-id) -appl "optin():void" --app-arg="int:$ASSETID3" --foreign-asset="$ASSETID3" --from="$SMALL" +appl "optin():void" --foreign-asset="$ASSETID3" --from="$SMALL" IDs=" $ASSETID \n $ASSETID2 \n $ASSETID3 \n " -[[ "$(held_assets "$APPACCT")" = $IDs ]] # account has 3 assets +[[ "$(assets "$APPACCT")" = $IDs ]] # account has 3 assets # opt out of assets -appl "close(uint64):void" --app-arg="int:$ASSETID2" --foreign-asset="$ASSETID2" --from="$SMALL" +appl "close():void" --foreign-asset="$ASSETID2" --from="$SMALL" IDs=" $ASSETID \n $ASSETID3 \n " -[[ "$(held_assets "$APPACCT")" = "$IDs" ]] # account has 2 assets -appl "close(uint64):void" --app-arg="int:$ASSETID" --foreign-asset="$ASSETID" --from="$SMALL" -appl "close(uint64):void" --app-arg="int:$ASSETID3" --foreign-asset="$ASSETID3" --from="$SMALL" -[[ "$(held_assets "$APPACCT")" = "" ]] # account has no assets - - -#app creates asset -#mint asset -#app afrz +[[ "$(assets "$APPACCT")" = "$IDs" ]] # account has 2 assets +appl "close():void" --foreign-asset="$ASSETID" --from="$SMALL" +appl "close():void" --foreign-asset="$ASSETID3" --from="$SMALL" +[[ "$(assets "$APPACCT")" = "" ]] # account has no assets + +# app creates asset +appl "create(uint64):void" --app-arg="int:1000000" --from="$SMALL" +[ "$(created_assets "$APPACCT")" = "X" ] +[ "$(created_supply "$APPACCT")" = 1000000 ] + +# mint asset +APPASSETID=$(asset_ids "$APPACCT") +asset-optin -f "$SMALL" -t "$SMALL" --assetid "$APPASSETID" #opt in to asset +appl "mint():void" --from="$SMALL" --foreign-asset="$APPASSETID" -o "$T/mint.tx" +payin 1000 -o "$T/pay1.tx" +cat "$T/mint.tx" "$T/pay1.tx" | ${gcmd} clerk group -i - -o "$T/group.tx" +sign group +${gcmd} clerk rawsend -f "$T/group.stx" +IDs=" +$ASSETID \n +$ASSETID2 \n +$ASSETID3 \n +$APPASSETID +" +[[ "$(assets "$SMALL")" = "$IDs" ]] # has new asset +[[ "$(asset_bal "$SMALL")" = "999100\n1000000\n1000000\n1000" ]] # correct balances +[ "$(asset_bal "$APPACCT")" = 999000 ] # 1k sent + +# freeze asset +appl "freeze(uint64):void" --app-arg="int:1" --foreign-asset="$APPASSETID" --from="$SMALL" +# fail since asset is frozen on $SMALL +appl "mint():void" --from="$SMALL" -o "$T/mint.tx" --foreign-asset="$APPASSETID" +payin 1000 -o "$T/pay1.tx" +cat "$T/mint.tx" "$T/pay1.tx" | ${gcmd} clerk group -i - -o "$T/group.tx" +sign group +${gcmd} clerk rawsend -f "$T/group.stx" && exit 1 +# unfreeze asset +appl "freeze(uint64):void" --app-arg="int:0" --foreign-asset="$APPASSETID" --from="$SMALL" +appl "mint():void" --from="$SMALL" -o "$T/mint.tx" --foreign-asset="$APPASSETID" +payin 1000 -o "$T/pay1.tx" +cat "$T/mint.tx" "$T/pay1.tx" | ${gcmd} clerk group -i - -o "$T/group.tx" +sign group +${gcmd} clerk rawsend -f "$T/group.stx" +[[ "$(asset_bal "$SMALL")" = "999100\n1000000\n1000000\n2000" ]] # minted 1000 date "+${scriptname} OK %Y%m%d_%H%M%S" diff --git a/test/scripts/e2e_subs/tealprogs/assets-escrow.teal b/test/scripts/e2e_subs/tealprogs/assets-escrow.teal index 63826e6c5f..3ddf42a197 100644 --- a/test/scripts/e2e_subs/tealprogs/assets-escrow.teal +++ b/test/scripts/e2e_subs/tealprogs/assets-escrow.teal @@ -1,16 +1,15 @@ #pragma version 5 - // This application creates assets and pays out an asset for some algo. It - // also accepts assets, and holds them until the requester asks for them back. - // Depositors invoke the app with "deposit" as arg[0], and make a pay - // transaction to the app account in the following txn slot. - // The app records the deposit in the user's local state (so - // the user must be opted-in), though they may do so in their - // initial call. - - // To withdraw, users call with "withdraw" in arg[0], and a - // big-endian integer amount in arg[1]. If the withdrawal - // amount is less than the amount deposited, the app pays out - // the request, and decrements the user's balance. + // This application accepts these actions on assets + // optin():void - opt in the app to an asset + // close():void - opt out the app from an asset + // deposit():void - deposit assets on app and hold until withdraw is requested; + // update the asset balance in app's local state + // withdraw(uint64):void - withdraw assets from and update the asset balance in app's local state. + // approve if withdraw amount < balance + // transfer(uint64):void - app has clawback auth to transfer assets between accounts + // create(uint64):void - app creates assets + // mint():void - withdraw assets created by app + // freeze(uint64):void - freeze/unfreeze an asset on an account // ApplicationID is zero in inital creation txn txn ApplicationID @@ -65,6 +64,7 @@ handle_optin: return handle_noop: + // opt in app to asset to enable axfer txn ApplicationArgs 0 byte "optin():void" == @@ -79,8 +79,7 @@ handle_noop: int 0 itxn_field AssetAmount - txn ApplicationArgs 1 - btoi + txna Assets 0 itxn_field XferAsset global CurrentApplicationAddress @@ -122,7 +121,7 @@ not_optin: byte "balance" dup2 app_local_get - uncover 3 // pull up the Amount + uncover 3 // pull up the Amount + app_local_put @@ -142,7 +141,7 @@ not_deposit: app_local_get // Subtract the request and replace. Rejects on underflow - txn ApplicationArgs 2 + txn ApplicationArgs 1 btoi - app_local_put @@ -151,11 +150,10 @@ not_deposit: int axfer itxn_field TypeEnum - txn ApplicationArgs 1 - btoi + txna Assets 0 itxn_field XferAsset - txn ApplicationArgs 2 + txn ApplicationArgs 1 btoi itxn_field AssetAmount @@ -167,7 +165,7 @@ not_deposit: return not_withdraw: txn ApplicationArgs 0 - byte "close(uint64):void" + byte "close():void" == bz not_close @@ -176,8 +174,7 @@ not_withdraw: int axfer itxn_field TypeEnum - txn ApplicationArgs 1 - btoi + txna Assets 0 itxn_field XferAsset int 0 @@ -203,18 +200,17 @@ not_close: int axfer itxn_field TypeEnum - txn ApplicationArgs 1 - btoi + txna Assets 0 itxn_field XferAsset - txn ApplicationArgs 2 + txn ApplicationArgs 1 btoi itxn_field AssetAmount txn Sender itxn_field AssetSender - txn ApplicationArgs 3 + txna Accounts 1 itxn_field AssetReceiver itxn_submit @@ -223,6 +219,95 @@ not_close: return not_transfer: + txn ApplicationArgs 0 + byte "create(uint64):void" + == + bz not_create + // Handle create. + itxn_begin + int acfg + itxn_field TypeEnum + + txn ApplicationArgs 1 + btoi + itxn_field ConfigAssetTotal + int 0 + itxn_field ConfigAssetDecimals + byte "x" + itxn_field ConfigAssetUnitName + byte "X" + itxn_field ConfigAssetName + global CurrentApplicationAddress + itxn_field ConfigAssetFreeze + + itxn_submit + int 1 + return +not_create: + txn ApplicationArgs 0 + byte "mint():void" + == + bz not_mint + // Handle mint. Next txn slot must pay our app account + txn GroupIndex + int 1 + + + dup + dup + + gtxns TypeEnum + int pay + == + assert + + gtxns Receiver + global CurrentApplicationAddress + == + assert + + // mint asset + itxn_begin + int axfer + itxn_field TypeEnum + + txna Assets 0 + itxn_field XferAsset + + gtxns Amount + itxn_field AssetAmount + + txn Sender + itxn_field AssetReceiver + itxn_submit + + int 1 + return +not_mint: + txn ApplicationArgs 0 + byte "freeze(uint64):void" + == + bz not_freeze + + //Handle freeze + itxn_begin + int afrz + itxn_field TypeEnum + + txna Assets 0 + itxn_field FreezeAsset + + txn ApplicationArgs 1 + btoi + itxn_field FreezeAssetFrozen + + txn Sender + itxn_field FreezeAssetAccount + + itxn_submit + + int 1 + return +not_freeze: // Unknown call "method" err From ce9c7bfce14351b690e08a74175831d172c270b0 Mon Sep 17 00:00:00 2001 From: "shiqi.zheng@algorand.com" Date: Fri, 24 Sep 2021 10:47:22 -0400 Subject: [PATCH 03/10] fix failing test --- test/scripts/e2e_subs/app-assets.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/scripts/e2e_subs/app-assets.sh b/test/scripts/e2e_subs/app-assets.sh index b1cdf3685e..aaa1dedc86 100755 --- a/test/scripts/e2e_subs/app-assets.sh +++ b/test/scripts/e2e_subs/app-assets.sh @@ -224,10 +224,10 @@ IDs=" $ASSETID \n $ASSETID3 \n " -[[ "$(assets "$APPACCT")" = "$IDs" ]] # account has 2 assets +[[ "$(asset_ids "$APPACCT")" = "$IDs" ]] # account has 2 assets appl "close():void" --foreign-asset="$ASSETID" --from="$SMALL" appl "close():void" --foreign-asset="$ASSETID3" --from="$SMALL" -[[ "$(assets "$APPACCT")" = "" ]] # account has no assets +[[ "$(asset_ids "$APPACCT")" = "" ]] # account has no assets # app creates asset appl "create(uint64):void" --app-arg="int:1000000" --from="$SMALL" @@ -249,7 +249,7 @@ $ASSETID2 \n $ASSETID3 \n $APPASSETID " -[[ "$(assets "$SMALL")" = "$IDs" ]] # has new asset +[[ "$(asset_ids "$SMALL")" = "$IDs" ]] # has new asset [[ "$(asset_bal "$SMALL")" = "999100\n1000000\n1000000\n1000" ]] # correct balances [ "$(asset_bal "$APPACCT")" = 999000 ] # 1k sent From 4363f0976686ed081e8b6ca7da15bf140c052974 Mon Sep 17 00:00:00 2001 From: "shiqi.zheng@algorand.com" Date: Fri, 24 Sep 2021 11:43:33 -0400 Subject: [PATCH 04/10] fix --- test/scripts/e2e_subs/app-assets.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/scripts/e2e_subs/app-assets.sh b/test/scripts/e2e_subs/app-assets.sh index aaa1dedc86..1ad9336a09 100755 --- a/test/scripts/e2e_subs/app-assets.sh +++ b/test/scripts/e2e_subs/app-assets.sh @@ -216,7 +216,7 @@ $ASSETID \n $ASSETID2 \n $ASSETID3 \n " -[[ "$(assets "$APPACCT")" = $IDs ]] # account has 3 assets +[[ "$(asset_ids "$APPACCT")" = $IDs ]] # account has 3 assets # opt out of assets appl "close():void" --foreign-asset="$ASSETID2" --from="$SMALL" From 3ecce761c346ab54ac6bd907da7ba53eba15abfd Mon Sep 17 00:00:00 2001 From: "shiqi.zheng@algorand.com" Date: Fri, 24 Sep 2021 13:31:06 -0400 Subject: [PATCH 05/10] rm newline --- test/scripts/e2e_subs/app-assets.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/scripts/e2e_subs/app-assets.sh b/test/scripts/e2e_subs/app-assets.sh index 1ad9336a09..86bed54fa7 100755 --- a/test/scripts/e2e_subs/app-assets.sh +++ b/test/scripts/e2e_subs/app-assets.sh @@ -212,17 +212,17 @@ ASSETID3=$(asset-create 1000000 --name "beta" --unitname "b" | asset-id) appl "optin():void" --foreign-asset="$ASSETID3" --from="$SMALL" IDs=" -$ASSETID \n -$ASSETID2 \n -$ASSETID3 \n +$ASSETID +$ASSETID2 +$ASSETID3 " [[ "$(asset_ids "$APPACCT")" = $IDs ]] # account has 3 assets # opt out of assets appl "close():void" --foreign-asset="$ASSETID2" --from="$SMALL" IDs=" -$ASSETID \n -$ASSETID3 \n +$ASSETID +$ASSETID3 " [[ "$(asset_ids "$APPACCT")" = "$IDs" ]] # account has 2 assets appl "close():void" --foreign-asset="$ASSETID" --from="$SMALL" @@ -244,9 +244,9 @@ sign group ${gcmd} clerk rawsend -f "$T/group.stx" IDs=" -$ASSETID \n -$ASSETID2 \n -$ASSETID3 \n +$ASSETID +$ASSETID2 +$ASSETID3 $APPASSETID " [[ "$(asset_ids "$SMALL")" = "$IDs" ]] # has new asset From 96883ef5d0f8a28bfa4d5da24e3640e6844df1ae Mon Sep 17 00:00:00 2001 From: "shiqi.zheng@algorand.com" Date: Fri, 24 Sep 2021 15:37:32 -0400 Subject: [PATCH 06/10] maybe fixes the failing test --- test/scripts/e2e_subs/app-assets.sh | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/scripts/e2e_subs/app-assets.sh b/test/scripts/e2e_subs/app-assets.sh index 86bed54fa7..11524ce20a 100755 --- a/test/scripts/e2e_subs/app-assets.sh +++ b/test/scripts/e2e_subs/app-assets.sh @@ -211,8 +211,7 @@ appl "optin():void" --foreign-asset="$ASSETID2" --from="$SMALL" ASSETID3=$(asset-create 1000000 --name "beta" --unitname "b" | asset-id) appl "optin():void" --foreign-asset="$ASSETID3" --from="$SMALL" -IDs=" -$ASSETID +IDs="$ASSETID $ASSETID2 $ASSETID3 " @@ -220,8 +219,7 @@ $ASSETID3 # opt out of assets appl "close():void" --foreign-asset="$ASSETID2" --from="$SMALL" -IDs=" -$ASSETID +IDs="$ASSETID $ASSETID3 " [[ "$(asset_ids "$APPACCT")" = "$IDs" ]] # account has 2 assets @@ -243,8 +241,7 @@ cat "$T/mint.tx" "$T/pay1.tx" | ${gcmd} clerk group -i - -o "$T/group.tx" sign group ${gcmd} clerk rawsend -f "$T/group.stx" -IDs=" -$ASSETID +IDs="$ASSETID $ASSETID2 $ASSETID3 $APPASSETID From f738005e2351c51efe717a395e96b3005e62c574 Mon Sep 17 00:00:00 2001 From: "shiqi.zheng@algorand.com" Date: Mon, 27 Sep 2021 09:20:53 -0400 Subject: [PATCH 07/10] syntax fix --- test/scripts/e2e_subs/app-assets.sh | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/test/scripts/e2e_subs/app-assets.sh b/test/scripts/e2e_subs/app-assets.sh index 11524ce20a..5617173340 100755 --- a/test/scripts/e2e_subs/app-assets.sh +++ b/test/scripts/e2e_subs/app-assets.sh @@ -213,16 +213,14 @@ appl "optin():void" --foreign-asset="$ASSETID3" --from="$SMALL" IDs="$ASSETID $ASSETID2 -$ASSETID3 -" +$ASSETID3" [[ "$(asset_ids "$APPACCT")" = $IDs ]] # account has 3 assets # opt out of assets appl "close():void" --foreign-asset="$ASSETID2" --from="$SMALL" IDs="$ASSETID -$ASSETID3 -" -[[ "$(asset_ids "$APPACCT")" = "$IDs" ]] # account has 2 assets +$ASSETID3" +[[ "$(asset_ids "$APPACCT")" = $IDs ]] # account has 2 assets appl "close():void" --foreign-asset="$ASSETID" --from="$SMALL" appl "close():void" --foreign-asset="$ASSETID3" --from="$SMALL" [[ "$(asset_ids "$APPACCT")" = "" ]] # account has no assets @@ -244,10 +242,9 @@ ${gcmd} clerk rawsend -f "$T/group.stx" IDs="$ASSETID $ASSETID2 $ASSETID3 -$APPASSETID -" -[[ "$(asset_ids "$SMALL")" = "$IDs" ]] # has new asset -[[ "$(asset_bal "$SMALL")" = "999100\n1000000\n1000000\n1000" ]] # correct balances +$APPASSETID" +[[ "$(asset_ids "$SMALL")" = $IDs ]] # has new asset +[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = 1000 ] # correct balances [ "$(asset_bal "$APPACCT")" = 999000 ] # 1k sent # freeze asset @@ -265,6 +262,6 @@ payin 1000 -o "$T/pay1.tx" cat "$T/mint.tx" "$T/pay1.tx" | ${gcmd} clerk group -i - -o "$T/group.tx" sign group ${gcmd} clerk rawsend -f "$T/group.stx" -[[ "$(asset_bal "$SMALL")" = "999100\n1000000\n1000000\n2000" ]] # minted 1000 +[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = 2000 ] # minted 1000 date "+${scriptname} OK %Y%m%d_%H%M%S" From 87aa418a10461eadc29f27d04750bff9b506193f Mon Sep 17 00:00:00 2001 From: "shiqi.zheng@algorand.com" Date: Mon, 27 Sep 2021 09:34:50 -0400 Subject: [PATCH 08/10] update strings --- test/scripts/e2e_subs/app-assets.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/scripts/e2e_subs/app-assets.sh b/test/scripts/e2e_subs/app-assets.sh index 5617173340..e487f09c9b 100755 --- a/test/scripts/e2e_subs/app-assets.sh +++ b/test/scripts/e2e_subs/app-assets.sh @@ -244,7 +244,7 @@ $ASSETID2 $ASSETID3 $APPASSETID" [[ "$(asset_ids "$SMALL")" = $IDs ]] # has new asset -[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = 1000 ] # correct balances +[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = "1000 " ] # correct balances [ "$(asset_bal "$APPACCT")" = 999000 ] # 1k sent # freeze asset @@ -262,6 +262,6 @@ payin 1000 -o "$T/pay1.tx" cat "$T/mint.tx" "$T/pay1.tx" | ${gcmd} clerk group -i - -o "$T/group.tx" sign group ${gcmd} clerk rawsend -f "$T/group.stx" -[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = 2000 ] # minted 1000 +[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = " 2000" ] # minted 1000 date "+${scriptname} OK %Y%m%d_%H%M%S" From ed063794663fe7760189865065081d660e94ea55 Mon Sep 17 00:00:00 2001 From: "shiqi.zheng@algorand.com" Date: Mon, 27 Sep 2021 10:58:35 -0400 Subject: [PATCH 09/10] fix string match --- test/scripts/e2e_subs/app-assets.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/scripts/e2e_subs/app-assets.sh b/test/scripts/e2e_subs/app-assets.sh index e487f09c9b..a878d869a5 100755 --- a/test/scripts/e2e_subs/app-assets.sh +++ b/test/scripts/e2e_subs/app-assets.sh @@ -244,7 +244,7 @@ $ASSETID2 $ASSETID3 $APPASSETID" [[ "$(asset_ids "$SMALL")" = $IDs ]] # has new asset -[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = "1000 " ] # correct balances +[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = " 1000" ] # correct balances [ "$(asset_bal "$APPACCT")" = 999000 ] # 1k sent # freeze asset From b598a1a780c69d31d6f676394cbaaeeb6a078a36 Mon Sep 17 00:00:00 2001 From: "shiqi.zheng@algorand.com" Date: Mon, 27 Sep 2021 11:50:05 -0400 Subject: [PATCH 10/10] fix failing tests --- test/scripts/e2e_subs/app-assets.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/scripts/e2e_subs/app-assets.sh b/test/scripts/e2e_subs/app-assets.sh index a878d869a5..65fd5e9270 100755 --- a/test/scripts/e2e_subs/app-assets.sh +++ b/test/scripts/e2e_subs/app-assets.sh @@ -244,7 +244,7 @@ $ASSETID2 $ASSETID3 $APPASSETID" [[ "$(asset_ids "$SMALL")" = $IDs ]] # has new asset -[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = " 1000" ] # correct balances +[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = 1000 ] # correct balances [ "$(asset_bal "$APPACCT")" = 999000 ] # 1k sent # freeze asset @@ -262,6 +262,6 @@ payin 1000 -o "$T/pay1.tx" cat "$T/mint.tx" "$T/pay1.tx" | ${gcmd} clerk group -i - -o "$T/group.tx" sign group ${gcmd} clerk rawsend -f "$T/group.stx" -[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = " 2000" ] # minted 1000 +[ "$(asset_bal "$SMALL" | awk 'FNR==4{print $0}')" = 2000 ] # minted 1000 date "+${scriptname} OK %Y%m%d_%H%M%S"