Telegram Group & Telegram Channel
Laisky's Notes
最近都在学习 TON 开发,积累了一些经验。TON 有很多独特的设计和开发范式,值得先记个小笔记,主要以 Ethereum/EVM 作对比。这是基于我目前的理解,不一定正确,有兴趣欢迎讨论。 1. 所有的调用都是消息,所有的消息都是异步的。也就是说,你没法同步取得跨合约调用的结果。 2. 每次调用的费用是非确定性的,而且因为整个调用是异步的,没法预知调用的总费用。所以只能先给一个相对较高的手续费,然后等合约执行完后退还余款。TON 的费用的精确控制非常困难,我花了很多时间在这上面,这个以后可以单独讲一…
终于学会相对精确的控制 TON 的 value flow 了,先记个小笔记防止忘了。这也只是我目前的理解,不一定正确,欢迎讨论或指正。

有三个主要费用 network、computing、action。network 包含消息发送的费用,一般由 sender 付,钱包里一般消息的 value 和 network fee 是分别支付的,而合约中默认是从消息的 value 中支付的。computing 是执行合约的费用,action 是合约发送 external message 的费用,主要就是 emit。(此外还有 storage fee 先不管了)

ctx.value 代表的是 inflow msg 携带的 value,sender 已经付过了 network fee,还没支付 computing 和 action。合约执行后会扣除 computing,执行完成后如果有需要发送的 outflow 消息,那么再扣除 action fee。

可以把 ctx.value 理解为税前收入,而且你无法知道会收多少税(computing & action fees)。

补充:FunC 里是可以精确计算 fwd_fee 的,但是感觉 tact 不太行,可参考:
* https://docs.ton.org/develop/smart-contracts/fee-calculation
* https://github.com/ton-blockchain/stablecoin-contract/blob/main/contracts/gas.fc
* https://tonviewer.com/config#24

myBalance() - ctx.value 代表合约执行前的余额。可以用 nativeReserve(myBalance() - ctx.value, ReserveExact); 来保护合约余额不被用户消息所消耗。

但是你在合约代码中极难通过控制 ctx.value 来计算你能操作的 inflow 余额,因为 computing 和 action 的开销是很难计算的。比如如果你想要把剩余的 inflow value 全部转走,可行的办法是用 nativeReserve 锁定不想转走的余额,然后再用 SendRemainingBalance mode 发送一条 value 为 0 的消息。

SendRemainingBalance 会转走未被 nativeReserve 锁定的全部金额,按照上文描述,也就是所有剩余的 ctx.value注意 send 的 value 并不会支付 action fee,而仅会包含 network fee。也就是说,当你在代码中 send 一个消息,这个消息的 network fee 会从 send 的 value 中扣除,但是 action fee 会从合约余额中扣除。这其中有一个坑,emit 的费用是在 action 阶段计算的,如果你的代码中使用了 emit,那么就需要记住在 action 阶段合约余额会被扣钱,你最好在 nativeReserve 的锁定中包含 emit 的费用。
经过多次测试,nativeReserve 锁定余额后,仅有 storage_fee 是从 balance 扣除的,其他都是从 send 的 value 里扣除的。

但是 emit 具体花多少钱?我还不知道怎么算,目前是估计一个经验值,测试网是 0.007,你可以多锁定 0.01 给 emit。 #blockchain

Ps. 我真是行业冥灯,搞啥啥死

prev: https://www.tg-me.com/us/telegram/com.laiskynotes/318
next: https://www.tg-me.com/us/telegram/com.laiskynotes/324



tg-me.com/laiskynotes/319
Create:
Last Update:

终于学会相对精确的控制 TON 的 value flow 了,先记个小笔记防止忘了。这也只是我目前的理解,不一定正确,欢迎讨论或指正。

有三个主要费用 network、computing、action。network 包含消息发送的费用,一般由 sender 付,钱包里一般消息的 value 和 network fee 是分别支付的,而合约中默认是从消息的 value 中支付的。computing 是执行合约的费用,action 是合约发送 external message 的费用,主要就是 emit。(此外还有 storage fee 先不管了)

