许多开发人员在开发Solidity之前实现了Java,Go,Python …感觉就像回到80年代后期的DeLorean一样。但是Solidity的稳定性非常有限。
我正在使用名为#ScriptIt的队长的NodeJS oracle用于以下用例:
新用户获得256分
每次新呼叫,用户的积分将减少log2
船长将直接从Docker容器中的Solidity运行NodeJS调用,并将结果返回给您的合约。
智能合约
异步联系将派生自usingCaptainJS,其中包括异步调用和回调功能。
要在回调发生时记住异步调用,您需要一个JobCounter以及作业ID和发件人地址的映射:
uintJobCounter=0;mapping(uint=>address)JobToSenderMap;
事件
在以太坊中,当同步事务处于挂起状态,事务只有失败或成功两种状态。异步事务将要求发出事件时,通知用户事务是否挂起、成功或失败。
因此,您定义三个这些事件,并且每个事件至少应包含发件人地址:
eventGetPoints_Success(addressSender,uintPoints);eventGetPoints_Pending(addressSender);eventGetPoints_Failed(addressSender,stringErrorMsg);
函数
以太坊的默认模式是每个用户调用一个合约函数,并支付在一个同步事务环境中执行代码所需的GAS。
但现在我们有了一个异步事务环境, 这意味着在同步函数调用终止后将需要额外的气体。
因此,您的函数必须是payable,您的首次检查必须是验证用户是否转移了足够的额外gas费用:
uintGasRequired=DEFAULT_GAS_UNITS*tx.gasprice+70szabo;require(msg.value>=GasRequired,“pleasesendsomeextragas…”);
在这个演示用例中,我们将要求使用usingCaptainJS中定义的默认gas单位乘以当前的交易gas价格加上70 Szabo的交易费。
一旦用户输送了足够gas,你可以根据船长在GitHub上的描述来调用mathjs的log2函数:
Run(JobCounter,concat(“math:log2(”,uintToString(PointsPerUser[msg.sender]),")"),"","",1,DEFAULT_GAS_UNITS,tx.gasprice);emitGetPoints_Pending(msg.sender);
在调用Run(…)之后,您必须发出pending事件。如果调用Run(…)失败,则同步调用将失败。
回调
一旦船长计算了用户积分的log2值,他就会通过调用CaptainsResult函数将结果发送回合约。通过仅添加CaptainsOrdersAllowed确保只有队长调用此功能。
确保在函数结束时发出成功事件。
functionCaptainsResult(uintJobCounter,stringLog2Result)
externalonlyCaptainsOrdersAllowed{//thereturnoftheasynccalladdresssender=JobToSenderMap[JobCounter];uintPoints=StringToUint(Log2Result);PointsPerUser[sender]=Points;emitGetPoints_Success(sender,Points);}
果队长无法调用您提交的代码(也许您的JavaScript代码中有拼写错误),他会通过调用合同的CaptainsError函数通知您。
确保在函数结束时发出失败的事件。
functionCaptainsError(uintJobCounter,stringErrorMsg)
externalonlyCaptainsOrdersAllowed{//thereturnoftheasynccalladdresssender=JobToSenderMap[JobCounter];emitGetPoints_Failed(sender,ErrorMsg);}
这是完整的代码:
pragmasolidity^0.4.25;import"./usingCaptainJS_v2.sol";contractAsyncPatternisusingCaptainJS{//toidentifyasynccallsuintJobCounter=0;mapping(uint=>address)JobToSenderMap;//demousecase:pointspersendermapping(address=>uint)PointsPerUser;eventGetPoints_Success(addressSender,uintPoints);eventGetPoints_Pending(addressSender);eventGetPoints_Failed(addressSender,stringErrorMsg);functionGetPoints()publicpayable{//makesuretohaveenoughgasfortheasynccallbackuintGasRequired=DEFAULT_GAS_UNITS*tx.gasprice+70szabo;require(msg.value>=GasRequired,“pleasesendsomeextragas…”);//rememberthiscallJobToSenderMap[++JobCounter]=msg.sender;//nowdothemath-butmixasync+async…//everyuserhas256pointsatthebeginningandwitheverynext//callitislog2ofhispointsif(PointsPerUser[msg.sender]==0){//firstcall!PointsPerUser[msg.sender]=256;emitGetPoints_Success(msg.sender,256);}else{//everyothercallRun(JobCounter,concat(“math:log2(”,uintToString(PointsPerUser[msg.sender]),")"),"","",1,DEFAULT_GAS_UNITS,tx.gasprice);emitGetPoints_Pending(msg.sender);}}functionCaptainsResult(uintJobCounter,stringLog2Result)externalonlyCaptainsOrdersAllowed{//thereturnoftheasynccalladdresssender=JobToSenderMap[JobCounter];uintPoints=StringToUint(Log2Result);PointsPerUser[sender]=Points;emitGetPoints_Success(sender,Points);}functionCaptainsError(uintJobCounter,stringErrorMsg)externalonlyCaptainsOrdersAllowed{//thereturnoftheasynccalladdresssender=JobToSenderMap[JobCounter];emitGetPoints_Failed(sender,ErrorMsg);}}
本文来源于陀螺财经专栏作家:区块链研究实验室,区块链研究实验室简介:专注区块链技术研发,区块链大学知识培训,区块链技术研发社区和区块链孵化
区块链研究实验室专栏:https://www.tuoluocaijing.cn/columns/author355424