如何使用Solidity实现智能合约?

如何实现一个智能合约?

扫码关注《Java学研大本营》,加入读者群,分享更多精彩

什么是Solidity?

它是一种面向合约的高级语言,用于实现智能合约。 它受到 C++、JavaScript 和 Python 的影响,旨在针对 EVM。

合约

Solidity 中的契约类似于面向对象语言中的类。 可以使用“new”关键字创建合约。

contract L {
function add(unit_p, unit_q) returns(unit){
return_p+_q;
}
}
contract M{
address p;
function f(unit_p){
p = new L();
}
}

智能合约

它是一种计算机协议,用于通过数字化执行、验证和管理合同来简化合同流程。

Remix, Solium & Doxity

- Remix: It is a browser based IDE with integrated compiler and solidity run-time environment without server side components.
- Solium: A linter to identity and fix style security issues in solidity.
- Doxity: Used in solidity as a documentation generator.

Solograph & Assembly

- Solograph: Used to visualize solidity control flow and highlight potential security vulnerabilities.
- Solidity Assembly: An assembly language which is used without solidity & inline assembly inside solidity source code.

Gas & Block Gas Limit

- Gas: A measurement roughly equivalent to the computational steps.
- Block Gas limit: It is used to control the amount of gas consumed during the transactions.

Solidity 编译器

    Contract Definition
                |
         Solidity Compiler
                |
      Byte code - Application Binary Interface (ABI)

ABI & WEI

- ABI: A data encoding scheme called "Application Binary Interface" (ABI) is used in for working with smart contracts.
- WEI: The smallest denomination or base unit of Ether.

Global Variables

- block.blockhash(uint numberOfBlock)returns(bytes32): hash function of the given block which works for the 256 most recent blocks excluding the current block.
- block.coinbase(address): shows miner's address of current block.
- block.numer(uint): number of current block.
- block.gaslimit(uint): gaslimit of the current block.
- block.timestamp(uint): timestamp of current block.
- msg.gas(uint): the gas that remains.
- msg.value(uint): the amount of WEI sent along with the message.
- msg.sender(address): address of the message sender(current call).
- msg.sig(bytes4): first four bytes of the call data.
- now (uint): timestamp for the current block..
- tx.origin(address): the sender of the transaction(whole call chain).

Pragma

Used to specify certain conditions under which the source files can or cannot run.

Example:
- pragma solidity ^0.8.15
       This code compiles with a compiler function >=0.8.15
- A pseudocode example for pragma syntax
       'pragma' Identifier([^;]+)';'

Interface

Interface in solidity are defined as contractsm but the function bodies are omitted for the functions.
Example:
pragma solidity ^0.22
interfaceToken
{
       function transfer(address recipient, uint amount);
}

数据类型

- Boolean:
true or false
Logical:!(Logical negation), && (AND), || (OR)
Comparisons:==(equality),!=(inequality)
- Integer: Unsigned Integer, and Signed integer Comparisons:<=(Less than or equal to), <(Less than),==(equal to),!=(Not equal to),>=(Greater than or equal to),>(Greater than) Arithmetic Operators: + (Addition), - (Subtraction), Unary —, Unary +, * (Multiplication), / (Division), ** (Exponential), << (Left shift), >>(Right shift)
- Address: Holds an Ethereum address(20 byte value).
Operators: Comparisons: <=, <, ==, !=, >= and >
Balance:
- <address>.balance(uint256): balance of address in WEI
Transfer and send:
- <address>. transfer(uint256 amount): send given amount of Wei to Address, throws on failure.
- <addres>.send(uint256 amount)returns(bool): send given amount of Wei to Address, returns false on failure.

全局函数:

