I'm trying to write a smart contract that gives me the current timestamp. The very important part is that i want this to happen locally, not on test networks. After some research, i installed ethereum-bridge and with truffle and some example, I managed to write this:
pragma solidity ^0.5.10;
import "./usingOraclize.sol";
contract TimeMachine is usingProvable {
string public timestamp;
event LogConstructorInitiated(string nextStep);
event LogPriceUpdated(string price);
event LogNewProvableQuery(string description);
constructor () public payable {
OAR = OracleAddrResolverI(0x8bE8DC18a310dcb46XXXXXXX30aA85f01499903b);
emit LogConstructorInitiated("Constructor was initiated. Call 'updateTime()' to send the Provable Query.");
}
function pay() public payable {}
function balance() public view returns(uint256) {
return address(this).balance;
}
function __callback(bytes32 myid, string memory result) public {
if (msg.sender != provable_cbAddress()) revert();
timestamp = result;
emit LogPriceUpdated(result);
}
function updateTime() public payable {
if (provable_getPrice("URL") > address(this).balance) {
emit LogNewProvableQuery("Provable query was NOT sent, please add some ETH to cover for the query fee");
} else {
emit LogNewProvableQuery("Provable query was sent, standing by for the answer..");
bytes32 queryId = provable_query("URL", "json(http://worldtimeapi.org/api/timezone/Europe/Bucharest).unixtime");
}
}
}
I started ganache-cli and ethereum bridge with command node bridge -a 9
The thing is compiling, i can deploy it.
The only problem is that it start in a empty string state, i can call the updateTime
method and it will call the callback function, the timestamp will be updated but only once. After this state, i can call updateTime
how many times i want but the timestamp won't change.
truffle(development)> let instance = await TimeMachine.deployed()
undefined
truffle(development)> instance.balance()
<BN: 0>
truffle(development)> instance.timestamp()
''
truffle(development)> instance.updateTime()
{ tx:
'0xd4aa6df2a03a234bc4792609c308f72526efed90cc5ab3ecb81617a5344df4c0',
receipt:
{ transactionHash:
'0xd4aa6df2a03a234bc4792609c308f72526efed90cc5ab3ecb81617a5344df4c0',
transactionIndex: 0,
blockHash:
'0x6fd99f88fe74d73978a693bf582a6b6226d63cfff15997606c1b71cf9bdf1f78',
blockNumber: 123,
from: '0xf22e0285c7e077c8cb6eef1173912463e01aaa67',
to: '0xd29f091644cfcd2367b790229957b3d9385d26c9',
gasUsed: 115901,
cumulativeGasUsed: 115901,
contractAddress: null,
logs: [ [Object] ],
status: true,
logsBloom:
'0x00000000000000000000008000000000000800000000000000000000000000000000400000000000000000000000000000000004000000000000000000000000000000000000000000000000040000000000000000000
00000000000000000000000000000000000000010000000000040000800000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000080000000000000000',
v: '0x25',
r:
'0x5504eca0e886cd0b3998ac3600efbc83168c25481a368ffa3c198552451ac0a5',
s:
'0x26b84c2b716b221ab7a5162e6038d0737e0fc50f972c8a0900a424bc048cb665',
rawLogs: [ [Object], [Object] ] },
logs:
[ { logIndex: 0,
transactionIndex: 0,
transactionHash:
'0xd4aa6df2a03a234bc4792609c308f72526efed90cc5ab3ecb81617a5344df4c0',
blockHash:
'0x6fd99f88fe74d73978a693bf582a6b6226d63cfff15997606c1b71cf9bdf1f78',
blockNumber: 123,
address: '0xd29f091644cFcD2367B790229957b3D9385D26C9',
type: 'mined',
id: 'log_7cb232ff',
event: 'LogNewProvableQuery',
args: [Result] } ] }
truffle(development)> instance.timestamp()
'1579889839'
Can you please help me how to figure this out?