如何存入墨水!智能合约
我对智能合约有一些想法,我将使用 Ink 来实现它们!在基板上。
大多数这些想法都涉及调用者将存款存入智能合约,该合约将无限期持有,然后调用者可能能够在未来的某个时间点提取,具体取决于其他因素。
我找到了一个允许调用者退出智能合约的示例:
https://github.com/paritytech/ink/blob/master/examples/contract-transfer/lib.rs
这暗示了调用者进行存款的一种方法 - self.env().transferred_value() 方法建议调用者可以/已经发送/发送值。
我正在努力寻找一个将资金存入智能合约的示例。也许我在这里遗漏了一些基本的东西?
理想情况下,我想避免实现一个需要 2 个地址并从一个地址转移到另一个地址的函数(合约已经拥有并知道自己的地址!),而是有利于调用者发送金额,并将其存入智能合约中。
我认为这可能通过不带参数的智能合约方法实现,但不确定且完全不清楚合约将如何接收和接收参数。持有资金。
寻找一个具体的代码示例来展示它是如何完整工作的,但也感谢任何评论来澄清或纠正我的(很可能是不正确的)理解。
提前致谢!
I have a few ideas for smart contracts that I will be implementing using Ink! on Substrate.
Most of these ideas involve callers making deposits into the smart contract which will be held indefinitely, and then the caller may be able to withdraw at some future point, depending on other factors.
I have found an example that allows callers to withdraw from a smart contract:
https://github.com/paritytech/ink/blob/master/examples/contract-transfer/lib.rs
This hints at an approach to the caller making a deposit - the self.env().transferred_value()
method suggests the caller can/has send/sent value.
I am struggling to find an example that deposits funds into a smart contract. Perhaps I am missing something fundamental here?
Ideally I want to avoid implementing a function that takes 2 addresses and transfers from one to the other (the contract already having & knowing its own address!), instead favouring the caller sending an amount, and it being deposited into the smart contract.
I think this may be possible with a smart contract method that takes no parameters but not certain and not at all clear on how the contract would then receive & hold the funds.
Looking for a concrete code example to show how this works in full, but also appreciate any comments to clarify or correct my (quite possibly incorrect) understanding.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
期望付款的合同方法应标有
#[ink(message, payable)]
例如 此处。在调用该方法期间,用户应指定以本机货币发送的金额(在polkadot-js
的情况下,它是应付方法的value
字段)。在该方法中,您可以使用
let amount = Self::env().transferred_value();
来获取发送的金额。如果您的交易未被拒绝,您的合约将在其余额中持有amount
代币。它的工作方式与 Solidity 中的工作方式相同。获取合约当前余额
Self::env().balance()
。发送本地货币Self::env().transfer(to, amount)
。也许检查 会很有用那个例子。
The method of your contract, that expects the payments should be marked with
#[ink(message, payable)]
for example like here. During the call of that method the user should specify that amount to send in the native currency(in the case ofpolkadot-js
it isvalue
field of payable method).In the method, you can use
let amount = Self::env().transferred_value();
to get a sent amount. If your transaction is not rejected your contract will holdamount
tokens on its balance. It works the same way as in Solidity.To get the current balance of the contract
Self::env().balance()
. To send a native currencySelf::env().transfer(to, amount)
.Maybe it will be useful to check that example.