目前以太坊的編程語(yǔ)言Solidity還是非?;A(chǔ)。Tron也使用Solidity,因此具有相同的限制。目前,Solidity將智能合約簡(jiǎn)化為僅存儲(chǔ)數(shù)值并進(jìn)行一
目前以太坊的編程語(yǔ)言Solidity還是非?;A(chǔ)。Tron也使用Solidity,因此具有相同的限制。
目前,Solidity將智能合約簡(jiǎn)化為僅存儲(chǔ)數(shù)值并進(jìn)行一些基本的數(shù)學(xué)運(yùn)算。 并且大多數(shù)智能邏輯都寫(xiě)在智能合約之外:在DAPP或服務(wù)器中。
#TRON中異步調(diào)用的兩種情況是什么?
· 外部調(diào)用...到數(shù)據(jù)庫(kù),Web服務(wù),(分布式)文件系統(tǒng),郵件服務(wù),到NodeJS oracle以克服Solidity的限制等。
· 自動(dòng)回調(diào)...到合約本身調(diào)用代碼(例如股息支付)。
為了實(shí)現(xiàn)這兩個(gè)場(chǎng)景,我們需要進(jìn)行TRON擴(kuò)展。這是如何做到的:
步驟1:下載客戶端庫(kù)
輸入下面鏈接,進(jìn)行客戶端庫(kù)下載,
https://github.com/CaptainJavaScript/Tron
步驟2:創(chuàng)建智能合約
您可以復(fù)用位于客戶端庫(kù)中的HelloSeaman_v1.sol示例。只需從usingCaptainJSAtTron_v2擴(kuò)展你的合同,整個(gè)魔法就會(huì)開(kāi)始發(fā)生:
pragma solidity ^0.4.25;
import "./usingCaptainJSAtTron_v2.sol";
contract HelloSeaman_v1 is usingCaptainJSAtTron_v2 {
constructor () public { }
...
}
要在TRON中實(shí)現(xiàn)異步調(diào)用,只需使用Run-method執(zhí)行NodeJS代碼,或調(diào)用Callback方法進(jìn)行簡(jiǎn)單回調(diào)。
...
uint constant EXAMPLE1 = 1;
uint constant EXAMPLE2 = 2;
function Demo() public payable
{
Run(EXAMPLE1, "json:https://api.kraken.com/0/public/Ticker?
pair=ETHUSD",
"result.XETHZUSD.a[0]", "-", 2, 5000);
Callback(EXAMPLE2, 20, 100000);
}
function CaptainsResult(uint UniqueIdentifier,
string Result, bool IsError) external onlyCaptainsOrdersAllowed {
emit LogEvent(
concat("CaptainsResult received with UID = ",
uintToString(UniqueIdentifier), " and a result of ", Result)
);
}
function CaptainsCallback(uint UniqueIdentifier) external
onlyCaptainsOrdersAllowed {
emit LogEvent(
concat("Callback received with UID = ", uintToString(
UniqueIdentifier))
);
}
步驟3:調(diào)用TronWeb的智能合約
將合同部署到SHASTA后,從GitHub存儲(chǔ)庫(kù)編輯Test-Shasta-public.js文件,
1. 添加您的私鑰(第6行)
2. 更改合同的地址(第11行) - 否則你將調(diào)用我的演示合同地址
3. 在第一次運(yùn)行中調(diào)用SetCaptainsAddress()
const TronWeb = require('tronweb');
const SHASTA = 'https://api.shasta.trongrid.io';
const tronWeb = new TronWeb(
SHASTA, SHASTA, SHASTA,
'<your private="" key="" goes="" here'
);
const OwnersWalletAtSHASTA = "
...
const HelloSeaman_v1AtSHASTA = "TB4TEvEnbjM66ici2QjP92rpYkJWJPJajS";
...
function RunTest() {
var Budget = ToTRX(0.01);
var Transfer = ToTRX(0.5);
console.log("RunTest / Budget = " + Budget + ", Transfer Value =
" + Transfer);
SeamansContract.Run(1, "math:log2(16)", "", "mathjs", 1, Budget).
send({shouldPollResponse: true, callValue: Transfer}).catch(function(
err) { console.log(err); }).then( console.log("RUN EXECUTED") );
}
function SetCaptainsAddress() {
console.log("SetCaptainsAddress to " + CaptainsAddressAtSHASTAhex
);
SeamansContract.SetCaptainsAddress(CaptainsAddressAtSHASTAhex).
send({shouldPollResponse: true, callValue: 0}).catch(function(err)
{ console.log(err); }).then( console.log("SETCAPTAINSADDRESS EXECUTED"
) );
}
function Demo() {
console.log("Demo run...");
SeamansContract.Demo().send({shouldPollResponse: true, callValue:
ToTRX(1)}).catch(function(err) { console.log(err); }).then
( console.log("DEMO EXECUTED") );
}
async function Go() {
...
SeamansContract = await tronWeb.contract().at(HelloSeaman_v1AtSHASTA
);
SetCaptainsAddress();
// RunTest();
// Demo();
}
Go();
調(diào)用完成。SHASTA還是值得我們?nèi)リP(guān)注的。(鏈三豐)