Return specific value - General - Algorand
Return specific value
post by isha on Jun 21, 2022
Hi all,
When I am trying to make application call contract function returns a value. How do I get that value in a transaction.
This is my code
contract.py
on_result = Seq(
Assert(
And(
Global.latest_timestamp() >= App.globalGet(proposal_start_time),
Global.latest_timestamp() < App.globalGet(proposal_end_time)
)
),
If(App.globalGet(yes_count) == App.globalGet(min_num_approval)).Then (
If(App.globalGet(yes_count) == App.globalGet(total_count)).Then(
Seq(
App.globalPut(vote_result,Int(1)),
)
)
.ElseIf(App.globalGet(min_per_approval) >= Btoi(Txn.application_args[2])).Then(
(App.globalPut(vote_result,Int(0))),
)
),
Return (Btoi(vote_result)),
program = Cond(
[Txn.application_args[0] == Bytes("result"),on_result],
)
)
Operations.py(transaction): Need to “vote_result” returned by the on_result call in contract.
def proposal_result(
client: AlgodClient,
appID: int,
proposal_creator: Account,
proposal_creator_private_key:str,
) -> int:
suggestedParams = client.suggested_params()
appGlobalState = getAppGlobalState(client, appID)
appAddr = get_application_address(appID)
quotient = yes_count/total_count
percentage = quotient * 100
votePercent = int(percentage)
print ("Converted: ", type(votePercent))
txn = transaction.ApplicationCallTxn(
sender=proposal_creator,
index=appID,
on_complete=transaction.OnComplete.NoOpOC,
app_args=[b"result",votePercent],
sp=suggestedParams,
)
post by fabrice on Jun 21, 2022
Applications can only return 0 to reject the transaction or a non-zero value to accept the transaction via return/Return.
This value is not recorded and just meant to indicate whether the transaction gets recorded or not.
See PyTeal Package — PyTeal documentation
If you want to actually return a value that can be used/read by other applications/the SDK, you need to use the ABI convention:
see step 5 of ARC-4: Algorand Application Binary Interface (ABI)
in other words, you need to log the value prefixed by the four bytes 151f7c75 (these should be the 4 bytes corresponding to the indicated hexadecimal and NOT the 8 bytes “151f7c75”).
post by isha on Jun 22, 2022
Thanks @fabrice this worked!