Transactions
Oraclize 隨機數只工作一次,然後不觸發事件,需要大量的 ETH
所以我使用下面的程式碼
contract Damble is usingOraclize{ function Damble() { getRandomNumber(); } function __callback(bytes32 _queryId, string _result, bytes _proof) { if (msg.sender != oraclize_cbAddress()) throw; if (oraclize_randomDS_proofVerify__returnCode(_queryId, _result, _proof) != 0) { // the proof verification has failed, do we need to take any action here? (depends on the use case) } else { // the proof verification has passed // now that we know that the random number was safely generated, let's use it.. newRandomNumber_bytes(bytes(_result)); // this is the resulting random number (bytes) // for simplicity of use, let's also convert the random bytes to uint if we need uint maxRange = 10000000; // this is the highest uint we want to get. It should never be greater than 2^(8*N), where N is the number of random bytes we had asked the datasource to return uint randomNumber = uint(sha3(_result)) % maxRange; // this is an efficient way to get the uint out in the [0, maxRange] range newRandomNumber_uint(randomNumber); // this is the resulting random number (uint) } } function getRandomNumber() payable{ oraclize_setProof(proofType_Ledger); // sets the Ledger authenticity proof uint N = 4; // number of random bytes we want the datasource to return uint delay = 0; // number of seconds to wait before the execution takes place uint callbackGas = 200000; // amount of gas we want Oraclize to set for the callback function bytes32 queryId = oraclize_newRandomDSQuery(delay, N, callbackGas); }
第一次
getRandomNumber()
從建構子呼叫一切正常。當我想用某個值手動呼叫 getRandomNumber(以支付 Oraclize 值)時,我需要投入價值超過 5 美元的 eth,否則交易會失敗,但會稀釋我的合約乙太幣餘額。除此之外,當我getRandomNumber()
在從建構子呼叫的建構子之後呼叫時,即使事務來自 Oraclize 地址,也不會觸發任何事件。編輯:這是我的契約的連結:https ://ropsten.etherscan.io/address/0x92f0dbf8b218e03f288da42c0f6d4df7de40c3f2
問題似乎已經解決了,可能是通過在契約本身內取得一些平衡。