Noob cannot deploy smart contract - Algorand
Noob cannot deploy smart contract
post by ncoin on Mar 2, 2022
Hi, I decided to create a simple web interface that uses myalgo connect to deploy smart contracts.
I get this error when deploying the contract:
Received status 400: TransactionPool.Remember: transaction XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: logic eval error: gtxn lookup TxnGroup[1] but it only has 1. Details: pc=62, opcodes=pushint 3
==
gtxn 1 Amount
I’m not sure what this error is about.
post by fabrice on Mar 2, 2022
Your smart contract expects a group of two transactions and inspect the amount of the second transactions.
See Atomic transfers - Algorand Developer Portal for group transaction / atomic transfers.
See Build with Python - Algorand Developer Portal for a tutorial on smart contracts.
post by ncoin on Mar 3, 2022
Thanks fabrice, but I have tried so many things and it hasn’t worked.
My pyteal is
from pyteal import *
def teal():
# deposit
deposit = Seq(
App.localPut(Txn.sender(), Bytes("AmountDeposited"), Gtxn[0].asset_amount()),
App.localPut(Txn.sender(), Bytes("TimeDepositedUnix"), Global.latest_timestamp()),
Return(Int(1))
)
def claim():
return Seq([
# then
Assert((App.localGet(Txn.sender(), Bytes("TimeDepositedUnix")) - Global.latest_timestamp()) % Int(86400) > Int(0)),
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.AssetTransfer,
TxnField.xfer_asset: Int(75312966),
TxnField.asset_amount: (App.localGet(Txn.sender(), Bytes("AmountDeposited")) * ((App.localGet(Txn.sender(), Bytes("TimeDepositedUnix")) - Global.latest_timestamp()) % Int(86400)) * Int(10)) / Int(10),
TxnField.sender: Global.current_application_address(),
TxnField.receiver: Txn.sender(),
TxnField.fee: Global.min_txn_fee()
}
),
InnerTxnBuilder.Submit(),
Return(Int(1))
])
def withdraw():
return Seq([
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.AssetTransfer,
TxnField.xfer_asset: Int(552767025),
TxnField.asset_amount: App.localGet(Txn.sender(), Bytes("AmountDeposited")),
TxnField.sender: Global.current_application_address(),
TxnField.receiver: Txn.sender(),
TxnField.fee: Global.min_txn_fee()
}
),
InnerTxnBuilder.Submit(),
Return(Int(1))
])
def optin():
return Seq([
Seq([
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.AssetTransfer,
TxnField.xfer_asset: Int(75313288),
TxnField.asset_amount: Int(0),
TxnField.sender: Global.current_application_address(),
TxnField.receiver: Global.current_application_address(),
TxnField.fee: Global.min_txn_fee()
}
),
InnerTxnBuilder.Submit(),
Return(Int(1))
]),
Seq([
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.AssetTransfer,
TxnField.xfer_asset: Int(75312966),
TxnField.asset_amount: Int(0),
TxnField.sender: Global.current_application_address(),
TxnField.receiver: Global.current_application_address(),
TxnField.fee: Global.min_txn_fee()
}
),
InnerTxnBuilder.Submit(),
Return(Int(1))
])
])
# Check what action is being completed
conditions = Cond(
# check if create txn
[Global.current_application_id() == Int(0), Return(Int(1))],
# is deposit transaction
[And(Global.group_size() == Int(3), Gtxn[0].asset_amount() >= Int(1000000), Gtxn[0].xfer_asset() == Int(75313288), Gtxn[1].amount() == Global.min_txn_fee()), deposit],
# is claim transaction
[And(Global.group_size() == Int(2), Gtxn[0].amount() == Global.min_txn_fee(), Gtxn[1].type_enum() == TxnType.ApplicationCall), claim()],
# is withdraw transaction
[Txn.type_enum() == TxnType.ApplicationCall, withdraw()],
# is contract asset opt-in transaction
[And(Txn.sender() == Addr("RABQ4PRRWJDCMHCCCZ24YTLTT44MI4KFZEMRWGFEYESDRBIKD6QFPNNGBQ"), Txn.note() == Bytes("OptIn")), optin()],
# check if update txn
[Txn.on_completion() == OnComplete().UpdateApplication, Return(Int(0))],
# check if optin
[Txn.on_completion() == OnComplete().OptIn, Return(Int(1))],
# check if optout
[Txn.on_completion() == OnComplete().CloseOut, Return(Int(1))],
# check if delete
[Txn.on_completion() == OnComplete().DeleteApplication, Return(Int(1))],
)
return conditions
# compile
print(compileTeal(teal(), mode=Mode.Application, version=5))
save_to_file_bool = True
def save_to_file():
with open('testnet-contract.teal', 'w') as f:
f.write(compileTeal(teal(), mode=Mode.Application, version=5))
f.close()
if save_to_file_bool == True:
save_to_file()
post by fabrice on Mar 3, 2022
In all the parts of the And are evaluated immediately, even if the first part fails.
It is like the & in C++, not like the &&.
You need to have an If…
In general, before ABI is published (which it will be published very soon), I strongly recommend having the first argument indicate the type of operation, withdraw, claim, … like in the on_call in https://developer.algorand.org/docs/get-started/dapps/pyteal/#create-auction
After the ABI is published, the ABI should be followed. Documentation is getting updated soon.
post by ncoin on Mar 4, 2022
Thanks, I’ll try that!
post by ncoin on Mar 4, 2022
Hello,
I fixed the errors regarding the transaction group, however, I’m now receiving errors about invalid asset reference 75313288 when deploying to the testnet.
What could cause this problem? It definitely exists: Algorand
post by fabrice on Mar 4, 2022
You need the asset ID to be in the foreign asset array of the transaction.
See resource availability in The Algorand Virtual Machine (AVM) and TEAL. - Algorand Developer Portal
You can see its use there:
Search for foreign_assets.