Closed huahuayu closed 6 years ago
As per Readme:
For the contract examples, they will not work with Javascript VM in browser-solidity due to some incompatibility of the VM with libraries. It will work fine with ganache, privatenets, testnets, mainnet.
when deploying below code in oraclize online ide(no compile error), press create button, comes out "Error deploying required libraries: Invalid bytecode format."
but in remix, don't have this error, deploy successed, so either the library have problem or oraclize ide have problem, please check.
pragma solidity ^0.4.18;
//
/*
Copyright (c) 2015-2016 Oraclize SRL
Copyright (c) 2016 Oraclize LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
contract OraclizeI { address public cbAddress; function query(uint _timestamp, string _datasource, string _arg) external payable returns (bytes32 _id); function query_withGasLimit(uint _timestamp, string _datasource, string _arg, uint _gaslimit) external payable returns (bytes32 _id); function query2(uint _timestamp, string _datasource, string _arg1, string _arg2) public payable returns (bytes32 _id); function query2_withGasLimit(uint _timestamp, string _datasource, string _arg1, string _arg2, uint _gaslimit) external payable returns (bytes32 _id); function queryN(uint _timestamp, string _datasource, bytes _argN) public payable returns (bytes32 _id); function queryN_withGasLimit(uint _timestamp, string _datasource, bytes _argN, uint _gaslimit) external payable returns (bytes32 _id); function getPrice(string _datasource) public view returns (uint _dsprice); function getPrice(string _datasource, uint gaslimit) public view returns (uint _dsprice); function setProofType(byte _proofType) external; function setCustomGasPrice(uint _gasPrice) external; function randomDS_getSessionPubKeyHash() external view returns(bytes32); } contract OraclizeAddrResolverI { function getAddress() public view returns (address _addr); } library oraclizeLib {
} //
library DateTime { /*
}
contract DSSafeAddSub { function safeToAdd(uint a, uint b) pure internal returns (bool) { return (a + b >= a); } function safeAdd(uint a, uint b) pure internal returns (uint) { require(safeToAdd(a, b)); return (a + b); }
}
/**
etherindex contract / contract Etherindex is DSSafeAddSub { /
/*
/*
/*
/*
/*
/*
/*
mapping (bytes32 => uint8) public queryType; // 将queryId和query type绑定 mapping(string => mapping(uint => closePrice)) closePrices; // 实际收盘价 string代表指数名,一个uint代表收盘日期
struct closePrice { uint price; bool valid; }
/*
address[] addrArray; //用户地址数组 todo: 暂时未用上 uint breakPoint; // 断点 todo: check later mapping (bytes32 => address) public userAddress; // 用户地址 mapping (bytes32 => address) public userTempAddress; // 暂存用户地址,防止重入攻击 mapping (bytes32 => bytes32) public userBetId; // 用户投注id mapping (bytes32 => uint) public userBetValue; // 用户投注金额 mapping (bytes32 => uint) public userTempBetValue; // 暂存投注金额,防止重入攻击 mapping (bytes32 => uint) public userProfit; // 用户目标收益 mapping (bytes32 => uint) public userTempProfit; // 暂存用户目标收益,防止重入攻击 mapping (bytes32 => uint) public userMaxProfitLimit; // 本次投注允许的最大投注金额 mapping (bytes32 => string) public userUnderlyingIndex; // 用户选择的标的指数 mapping (bytes32 => uint) public userBetDate; // 用户选择的标的指数收盘日期 mapping (bytes32 => uint) public userIntervalLow; // 用户投注区间下限
mapping (bytes32 => uint) public userIntervalHigh; // 用户投注区间上限 mapping (bytes32 => uint) public userBetTimestamp; // 用户投注时间戳 in UTC
mapping (bytes32 => uint8) public userBetStatus; // 投注状态
mapping (address => uint) public userPendingWithdrawals; // 待支付给用户的金额
/*
/ output to web3 UI on bet result/ // Status: 0-INITIAL, 1-PENDING, 2-INVALID, 3-LOSE, 4-WIN, 5-WIN_FAILED_SEND, 6-REFUND, 7-REFUND_FAILED_SEND event LogResult(bytes32 indexed _userBetId, address indexed _userAddress, uint _userRewardValue, uint _userProfit, uint _userBetValue, uint _userIntervalLow, uint _userIntervalhigh, uint8 _betStatus);
/ log manual refunds / event LogRefund(bytes32 indexed _userBetId, address indexed _userAddress, uint indexed RefundValue); / log owner transfers / event LogOwnerTransfer(address indexed SentToAddress, uint indexed AmountTransferred); / owner fund contract / event LogOwnerFundContract(address _ownerAddress, uint _currentContractBalance, uint _fundAmount, DateTime._DateTime _fundTimestamp);
//debug @test event logUint8(string item, uint8 variable); event logUint16(string item, uint16 variable); event logUint32(string item, uint32 variable); event logUint64(string item, uint64 variable); event logUint128(string item, uint128 variable); event logUint256(string item, uint256 variable); event logUint(string item, uint variable); event logInt(string item, int variable); event logBool(string item, bool variable); event logBytes(string item, bytes variable); event logBytes32(string item, bytes32 variable); event logAddress(string item, address variable); event logString(string item, string variable); event trace(string toDisplay);
/*
init */ function Etherindex() public {
owner = msg.sender; treasury = msg.sender;
//oraclizeLib.oraclize_setNetwork(networkID_auto);
/ use TLSNotary for oraclize call / //oraclizeLib.oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS); / init gas for oraclize /
gasForOraclize = 235000;
/ init gas price for callback (default 20 gwei)/ oraclizeLib.oraclize_setCustomGasPrice(20000000000 wei);
}
/*
only if game is active & bet is valid can query oraclize and set user vars
*/ function userBet(string _userUnderlyingIndex, uint _userBetDate, uint _userIntervalLow, uint _userIntervalHigh, uint _userProfit, uint _userMaxProfitLimit, uint _userBetTimestamp) public payable systemIsActive { // 输入检查: // 投注金额 > 0 require(msg.value > 0);
// 用户目标收益 < 本次投注允许的最大投注金额 require(_userProfit < _userMaxProfitLimit);
// 用户目标收益 > 0 require(_userProfit > 0);
// 投注日期合法 todo: 目前仅检查投注日期是否大于0 require(_userBetDate > 0);
// 用户投注区间下限 > 0 require(_userIntervalLow > 0);
// 用户投注区间上限 > 用户投注区间下限 require(_userIntervalHigh > _userIntervalLow);
// todo: api有待确认 bytes32 queryId = oraclizeLib.oraclize_query("URL", "https://api.iextrading.com/1.0/stock/aapl/price");
queryType[queryId] = VALIDATECHECK;
// @test emit logBytes32("queryId",queryId);
// 将用户信息和betId关联 userAddress[queryId] = msg.sender; addrArray.push(msg.sender); userBetId[queryId] = queryId; userBetValue[queryId] = msg.value; userProfit[queryId] = _userProfit; userMaxProfitLimit[queryId] = _userMaxProfitLimit; userUnderlyingIndex[queryId] = _userUnderlyingIndex; userBetDate[queryId] = _userBetDate; userIntervalLow[queryId] = _userIntervalLow; userIntervalHigh[queryId] = _userIntervalHigh; userBetTimestamp[queryId] = _userBetTimestamp;
// bet状态为初始状态 userBetStatus[queryId] = INITIAL;
// 记录投注信息 emit LogBet(queryId, msg.sender, safeAdd(msg.value, _userProfit), _userProfit, msg.value, _userIntervalLow, _userIntervalHigh, userBetStatus[queryId]);
}
//收盘时发起当日收盘价查询,每个指数只查询一次 function inqueryIndexPrice(string _indexName, uint _date) public onlyOwner returns(bool)
{ // 检查本地是否存有查询过的数据,无则调用oraclize查询,且返回true todo:api有待确认 if (closePrices[_indexName][_date].price == 0){ bytes32 queryId = oraclizeLib.oraclize_query("URL", "https://api.iextrading.com/1.0/stock/goog/price"); queryType[queryId] = PAYOUTCHECK; //@test emit logBool("inquery index price",true); emit logBytes32("query id",queryId); return true; } else{ //@test emit logBool("inquery index price",false); return false; } }
function payoutProcess(bytes32 myid) public{ //@test emit logBytes32("myid",myid); emit logString("underlying index",userUnderlyingIndex[myid]); emit logUint("user bet date",userBetDate[myid]); emit logUint("close price",closePrices[userUnderlyingIndex[myid]][userBetDate[myid]].price); emit logBool("close price valid",closePrices[userUnderlyingIndex[myid]][userBetDate[myid]].valid);
}
function __callback(bytes32 myid, string result, bytes proof) public
onlyOraclize payoutsAreActive { //@test emit trace("callback triggered");
}
/*
/ check for pending withdrawals / function userGetPendingTxByAddress(address addressToCheck) public constant returns (uint) { return userPendingWithdrawals[addressToCheck]; }
/*
/ set gas price for oraclize callback / function ownerSetCallbackGasPrice(uint newCallbackGasPrice) public onlyOwner { oraclizeLib.oraclize_setCustomGasPrice(newCallbackGasPrice); }
/ set gas limit for oraclize query / function ownerSetOraclizeSafeGas(uint32 newSafeGasToOraclize) public onlyOwner { gasForOraclize = newSafeGasToOraclize; }
/ only owner address can set minBet / function ownerSetMinBet(uint newMinimumBet) public onlyOwner { minBet = newMinimumBet; }
/ only owner address can transfer ether / function ownerTransferEther(address sendTo, uint amount) public onlyOwner {
sendTo.transfer(amount); emit LogOwnerTransfer(sendTo, amount); }
/* only owner address can do manual refund
/ safely reduce pendingPayouts by userProfit[rngId] / maxPendingPayouts = safeSub(maxPendingPayouts, originalUserProfit); / send refund / sendTo.transfer(originalUserBetValue); / log refunds */ emit LogRefund(originalUserBetId, sendTo, originalUserBetValue);
}
/ only owner address can set emergency pause #1 / function ownerPauseGame(bool newStatus) public onlyOwner { gamePaused = newStatus; }
/ only owner address can set emergency pause #2 / function ownerPausePayouts(bool newPayoutStatus) public onlyOwner { payoutsPaused = newPayoutStatus; }
/ only owner address can set treasury address / function ownerSetTreasury(address newTreasury) public onlyOwner { treasury = newTreasury; }
/ only owner address can set owner address / function ownerChangeOwner(address newOwner) public onlyOwner { owner = newOwner; }
/ only owner address can suicide - emergency / function ownerkill() public onlyOwner { selfdestruct(owner); }
//@test function function getUserInfo(bytes32 queryId) public { emit logAddress("userAddress",userAddress[queryId]); emit logAddress("userTempAddress",userTempAddress[queryId]); emit logUint("userBetValue",userBetValue[queryId]); emit logUint("userTempBetValue",userTempBetValue[queryId]); emit logUint("userProfit",userProfit[queryId]); emit logUint("userTempProfit",userTempProfit[queryId]); emit logUint("userMaxProfitLimit",userMaxProfitLimit[queryId]); emit logString("userUnderlyingIndex",userUnderlyingIndex[queryId]); emit logUint("userBetDate",userBetDate[queryId]); emit logUint("userIntervalLow",userIntervalLow[queryId]); emit logUint("userIntervalHigh",userIntervalHigh[queryId]); emit logUint("userBetTimestamp",userBetTimestamp[queryId]); emit logUint8("loguserBetStatus",userBetStatus[queryId]); emit logUint("userPendingWithdrawals",userPendingWithdrawals[userAddress[queryId]]); emit logUint("userTempPendingWithdrawals",userPendingWithdrawals[userTempAddress[queryId]]); }
//@test function function getClosePrice (string _indexName, uint _date) public { emit logUint("price",closePrices[_indexName][_date].price); }
}