- keccak256(...)returns(bytes32)-computes the ethereum SHA-3 hash associated with the arguments.
- SHA256(..)returns(bytes32) — Computes the SHA-256 argument hash.
- mulmod( uint a, uint b, uint c)returns(uint): It computes (a*b)/c, if the multiplication gets executed using arbitrary precision, thus not wrapping around 2**256
- addmod(uint p, unint q, uint m)returns(uint): Computes (p+q)/m
- ecrecover(bytes32 _hash, uint8 _v, bytes32 _r, bytes32 _s)
returns (address): recovers the address linked with the public key and if an error occurs, zero is returned.
- ripemd160(...)returns(bytes20): computes the RIPEMD-160
(tightly packed) argument hash.
- <address>.balance(uint256): Returns the balance of the specified address in WEI.
- <address>.send(uint256 amount)returns(bool): It sends the specified number of WEI to the address given, on failure it returns false as an output.
- <address>.transfer(uint256 amount): Sends specified number of WEI to the particular address, on failure throws.

访问修饰符:

- Public: Accessible from the current contract, inherited contracts externally.
- Private: Accessible only form the current contract.
- Internal: Accessible only from current contract and contracts inheriting from it.
- External: Can be accessed externally only.

重要条款

- Voting: When the contract is quiet complex, it uses voting contract, it shows how delegated voting can be done so that vote counting is automatic and completely transparent at the same time.
- Delegate call: It is a reusable library code that can be applied to a contracts storage in solidity.
- Logs: A feature called logs is used in solidity to implement events.
- NPM: It is a convenient and portable way to install Solidity compiler.
- Truffle: It is a test bed that will allow to easily test and compile the smart contract.
- Inheritance: In solidity inheritance is more syntactic. In the final compilation the compiler copies the parent class members to create the byte-code of the derived contract with the copied members.
- This: This is a keyword that refers to the instance of the contract where the call is made.
- msg.sender: This function refers to the address where the contract is being called from.
- Pure: It is a modifier which assures not to be read from or modified.
- View: In solidity, view is a function that promises to not modify the state of the smart contract.
- Suicide: It is a alias for self destruct function, that is used to destroy the current contract.
- Delete: Delete is used to delete all elements in an array.
- Block.coinbase(address): It refers to the minter's address of the current block.
- Address(contractVar).send(amount): If a built-in send function needs to be used, it can be accessed using this function.
- Super: It is used to refer the contract that is higher by one level in the inheritance hierarchy.

交易示例

https://dev.to/luislucena16/solidity-complete-fundamentals-cheat-sheet-26a6

推荐书单

《项目驱动零起点学Java》

购买链接:https://item.jd.com/13607758.html

《项目驱动零起点学Java》贯穿6个完整项目,经过作者多年教学经验提炼而得,项目从小到大、从短到长,可以让读者在练习项目的过程中,快速掌握一系列知识点。

作者是国内知名Java教学者和传播者,一路披荆斩棘,兢兢业业20余年。积累了丰富的“培”“训”经验,也产出了很多优质的教学理论。

Java语言经过数十年的发展,体系逐渐变得庞大而复杂,本书芟繁就简,提炼出了最为重要的知识点,可以让读者轻松上手。本书配套有专栏课程,课程中提供了扩展内容。

《项目驱动零起点学Java》共分 13 章,围绕 6 个项目和 258 个代码示例,分别介绍了走进Java 的世界、变量与数据类型、运算符、流程控制、方法、数组、面向对象、异常、常用类、集合、I/O流、多线程、网络编程相关内容。《项目驱动零起点学Java》总结了马士兵老师从事Java培训十余年来经受了市场检验的教研成果,通过6 个项目以及每章的示例和习题,可以帮助读者快速掌握Java 编程的语法以及算法实现。扫描每章提供的二维码可观看相应章节内容的视频讲解。

精彩回顾

想要代码干净又整洁?这里有十大原则

通过自学成为开发者的 9 种方法

怎么做一个有产品意识的软件工程师?

扫码关注《Java学研大本营》,加入读者群,分享更多精彩

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值