forked from cennznet/hackathon-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
110 lines (90 loc) · 3.83 KB
/
Copy pathserver.js
File metadata and controls
110 lines (90 loc) · 3.83 KB
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
const { Api } = require('@cennznet/api');
const express = require('express')
const cors = require('cors')
const app = express();
const bodyParser = require('body-parser');
const util = require('./util')
const createKeypair = require('./util/createKeypair')
const NIKAU_WS = 'wss://nikau.centrality.me/public/ws';
const PETER = '5GWVMKzwKVhdUXAv9dgTUZ4XUxXXTixgFZHnvKHRfwK93Hdn';
const CENNZ = 16000;
//Fonterra Account
const FONTERRA_PATH = './util/5GhH2czRJFktx6mtLjj7jcD3fJPCHB3ofo3PMKAT7xzSRso2.json'
const FONTERRA = '5GhH2czRJFktx6mtLjj7jcD3fJPCHB3ofo3PMKAT7xzSRso2'
const PASSWORD = 'fonterra123'
let delegators = [
[0, "farmer0", "5GWVMKzwKVhdUXAv9dgTUZ4XUxXXTixgFZHnvKHRfwK93Hdn"],
[1, "farmer1", "5GWVMKzwKVhdUXAv9dgTUZ4XUxXXTixgFZHnvKHRfwK93Hdn"],
[2, "farmer2", "5GWVMKzwKVhdUXAv9dgTUZ4XUxXXTixgFZHnvKHRfwK93Hdn"],
[3, "farmer3", "5GWVMKzwKVhdUXAv9dgTUZ4XUxXXTixgFZHnvKHRfwK93Hdn"],
[4, "farmer4", "5GWVMKzwKVhdUXAv9dgTUZ4XUxXXTixgFZHnvKHRfwK93Hdn"],
[5, "farmer5", "5GWVMKzwKVhdUXAv9dgTUZ4XUxXXTixgFZHnvKHRfwK93Hdn"],
[6, "farmer6", "5GWVMKzwKVhdUXAv9dgTUZ4XUxXXTixgFZHnvKHRfwK93Hdn"],
[7, "farmer7", "5GWVMKzwKVhdUXAv9dgTUZ4XUxXXTixgFZHnvKHRfwK93Hdn"],
]
async function _initialize(api, tokenId) {
let data = {}
data.stakingAssetId = await api.query.genericAsset.stakingAssetId(); // 1 on MainNet
data.spendingAssetId = await api.query.genericAsset.spendingAssetId(); // 2 on MainNet
data.CENNZ = await api.query.genericAsset.freeBalance(16000, FONTERRA);
data.CPAY = await api.query.genericAsset.freeBalance(16001, FONTERRA);
data.locks = await api.query.genericAsset.locks(FONTERRA);
data.asset = await api.rpc.genericAsset.registeredAssets();
data.delegators = delegators;
data.timestamp = Date.now();
return data;
}
async function main() {
const api = await Api.create({ provider: NIKAU_WS });
const fonterra = await createKeypair('./util/5GhH2czRJFktx6mtLjj7jcD3fJPCHB3ofo3PMKAT7xzSRso2.json', 'fonterra123');
const init_data = await _initialize(api);
app.use(cors());
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.get('/', async function (req, res) {
res.send(await _initialize(api))
})
app.get('/admin/balance', async (req, res) => {
res.send(await _initialize(api));
})
app.get('/admin/delegators', async (req, res) => {
const delegators = await util.getDelegates(api, 75);
res.send(delegators);
})
app.post('/addDelegate', (req, res) => {
const address = req.body.address;
util.delegatePermission(api, fonterra, address);
})
app.post('/mint', async (req, res) => {
const metadata = req.body.metadata;
await util.mintNew(api, fonterra, fonterra.address, 74, fonterra.address)
.then((tx) => {
console.log(tx)
res.send(`Success! Block hash: ${tx}`)
})
})
app.post('/revokeDelegate', async (req, res) => {
const address = req.body.address;
const delegators = await util.getDelegates(api, 75);
let tokenId = [];
for (let i = 0; i < delegators.length; i++) {
const c = delegators[i];
console.log(c)
if (c.attributes[0].Text == address) {
const cId = parseInt(c.tokenId.get('collectionId'));
const sId = parseInt(c.tokenId.get('seriesId'));
const sN = parseInt(c.tokenId.get('serialNumber'));
tokenId = [cId, sId, sN];
}
}
if (tokenId.length > 0)
await util.revoke(api, fonterra, tokenId);
})
const PORT = 7000 || process.env.PORT;
app.listen(PORT, () => {
console.log(`Example app listening at http://localhost:${PORT}`)
})
}
main().then(() => { }).catch((err) => {
console.log(`Main Error: ${err}`)
});