pyteal.TealCompileError: Scratch slot load occurs before store

Hi I’ve alredy read this post How to access asset URL in PyTeal? - #4 by fabrice but I don’t get it. I’m gettiing the error with this condition
AssetParam.reserve(Txn.assets[0]).value() == Txn.sender()

I don’t really know what the scratch space should needed for or how to check that condition. Can sombody help me?

Hi @aleve99 ,

AssetParam returns a MaybeValue, so make sure to compute it in the Seq before trying to access it. You can not call value() just-in-time.

As per AssetParam example:

assetTotal = AssetParam.total(Txn.assets[0])

program = Seq([
    assetTotal,
    Assert(assetTotal.hasValue()),
    assetTotal.value()
])
1 Like

Okay thanks I got it. Only a question. If I uses a cond statement to choose what function use, then inside that function i use the Assert(AssetParam.reserve(Txn.assets[0]).value() == Txn.sender()) so now I have done this:
`assetReserve = AssetParam.reserve(Txn.assets[0])

reserve = Seq([
    assetReserve,
    Assert(assetReserve.hasValue()),
    assetReserve.value()
])`

That is putted outside the function (I think that this is the only way to put it, right?). But my question is if I don’t pass any foreign assets to the application, what will happen? Or are the assignments used only when the main one is called.
my final verision is something like this:
Assert(reserve == Txn.sender())

Is it right or exists a better way to handle it?

If you are using Cond as a way to dispatch your logic then you will have to execute a Seq for each branch of the Cond:

Cond(
    [cond_expr_1, Seq(
        assetReserve,
        Assert(assetReserve.hasValue()),
        Assert(assetReserve.value() == Txn.sender()),
        ... ,
    )],
    [cond_expr_2, Seq(
        assetReserve,
        Assert(assetReserve.hasValue()),
        Assert(assetReserve.value() == Txn.sender()),
        ... ,
    )],
    ... ,
)

AssetParam will fail if you don’t reference the AssetID into the ForeignAssets array.

Maybe using a @Subroutine can avoid repetitions and make your code more modular.

Also: with latest version of PyTeal you can avoid square brackets in Seq().

1 Like