neo-project / neo

NEO Smart Economy
MIT License
3.47k stars 1.03k forks source link

How can my SC detect if the incoming transaction has attached NEO, GAS or other NEP-5 token? (Neo 3) #2035

Closed lock9 closed 3 years ago

lock9 commented 3 years ago

Hi,

I would like to have my contract to behave differently if it is invoked with attached tokens. On Neo 2, we could view the 'attachments', but I don't think this is possible on Neo 3. How can I detect if my contract is receiving tokens? How can we mimic the ICO behavior on Neo 3?

Thanks.

roman-khimov commented 3 years ago

Maybe neo-project/proposals#108 and associated PRs? Otherwise it's about notifications, either System.Runtime.GetNotifications (if transaction is making a transfer and then calling your contract) or #2034 (for historic transfers).

shargon commented 3 years ago

Hi @lock9, good to see you :) You have an example here https://github.com/neo-project/neo-devpack-dotnet/blob/ff34e4244681c7e17e99f134772fe52455c890aa/templates/Template.NEP5.CSharp/NEP5.Crowdsale.cs#L26

lock9 commented 3 years ago

Hi Shargon, good to see you too.

Is it safe to use notifications only? Why the invocation counter must be 1? Can you explain it to me @shargon ?

shargon commented 3 years ago

Is it safe to use notifications only?

Yes, because we check the originator's script hash

Why the invocation counter must be 1? Can you explain it to me @shargon ?

It's mandatory because if you call again to this method you will earn twice. This prevent reentrancy.

lock9 commented 3 years ago

I'll try that then. Thanks @shargon

vncoelho commented 3 years ago

Hi @lock9, good to see you active as well.

@shargon, if the Notification comes from another contract B that dynamic invoked called A. Could the notification be forged on B and verified on A and have the properties that simulate NEO or GAS?

I believe that "onPayment" will be a better generic solution. However, the notifications are also good as well.

lllwvlvwlll commented 3 years ago

It's mandatory because if you call again to this method you will earn twice. This prevent reentrancy.

@shargon Will that scale with dynamic invokes which are nested within the second contract?

Question: I make an initial call to a contract 1 and transfer funds, then invoke a contract 2 to read the notification and mint. Within the invocation of the contract 2, it does a dynamic invoke to contract 3. Within contract 3, will invocation counter still be 1 (a global counter) or will it in increment on dynamic invokes (a local counter)?