简体   繁体   English

如何查看智能合约的 tr balance 是什么?

[英]How do I see what is tr balance of a smart contract?

If I send some ether to a samrt contract address, what function can I use to check the total balance of the smart contract?如果我向智能合约地址发送一些以太币,我可以使用什么功能来检查智能合约的总余额?

To send ether to smart other wallet address from smart contract, is there any special function in solidity or mere send/transafer function works?要从智能合约向智能其他钱包地址发送以太币,solidity 是否有任何特殊功能或仅发送/转移功能有效?

A contract accepting ETH needs to implement either the receive() or the fallback() special functions .接受 ETH 的合约需要实现receive()fallback() 特殊功能

Its current balance is returned in property .balance of address(this) , so you can wrap it in a function to retrieve the balance.它的当前余额在address(this)的属性.balance中返回,因此您可以将其包装在一个函数中以检索余额。

pragma solidity ^0.8;

contract MyContract {
    receive() external payable {}

    function getBalance() external view returns (uint256) {
        return address(this).balance;
    }
}

First question answer:第一个问题答案:

  • To check the balance of an Smart Contract from another Smart contract you can do contractAddress.balance要从另一个智能合约检查智能合约的余额,您可以执行contractAddress.balance
  • To check directly off-chain you can query an RPC Node directly, or you can use a library like web3 to wrap this call after connecting to a Provider (the Node): web3.eth.getBalance(accounts[0])要直接在链下检查,您可以直接查询 RPC 节点,或者您可以在连接到提供者(节点)后使用 web3 之类的库来包装此调用: web3.eth.getBalance(accounts[0])

Second question answer:第二题答案:

You have multiple ways for transferring balance from a Smart Contract to Another (I'm going to list 5 of them):您有多种方法可以将余额从智能合约转移到另一个(我将列出其中的 5 种):

  • Using transfer function.使用传递函数。 Ex: contractAddress.transfer(1 ethers)例如: contractAddress.transfer(1 ethers)
  • Using send function.使用发送功能。 Ex: contractAddress.send(1 ethers)例如: contractAddress.send(1 ethers)
  • Using call function.使用调用功能。 Ex: contractAddress.call{value: msg.value}("");例如: contractAddress.call{value: msg.value}("");

To check the differences among this methods, check this article .要检查这些方法之间的差异, 请查看这篇文章

This first 3 approaches required that, if the receiver is an Smart Contract, this needs to implement the receive() function, or the fallback() function, both explicitly payable.前 3 种方法要求,如果接收方是智能合约,则需要实现receive()函数或fallback()函数,两者都是明确支付的。

The recommended approach is using call (not transfer or send).推荐的方法是使用呼叫(不是转移或发送)。 To know why check why Access List feature was added in a previous network fork.要知道为什么要检查为什么在以前的网络分叉中添加了访问列表功能。

  • You can also send balance from one contract to other account by using the SELFDESTRUCT opcode.您还可以使用 SELFDESTRUCT 操作码将余额从一个合约发送到另一个账户。
  • And also is important to know that you can transfer balance to an Smart Contract before its creation, because Smart Contract addresses are deterministic over the address and the nonce of the deployer.同样重要的是要知道,您可以在创建智能合约之前将余额转移到智能合约,因为智能合约地址对于地址和部署者的随机数是确定性的。

This two last considerations are really important, because an Smart Contract can receive funds even without implement receive() or fallback() functions.最后两个考虑因素非常重要,因为即使没有实现receive()fallback()函数,智能合约也可以接收资金。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在以太坊中查看合同日志事件? - How do I see the contract log events in Ethereum? 在我的智能合约中创建函数时,我如何知道要指定多少数量进行测试? - When creating a function in my smart contract, how can I know what amount to specify for testing? 在以太坊区块链中,智能合约可以拥有代币余额吗 - In Ethereum blockchain does smart contract can have token balance 如何自动执行智能合约代码? - How can I automate Smart Contract code execution? 我如何在智能合约中从 pancakeswap 获取 BNB 价格? - how i can fetch BNB price from pancakeswap in smart contract? 分叉智能合约实际上意味着什么? 如何分叉 Sushiswap 的 Sushibar 智能合约并实现 Staking 功能? - What is forking a smart contract actually mean? How to fork Sushiswap's Sushibar smart conract and implement staking features? 如何使用Quorum进行智能合约存储迁移? - How to do the Smart Contract Storage Migration using Quorum? 如何将智能合约与Java应用程序集成? - How can i integrate smart contract with java application? 如何在智能合约中将 Struct 转换为 Array? - How can I convert a Struct to Array in smart contract? 如何将我的智能合约与另一个已部署的智能合约连接起来? - How to connect my smart contract with another deployed smart contract?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM