tact-lang / tact-docs

Tact main documentation
https://docs.tact-lang.org
54 stars 41 forks source link

Fix counter example on landing page to return excess TONs to contract caller #231

Closed anton-trunov closed 4 months ago

anton-trunov commented 4 months ago

Let's say the deployer sends 0.05 TONs and calls a simple receiver. They should get the excess back

novusnota commented 4 months ago

This would also have to be updated in ton-org/blueprint, as this example is taken from there and it is what users get when they run npm create ton -- simple-counter --type tact-counter --contractName SimpleCounter.

novusnota commented 4 months ago

We may change its receiver function from this:

receive(msg: Add) {
    self.counter += msg.amount;
}

To this:

receive(msg: Add) {
    self.counter += msg.amount;
    self.reply(null); // reply with excessive amount of Toncoin
    // or
    self.notify(null); // does the same, but would not bounce
                       // it is used in Deployable trait to send `DeployOk` with excessive funds back
}

But other than that, the deploy seems to be giving the excess back already — with DeployOk message provided by the @stdlib/deploy library and Deployable trait. It's just that they're using self.notify() function under the hood, which isn't bounceable and, thus, won't return funds in case of failed deployments.

Oh, and if users aren't using Deployable trait for deployments, then it won't be giving any excess values unless explicitly made to. But as we're using Deployable nearly everywhere that shouldn't be a frequent issue, if issue at all.