-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
398 lines (348 loc) · 9.72 KB
/
App.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import Draw from 'ol/interaction/Draw'
import Map from 'ol/Map'
import Overlay from 'ol/Overlay'
import View from 'ol/View'
import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style'
import { LineString, Polygon } from 'ol/geom'
import { OSM, Vector as VectorSource } from 'ol/source'
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer'
import { getArea, getLength } from 'ol/sphere'
import { unByKey } from 'ol/Observable'
import * as olProj from 'ol/proj'
import {useGeographic} from 'ol/proj';
import abi from "./abi.json"
import Web3 from 'web3'
import { UnsupportedChainIdError } from '@web3-react/core'
const raster = new TileLayer({
source: new OSM(),
})
const source = new VectorSource()
const vector = new VectorLayer({
source: source,
style: {
'fill-color': 'rgba(255, 255, 255, 0.2)',
'stroke-color': '#ffcc33',
'stroke-width': 2,
'circle-radius': 7,
'circle-fill-color': '#ffcc33',
},
})
/**
* Currently drawn feature.
* @type {import("../src/ol/Feature.js").default}
*/
let sketch
/**
* The help tooltip element.
* @type {HTMLElement}
*/
let helpTooltipElement
/**
* The help tooltip element.
* @type {HTMLElement}
*/
let coordinates
/**
* Overlay to show the help messages.
* @type {Overlay}
*/
let helpTooltip
/**
* The measure tooltip element.
* @type {HTMLElement}
*/
let measureTooltipElement
/**
* Overlay to show the measurement.
* @type {Overlay}
*/
let measureTooltip
/**
* Message to show when the user is drawing a polygon.
* @type {string}
*/
const continuePolygonMsg = 'Click to continue drawing the polygon'
/**
* Message to show when the user is drawing a line.
* @type {string}
*/
const continueLineMsg = 'Click to continue drawing the line'
//to store user created polygon cooordinates
let coordinatesArray = []
/**
* Handle pointer move.
* @param {import("../src/ol/MapBrowserEvent").default} evt The event.
*/
const pointerMoveHandler = function (evt) {
if (evt.dragging) {
return
}
/** @type {string} */
let helpMsg = 'Click to start drawing'
if (sketch) {
const geom = sketch.getGeometry()
if (geom instanceof Polygon) {
helpMsg = continuePolygonMsg
} else if (geom instanceof LineString) {
helpMsg = continueLineMsg
}
}
helpTooltipElement.innerHTML = helpMsg
helpTooltip.setPosition(evt.coordinate)
helpTooltipElement.classList.remove('hidden')
}
useGeographic();
const map = new Map({
layers: [raster, vector],
target: 'map',
view: new View({
center: [121.1867, 37.6739],
zoom: 2,
}),
})
map.on('pointermove', pointerMoveHandler)
map.getViewport().addEventListener('mouseout', function () {
helpTooltipElement.classList.add('hidden')
})
const typeSelect = document.getElementById('type')
let draw // global so we can remove it later
/**
* Format length output.
* @param {LineString} line The line.
* @return {string} The formatted length.
*/
const formatLength = function (line) {
const length = getLength(line)
let output
if (length > 100) {
output = Math.round((length / 1000) * 100) / 100 + ' ' + 'km'
} else {
output = Math.round(length * 100) / 100 + ' ' + 'm'
}
return output
}
/**
* Format area output.
* @param {Polygon} polygon The polygon.
* @return {string} Formatted area.
*/
const formatArea = function (polygon) {
const area = getArea(polygon)
let output
if (area > 10000) {
output = Math.round((area / 1000000) * 100) / 100 + ' ' + 'km<sup>2</sup>'
} else {
output = Math.round(area * 100) / 100 + ' ' + 'm<sup>2</sup>'
}
return output
}
function addInteraction() {
const type = typeSelect.value == 'area' ? 'Polygon' : 'LineString'
draw = new Draw({
source: source,
type: type,
style: new Style({
fill: new Fill({
color: 'rgba(255, 255, 255, 0.2)',
}),
stroke: new Stroke({
color: 'rgba(0, 0, 0, 0.5)',
lineDash: [10, 10],
width: 2,
}),
image: new CircleStyle({
radius: 5,
stroke: new Stroke({
color: 'rgba(0, 0, 0, 0.7)',
}),
fill: new Fill({
color: 'rgba(255, 255, 255, 0.2)',
}),
}),
}),
})
map.addInteraction(draw)
createMeasureTooltip()
createHelpTooltip()
let listener
draw.on('drawstart', function (evt) {
// set sketch
sketch = evt.feature
/** @type {import("../src/ol/coordinate.js").Coordinate|undefined} */
let tooltipCoord = evt.coordinate
// console.log(tooltipCoord)
listener = sketch.getGeometry().on('change', function (evt) {
const geom = evt.target
let output
if (geom instanceof Polygon) {
output = formatArea(geom)
tooltipCoord = geom.getInteriorPoint().getCoordinates()
} else if (geom instanceof LineString) {
output = formatLength(geom)
tooltipCoord = geom.getLastCoordinate()
}
measureTooltipElement.innerHTML = output
measureTooltip.setPosition(tooltipCoord)
})
})
draw.on('drawend', function (evt) {
const geom = evt.feature.getGeometry()
const tooltipCoord = geom.getCoordinates()
tooltipCoord[0].map((coordPair) => {
var lonlat = olProj.toLonLat(coordPair, 'EPSG:3857')
coordinatesArray.push(lonlat)
})
console.log(coordinatesArray)
measureTooltipElement.className = 'ol-tooltip ol-tooltip-static'
measureTooltip.setOffset([0, -7])
// unset sketch
sketch = null
// unset tooltip so that a new one can be created
measureTooltipElement = null
createMeasureTooltip()
unByKey(listener)
})
}
/**
* Creates a new help tooltip
*/
function createHelpTooltip() {
if (helpTooltipElement) {
helpTooltipElement.parentNode.removeChild(helpTooltipElement)
}
helpTooltipElement = document.createElement('div')
helpTooltipElement.className = 'ol-tooltip hidden'
helpTooltip = new Overlay({
element: helpTooltipElement,
offset: [15, 0],
positioning: 'center-left',
})
map.addOverlay(helpTooltip)
}
/**
* Creates a new measure tooltip
*/
function createMeasureTooltip() {
if (measureTooltipElement) {
measureTooltipElement.parentNode.removeChild(measureTooltipElement)
}
measureTooltipElement = document.createElement('div')
measureTooltipElement.className = 'ol-tooltip ol-tooltip-measure'
measureTooltip = new Overlay({
element: measureTooltipElement,
offset: [0, -15],
positioning: 'bottom-center',
stopEvent: false,
insertFirst: false,
})
map.addOverlay(measureTooltip)
}
/**
* Let user change the geometry type.
*/
typeSelect.onchange = function () {
map.removeInteraction(draw)
addInteraction()
}
addInteraction()
/**
* Get polygon coordinates into smart contract
* */
const API_KEY = '6e9140f0770d3c84709cd033b2918192'
let area = 0
document.getElementById('btn-mint').addEventListener('click', function () {
const api_url = `http://api.agromonitoring.com/agro/1.0/polygons?appid=${API_KEY}&name=chainlink&geo_json=${coordinatesArray}`
let ndvi_url = ""
fetch(api_url)
.then((response) => response.json())
.then((data) => {
console.log(data)
area = data[0].area;
setAreaInHectares(area)
ndvi_url = `http://api.agromonitoring.com/agro/1.0/ndvi/history?start=1530336000&end=1534976000&polyid=${data[0].id}&appid=${API_KEY}`
fetch(ndvi_url)
.then((response) => response.json())
// .then((_) => requestNVRI())
.then((_) => calculateCarbonCreditFromNVRI())
.then((_) => getCarbonCreditsAmount())
})
}
)
document.getElementById('btn-connect').addEventListener('click',async function connect() {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
connectedAccount = account;
if (account) {
document.getElementById("btn-connect").innerHTML = (account.substring(0, 6) + "...");
}
}
)
//CONTRACT INTERACTION
const contractAddress = "0x5e88469cBC070953086b035367C64E404Db6CE30";
let connectedAccount = "0x2Ed70fa3D40d6BFd2288f37c413386A7149eaBc2"
const web3 = new Web3(window.ethereum)
var contract = new web3.eth.Contract(abi, contractAddress)
contract.setProvider(window.ethereum)
window.addEventListener('load', (event) => {
console.log("we've loaded")
//populate proejct data onto page
getNVRI()
});
//SEND FUNCTIONS
function requestNVRI() {
contract.methods
.requestNVRI
.send({ from: connectedAccount})
.on('transactionHash', function (hash) {
console.log(hash)
})
}
function setAreaInHectares(area) {
area = area * 10 ** 5
contract.methods
.setAreaInHectares(area)
.send({ from: connectedAccount})
.on('transactionHash', function (hash) {
console.log(hash)
})
}
// tokens to mint
function calculateCarbonCreditFromNVRI() {
contract.methods
.calculateCarbonCreditFromNVRI()
.send({ from: connectedAccount})
.on('transactionHash', function (hash) {
console.log(hash)
})
}
// function mintCarbonTokens() {
// contract.methods
// .mint(carbon)
// .send({ from: connectedAccount })
// .then(function (result, error) {
// console.log(`TOTAL SUPPLY ${result}`)
// console.log(error)
// })
// }
//CALL FUNCTIONS
function getNVRI() {
contract.methods
.getNVRI()
.call({ from: connectedAccount })
.then(function (result, error) {
console.log(`CARBON SCORE ${result}`)
console.log(error)
document.getElementById("carbon-score").innerHTML = `${(result / (10 ** 16))} / 100`;
})
}
function getCarbonCreditsAmount() {
contract.methods
.getCarbonCreditsAmount()
.call({ from: connectedAccount })
.then(function (result, error) {
console.log(`CARBON CREDITS AMOUNT ${result}`)
console.log(error)
result = result / (10 ** 15)
document.getElementById("carbon").innerHTML = `You are set to receive ${result} CARBON`;
})
}