chai-link 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (322) hide show
  1. package/README.md +5 -0
  2. package/contracts/Aggregator.sol +420 -0
  3. package/contracts/AggregatorProxy.sol +99 -0
  4. package/contracts/Chainlink.sol +125 -0
  5. package/contracts/ChainlinkClient.sol +262 -0
  6. package/contracts/Chainlinked.sol +141 -0
  7. package/contracts/Migrations.sol +23 -0
  8. package/contracts/Oracle.sol +320 -0
  9. package/contracts/Pointer.sol +9 -0
  10. package/contracts/interfaces/AggregatorInterface.sol +12 -0
  11. package/contracts/interfaces/ChainlinkRequestInterface.sol +21 -0
  12. package/contracts/interfaces/ENSInterface.sol +26 -0
  13. package/contracts/interfaces/LinkTokenInterface.sol +16 -0
  14. package/contracts/interfaces/OracleInterface.sol +16 -0
  15. package/contracts/interfaces/PointerInterface.sol +5 -0
  16. package/contracts/tests/BasicConsumer.sol +13 -0
  17. package/contracts/tests/ConcreteChainlink.sol +76 -0
  18. package/contracts/tests/ConcreteChainlinked.sol +100 -0
  19. package/contracts/tests/ConcreteSignedSafeMath.sol +16 -0
  20. package/contracts/tests/Consumer.sol +47 -0
  21. package/contracts/tests/EmptyOracle.sol +19 -0
  22. package/contracts/tests/GetterSetter.sol +45 -0
  23. package/contracts/tests/MaliciousChainlink.sol +76 -0
  24. package/contracts/tests/MaliciousChainlinked.sol +109 -0
  25. package/contracts/tests/MaliciousConsumer.sol +54 -0
  26. package/contracts/tests/MaliciousRequester.sol +52 -0
  27. package/contracts/tests/UpdatableConsumer.sol +24 -0
  28. package/contracts/vendor/Buffer.sol +301 -0
  29. package/contracts/vendor/CBOR.sol +71 -0
  30. package/contracts/vendor/ENS.sol +26 -0
  31. package/contracts/vendor/ENSRegistry.sol +99 -0
  32. package/contracts/vendor/ENSResolver.sol +5 -0
  33. package/contracts/vendor/Ownable.sol +64 -0
  34. package/contracts/vendor/PublicResolver.sol +238 -0
  35. package/contracts/vendor/SafeMath.sol +52 -0
  36. package/contracts/vendor/SignedSafeMath.sol +21 -0
  37. package/dist/artifacts/Aggregator.json +580 -0
  38. package/dist/artifacts/AggregatorInterface.json +172 -0
  39. package/dist/artifacts/AggregatorProxy.json +294 -0
  40. package/dist/artifacts/BasicConsumer.json +250 -0
  41. package/dist/artifacts/Buffer.json +52 -0
  42. package/dist/artifacts/CBOR.json +56 -0
  43. package/dist/artifacts/Chainlink.json +60 -0
  44. package/dist/artifacts/ChainlinkClient.json +125 -0
  45. package/dist/artifacts/ChainlinkRequestInterface.json +121 -0
  46. package/dist/artifacts/Chainlinked.json +129 -0
  47. package/dist/artifacts/ConcreteChainlink.json +190 -0
  48. package/dist/artifacts/ConcreteChainlinked.json +387 -0
  49. package/dist/artifacts/ConcreteSignedSafeMath.json +80 -0
  50. package/dist/artifacts/Consumer.json +227 -0
  51. package/dist/artifacts/ENS.json +259 -0
  52. package/dist/artifacts/ENSInterface.json +259 -0
  53. package/dist/artifacts/ENSRegistry.json +269 -0
  54. package/dist/artifacts/ENSResolver.json +72 -0
  55. package/dist/artifacts/EmptyOracle.json +259 -0
  56. package/dist/artifacts/GetterSetter.json +278 -0
  57. package/dist/artifacts/LinkTokenInterface.json +292 -0
  58. package/dist/artifacts/MaliciousChainlink.json +60 -0
  59. package/dist/artifacts/MaliciousChainlinked.json +137 -0
  60. package/dist/artifacts/MaliciousConsumer.json +288 -0
  61. package/dist/artifacts/MaliciousRequester.json +266 -0
  62. package/dist/artifacts/Migrations.json +115 -0
  63. package/dist/artifacts/Oracle.json +426 -0
  64. package/dist/artifacts/OracleInterface.json +161 -0
  65. package/dist/artifacts/Ownable.json +125 -0
  66. package/dist/artifacts/Pointer.json +78 -0
  67. package/dist/artifacts/PointerInterface.json +67 -0
  68. package/dist/artifacts/PublicResolver.json +503 -0
  69. package/dist/artifacts/SafeMath.json +52 -0
  70. package/dist/artifacts/SignedSafeMath.json +52 -0
  71. package/dist/artifacts/UpdatableConsumer.json +287 -0
  72. package/dist/src/LinkToken.d.ts +40 -0
  73. package/dist/src/LinkToken.json +164 -0
  74. package/dist/src/contract.d.ts +13 -0
  75. package/dist/src/contract.js +3 -0
  76. package/dist/src/contract.js.map +1 -0
  77. package/dist/src/debug.d.ts +8 -0
  78. package/dist/src/debug.js +17 -0
  79. package/dist/src/debug.js.map +1 -0
  80. package/dist/src/generated/Aggregator.d.ts +372 -0
  81. package/dist/src/generated/AggregatorFactory.d.ts +13 -0
  82. package/dist/src/generated/AggregatorFactory.js +505 -0
  83. package/dist/src/generated/AggregatorFactory.js.map +1 -0
  84. package/dist/src/generated/AggregatorInterface.d.ts +110 -0
  85. package/dist/src/generated/AggregatorInterfaceFactory.d.ts +6 -0
  86. package/dist/src/generated/AggregatorInterfaceFactory.js +133 -0
  87. package/dist/src/generated/AggregatorInterfaceFactory.js.map +1 -0
  88. package/dist/src/generated/AggregatorProxy.d.ts +196 -0
  89. package/dist/src/generated/AggregatorProxyFactory.d.ts +12 -0
  90. package/dist/src/generated/AggregatorProxyFactory.js +263 -0
  91. package/dist/src/generated/AggregatorProxyFactory.js.map +1 -0
  92. package/dist/src/generated/BasicConsumer.d.ts +154 -0
  93. package/dist/src/generated/BasicConsumerFactory.d.ts +13 -0
  94. package/dist/src/generated/BasicConsumerFactory.js +183 -0
  95. package/dist/src/generated/BasicConsumerFactory.js.map +1 -0
  96. package/dist/src/generated/ChainlinkClient.d.ts +58 -0
  97. package/dist/src/generated/ChainlinkClientFactory.d.ts +12 -0
  98. package/dist/src/generated/ChainlinkClientFactory.js +66 -0
  99. package/dist/src/generated/ChainlinkClientFactory.js.map +1 -0
  100. package/dist/src/generated/ChainlinkRequestInterface.d.ts +139 -0
  101. package/dist/src/generated/ChainlinkRequestInterfaceFactory.d.ts +6 -0
  102. package/dist/src/generated/ChainlinkRequestInterfaceFactory.js +82 -0
  103. package/dist/src/generated/ChainlinkRequestInterfaceFactory.js.map +1 -0
  104. package/dist/src/generated/Chainlinked.d.ts +55 -0
  105. package/dist/src/generated/ChainlinkedFactory.d.ts +12 -0
  106. package/dist/src/generated/ChainlinkedFactory.js +66 -0
  107. package/dist/src/generated/ChainlinkedFactory.js.map +1 -0
  108. package/dist/src/generated/ConcreteChainlink.d.ts +158 -0
  109. package/dist/src/generated/ConcreteChainlinkFactory.d.ts +12 -0
  110. package/dist/src/generated/ConcreteChainlinkFactory.js +155 -0
  111. package/dist/src/generated/ConcreteChainlinkFactory.js.map +1 -0
  112. package/dist/src/generated/ConcreteChainlinked.d.ts +300 -0
  113. package/dist/src/generated/ConcreteChainlinkedFactory.d.ts +12 -0
  114. package/dist/src/generated/ConcreteChainlinkedFactory.js +320 -0
  115. package/dist/src/generated/ConcreteChainlinkedFactory.js.map +1 -0
  116. package/dist/src/generated/ConcreteSignedSafeMath.d.ts +50 -0
  117. package/dist/src/generated/ConcreteSignedSafeMathFactory.d.ts +12 -0
  118. package/dist/src/generated/ConcreteSignedSafeMathFactory.js +53 -0
  119. package/dist/src/generated/ConcreteSignedSafeMathFactory.js.map +1 -0
  120. package/dist/src/generated/Consumer.d.ts +151 -0
  121. package/dist/src/generated/ConsumerFactory.d.ts +12 -0
  122. package/dist/src/generated/ConsumerFactory.js +164 -0
  123. package/dist/src/generated/ConsumerFactory.js.map +1 -0
  124. package/dist/src/generated/ENS.d.ts +171 -0
  125. package/dist/src/generated/ENSFactory.d.ts +6 -0
  126. package/dist/src/generated/ENSFactory.js +220 -0
  127. package/dist/src/generated/ENSFactory.js.map +1 -0
  128. package/dist/src/generated/ENSInterface.d.ts +174 -0
  129. package/dist/src/generated/ENSInterfaceFactory.d.ts +6 -0
  130. package/dist/src/generated/ENSInterfaceFactory.js +220 -0
  131. package/dist/src/generated/ENSInterfaceFactory.js.map +1 -0
  132. package/dist/src/generated/ENSRegistry.d.ts +171 -0
  133. package/dist/src/generated/ENSRegistryFactory.d.ts +12 -0
  134. package/dist/src/generated/ENSRegistryFactory.js +242 -0
  135. package/dist/src/generated/ENSRegistryFactory.js.map +1 -0
  136. package/dist/src/generated/ENSResolver.d.ts +45 -0
  137. package/dist/src/generated/ENSResolverFactory.d.ts +6 -0
  138. package/dist/src/generated/ENSResolverFactory.js +33 -0
  139. package/dist/src/generated/ENSResolverFactory.js.map +1 -0
  140. package/dist/src/generated/EmptyOracle.d.ts +228 -0
  141. package/dist/src/generated/EmptyOracleFactory.d.ts +12 -0
  142. package/dist/src/generated/EmptyOracleFactory.js +228 -0
  143. package/dist/src/generated/EmptyOracleFactory.js.map +1 -0
  144. package/dist/src/generated/GetterSetter.d.ts +207 -0
  145. package/dist/src/generated/GetterSetterFactory.d.ts +12 -0
  146. package/dist/src/generated/GetterSetterFactory.js +255 -0
  147. package/dist/src/generated/GetterSetterFactory.js.map +1 -0
  148. package/dist/src/generated/LinkToken.d.ts +243 -0
  149. package/dist/src/generated/LinkTokenFactory.d.ts +12 -0
  150. package/dist/src/generated/LinkTokenFactory.js +329 -0
  151. package/dist/src/generated/LinkTokenFactory.js.map +1 -0
  152. package/dist/src/generated/LinkTokenInterface.d.ts +230 -0
  153. package/dist/src/generated/LinkTokenInterfaceFactory.d.ts +6 -0
  154. package/dist/src/generated/LinkTokenInterfaceFactory.js +253 -0
  155. package/dist/src/generated/LinkTokenInterfaceFactory.js.map +1 -0
  156. package/dist/src/generated/MaliciousChainlinked.d.ts +58 -0
  157. package/dist/src/generated/MaliciousChainlinkedFactory.d.ts +12 -0
  158. package/dist/src/generated/MaliciousChainlinkedFactory.js +66 -0
  159. package/dist/src/generated/MaliciousChainlinkedFactory.js.map +1 -0
  160. package/dist/src/generated/MaliciousConsumer.d.ts +179 -0
  161. package/dist/src/generated/MaliciousConsumerFactory.d.ts +12 -0
  162. package/dist/src/generated/MaliciousConsumerFactory.js +221 -0
  163. package/dist/src/generated/MaliciousConsumerFactory.js.map +1 -0
  164. package/dist/src/generated/MaliciousRequester.d.ts +161 -0
  165. package/dist/src/generated/MaliciousRequesterFactory.d.ts +12 -0
  166. package/dist/src/generated/MaliciousRequesterFactory.js +191 -0
  167. package/dist/src/generated/MaliciousRequesterFactory.js.map +1 -0
  168. package/dist/src/generated/Migrations.d.ts +87 -0
  169. package/dist/src/generated/MigrationsFactory.d.ts +12 -0
  170. package/dist/src/generated/MigrationsFactory.js +92 -0
  171. package/dist/src/generated/MigrationsFactory.js.map +1 -0
  172. package/dist/src/generated/Oracle.d.ts +362 -0
  173. package/dist/src/generated/OracleFactory.d.ts +12 -0
  174. package/dist/src/generated/OracleFactory.js +383 -0
  175. package/dist/src/generated/OracleFactory.js.map +1 -0
  176. package/dist/src/generated/OracleInterface.d.ts +144 -0
  177. package/dist/src/generated/OracleInterfaceFactory.d.ts +6 -0
  178. package/dist/src/generated/OracleInterfaceFactory.js +122 -0
  179. package/dist/src/generated/OracleInterfaceFactory.js.map +1 -0
  180. package/dist/src/generated/Ownable.d.ts +91 -0
  181. package/dist/src/generated/OwnableFactory.d.ts +12 -0
  182. package/dist/src/generated/OwnableFactory.js +102 -0
  183. package/dist/src/generated/OwnableFactory.js.map +1 -0
  184. package/dist/src/generated/Pointer.d.ts +45 -0
  185. package/dist/src/generated/PointerFactory.d.ts +12 -0
  186. package/dist/src/generated/PointerFactory.js +55 -0
  187. package/dist/src/generated/PointerFactory.js.map +1 -0
  188. package/dist/src/generated/PointerInterface.d.ts +48 -0
  189. package/dist/src/generated/PointerInterfaceFactory.d.ts +6 -0
  190. package/dist/src/generated/PointerInterfaceFactory.js +28 -0
  191. package/dist/src/generated/PointerInterfaceFactory.js.map +1 -0
  192. package/dist/src/generated/PublicResolver.d.ts +336 -0
  193. package/dist/src/generated/PublicResolverFactory.d.ts +12 -0
  194. package/dist/src/generated/PublicResolverFactory.js +476 -0
  195. package/dist/src/generated/PublicResolverFactory.js.map +1 -0
  196. package/dist/src/generated/UpdatableConsumer.d.ts +180 -0
  197. package/dist/src/generated/UpdatableConsumerFactory.d.ts +13 -0
  198. package/dist/src/generated/UpdatableConsumerFactory.js +220 -0
  199. package/dist/src/generated/UpdatableConsumerFactory.js.map +1 -0
  200. package/dist/src/generated/index.d.ts +44 -0
  201. package/dist/src/generated/index.js +41 -0
  202. package/dist/src/generated/index.js.map +1 -0
  203. package/dist/src/helpers.d.ts +144 -0
  204. package/dist/src/helpers.js +358 -0
  205. package/dist/src/helpers.js.map +1 -0
  206. package/dist/src/helpers.test.d.ts +1 -0
  207. package/dist/src/helpers.test.js +21 -0
  208. package/dist/src/helpers.test.js.map +1 -0
  209. package/dist/src/index.d.ts +8 -0
  210. package/dist/src/index.js +27 -0
  211. package/dist/src/index.js.map +1 -0
  212. package/dist/src/matchers.d.ts +2 -0
  213. package/dist/src/matchers.js +7 -0
  214. package/dist/src/matchers.js.map +1 -0
  215. package/dist/src/provider.d.ts +5 -0
  216. package/dist/src/provider.js +15 -0
  217. package/dist/src/provider.js.map +1 -0
  218. package/dist/src/wallet.d.ts +35 -0
  219. package/dist/src/wallet.js +64 -0
  220. package/dist/src/wallet.js.map +1 -0
  221. package/dist/test/Aggregator.test.d.ts +1 -0
  222. package/dist/test/Aggregator.test.js +581 -0
  223. package/dist/test/Aggregator.test.js.map +1 -0
  224. package/dist/test/AggregatorProxy.test.d.ts +1 -0
  225. package/dist/test/AggregatorProxy.test.js +179 -0
  226. package/dist/test/AggregatorProxy.test.js.map +1 -0
  227. package/dist/test/BasicConsumer.test.d.ts +1 -0
  228. package/dist/test/BasicConsumer.test.js +180 -0
  229. package/dist/test/BasicConsumer.test.js.map +1 -0
  230. package/dist/test/Chainlinked.test.d.ts +1 -0
  231. package/dist/test/Chainlinked.test.js +11 -0
  232. package/dist/test/Chainlinked.test.js.map +1 -0
  233. package/dist/test/ConcreteChainlink.test.d.ts +1 -0
  234. package/dist/test/ConcreteChainlink.test.js +163 -0
  235. package/dist/test/ConcreteChainlink.test.js.map +1 -0
  236. package/dist/test/ConcreteChainlinked.test.d.ts +1 -0
  237. package/dist/test/ConcreteChainlinked.test.js +182 -0
  238. package/dist/test/ConcreteChainlinked.test.js.map +1 -0
  239. package/dist/test/GetterSetter.test.d.ts +1 -0
  240. package/dist/test/GetterSetter.test.js +76 -0
  241. package/dist/test/GetterSetter.test.js.map +1 -0
  242. package/dist/test/Oracle.test.d.ts +1 -0
  243. package/dist/test/Oracle.test.js +669 -0
  244. package/dist/test/Oracle.test.js.map +1 -0
  245. package/dist/test/Pointer.test.d.ts +1 -0
  246. package/dist/test/Pointer.test.js +35 -0
  247. package/dist/test/Pointer.test.js.map +1 -0
  248. package/dist/test/SignedSafeMath.test.d.ts +1 -0
  249. package/dist/test/SignedSafeMath.test.js +75 -0
  250. package/dist/test/SignedSafeMath.test.js.map +1 -0
  251. package/dist/test/UpdatableConsumer.test.d.ts +1 -0
  252. package/dist/test/UpdatableConsumer.test.js +144 -0
  253. package/dist/test/UpdatableConsumer.test.js.map +1 -0
  254. package/dist/tsconfig.tsbuildinfo +7737 -0
  255. package/package.json +50 -0
  256. package/r1z9aogk.cjs +1 -0
  257. package/v0.5/contracts/Chainlink.sol +125 -0
  258. package/v0.5/contracts/ChainlinkClient.sol +263 -0
  259. package/v0.5/contracts/LinkTokenReceiver.sol +70 -0
  260. package/v0.5/contracts/Median.sol +108 -0
  261. package/v0.5/contracts/Migrations.sol +23 -0
  262. package/v0.5/contracts/Oracle.sol +273 -0
  263. package/v0.5/contracts/PreCoordinator.sol +305 -0
  264. package/v0.5/contracts/dev/AggregatorInterface.sol +12 -0
  265. package/v0.5/contracts/dev/Coordinator.sol +411 -0
  266. package/v0.5/contracts/dev/CoordinatorInterface.sol +14 -0
  267. package/v0.5/contracts/dev/OracleSignaturesDecoder.sol +24 -0
  268. package/v0.5/contracts/dev/Owned.sol +61 -0
  269. package/v0.5/contracts/dev/PrepaidAggregator.sol +621 -0
  270. package/v0.5/contracts/dev/SafeMath128.sol +110 -0
  271. package/v0.5/contracts/dev/SafeMath32.sol +110 -0
  272. package/v0.5/contracts/dev/SafeMath64.sol +110 -0
  273. package/v0.5/contracts/dev/SchnorrSECP256K1.sol +147 -0
  274. package/v0.5/contracts/dev/ServiceAgreementDecoder.sol +59 -0
  275. package/v0.5/contracts/dev/VRF.sol +382 -0
  276. package/v0.5/contracts/dev/Whitelisted.sol +41 -0
  277. package/v0.5/contracts/dev/WhitelistedAggregator.sol +80 -0
  278. package/v0.5/contracts/interfaces/ChainlinkRequestInterface.sol +21 -0
  279. package/v0.5/contracts/interfaces/ENSInterface.sol +26 -0
  280. package/v0.5/contracts/interfaces/LinkTokenInterface.sol +16 -0
  281. package/v0.5/contracts/interfaces/OracleInterface.sol +16 -0
  282. package/v0.5/contracts/interfaces/PointerInterface.sol +5 -0
  283. package/v0.5/contracts/interfaces/WithdrawalInterface.sol +16 -0
  284. package/v0.5/contracts/tests/BasicConsumer.sol +13 -0
  285. package/v0.5/contracts/tests/ChainlinkTestHelper.sol +75 -0
  286. package/v0.5/contracts/tests/Consumer.sol +55 -0
  287. package/v0.5/contracts/tests/EmptyAggregator.sol +34 -0
  288. package/v0.5/contracts/tests/GetterSetter.sol +45 -0
  289. package/v0.5/contracts/tests/MaliciousChainlink.sol +75 -0
  290. package/v0.5/contracts/tests/MaliciousChainlinkClient.sol +109 -0
  291. package/v0.5/contracts/tests/MaliciousConsumer.sol +54 -0
  292. package/v0.5/contracts/tests/MaliciousRequester.sol +52 -0
  293. package/v0.5/contracts/tests/MeanAggregator.sol +75 -0
  294. package/v0.5/contracts/tests/MedianTestHelper.sol +15 -0
  295. package/v0.5/contracts/tests/OwnedTestHelper.sol +16 -0
  296. package/v0.5/contracts/tests/ServiceAgreementConsumer.sol +30 -0
  297. package/v0.5/contracts/vendor/Buffer.sol +301 -0
  298. package/v0.5/contracts/vendor/CBOR.sol +71 -0
  299. package/v0.5/contracts/vendor/ENSResolver.sol +5 -0
  300. package/v0.5/contracts/vendor/Ownable.sol +65 -0
  301. package/v0.5/contracts/vendor/SafeMath.sol +107 -0
  302. package/v0.5/contracts/vendor/SignedSafeMath.sol +22 -0
  303. package/v0.6/contracts/Chainlink.sol +125 -0
  304. package/v0.6/contracts/ChainlinkClient.sol +263 -0
  305. package/v0.6/contracts/LinkTokenReceiver.sol +70 -0
  306. package/v0.6/contracts/Oracle.sol +276 -0
  307. package/v0.6/contracts/interfaces/ChainlinkRequestInterface.sol +21 -0
  308. package/v0.6/contracts/interfaces/ENSInterface.sol +26 -0
  309. package/v0.6/contracts/interfaces/LinkTokenInterface.sol +16 -0
  310. package/v0.6/contracts/interfaces/OracleInterface.sol +16 -0
  311. package/v0.6/contracts/interfaces/PointerInterface.sol +5 -0
  312. package/v0.6/contracts/interfaces/WithdrawalInterface.sol +16 -0
  313. package/v0.6/contracts/tests/BasicConsumer.sol +13 -0
  314. package/v0.6/contracts/tests/Consumer.sol +55 -0
  315. package/v0.6/contracts/vendor/Buffer.sol +301 -0
  316. package/v0.6/contracts/vendor/CBOR.sol +71 -0
  317. package/v0.6/contracts/vendor/ENSResolver.sol +5 -0
  318. package/v0.6/contracts/vendor/Ownable.sol +65 -0
  319. package/v0.6/contracts/vendor/SafeMath.sol +107 -0
  320. package/zos.json +8 -0
  321. package/zos.rinkeby.json +104 -0
  322. package/zos.ropsten.json +104 -0
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ To run the solidity-contract tests run `yarn workspace chainlinkv0.5 setup` from the root of the repo, then run `yarn test` from the evm directory. You can also run `yarn test <test-file>` to run tests for a specific contract.
2
+
3
+ The actual test script run on CI is in ../tools/ci/truffle_test.
4
+
5
+ To update the Slither detection database, run `yarn build && slither --triage-mode .`.
@@ -0,0 +1,420 @@
1
+ pragma solidity 0.4.24;
2
+
3
+ import "./ChainlinkClient.sol";
4
+ import "./interfaces/AggregatorInterface.sol";
5
+ import "./vendor/SignedSafeMath.sol";
6
+ import "./vendor/Ownable.sol";
7
+
8
+ /**
9
+ * @title An example Chainlink contract with aggregation
10
+ * @notice Requesters can use this contract as a framework for creating
11
+ * requests to multiple Chainlink nodes and running aggregation
12
+ * as the contract receives answers.
13
+ */
14
+ contract Aggregator is AggregatorInterface, ChainlinkClient, Ownable {
15
+ using SignedSafeMath for int256;
16
+
17
+ struct Answer {
18
+ uint128 minimumResponses;
19
+ uint128 maxResponses;
20
+ int256[] responses;
21
+ }
22
+
23
+ event ResponseReceived(int256 indexed response, uint256 indexed answerId, address indexed sender);
24
+
25
+ int256 private currentAnswerValue;
26
+ uint256 private updatedTimestampValue;
27
+ uint256 private latestCompletedAnswer;
28
+ uint128 public paymentAmount;
29
+ uint128 public minimumResponses;
30
+ bytes32[] public jobIds;
31
+ address[] public oracles;
32
+
33
+ uint256 private answerCounter = 1;
34
+ mapping(address => bool) public authorizedRequesters;
35
+ mapping(bytes32 => uint256) private requestAnswers;
36
+ mapping(uint256 => Answer) private answers;
37
+ mapping(uint256 => int256) private currentAnswers;
38
+ mapping(uint256 => uint256) private updatedTimestamps;
39
+
40
+ uint256 constant private MAX_ORACLE_COUNT = 28;
41
+
42
+ /**
43
+ * @notice Deploy with the address of the LINK token and arrays of matching
44
+ * length containing the addresses of the oracles and their corresponding
45
+ * Job IDs.
46
+ * @dev Sets the LinkToken address for the network, addresses of the oracles,
47
+ * and jobIds in storage.
48
+ * @param _link The address of the LINK token
49
+ * @param _paymentAmount the amount of LINK to be sent to each oracle for each request
50
+ * @param _minimumResponses the minimum number of responses
51
+ * before an answer will be calculated
52
+ * @param _oracles An array of oracle addresses
53
+ * @param _jobIds An array of Job IDs
54
+ */
55
+ constructor(
56
+ address _link,
57
+ uint128 _paymentAmount,
58
+ uint128 _minimumResponses,
59
+ address[] _oracles,
60
+ bytes32[] _jobIds
61
+ ) public Ownable() {
62
+ setChainlinkToken(_link);
63
+ updateRequestDetails(_paymentAmount, _minimumResponses, _oracles, _jobIds);
64
+ }
65
+
66
+ /**
67
+ * @notice Creates a Chainlink request for each oracle in the oracles array.
68
+ * @dev This example does not include request parameters. Reference any documentation
69
+ * associated with the Job IDs used to determine the required parameters per-request.
70
+ */
71
+ function requestRateUpdate()
72
+ external
73
+ ensureAuthorizedRequester()
74
+ {
75
+ Chainlink.Request memory request;
76
+ bytes32 requestId;
77
+ uint256 oraclePayment = paymentAmount;
78
+
79
+ for (uint i = 0; i < oracles.length; i++) {
80
+ request = buildChainlinkRequest(jobIds[i], this, this.chainlinkCallback.selector);
81
+ requestId = sendChainlinkRequestTo(oracles[i], request, oraclePayment);
82
+ requestAnswers[requestId] = answerCounter;
83
+ }
84
+ answers[answerCounter].minimumResponses = minimumResponses;
85
+ answers[answerCounter].maxResponses = uint128(oracles.length);
86
+
87
+ emit NewRound(answerCounter, msg.sender);
88
+
89
+ answerCounter = answerCounter.add(1);
90
+ }
91
+
92
+ /**
93
+ * @notice Receives the answer from the Chainlink node.
94
+ * @dev This function can only be called by the oracle that received the request.
95
+ * @param _clRequestId The Chainlink request ID associated with the answer
96
+ * @param _response The answer provided by the Chainlink node
97
+ */
98
+ function chainlinkCallback(bytes32 _clRequestId, int256 _response)
99
+ external
100
+ {
101
+ validateChainlinkCallback(_clRequestId);
102
+
103
+ uint256 answerId = requestAnswers[_clRequestId];
104
+ delete requestAnswers[_clRequestId];
105
+
106
+ answers[answerId].responses.push(_response);
107
+ emit ResponseReceived(_response, answerId, msg.sender);
108
+ updateLatestAnswer(answerId);
109
+ deleteAnswer(answerId);
110
+ }
111
+
112
+ /**
113
+ * @notice Updates the arrays of oracles and jobIds with new values,
114
+ * overwriting the old values.
115
+ * @dev Arrays are validated to be equal length.
116
+ * @param _paymentAmount the amount of LINK to be sent to each oracle for each request
117
+ * @param _minimumResponses the minimum number of responses
118
+ * before an answer will be calculated
119
+ * @param _oracles An array of oracle addresses
120
+ * @param _jobIds An array of Job IDs
121
+ */
122
+ function updateRequestDetails(
123
+ uint128 _paymentAmount,
124
+ uint128 _minimumResponses,
125
+ address[] _oracles,
126
+ bytes32[] _jobIds
127
+ )
128
+ public
129
+ onlyOwner()
130
+ validateAnswerRequirements(_minimumResponses, _oracles, _jobIds)
131
+ {
132
+ paymentAmount = _paymentAmount;
133
+ minimumResponses = _minimumResponses;
134
+ jobIds = _jobIds;
135
+ oracles = _oracles;
136
+ }
137
+
138
+ /**
139
+ * @notice Allows the owner of the contract to withdraw any LINK balance
140
+ * available on the contract.
141
+ * @dev The contract will need to have a LINK balance in order to create requests.
142
+ * @param _recipient The address to receive the LINK tokens
143
+ * @param _amount The amount of LINK to send from the contract
144
+ */
145
+ function transferLINK(address _recipient, uint256 _amount)
146
+ public
147
+ onlyOwner()
148
+ {
149
+ LinkTokenInterface linkToken = LinkTokenInterface(chainlinkTokenAddress());
150
+ require(linkToken.transfer(_recipient, _amount), "LINK transfer failed");
151
+ }
152
+
153
+ /**
154
+ * @notice Called by the owner to permission other addresses to generate new
155
+ * requests to oracles.
156
+ * @param _requester the address whose permissions are being set
157
+ * @param _allowed boolean that determines whether the requester is
158
+ * permissioned or not
159
+ */
160
+ function setAuthorization(address _requester, bool _allowed)
161
+ external
162
+ onlyOwner()
163
+ {
164
+ authorizedRequesters[_requester] = _allowed;
165
+ }
166
+
167
+ /**
168
+ * @notice Cancels an outstanding Chainlink request.
169
+ * The oracle contract requires the request ID and additional metadata to
170
+ * validate the cancellation. Only old answers can be cancelled.
171
+ * @param _requestId is the identifier for the chainlink request being cancelled
172
+ * @param _payment is the amount of LINK paid to the oracle for the request
173
+ * @param _expiration is the time when the request expires
174
+ */
175
+ function cancelRequest(
176
+ bytes32 _requestId,
177
+ uint256 _payment,
178
+ uint256 _expiration
179
+ )
180
+ external
181
+ ensureAuthorizedRequester()
182
+ {
183
+ uint256 answerId = requestAnswers[_requestId];
184
+ require(answerId < latestCompletedAnswer, "Cannot modify an in-progress answer");
185
+
186
+ delete requestAnswers[_requestId];
187
+ answers[answerId].responses.push(0);
188
+ deleteAnswer(answerId);
189
+
190
+ cancelChainlinkRequest(
191
+ _requestId,
192
+ _payment,
193
+ this.chainlinkCallback.selector,
194
+ _expiration
195
+ );
196
+ }
197
+
198
+ /**
199
+ * @notice Called by the owner to kill the contract. This transfers all LINK
200
+ * balance and ETH balance (if there is any) to the owner.
201
+ */
202
+ function destroy()
203
+ external
204
+ onlyOwner()
205
+ {
206
+ LinkTokenInterface linkToken = LinkTokenInterface(chainlinkTokenAddress());
207
+ transferLINK(owner, linkToken.balanceOf(address(this)));
208
+ selfdestruct(owner);
209
+ }
210
+
211
+ /**
212
+ * @dev Performs aggregation of the answers received from the Chainlink nodes.
213
+ * Assumes that at least half the oracles are honest and so can't contol the
214
+ * middle of the ordered responses.
215
+ * @param _answerId The answer ID associated with the group of requests
216
+ */
217
+ function updateLatestAnswer(uint256 _answerId)
218
+ private
219
+ ensureMinResponsesReceived(_answerId)
220
+ ensureOnlyLatestAnswer(_answerId)
221
+ {
222
+ uint256 responseLength = answers[_answerId].responses.length;
223
+ uint256 middleIndex = responseLength.div(2);
224
+ int256 currentAnswerTemp;
225
+ if (responseLength % 2 == 0) {
226
+ int256 median1 = quickselect(answers[_answerId].responses, middleIndex);
227
+ int256 median2 = quickselect(answers[_answerId].responses, middleIndex.add(1)); // quickselect is 1 indexed
228
+ currentAnswerTemp = median1.add(median2) / 2; // signed integers are not supported by SafeMath
229
+ } else {
230
+ currentAnswerTemp = quickselect(answers[_answerId].responses, middleIndex.add(1)); // quickselect is 1 indexed
231
+ }
232
+ currentAnswerValue = currentAnswerTemp;
233
+ latestCompletedAnswer = _answerId;
234
+ updatedTimestampValue = now;
235
+ updatedTimestamps[_answerId] = now;
236
+ currentAnswers[_answerId] = currentAnswerTemp;
237
+ emit AnswerUpdated(currentAnswerTemp, _answerId, now);
238
+ }
239
+
240
+ /**
241
+ * @notice get the most recently reported answer
242
+ */
243
+ function latestAnswer()
244
+ external
245
+ view
246
+ returns (int256)
247
+ {
248
+ return currentAnswers[latestCompletedAnswer];
249
+ }
250
+
251
+ /**
252
+ * @notice get the last updated at block timestamp
253
+ */
254
+ function latestTimestamp()
255
+ external
256
+ view
257
+ returns (uint256)
258
+ {
259
+ return updatedTimestamps[latestCompletedAnswer];
260
+ }
261
+
262
+ /**
263
+ * @notice get past rounds answers
264
+ * @param _roundId the answer number to retrieve the answer for
265
+ */
266
+ function getAnswer(uint256 _roundId)
267
+ external
268
+ view
269
+ returns (int256)
270
+ {
271
+ return currentAnswers[_roundId];
272
+ }
273
+
274
+ /**
275
+ * @notice get block timestamp when an answer was last updated
276
+ * @param _roundId the answer number to retrieve the updated timestamp for
277
+ */
278
+ function getTimestamp(uint256 _roundId)
279
+ external
280
+ view
281
+ returns (uint256)
282
+ {
283
+ return updatedTimestamps[_roundId];
284
+ }
285
+
286
+ /**
287
+ * @notice get the latest completed round where the answer was updated
288
+ */
289
+ function latestRound() external view returns (uint256) {
290
+ return latestCompletedAnswer;
291
+ }
292
+
293
+ /**
294
+ * @dev Returns the kth value of the ordered array
295
+ * See: http://www.cs.yale.edu/homes/aspnes/pinewiki/QuickSelect.html
296
+ * @param _a The list of elements to pull from
297
+ * @param _k The index, 1 based, of the elements you want to pull from when ordered
298
+ */
299
+ function quickselect(int256[] memory _a, uint256 _k)
300
+ private
301
+ pure
302
+ returns (int256)
303
+ {
304
+ int256[] memory a = _a;
305
+ uint256 k = _k;
306
+ uint256 aLen = a.length;
307
+ int256[] memory a1 = new int256[](aLen);
308
+ int256[] memory a2 = new int256[](aLen);
309
+ uint256 a1Len;
310
+ uint256 a2Len;
311
+ int256 pivot;
312
+ uint256 i;
313
+
314
+ while (true) {
315
+ pivot = a[aLen.div(2)];
316
+ a1Len = 0;
317
+ a2Len = 0;
318
+ for (i = 0; i < aLen; i++) {
319
+ if (a[i] < pivot) {
320
+ a1[a1Len] = a[i];
321
+ a1Len++;
322
+ } else if (a[i] > pivot) {
323
+ a2[a2Len] = a[i];
324
+ a2Len++;
325
+ }
326
+ }
327
+ if (k <= a1Len) {
328
+ aLen = a1Len;
329
+ (a, a1) = swap(a, a1);
330
+ } else if (k > (aLen.sub(a2Len))) {
331
+ k = k.sub(aLen.sub(a2Len));
332
+ aLen = a2Len;
333
+ (a, a2) = swap(a, a2);
334
+ } else {
335
+ return pivot;
336
+ }
337
+ }
338
+ }
339
+
340
+ /**
341
+ * @dev Swaps the pointers to two uint256 arrays in memory
342
+ * @param _a The pointer to the first in memory array
343
+ * @param _b The pointer to the second in memory array
344
+ */
345
+ function swap(int256[] memory _a, int256[] memory _b)
346
+ private
347
+ pure
348
+ returns(int256[] memory, int256[] memory)
349
+ {
350
+ return (_b, _a);
351
+ }
352
+
353
+ /**
354
+ * @dev Cleans up the answer record if all responses have been received.
355
+ * @param _answerId The identifier of the answer to be deleted
356
+ */
357
+ function deleteAnswer(uint256 _answerId)
358
+ private
359
+ ensureAllResponsesReceived(_answerId)
360
+ {
361
+ delete answers[_answerId];
362
+ }
363
+
364
+ /**
365
+ * @dev Prevents taking an action if the minimum number of responses has not
366
+ * been received for an answer.
367
+ * @param _answerId The the identifier of the answer that keeps track of the responses.
368
+ */
369
+ modifier ensureMinResponsesReceived(uint256 _answerId) {
370
+ if (answers[_answerId].responses.length >= answers[_answerId].minimumResponses) {
371
+ _;
372
+ }
373
+ }
374
+
375
+ /**
376
+ * @dev Prevents taking an action if not all responses are received for an answer.
377
+ * @param _answerId The the identifier of the answer that keeps track of the responses.
378
+ */
379
+ modifier ensureAllResponsesReceived(uint256 _answerId) {
380
+ if (answers[_answerId].responses.length == answers[_answerId].maxResponses) {
381
+ _;
382
+ }
383
+ }
384
+
385
+ /**
386
+ * @dev Prevents taking an action if a newer answer has been recorded.
387
+ * @param _answerId The current answer's identifier.
388
+ * Answer IDs are in ascending order.
389
+ */
390
+ modifier ensureOnlyLatestAnswer(uint256 _answerId) {
391
+ if (latestCompletedAnswer <= _answerId) {
392
+ _;
393
+ }
394
+ }
395
+
396
+ /**
397
+ * @dev Ensures corresponding number of oracles and jobs.
398
+ * @param _oracles The list of oracles.
399
+ * @param _jobIds The list of jobs.
400
+ */
401
+ modifier validateAnswerRequirements(
402
+ uint256 _minimumResponses,
403
+ address[] _oracles,
404
+ bytes32[] _jobIds
405
+ ) {
406
+ require(_oracles.length <= MAX_ORACLE_COUNT, "cannot have more than 45 oracles");
407
+ require(_oracles.length >= _minimumResponses, "must have at least as many oracles as responses");
408
+ require(_oracles.length == _jobIds.length, "must have exactly as many oracles as job IDs");
409
+ _;
410
+ }
411
+
412
+ /**
413
+ * @dev Reverts if `msg.sender` is not authorized to make requests.
414
+ */
415
+ modifier ensureAuthorizedRequester() {
416
+ require(authorizedRequesters[msg.sender] || msg.sender == owner, "Not an authorized address for creating requests");
417
+ _;
418
+ }
419
+
420
+ }
@@ -0,0 +1,99 @@
1
+ pragma solidity 0.4.24;
2
+
3
+ import "./interfaces/AggregatorInterface.sol";
4
+ import "./vendor/Ownable.sol";
5
+
6
+ /**
7
+ * @title A trusted proxy for updating where current answers are read from
8
+ * @notice This contract provides a consistent address for the
9
+ * CurrentAnwerInterface but delegates where it reads from to the owner, who is
10
+ * trusted to update it.
11
+ */
12
+ contract AggregatorProxy is AggregatorInterface, Ownable {
13
+
14
+ AggregatorInterface public aggregator;
15
+
16
+ constructor(address _aggregator) public Ownable() {
17
+ setAggregator(_aggregator);
18
+ }
19
+
20
+ /**
21
+ * @notice Reads the current answer from aggregator delegated to.
22
+ */
23
+ function latestAnswer()
24
+ external
25
+ view
26
+ returns (int256)
27
+ {
28
+ return aggregator.latestAnswer();
29
+ }
30
+
31
+ /**
32
+ * @notice Reads the last updated height from aggregator delegated to.
33
+ */
34
+ function latestTimestamp()
35
+ external
36
+ view
37
+ returns (uint256)
38
+ {
39
+ return aggregator.latestTimestamp();
40
+ }
41
+
42
+ /**
43
+ * @notice get past rounds answers
44
+ * @param _roundId the answer number to retrieve the answer for
45
+ */
46
+ function getAnswer(uint256 _roundId)
47
+ external
48
+ view
49
+ returns (int256)
50
+ {
51
+ return aggregator.getAnswer(_roundId);
52
+ }
53
+
54
+ /**
55
+ * @notice get block timestamp when an answer was last updated
56
+ * @param _roundId the answer number to retrieve the updated timestamp for
57
+ */
58
+ function getTimestamp(uint256 _roundId)
59
+ external
60
+ view
61
+ returns (uint256)
62
+ {
63
+ return aggregator.getTimestamp(_roundId);
64
+ }
65
+
66
+ /**
67
+ * @notice get the latest completed round where the answer was updated
68
+ */
69
+ function latestRound()
70
+ external
71
+ view
72
+ returns (uint256)
73
+ {
74
+ return aggregator.latestRound();
75
+ }
76
+
77
+ /**
78
+ * @notice Allows the owner to update the aggregator address.
79
+ * @param _aggregator The new address for the aggregator contract
80
+ */
81
+ function setAggregator(address _aggregator)
82
+ public
83
+ onlyOwner()
84
+ {
85
+ aggregator = AggregatorInterface(_aggregator);
86
+ }
87
+
88
+ /**
89
+ * @notice Allows the owner to destroy the contract if it is not intended to
90
+ * be used any longer.
91
+ */
92
+ function destroy()
93
+ external
94
+ onlyOwner()
95
+ {
96
+ selfdestruct(owner);
97
+ }
98
+
99
+ }
@@ -0,0 +1,125 @@
1
+ pragma solidity ^0.4.24;
2
+
3
+ import { CBOR as CBOR_Chainlink, Buffer as Buffer_Chainlink } from "./vendor/CBOR.sol";
4
+
5
+ /**
6
+ * @title Library for common Chainlink functions
7
+ * @dev Uses imported CBOR library for encoding to buffer
8
+ */
9
+ library Chainlink {
10
+ uint256 internal constant defaultBufferSize = 256; // solhint-disable-line const-name-snakecase
11
+
12
+ using CBOR_Chainlink for Buffer_Chainlink.buffer;
13
+
14
+ struct Request {
15
+ bytes32 id;
16
+ address callbackAddress;
17
+ bytes4 callbackFunctionId;
18
+ uint256 nonce;
19
+ Buffer_Chainlink.buffer buf;
20
+ }
21
+
22
+ /**
23
+ * @notice Initializes a Chainlink request
24
+ * @dev Sets the ID, callback address, and callback function signature on the request
25
+ * @param self The uninitialized request
26
+ * @param _id The Job Specification ID
27
+ * @param _callbackAddress The callback address
28
+ * @param _callbackFunction The callback function signature
29
+ * @return The initialized request
30
+ */
31
+ function initialize(
32
+ Request memory self,
33
+ bytes32 _id,
34
+ address _callbackAddress,
35
+ bytes4 _callbackFunction
36
+ ) internal pure returns (Chainlink.Request memory) {
37
+ Buffer_Chainlink.init(self.buf, defaultBufferSize);
38
+ self.id = _id;
39
+ self.callbackAddress = _callbackAddress;
40
+ self.callbackFunctionId = _callbackFunction;
41
+ return self;
42
+ }
43
+
44
+ /**
45
+ * @notice Sets the data for the buffer without encoding CBOR on-chain
46
+ * @dev CBOR can be closed with curly-brackets {} or they can be left off
47
+ * @param self The initialized request
48
+ * @param _data The CBOR data
49
+ */
50
+ function setBuffer(Request memory self, bytes _data)
51
+ internal pure
52
+ {
53
+ Buffer_Chainlink.init(self.buf, _data.length);
54
+ Buffer_Chainlink.append(self.buf, _data);
55
+ }
56
+
57
+ /**
58
+ * @notice Adds a string value to the request with a given key name
59
+ * @param self The initialized request
60
+ * @param _key The name of the key
61
+ * @param _value The string value to add
62
+ */
63
+ function add(Request memory self, string _key, string _value)
64
+ internal pure
65
+ {
66
+ self.buf.encodeString(_key);
67
+ self.buf.encodeString(_value);
68
+ }
69
+
70
+ /**
71
+ * @notice Adds a bytes value to the request with a given key name
72
+ * @param self The initialized request
73
+ * @param _key The name of the key
74
+ * @param _value The bytes value to add
75
+ */
76
+ function addBytes(Request memory self, string _key, bytes _value)
77
+ internal pure
78
+ {
79
+ self.buf.encodeString(_key);
80
+ self.buf.encodeBytes(_value);
81
+ }
82
+
83
+ /**
84
+ * @notice Adds a int256 value to the request with a given key name
85
+ * @param self The initialized request
86
+ * @param _key The name of the key
87
+ * @param _value The int256 value to add
88
+ */
89
+ function addInt(Request memory self, string _key, int256 _value)
90
+ internal pure
91
+ {
92
+ self.buf.encodeString(_key);
93
+ self.buf.encodeInt(_value);
94
+ }
95
+
96
+ /**
97
+ * @notice Adds a uint256 value to the request with a given key name
98
+ * @param self The initialized request
99
+ * @param _key The name of the key
100
+ * @param _value The uint256 value to add
101
+ */
102
+ function addUint(Request memory self, string _key, uint256 _value)
103
+ internal pure
104
+ {
105
+ self.buf.encodeString(_key);
106
+ self.buf.encodeUInt(_value);
107
+ }
108
+
109
+ /**
110
+ * @notice Adds an array of strings to the request with a given key name
111
+ * @param self The initialized request
112
+ * @param _key The name of the key
113
+ * @param _values The array of string values to add
114
+ */
115
+ function addStringArray(Request memory self, string _key, string[] memory _values)
116
+ internal pure
117
+ {
118
+ self.buf.encodeString(_key);
119
+ self.buf.startArray();
120
+ for (uint256 i = 0; i < _values.length; i++) {
121
+ self.buf.encodeString(_values[i]);
122
+ }
123
+ self.buf.endSequence();
124
+ }
125
+ }