How do we pass the string as an application argument? (Algorand, JS SDK)

214 Views Asked by At

I'm using the Algorand JS SDK and do not know how to pass string as an application args. I tried multiple methods but did not get any positive result.

1

There are 1 best solutions below

1
Fabrice On

You can see examples there: https://developer.algorand.org/docs/get-started/dapps/pyteal/#deploy-and-communicate-with-the-smart-contract

See in particular, this code which passes the string "setup" as an application argument.

    setupTxn = transaction.ApplicationCallTxn(
        sender=funder.getAddress(),
        index=appID,
        on_complete=transaction.OnComplete.NoOpOC,
        app_args=[b"setup"],
        foreign_assets=[nftID],
        sp=suggestedParams,
    )

One important point is that the string must be encoded into bytes, hence the b at the beginning of b"setup". If the string is a Python string object, you need to use the .encode() method. See https://docs.python.org/3/howto/unicode.html#converting-to-bytes for details.

Note also that nowadays, it is strongly recommended to create ABI-compatible smart contracts. In that, case the best way to call a smart contract is to use the Atomic Transaction Composer: https://developer.algorand.org/docs/get-details/atc/ rather than directly creating an ApplicationCallTxn object.

The same comment regarding the conversion of string to bytes still applies however.