AI > AI技术 > 详情

solidity的智能合约在tron网络异步调用怎样来实现

发布时间:2023-05-10 发布时间:
|

目前以太坊的编程语言Solidity还是非常基础。Tron也使用Solidity,因此具有相同的限制。

目前,Solidity将智能合约简化为仅存储数值并进行一些基本的数学运算。 并且大多数智能逻辑都写在智能合约之外:在DAPP或服务器中。

#TRON中异步调用的两种情况是什么?

基于solidity的智能合约在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还是值得我们去关注的。



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
怎样用排除证明来查询合约状态变量