algosdk.error.AlgodHTTPError: TransactionPool.Remember: transaction R5SC7OIHV2QWMGNL3FTB5I35476ZJCALAUEAXO3QTQK7CNIHKH3A: logic eval error: assert failed pc=254
from pyteal import *
def approval_program():
on_creation = Seq([
Assert(Txn.application_args.length() == Int(2)),
App.globalPut(Bytes("assetID"), Txn.application_args[1]),
App.globalPut(Bytes("total_token_supply"), Int(0)),
App.globalPut(Bytes("algo_balance"), Int(0)),
App.localPut(Int(0), Bytes("user_token_balance"), Int(0)),
Approve()
])
is_creator = Txn.sender() == Global.creator_address()
on_closeout = Seq([
App.globalPut(
Bytes("total_token_supply"),
App.globalGet(Bytes("total_token_supply")) + Int(1)
),
Approve()
])
register = Seq([
App.localPut(Int(0), Bytes("user_token_balance"), Int(0)),
Approve()
])
asset_id = App.globalGet(Bytes("assetID"))
transfer = Seq([
Assert(App.localGet(Int(0), Bytes("user_token_balance")) == Int(0)),
Assert(App.globalGet(Bytes("total_token_supply")) > Int(0)),
Assert(Txn.amount() >= Int(1)),
App.globalPut(Bytes("total_token_supply"), App.globalGet(
Bytes("total_token_supply")) - Int(1)),
App.localPut(Int(0), Bytes("user_token_balance"), Int(1)),
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields({
TxnField.type_enum: TxnType.AssetTransfer,
TxnField.xfer_asset: asset_id,
TxnField.asset_amount: Int(1),
TxnField.asset_receiver: Txn.sender(),
}),
InnerTxnBuilder.Submit(),
Approve(),
])
withdraw_amount = Btoi(Txn.application_args[1])
handle_algo_withdrawal = Seq([
Assert(is_creator),
Assert(withdraw_amount <= App.globalGet(Bytes("algo_balance"))),
App.globalPut(Bytes("algo_balance"), App.globalGet(
Bytes("algo_balance")) - withdraw_amount),
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields({
TxnField.type_enum: TxnType.Payment,
TxnField.amount: withdraw_amount,
TxnField.receiver: Global.creator_address(),
}),
InnerTxnBuilder.Submit(),
Approve(),
])
program = Cond(
[Txn.application_id() == Int(0), on_creation],
[Txn.on_completion() == OnComplete.OptIn, register],
[Txn.on_completion() == OnComplete.CloseOut, on_closeout],
[Txn.on_completion() == OnComplete.DeleteApplication, Return(is_creator)],
[Txn.on_completion() == OnComplete.UpdateApplication, Return(is_creator)],
[And(Txn.on_completion() == OnComplete.NoOp,
Txn.application_args[0] == Bytes("transfer")), transfer],
[And(Txn.on_completion() == OnComplete.NoOp, Txn.application_args[0] == Bytes(
"withdrawl")), handle_algo_withdrawal],
)
return program
def clear_state_program():
return Approve()
if __name__ == "__main__":
with open("test_approval.teal", "w") as f:
compiled = compileTeal(
approval_program(), mode=Mode.Application, version=5)
f.write(compiled)
with open("test_clear_state.teal", "w") as f:
compiled = compileTeal(clear_state_program(),
mode=Mode.Application, version=5)
f.write(compiled)
def compile_program(client, source_code):
compile_response = client.compile(source_code)
return base64.b64decode(compile_response['result'])
local_ints = 1
local_bytes = 0
global_ints = 3
global_bytes = 0
global_schema = transaction.StateSchema(global_ints, global_bytes)
local_schema = transaction.StateSchema(local_ints, local_bytes)
f = open("./ASC/test_approval.teal", "r")
approval_program_teal = f.read()
f = open("./ASC/test_clear_state.teal", "r")
clear_state_program_teal = f.read()
approval_program_compiled = compile_program(
algod_client, approval_program_teal)
clear_state_program_compiled = compile_program(
algod_client, clear_state_program_teal)