目前以太坊的编程语言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还是值得我们去关注的。
相关热词:#区块链
如何应对NB-IoT系统的低功耗挑战?
时间:2026-03-06
LPWAN物联网和RedCap技术介绍 LoRa和NB-IoT...
时间:2026-03-06
一对一和一对多的eHLK-L06 LoRa模块通信解决...
时间:2026-03-06
高速公路充电桩系统的物联网应用方案
时间:2026-03-06
设计一个高效的缩短型垂直天线有哪些步骤
时间:2026-03-06
如何选择最佳的开关式DC-DC转换器呢?
时间:2026-03-06
物联网与工业物联网的区别 基于系统概念的I...
时间:2026-03-06
如何测量开关电源中的噪声?
时间:2026-03-06
浅谈工业物联网应用的数据存取技术
时间:2026-03-06
借助COMSOL评估三维电感器的设计
时间:2026-03-06
配电变压器的工作原理和作用
时间:2026-03-05
低压配电方式有三种 低压配电的作用
时间:2026-03-05
中线安防保护器对电网中三次谐波的治理
时间:2026-03-05
什么是单母线接线?主接线的普遍规律
时间:2026-03-05
智能电网包括什么?它的先进性和优势是?
时间:2026-03-05
基于区块链技术的IP电话协议的分散式设计方...
时间:2026-03-05
未来什么技术占主流?会是区块链吗?
时间:2026-03-05
浅谈区块链下的智能合约
时间:2026-03-05
浅显易懂地揭开.Net生态系统的神秘面纱!
时间:2026-03-05
对技术的执着和美的追求变成了程序员的“诗...
时间:2026-03-05