-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
96 lines (72 loc) · 2.9 KB
/
handler.js
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
require('dotenv').config()
const { TezosToolkit } = require('@taquito/taquito')
const { InMemorySigner } = require('@taquito/signer')
const axios = require('axios')
const _ = require('lodash')
const ungrund = proccess.env.KT_ORACLE
const Tezos = new TezosToolkit(process.env.TZ_ENDPOINT)
Tezos.setProvider({
signer : new InMemorySigner(process.env.TZ_SK)
})
// FUNCTIONS
// key management
// fail configuration
const inject = async (reqId, result) => {
console.log([reqId, result])
try {
let kt = await Tezos.contract.at(ungrund)
let op = await kt.methods.fullFillRequest(reqId, result).send()
} catch (e) {
console.log(e)
}
}
//inject()
// requests link and path from storage for such stateId
const getStorage = async (network, kt) => {
let ptr = await axios.get(`https://api.better-call.dev/v1/contract/${network}/${kt}/storage`).then(res => res.data.children[0].value)
let bigMap = await axios.get(`https://api.better-call.dev/v1/bigmap/${network}/${ptr}/keys`).then(res => res.data)
let keys = bigMap.map(e => e.data).filter(e => e.value.children[1].value == false).map(e => parseInt(e.key.value)).sort()
console.log(keys)
let callback = bigMap.filter(e => e.data.key.value == keys[0])[0].data.value.children[0].value
let payload = bigMap.filter(e => e.data.key.value == keys[0])[0].data.value.children[2].children.map(e => e.value)
console.log(callback) //verify if callback is configured with proper parameters entrypoint
console.log(payload)
//verify if args follows a pattern
return [keys[0], payload]
}
//getStorage('delphinet', ungrund)
//getStorage('delphinet', 'KT1VNKfd8dVzQ1rvkRDQrMfJgwGFUdMBcxdZ')
const fullFillRequest = async (link) => {
return await axios.get(link).then(res => res.data)
}
const compare = async (request, response) => {
var response = await response
var allkeys = _.union(_.keys(request), _.keys(response))
var difference = _.reduce(allkeys, function (result, key) {
if (!_.isEqual(request[key], response[key])) {
result[key] = { request: request[key], response: response[key] }
}
return result
}, {})
const key = Object.keys(request).find(key => request[key] === "UNG")
//console.log(key)
//console.log(difference[key].response)
return difference[key].response
}
// PROCEDURE
const mult = (response, factor) => {
return response * factor
}
const procedure = async () => {
var arr = await getStorage('delphinet', ungrund)
console.log(arr)
var requestResult = await fullFillRequest(arr[1][0])
console.log(requestResult)
var nat = await compare(JSON.parse(arr[1][2]), requestResult)
await inject(arr[0], nat * arr[1][1])
}
procedure()
//let res = compare(JSON.parse('{"BRL":"UNG"}'), fullFillRequest('https://min-api.cryptocompare.com/data/price?fsym=USDT&tsyms=BRL'))
//let res = compare(JSON.parse('{"randomness" : "UNG"}'), fullFillRequest('https://api.drand.sh/public/latest'))
//console.log(mult(res, 1000))
// HANDLE