Pass address as argument to TEAL

Hello! I need to check USDT account balance in TEAL code. I’m passing the address as an argument to the TEAL application. However, I can not figure out how I can convert it from bytes back to the address in TEAL? Thank you!

txna ApplicationArgs 1 // address
txna ApplicationArgs 2 //asset id
btoi
asset_holding_get AssetBalance
assert

You need ApplicationArgs 1 to be the 32-byte raw address, not the 58-character string looking like DXTFUT4EGE76MJ2Z55ER5WD67PI7SJ3UZPK4BIFU546BDICNVXJ6OBNZ3Y.

For conversion between these two formats, see Encoding and Decoding - Algorand Developer Portal
If you call from the JS SDK, you need to call your smart contract with pk computed as:

const address = 'DXTFUT4EGE76MJ2Z55ER5WD67PI7SJ3UZPK4BIFU546BDICNVXJ6OBNZ3Y';
const pk = algosdk.decodeAddress(address);

and not with address.

There isn’t an easy way to convert between these two formats inside the smart contract in TEAL. I always recommend TEAL smart contracts to only deal with 32-bit raw addresses (aka pk above), and have the SDK do the conversions.

In general, I recommend using beaker for new smart contracts. Beaker also allows you to directly create an ABI-compliant smart contract that can easily be called from other smart contracts and from the SDK. You don’t need to worry about encoding when using beaker. For example, see beaker/examples/boxen/demo.py at fb76617ea49ed26dfa98b4dbda98649731c09bdc · algorand-devrel/beaker · GitHub

1 Like