Getting red squiggles when trying to refer to a field from another template

Hi everyone,
Just trying to refer to a field from another template, from imported module. Within ensure clause:

image

Interpreter says:

“…/daml/SPTokenTrade.daml:22:12: error:
• Possible Daml-specific reason for the following type error:
Tried to access nonexistent field ‘tokenBalance’ with type ‘Decimal’ on value of type ‘SPTokenLotId’
• No instance for (HasField “tokenBalance” SPTokenLotId Decimal)
arising from a use of ‘getField’
• In the second argument of ‘(<=)’, namely
‘(getField @“tokenBalance” tokenLot)’
In the first argument of ‘(&&)’, namely
‘tokenAmount <= (getField @“tokenBalance” tokenLot)’
In the expression:
tokenAmount <= (getField @“tokenBalance” tokenLot)
&& tokenLotPrice > 0.00typecheck”

Doesn’t make much sense to me.
Can anyone please help to explain what’s wrong?

Hi @Oleg,

If the hint in the comment line is correct, you are trying to access a field of a ContractId for SPTokenLot and not the payload of SPTokenLot itself. I believe you create such a statement as tokenLot first needs to be fetched from the ledger and that you cannot do in a template level. I believe the best way would be to create a Factory template for SPTokenSupply for which in a nonconsuming choice you may check for the current token balance with an assertion (after fetching it). Keep in mind though that in the end if two SPTokenSupplies are created at the same time, a simple assertion like this may allow the creation of both as the in small amount of time between the fetch and the creation, an update to the SPTokenLot (I suppose a result of the SPTokenSupply creation) could make it reach a balance less then the other amount.

I guess if you implement the nonconsuming creation choice on the SPTokenLot template you can avoid the need for the fetch, and ensure that new SPTokenSupply is not created when the balance is less than the amount.

Hi @Mate_Varga ,
Thanks for your reply. May I ask you to tell me more about “Factory template”? I remember I faced something similar in code examples but still not familiar enough with them. Maybe any links to well written reference about them, with examples? I tried to google/chatgpt it but still doubting if I get correctly how it works…
Thank you!!

well, seems I finally help myself on my own :slight_smile: Just replaced reference to ContractID of the template (that was the alias) to the template name itself and squiggles disappeared. Just noticed similar approach within Initiate Accept pattern reference, in example, when trying to sort out my another issue :slight_smile:

1 Like

Good job @Oleg!

1 Like