跳转至

帐户存储

原文:https://docs.elrond.com/developers/account-storage

## 描述

Elrond协议提供了将账户下的附加数据存储为键值对的可能性。这对于许多用例都很有用。

钱包所有者可以通过使用内置函数SaveKeyValue来存储键值对,该函数可以接收任意数量的键值对。

提示

ELROND开头的密钥将被拒绝,因为它们是为协议使用而保留的。

交易格式

SaveKeyValueTransaction {
 Sender: <account address of the wallet owner>
 Receiver: <same as sender>
 Value: 0
 GasLimit: <required_gas>
 Data: "SaveKeyValue" +
          "@" + <key in hexadecimal encoding> +
          "@" + <value in hexadecimal encoding> +
          "@" + <key in hexadecimal encoding> +
          "@" + <value in hexadecimal encoding> +
          ...
} 

关于参数如何编码的更多细节,请点击这里

使用的气体计算如下:

required_gas =  save_key_value_cost +
                move_balance_cost + 
                cost_per_byte * length(txData) + 
                persist_per_byte * length(key) +   // repeated if multiple pairs
                persist_per_byte * length(value) + // repetead if multiple pairs
                store_per_byte * length(value) +   // repeated if multiple pairs 

对于一个真实的例子,成本将是:

SaveKeyValue@6b657930@76616c756530将花费271000个气体单位。

如果我们分解气体使用操作,使用写作时的成本,我们将得到:

required_gas =  100000    + // save key value function cost
                50000     + // move balance cost
                1500 * 34 + // cost_per_byte * length(txData)
                1000 * 4 + // persist_per_byte * length(key)
                1000 * 6 + // persist_per_byte * length(value)
                10000 * 6 + // store_per_byte * length(value)

             =  271000 

举例

让我们保存一个键值对。键将是key0,值将是value0

SaveKeyValueTransaction {
 Sender: <account address of the wallet owner>
 Receiver: <same as sender>
 Value: 0
 GasLimit: 271000
 Data: "SaveKeyValue" +
          "@" + 6b657930 +    // key0
          "@" + 76616c756530  // value0
} 

关于参数如何编码的更多细节,点击这里

休息 API

有两个端点可用于获取帐户的键值对。它们是:



回到顶部