ctx.value 代表的是 inflow msg 携带的 value,sender 已经付过了 network fee,还没支付 computing 和 action。合约执行后会扣除 computing,执行完成后如果有需要发送的 outflow 消息,那么再扣除 action fee。

可以把 ctx.value 理解为税前收入,而且你无法知道会收多少税(computing & action fees)。

补充:FunC 里是可以精确计算 fwd_fee 的,但是感觉 tact 不太行,可参考:
* https://docs.ton.org/develop/smart-contracts/fee-calculation
* https://github.com/ton-blockchain/stablecoin-contract/blob/main/contracts/gas.fc
* https://tonviewer.com/config#24

myBalance() - ctx.value 代表合约执行前的余额。可以用 nativeReserve(myBalance() - ctx.value, ReserveExact); 来保护合约余额不被用户消息所消耗。

但是你在合约代码中极难通过控制 ctx.value 来计算你能操作的 inflow 余额,因为 computing 和 action 的开销是很难计算的。比如如果你想要把剩余的 inflow value 全部转走,可行的办法是用 nativeReserve 锁定不想转走的余额,然后再用 SendRemainingBalance mode 发送一条 value 为 0 的消息。

SendRemainingBalance 会转走未被 nativeReserve 锁定的全部金额,按照上文描述,也就是所有剩余的 ctx.value注意 send 的 value 并不会支付 action fee,而仅会包含 network fee。也就是说,当你在代码中 send 一个消息,这个消息的 network fee 会从 send 的 value 中扣除,但是 action fee 会从合约余额中扣除。这其中有一个坑,emit 的费用是在 action 阶段计算的,如果你的代码中使用了 emit,那么就需要记住在 action 阶段合约余额会被扣钱,你最好在 nativeReserve 的锁定中包含 emit 的费用。
经过多次测试,nativeReserve 锁定余额后,仅有 storage_fee 是从 balance 扣除的,其他都是从 send 的 value 里扣除的。

但是 emit 具体花多少钱?我还不知道怎么算,目前是估计一个经验值,测试网是 0.007,你可以多锁定 0.01 给 emit。 #blockchain

Ps. 我真是行业冥灯,搞啥啥死

prev: https://www.tg-me.com/us/telegram/com.laiskynotes/318
next: https://www.tg-me.com/us/telegram/com.laiskynotes/324

BY Laisky's Notes





Share with your friend now:
tg-me.com/laiskynotes/319

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Should You Buy Bitcoin?

In general, many financial experts support their clients’ desire to buy cryptocurrency, but they don’t recommend it unless clients express interest. “The biggest concern for us is if someone wants to invest in crypto and the investment they choose doesn’t do well, and then all of a sudden they can’t send their kids to college,” says Ian Harvey, a certified financial planner (CFP) in New York City. “Then it wasn’t worth the risk.” The speculative nature of cryptocurrency leads some planners to recommend it for clients’ “side” investments. “Some call it a Vegas account,” says Scott Hammel, a CFP in Dallas. “Let’s keep this away from our real long-term perspective, make sure it doesn’t become too large a portion of your portfolio.” In a very real sense, Bitcoin is like a single stock, and advisors wouldn’t recommend putting a sizable part of your portfolio into any one company. At most, planners suggest putting no more than 1% to 10% into Bitcoin if you’re passionate about it. “If it was one stock, you would never allocate any significant portion of your portfolio to it,” Hammel says.

What is Telegram?

Telegram is a cloud-based instant messaging service that has been making rounds as a popular option for those who wish to keep their messages secure. Telegram boasts a collection of different features, but it’s best known for its ability to secure messages and media by encrypting them during transit; this prevents third-parties from snooping on messages easily. Let’s take a look at what Telegram can do and why you might want to use it.

telegram from us


Telegram Laisky's Notes
FROM USA