Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Solidity API

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Solidity API". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Solidity API.

Attributes of blocks and transactions (Block And Transaction Properties)

Used to provide some current information about the blockchain.

Block.blockhash (uint blockNumber) returns (bytes32): returns a hash of the given chunk number, supporting only the last 256chunks and not including the current chunk.

Block.coinbase (address): the address of the current block miner.

Block.difficulty (uint): the difficulty of the current block.

Block.gaslimit (uint): the gaslimit of the current block.

Block.number (uint): the block number of the current block.

Block.timestamp (uint): the Unix timestamp of the current block (the number of seconds that have elapsed since 00:00:00 UTC on 1970-1-1)

Msg.data (bytes): the complete call data (calldata).

Msg.gas (uint): the current remaining gas.

Msg.sender (address): the address of the current call initiator.

Msg.sig (bytes4): the first four bytes of the call data (calldata) (for example, the function identifier).

Msg.value (uint): the etheric coin attached to this message, in wei.

Now (uint): timestamp of the current block (alias for block.timestamp)

Tx.gasprice (uint): the gas price of the transaction.

Tx.origin (address): the sender of the deal (full call chain)

Note: all member values of msg, such as msg.sender,msg.value, can change with each external function call, or library function call (because msg is the global variable associated with the call).

You should not generate a random number based on block.timestamp, now, and block.blockhash (unless you really need to), these values are partly influenced by miners (for example, in a gambling contract, dishonest miners may retry to choose a hash that works in their favor).

For contiguous blocks on the same chain, the timestamp of the current block is always greater than that of the previous block.

For scalability reasons, you can only check the last 256 blocks, and all the others will return 0. 0.

Error handling

Assert (bool condition) is used to judge internal errors and throws an exception when the condition is not satisfied

Require (bool condition): used to determine input or external component errors, and throw an exception if the condition is not met

Revert (): terminates execution and restores the state of the change

Mathematics and encryption function

Addmod (uint x, uint y, uint k) returns (uint): computes (x + y)% k, addition supports arbitrary precision and does not overflow at 2 precision 256, asserting k! = 0 since version 0.5.0.

Mulmod (uint x, uint y, uint k) returns (uint): compute (x * y)% k, multiplication supports arbitrary precision and does not overflow at 2 precision 256, asserting k! = 0 since version 0.5.0.

Keccak256 (...) Returns (bytes32): use the Keccak-256 of Ethernet Square to calculate the hash value. Tightly pack the parameters.

Sha256 (...) Returns (bytes32): use SHA-256 to calculate hash values and tightly package parameters.

Sha3 (...) Returns (bytes32): alias for keccak256

Ripemd160 (...) Returns (bytes20): uses RIPEMD-160 to calculate the hash value. Tightly pack the parameters.

Ecrecover (bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address): restores the address associated with the public key through an elliptic curve signature, or returns zero in case of an error. It can be used to verify the signature data. If the returned result is the public key address of the signer, then the data is correct.

The ecrecover function needs four parameters, the hash result value of the signed data, and the rquotiansjournal v comes from the signature result string, respectively. R = signature [0:64] s = signature [64virtual 128] v = signature [128glaze 130] where the value taken by v is either 00 or 01. When we want to use it, we have to convert it to an integer, plus 27, so we will get 27 or 28. V will be filled in 27 or 28 when the function is called.

Expressed in javascript as follows:

Var msg = '0x8CbaC5e4d803bE2A3A5cd3DbE7174504c6DD0c1C' var hash = web3.sha3 (msg) var sig = web3.eth.sign (address, h) .slice (2) var r = `0x$ {sig.slice (0,64)} `var s = `0x$ {sig.slice (64,128)} `var v = web3.toDecimal (sig.slice (128,130)) + 27

Subscribe to the blockchain technology column for complete usage examples.

The tightly packed parameter (tightly packed) means that the parameters are not patched, they are directly connected together, and the following are equal.

Keccak256 ("ab", "c") keccak256 ("abc") keccak256 (0x616263) / / hexkeccak256 (6382179) keccak256 (97, 98, 99) / / ascii

If you need to populate, you can use explicit type conversions: keccak256 ("\ x00\ x12") is the same as keccak256 (uint16 (0x12)).

Note that constants will be packaged with the minimum number of bytes required to store them, such as keccak256 (0) = = keccak256 (uint8 (0)) and keccak256 (0x12345678) = = keccak256 (uint32 (0x12345678))

Running sha256,ripemd160 or ecrecover on a private chain (private blockchain) may cause an Out-Of-Gas error. Because the private chain implements a precompiled contract, the contract does not really exist until the first message is received (although their contract code is hard-coded). And send a message to a contract that does not exist, which leads to a problem with Out-Of-Gas. One solution (workaround) is to each send 1 wei to these contracts to complete initialization before you actually use them. There is no such problem on the official and test chains.

Address dependence

.balance (uint256):

The balance of Address, in wei.

.transfer (uint256 amount):

Sends a given number of ether to an address, in wei units. Throw an exception on failure.

.send (uint256 amount) returns (bool):

Sends a given number of ether to an address, in wei units, and returns false on failure.

.call (...) Returns (bool):

Initiates the underlying call call. Returns false on failure.

.callcode (...) Returns (bool):

Initiates the underlying callcode call and returns false if it fails. Use is discouraged and may be removed in the future.

.delegateCall (...) Returns (bool):

Initiates the underlying delegatecall call, and returns false if it fails

Refer to the address section for more information.

Warning: send () execution has some risks: if the depth of the call stack exceeds 1024 or the gas runs out, the transaction will fail. Therefore, in order to ensure security, the return value of send must be checked and the etheric currency will be returned if the transaction fails. It would be better to use transfer.

Contract related

This (type of current contract): represents the current contract and can be explicitly converted to Address

Selfdestruct (address recipient): destroys the current contract and sends all its funds to the given address.

Suicide (address recipient): alias for selfdestruct

In addition, all functions in the current contract can be called, including the current function itself.

Thank you for your reading, the above is the content of "how to use Solidity API", after the study of this article, I believe you have a deeper understanding of how to use Solidity API, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report