Treat ALGO like an asset in the SDK to simplify a lot dApp development

The API would be far more homogeneous and simplified if the ALGO would be just an asset among others, for example with asset id = 0. It is just a special asset.
For now we need a lot of if everywhere in our dApp to know if we send ALGO or asset, to use the right function like PaymentTxn or AssetTransferTxn, or if we need to use Txn.sender or Txn.asset_sender (there are many many examples).

All those useless if would not be there with a unified API where ALGO is just a special asset.

Here is the kind of helper functions that we are forced to write as dApp developer to be agnostic to the asset sent (“true” asset or ALGO) :

def create_tx(addr_from, addr_to, asset_id, asset_qty):
    params = algod_client.suggested_params()
    if asset_id == ALGO_ASSET_ID:  # ALGO tx
        asset_qty = int(round(asset_qty * 1e6))  # API takes amount in microALGOs
        tx = algosdk.future.transaction.PaymentTxn(
            sender=addr_from, sp=params, receiver=addr_to, amt=asset_qty
        )
    else:
        tx = algosdk.future.transaction.AssetTransferTxn(
            sender=addr_from, sp=params, receiver=addr_to,
            amt=int(asset_qty), index=asset_id
        )
    return tx

Or this :

def get_condition_transfer(tx_id, asset_id, amount):
    tx = Gtxn[tx_id]
    if asset_id == 0:  # ALGO transfer
        cond = And(
            tx.type_enum() == TxnType.Payment,
            tx.amount() == Int(amount)
        )
    else:  # asset transfer
        cond = And(
            tx.type_enum() == TxnType.AssetTransfer,
            tx.asset_amount() == Int(amount),
            tx.xfer_asset() == Int(asset_id)
        )
    return cond

I think this should be part of the SDK.

This is a good point.

Note: Txn.asset_sender should actually only be used for clawback transactions.

1 Like

The reason the SDK wasn’t implementing it as a single transaction is because of the underlying protocol.

When implementing the SDKs, the primary goal was to clearly expose the core functionalities with inttoducing any higher level logic.

Fabrice gave a good example with the asset sender here.

I don’t think that what you proposed is wrong - but it does get to the gray area of weather the core level SDK should expose that, or maybe some more applicative wrapper.

1 Like