This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DeliveryLink.sol
155 lines (124 loc) · 4.25 KB
/
DeliveryLink.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
pragma solidity 0.4.24;
import "chainlink/contracts/ChainlinkClient.sol";
import "chainlink/contracts/vendor/Ownable.sol";
contract DeliveryLink is ChainlinkClient, Ownable {
uint256 constant private ORACLE_PAYMENT = 1 * LINK;
string private packageCarrier;
string private packageCode;
string private timestampJobId;
uint256 public timestamp;
event TimestampResponseReceived(
bytes32 indexed requestId,
uint256 indexed timestamp
);
string private deliveryStatusJobId;
string public deliveryStatus;
event DeliveryStatusResponseReceived(
bytes32 indexed requestId,
string indexed deliveryStatus
);
constructor(address _link, address _oracle) public Ownable() {
setChainlinkToken(_link);
setChainlinkOracle(_oracle);
}
function requestCurrentTimestamp()
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(timestampJobId), this, this.handleTimestampResponse.selector);
req.add("get", "https://showcase.linx.twenty57.net:8080/UnixTime/tounixtimestamp?datetime=now");
req.add("path", "UnixTimeStamp");
sendChainlinkRequest(req, ORACLE_PAYMENT);
}
function handleTimestampResponse(bytes32 _requestId, uint256 _timestamp)
public
recordChainlinkFulfillment(_requestId)
{
emit TimestampResponseReceived(_requestId, _timestamp);
timestamp = _timestamp;
}
function requestDeliveryStatus()
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(deliveryStatusJobId), this, this.handleDeliveryStatusResponse.selector);
req.add("car", packageCarrier);
req.add("code", packageCode);
req.add("copyPath", "status");
sendChainlinkRequest(req, ORACLE_PAYMENT);
}
function handleDeliveryStatusResponse(bytes32 _requestId, bytes32 _deliveryStatus)
public
recordChainlinkFulfillment(_requestId)
{
deliveryStatus = bytes32ToString(_deliveryStatus);
emit DeliveryStatusResponseReceived(_requestId, deliveryStatus);
}
function setPackageCarrier(string _packageCarrier) public onlyOwner {
packageCarrier = _packageCarrier;
}
function getPackageCarrier() public view returns (string) {
return packageCarrier;
}
function setPackageCode(string _packageCode) public onlyOwner {
packageCode = _packageCode;
}
function getPackageCode() public view returns (string) {
return packageCode;
}
function setTimestampJobId(string _timestampJobId) public onlyOwner {
timestampJobId = _timestampJobId;
}
function getTimestampJobId() public view returns (string) {
return timestampJobId;
}
function setDeliveryStatusJobId(string _deliveryStatusJobId) public onlyOwner {
deliveryStatusJobId = _deliveryStatusJobId;
}
function getDeliveryStatusJobId() public view returns (string) {
return deliveryStatusJobId;
}
function getChainlinkOracle() public view returns (address) {
return chainlinkOracleAddress();
}
function updateChainlinkOracle(address _oracle) public onlyOwner {
setChainlinkOracle(_oracle);
}
function getChainlinkToken() public view returns (address) {
return chainlinkTokenAddress();
}
function updateChainlinkToken(address _link) public onlyOwner {
setChainlinkToken(_link);
}
function withdrawLink() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer");
}
function cancelRequest(
bytes32 _requestId,
uint256 _payment,
bytes4 _callbackFunctionId,
uint256 _expiration
)
public
onlyOwner
{
cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration);
}
function stringToBytes32(string memory source) private pure returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly { // solhint-disable-line no-inline-assembly
result := mload(add(source, 32))
}
}
function bytes32ToString(bytes32 source) private pure returns (string result) {
bytes memory tempBytes = new bytes(32);
for (uint256 byteNdx; byteNdx < 32; ++byteNdx) {
tempBytes[byteNdx] = source[byteNdx];
}
return string(tempBytes);
}
}