Skip to content

Commit 75633a5

Browse files
committed
Interactions with SC
1 parent 560efa5 commit 75633a5

File tree

9 files changed

+89
-14
lines changed

9 files changed

+89
-14
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
node_modules
2+
node_modules
3+
config.js

config.js renamed to config.example.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ module.exports = {
22
name: 'API',
33
env: process.env.NODE_ENV || 'development',
44
port: process.env.PORT || 3000,
5-
base_url: process.env.BASE_URL || 'http://localhost:3000'
5+
contractAddress: '',
6+
RPCEndpoint: 'http://neo.marinpetrunic.com:30333/',
7+
wif: '',
68
};

controllers/exchange.controller.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const defaultCurrency = 'USD';
2+
3+
function getNEOPrice(currency = defaultCurrency) {
4+
// TODO: fetch live ticker to get the last price
5+
return 30; // 1 NEO = 30$
6+
}
7+
8+
exports.calculateAmountOfNEO = function(amount, currency = defaultCurrency) {
9+
const price = getNEOPrice();
10+
return Math.round(amount / price);
11+
};

controllers/payment.controller.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
const CronJob = require('cron').CronJob;
2+
const neo = require('neo-api-js');
3+
const Neon = require('neon-js');
24

3-
exports.create_savings = function (data) {
5+
const config = require('../config');
6+
const NodeService = require("../services/node");
47

8+
exports.open_savings = function (amount) {
9+
const account = Neon.getAccountFromWIFKey(config.wif);
10+
const scriptHash = Neon.getScriptHashFromAddress(config.contractAddress);
11+
const invoke = { operation: 'create', scriptHash };
12+
const intents = [{ assetId: Neon.ASSETS['NEO'], value: amount, scriptHash }];
13+
const gasCost = 0.5;
14+
let signedTx;
15+
console.log("HERE");
16+
NodeService.getBalance(config.contractAddress).then((response) => {
17+
// const balance = response.result.balance;
18+
console.log("Balance is:", response);
19+
const unsignedTx = Neon.create.invocation(account.publicKey, response, intents, invoke, gasCost, { version: 1 });
20+
signedTx = Neon.signTransaction(unsignedTx, account.privateKey);
21+
const hexTx = Neon.serializeTransaction(signedTx);
22+
return Neon.queryRPC(net, 'sendrawtransaction', [hexTx], 4);
23+
}).catch(function(error) {
24+
console.log(error);
25+
}).then((res) => {
26+
if (res.result === true) {
27+
res.txid = Neon.getTransactionHash(signedTx)
28+
}
29+
return res
30+
})
531
};
632

733
exports.start_payment_cron = function (until, period) {
34+
/*
35+
Should use pollingPolicy?
36+
const pollingPolicy = neo.service.createPollingPolicy(interval);
37+
pollingPolicy.onInterval(function () {
38+
});*/
39+
840
const endDate = new Date(until);
941
const currentDate = new Date();
1042
const hours = currentDate.getHours();

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const config = require('./config');
22
const restify = require('restify');
3-
const restifyPlugins = require('restify-plugins');
3+
const restifyPlugins = require('restify').plugins;
44
const restifyValidation = require('node-restify-validation');
55
const restifyErrors = require('restify-errors');
66

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"license": "MIT",
88
"dependencies": {
99
"cron": "^1.3.0",
10+
"neo-api-js": "git+https://github.com/CityOfZion/neo-api-js.git",
1011
"neon-js": "git+https://github.com/CityOfZion/neon-js.git",
1112
"node-restify-validation": "^1.1.1",
1213
"restify": "^6.3.1",

routes/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const paymentController = require('../controllers/payment.controller');
88
module.exports = function(server) {
99

1010
server.get('/test', (req, res, next) => {
11-
paymentController.start_payment_cron("11.8.2017. 00:17", "seconds");
12-
next();
11+
// paymentController.start_payment_cron("11.8.2017. 00:17", "seconds");
12+
paymentController.open_savings(10);
1313
});
1414

1515
/**
@@ -32,7 +32,7 @@ module.exports = function(server) {
3232
}
3333

3434
const data = req.body;
35-
paymentController.create_savings(data);
35+
paymentController.open_savings(data);
3636
});
3737

3838
/**

services/node.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const neo = require('neo-api-js');
2+
3+
const config = require('../config');
4+
5+
module.exports = {
6+
7+
getBalance: (address) => {
8+
const node = neo.node(config.RPCEndpoint);
9+
console.log(address);
10+
return node.getBalance(address);
11+
},
12+
13+
queryRPC: (node, method, params) => {
14+
const jsonRequest = _axios2.default.create({ headers: { 'Content-Type': 'application/json' } });
15+
const jsonRpcData = { method: method, params: params, id: id, jsonrpc: '2.0' };
16+
return getRPCEndpoint(net).then(function (rpcEndpoint) {
17+
return jsonRequest.post(rpcEndpoint, jsonRpcData).then(function (response) {
18+
return response.data;
19+
});
20+
});
21+
}
22+
};

yarn.lock

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ code-point-at@^1.0.0:
274274
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"
275275

276276
color-convert@^1.9.0:
277-
version "1.9.0"
278-
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.0.tgz#1accf97dd739b983bf994d56fec8f95853641b7a"
277+
version "1.9.1"
278+
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.1.tgz#c1261107aeb2f294ebffec9ed9ecad529a6097ed"
279279
dependencies:
280280
color-name "^1.1.1"
281281

@@ -1199,9 +1199,15 @@ negotiator@^0.6.1:
11991199
version "0.6.1"
12001200
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9"
12011201

1202+
"neo-api-js@git+https://github.com/CityOfZion/neo-api-js.git":
1203+
version "1.0.1"
1204+
resolved "git+https://github.com/CityOfZion/neo-api-js.git#0b051fce92627f1d3169b63481ab6d0d4e5c5bba"
1205+
dependencies:
1206+
axios "^0.16.2"
1207+
12021208
"neon-js@git+https://github.com/CityOfZion/neon-js.git":
12031209
version "1.1.1"
1204-
resolved "git+https://github.com/CityOfZion/neon-js.git#6fc9b5a4dbc8018f3d60701ebd5d2b261b1ddab6"
1210+
resolved "git+https://github.com/CityOfZion/neon-js.git#0ae7ed07bebe5fff325b9530e8e942068ae771c9"
12051211
dependencies:
12061212
axios "^0.16.2"
12071213
base-x "^3.0.2"
@@ -1375,7 +1381,7 @@ performance-now@^0.2.0:
13751381
version "0.2.0"
13761382
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-0.2.0.tgz#33ef30c5c77d4ea21c5a53869d91b56d8f2555e5"
13771383

1378-
pidusage@^1.1.6:
1384+
pidusage@^1.2.0:
13791385
version "1.2.0"
13801386
resolved "https://registry.yarnpkg.com/pidusage/-/pidusage-1.2.0.tgz#65ee96ace4e08a4cd3f9240996c85b367171ee92"
13811387

@@ -1550,8 +1556,8 @@ restify-plugins@^1.6.0:
15501556
vasync "^1.6.3"
15511557

15521558
restify@^6.3.1:
1553-
version "6.3.1"
1554-
resolved "https://registry.yarnpkg.com/restify/-/restify-6.3.1.tgz#23f68eb91cf184913f48bc3285311e56534167a1"
1559+
version "6.3.2"
1560+
resolved "https://registry.yarnpkg.com/restify/-/restify-6.3.2.tgz#959062c4403e687d39abe43a6c87b10cca12458a"
15551561
dependencies:
15561562
assert-plus "^1.0.0"
15571563
bunyan "^1.8.12"
@@ -1566,7 +1572,7 @@ restify@^6.3.1:
15661572
mime "^1.4.1"
15671573
negotiator "^0.6.1"
15681574
once "^1.4.0"
1569-
pidusage "^1.1.6"
1575+
pidusage "^1.2.0"
15701576
qs "^6.5.1"
15711577
restify-errors "^5.0.0"
15721578
semver "^5.4.1"

0 commit comments

Comments
 (0)