目前以太坊的编程语言Solidity还是非常基础。Tron也使用Solidity,因此具有相同的限制。
目前,Solidity将智能合约简化为仅存储数值并进行一些基本的数学运算。 并且大多数智能逻辑都写在智能合约之外:在DAPP或服务器中。
#TRON中异步调用的两种情况是什么?
· 外部调用。..到数据库,Web服务,(分布式)文件系统,邮件服务,到NodeJS oracle以克服Solidity的限制等。
· 自动回调。..到合约本身调用代码(例如股息支付)。
为了实现这两个场景,我们需要进行TRON扩展。这是如何做到的:
步骤1:下载客户端库
输入下面链接,进行客户端库下载,
https://github.com/CaptainJavaScript/Tron
步骤2:创建智能合约
您可以复用位于客户端库中的HelloSeaman_v1.sol示例。只需从usingCaptainJSAtTron_v2扩展你的合同,整个魔法就会开始发生:
pragma solidity ^0.4.25;
import “。/usingCaptainJSAtTron_v2.sol”;
contract HelloSeaman_v1 is usingCaptainJSAtTron_v2 {
constructor () public { }
。..
}
要在TRON中实现异步调用,只需使用Run-method执行NodeJS代码,或调用Callback方法进行简单回调。
。..
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:调用TronWeb的智能合约
将合同部署到SHASTA后,从GitHub存储库编辑Test-Shasta-public.js文件,
1. 添加您的私钥(第6行)
2. 更改合同的地址(第11行) - 否则你将调用我的演示合同地址
3. 在第一次运行中调用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 = “《your shasta wallet address》”;
。..
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();
调用完成。SHASTA还是值得我们去关注的。
来源: 区块链研究实验室
相关热词:#区块链
RS485基本知识介绍
时间:2026-04-18
什么是激光雷达?激光雷达的构成与分类
时间:2026-04-18
Excelpoint - 一文了解SiC MOS的应用
时间:2026-04-18
什么是磁电阻器?磁电阻特性及应用
时间:2026-04-18
什么是电场?电场在电容器中的应用
时间:2026-04-18
什么是ARM64?
时间:2026-04-17
vga和hdmi的区别
时间:2026-04-17
什么是ESD?ESD及TVS的原理和应用
时间:2026-04-17
开关电源原理与维修完整版 (10)_标清视频
时间:2026-04-16
开关电源原理与维修完整版 (11)_标清视频
时间:2026-04-16
GoKit3的硬件电路原理
时间:2026-03-06
基于N32G457和RT-Thread打造的私有化定制家...
时间:2026-03-06
如何搭建一个私有云平台
时间:2026-03-06
基于 STM32和机智云物联网平台的农作物需水...
时间:2026-03-06
物联网的前世今生:什么是物联网?实现物联...
时间:2026-03-06
xG22E SoC以能量采集技术实现“环境物联网”...
时间:2026-03-06
物联网的发展历程及技术特征
时间:2026-03-06
朗骏智能推出四款全新的可联网智能光控产品
时间:2026-03-06
如何实现热水器的远程控制功能
时间:2026-03-06
物联网中常用指令——AT命令
时间:2026-03-06