許多開發(fā)人員在開發(fā)Solidity之前實現(xiàn)了Java,Go,Python 感覺就像回到80年代后期的DeLorean一樣。 但是Solidity的穩(wěn)定性非常有限。
我正在使用名為#ScriptIt的隊長的NodeJS oracle用于以下用例:
1. 新用戶獲得256分
2. 每次新呼叫,用戶的積分將減少log2
船長將直接從Docker容器中的Solidity運行NodeJS調用,并將結果返回給您的合約。
智能合約
異步聯(lián)系將派生自usingCaptainJS,其中包括異步調用和回調功能。
要在回調發(fā)生時記住異步調用,您需要一個JobCounter以及作業(yè)ID和發(fā)件人地址的映射:
uint JobCounter = 0;
mapping (uint =》 address) JobToSenderMap;
事件
在以太坊中,當同步事務處于掛起狀態(tài),事務只有失敗或成功兩種狀態(tài)。異步事務將要求發(fā)出事件時,通知用戶事務是否掛起、成功或失敗。
因此,您定義三個這些事件,并且每個事件至少應包含發(fā)件人地址:
event GetPoints_Success(address Sender, uint Points);
event GetPoints_Pending(address Sender);
event GetPoints_Failed(address Sender, string ErrorMsg);
函數(shù)
以太坊的默認模式是每個用戶調用一個合約函數(shù),并支付在一個同步事務環(huán)境中執(zhí)行代碼所需的GAS。
但現(xiàn)在我們有了一個異步事務環(huán)境, 這意味著在同步函數(shù)調用終止后將需要額外的氣體。
因此,您的函數(shù)必須是payable,您的首次檢查必須是驗證用戶是否轉移了足夠的額外gas費用:
uint GasRequired = DEFAULT_GAS_UNITS * tx.gasprice + 70 szabo;
require(msg.value 》= GasRequired, “please send some extra gas.。.”);
在這個演示用例中,我們將要求使用usingCaptainJS中定義的默認gas單位乘以當前的交易gas價格加上70 Szabo的交易費。
一旦用戶輸送了足夠gas,你可以根據(jù)船長在GitHub上的描述來調用mathjs的log2函數(shù):
Run(
JobCounter,
concat(“math:log2(”,uintToString(PointsPerUser[msg.sender]), “)”),
“”, “”, 1, DEFAULT_GAS_UNITS, tx.gasprice
);
emit GetPoints_Pending(msg.sender);
在調用Run(。..)之后,您必須發(fā)出pending事件。如果調用Run(。..)失敗,則同步調用將失敗。
回調
一旦船長計算了用戶積分的log2值,他就會通過調用CaptainsResult函數(shù)將結果發(fā)送回合約。通過僅添加CaptainsOrdersAllowed確保只有隊長調用此功能。
確保在函數(shù)結束時發(fā)出成功事件。
function CaptainsResult(uint JobCounter, string Log2Result)
external onlyCaptainsOrdersAllowed {
// the return of the async call
address sender = JobToSenderMap[JobCounter];
uint Points = StringToUint(Log2Result);
PointsPerUser[sender] = Points;
emit GetPoints_Success(sender, Points);
}
果隊長無法調用您提交的代碼(也許您的JavaScript代碼中有拼寫錯誤),他會通過調用合同的CaptainsError函數(shù)通知您。
確保在函數(shù)結束時發(fā)出失敗的事件。
function CaptainsError(uint JobCounter, string ErrorMsg)
external onlyCaptainsOrdersAllowed {
// the return of the async call
address sender = JobToSenderMap[JobCounter];
emit GetPoints_Failed(sender, ErrorMsg);
}
這是完整的代碼:
pragma solidity ^0.4.25;
import “。/usingCaptainJS_v2.sol”;
contract AsyncPattern is usingCaptainJS {
// to identify async calls
uint JobCounter = 0;
mapping (uint =》 address) JobToSenderMap;
// demo use case: points per sender
mapping (address =》 uint) PointsPerUser;
event GetPoints_Success(address Sender, uint Points);
event GetPoints_Pending(address Sender);
event GetPoints_Failed(address Sender, string ErrorMsg);
function GetPoints() public payable {
// make sure to have enough gas for the async callback
uint GasRequired = DEFAULT_GAS_UNITS * tx.gasprice + 70 szabo;
require(msg.value 》= GasRequired, “please send some extra gas.。.”);
// remember this call
JobToSenderMap[++JobCounter] = msg.sender;
// now do the math - but mix async + async.。.
// every user has 256 points at the beginning and with every next
// call it is log2 of his points
if(PointsPerUser[msg.sender] == 0) {
// first call!
PointsPerUser[msg.sender] = 256;
emit GetPoints_Success(msg.sender, 256);
}
else {
// every other call
Run(
JobCounter, concat(“math:log2(”, uintToString(PointsPerUser[msg.sender]), “)”),
“”, “”, 1, DEFAULT_GAS_UNITS, tx.gasprice
);
emit GetPoints_Pending(msg.sender);
}
}
function CaptainsResult(uint JobCounter, string Log2Result) external onlyCaptainsOrdersAllowed {
// the return of the async call
address sender = JobToSenderMap[JobCounter];
uint Points = StringToUint(Log2Result);
PointsPerUser[sender] = Points;
emit GetPoints_Success(sender, Points);
}
function CaptainsError(uint JobCounter, string ErrorMsg) external onlyCaptainsOrdersAllowed {
// the return of the async call
address sender = JobToSenderMap[JobCounter];
emit GetPoints_Failed(sender, ErrorMsg);
}
}
評論
查看更多