1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-07 07:16:42 +02:00

Merge pull request #3432 from NervosBeijingCommunity/shooter

[solidity/en,cn] Solidity code should not be mixed with Javascript code
This commit is contained in:
Divay Prakash
2018-12-26 14:29:54 +05:30
committed by GitHub
2 changed files with 43 additions and 30 deletions

View File

@@ -5,6 +5,7 @@ contributors:
- ["Nemil Dalal", "https://www.nemil.com"] - ["Nemil Dalal", "https://www.nemil.com"]
- ["Joseph Chow", ""] - ["Joseph Chow", ""]
- ["Bhoomtawath Plinsut", "https://github.com/varshard"] - ["Bhoomtawath Plinsut", "https://github.com/varshard"]
- ["Shooter", "https://github.com/liushooter"]
--- ---
Solidity lets you program on [Ethereum](https://www.ethereum.org/), a Solidity lets you program on [Ethereum](https://www.ethereum.org/), a
@@ -237,7 +238,7 @@ uint x[][5]; // arr with 5 dynamic array elements (opp order of most languages)
// Dictionaries (any type to any other type) // Dictionaries (any type to any other type)
mapping (string => uint) public balances; mapping (string => uint) public balances;
balances["charles"] = 1; balances["charles"] = 1;
console.log(balances["ada"]); // is 0, all non-set key values return zeroes // balances["ada"] result is 0, all non-set key values return zeroes
// 'public' allows following from another contract // 'public' allows following from another contract
contractName.balances("charles"); // returns 1 contractName.balances("charles"); // returns 1
// 'public' created a getter (but not setter) like the following: // 'public' created a getter (but not setter) like the following:
@@ -403,8 +404,12 @@ event LogSent(address indexed from, address indexed to, uint amount); // note ca
// Call // Call
LogSent(from, to, amount); LogSent(from, to, amount);
// For an external party (a contract or external entity), to watch using /**
// the Web3 Javascript library:
For an external party (a contract or external entity), to watch using
the Web3 Javascript library:
// The following is Javascript code, not Solidity code
Coin.LogSent().watch({}, '', function(error, result) { Coin.LogSent().watch({}, '', function(error, result) {
if (!error) { if (!error) {
console.log("Coin transfer: " + result.args.amount + console.log("Coin transfer: " + result.args.amount +
@@ -415,6 +420,8 @@ Coin.LogSent().watch({}, '', function(error, result) {
"Receiver: " + Coin.balances.call(result.args.to)); "Receiver: " + Coin.balances.call(result.args.to));
} }
} }
**/
// Common paradigm for one contract to depend on another (e.g., a // Common paradigm for one contract to depend on another (e.g., a
// contract that depends on current exchange rate provided by another) // contract that depends on current exchange rate provided by another)

View File

@@ -6,6 +6,7 @@ contributors:
- ["Nemil Dalal", "https://www.nemil.com"] - ["Nemil Dalal", "https://www.nemil.com"]
- ["Joseph Chow", ""] - ["Joseph Chow", ""]
- ["Bhoomtawath Plinsut", "https://github.com/varshard"] - ["Bhoomtawath Plinsut", "https://github.com/varshard"]
- ["Shooter", "https://github.com/liushooter"]
translators: translators:
- ["Bob Jiang", "https://github.com/bobjiang"] - ["Bob Jiang", "https://github.com/bobjiang"]
--- ---
@@ -217,7 +218,7 @@ uint x[][5]; // 5个动态数组元素的数组(和多数语言的顺序相反)
// 字典类型 (任一类型到其他类型的映射) // 字典类型 (任一类型到其他类型的映射)
mapping (string => uint) public balances; mapping (string => uint) public balances;
balances["charles"] = 1; balances["charles"] = 1;
console.log(balances["ada"]); // 所有没有设定key值的返回0 // balances["ada"]得到 0, 所有没有设定key值的返回0
// 'public' 允许跟着(调用)另一份合约 // 'public' 允许跟着(调用)另一份合约
contractName.balances("charles"); // returns 1 contractName.balances("charles"); // returns 1
// 'public' 创建 getter (而不是 setter )如下: // 'public' 创建 getter (而不是 setter )如下:
@@ -373,8 +374,10 @@ event LogSent(address indexed from, address indexed to, uint amount); // 注意
// 调用 // 调用
LogSent(from, to, amount); LogSent(from, to, amount);
// 对于外部方(合约或外部实体),使用 Web3 Javascript 库来监听 /*
Coin.LogSent().watch({}, '', function(error, result) { // 对于外部方(合约或外部实体),使用 Web3 Javascript 库来监听
// 以下是javascript代码,不是solidity代码
Coin.LogSent().watch({}, '', function(error, result) {
if (!error) { if (!error) {
console.log("Coin transfer: " + result.args.amount + console.log("Coin transfer: " + result.args.amount +
" coins were sent from " + result.args.from + " coins were sent from " + result.args.from +
@@ -383,7 +386,10 @@ Coin.LogSent().watch({}, '', function(error, result) {
"Sender: " + Coin.balances.call(result.args.from) + "Sender: " + Coin.balances.call(result.args.from) +
"Receiver: " + Coin.balances.call(result.args.to)); "Receiver: " + Coin.balances.call(result.args.to));
} }
} }
*/
// 一个合约依赖另一个合约的共同范例(例如,合约取决于另一个合约提供的当前汇率) // 一个合约依赖另一个合约的共同范例(例如,合约取决于另一个合约提供的当前汇率)
// C. 修饰器 // C. 修饰器