简体   繁体   English

以太坊。 有没有办法知道我的智能合约需要的存储空间?

[英]Ethereum. Is there a way to know the storage space needed by my smart contract?

How can I quantify how much storage space (approximately) do I need for Ethereum transactions?如何量化以太坊交易需要多少存储空间(大约)? Is it per function/ transaction or how is it calculated?是按功能/交易计算还是如何计算? any leads on this is appreciated.对此的任何线索表示赞赏。 I am trying to quantify the storage space needed for the transactions created by my smart contract...我正在尝试量化我的智能合约创建的交易所需的存储空间......

How to calculate the data field size如何计算data字段大小

(when you're sending a transaction calling a smart contract function) (当您发送调用智能合约功能的交易时)

The data field value could be divided into three parts: data字段值可以分为三部分:

  • 0x
  • 4 bytes (8 hex characters) - This is the function signature, it identifies the function that you are going to call. 4 个字节(8 个十六进制字符)- 这是 function 签名,它标识您要调用的 function。
    • The value is first 4 bytes of keccak256 hash of the function name and argument types.该值是 function 名称和参数类型的 keccak256 hash 的前 4 个字节。 Example: transfer(address,uint256) => a9059cbb示例: transfer(address,uint256) => a9059cbb
  • the rest of data - Arguments passed into the function.数据的 rest - Arguments 传入 function。

The easiest way is to use a library that calculates the size for you.最简单的方法是使用为您计算大小的库。 :) For example web3js function encodeFunctionCall() . :) 例如web3js function encodeFunctionCall()

Example:例子:

const result = web3.eth.abi.encodeFunctionCall({
    name: 'transfer',
    type: 'function',
    inputs: [{
        type: 'address',
        name: ''
    },{
        type: 'uint256',
        name: ''
    }]
}, ['0x1231231231231231231231231231231231231231', '1']);
console.log(result);

Returns退货

0xa9059cbb00000000000000000000000012312312312312312312312312312312312312310000000000000000000000000000000000000000000000000000000000000001
  • a9059cbb is the function signature a9059cbb是 function 签名
  • 0000000000000000000000001231231231231231231231231231231231231231 is the address type (256bit, 64 hex chars) first argument 0000000000000000000000001231231231231231231231231231231231231231address类型(256位,64个十六进制字符)第一个参数
  • 0000000000000000000000000000000000000000000000000000000000000001 is the uint256 type (256bit, 64 hex chars) second argument 0000000000000000000000000000000000000000000000000000000000000001uint256类型(256位,64个十六进制字符)第二个参数

So the result is 0x and 136 hex characters, which makes 68 bytes.所以结果是0x和 136 个十六进制字符,即 68 个字节。

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

相关问题 我的后端可以调用智能合约 function 在某个日期执行吗? - Can my backend call a smart contract function to be executed on a certain date? 以太坊合约所有者可以支付交易的gas吗? - Can ethereum contract owner pay the gas of a transaction? 智能合约和交易有什么区别? - What is the difference between Smart Contract and Transaction? 智能合约的 State 已更新,但 etherscan 上未列出交易 - State of a smart contract updated, but no transaction listed on etherscan Eth,如何从智能合约中调用存款 function - Eth, how to call a deposit function from a smart contract 如何在智能合约中使用不同的代币进行批量交易? - how to do a bulk transaction with different tokens in a smart contract? 如何从solidity智能合约中发送带有编码数据的交易 - how to send a transaction with encoded data from within a solidity smart contract 以太坊交易(调用合约函数)是否会因 assert()/require()/revert() 或 gas 问题以外的原因而失败? - Can Ethereum transaction (calling contract function) fail for reasons other than assert()/require()/revert() or gas issues? NFT 智能合约,随着时间的推移不断添加 NFT。 每次添加都一定要花费gas吗? - NFT smart contract where NFTs keep being added over time. Does every addition necessarily cost gas? 为什么我的 Uniswap v3 合约交易失败 - Why is my Uniswap v3 contract failing transactions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM