Skip to content

Commit d547ec3

Browse files
committed
Fix invocation
1 parent e37ed95 commit d547ec3

File tree

7 files changed

+112
-32
lines changed

7 files changed

+112
-32
lines changed

controllers/payment.controller.js

+26-12
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,33 @@ const config = require('../config');
1111
const node = require("../services/blockchain");
1212
const calculateAmountOfNEO = require("./../services/exchange.js");
1313
const closeSavings = require("./../services/savings");
14+
const util = require('../util');
1415

15-
exports.open_savings = function (deadline, name) {
16-
const name = Neon.u.str2hexstring(name);
17-
// Build script
18-
const sb = Neon.sc.default.create.scriptBuilder();
19-
sb.emitAppCall(config.scriptHash, "create", [name, deadline]);
2016

21-
const tx = node.execute_transaction(sb);
22-
console.log(tx);
17+
exports.open_savings = function (duration, savingsName) {
18+
const name = util.str2hex(savingsName);
19+
const account = Neon.getAccountFromWIFKey(config.wif);
20+
const scriptHash = config.scriptHash;
21+
const args = [name, duration];
22+
const invoke = { operation: 'createSavings', scriptHash , args};
23+
const intents = [{ assetId: util.ASSETS['NEO'], value: 0, scriptHash }];
24+
const gasCost = 10;
25+
let signedTx;
26+
27+
node.getBalance(account.address).then((balances) => {
28+
const unsignedTx = Neon.create.invocation(account.publicKeyEncoded, balances, intents, invoke, gasCost, { version: 1 });
29+
signedTx = Neon.signTransaction(unsignedTx, account.privateKey);
30+
const hexTx = Neon.serializeTransaction(signedTx);
31+
return node.queryRPC('sendrawtransaction', [hexTx]);
32+
}).catch(function(error) {
33+
console.log(error);
34+
}).then((res) => {
35+
console.log(res);
36+
})
2337
};
2438

25-
function sendNEOToSmartContract(address, name, amount) {
26-
const name = Neon.u.str2hexstring(name);
39+
function sendNEOToSmartContract(address, savingsName, amount) {
40+
const name = Neon.u.str2hexstring(savingsName);
2741
// Build script
2842
const sb = Neon.sc.default.create.scriptBuilder();
2943
sb.emitAppCall(config.scriptHash, "addFunds", [address, name, amount]);
@@ -40,20 +54,20 @@ function makePayment(address, name, amount) {
4054
/**
4155
* Start cronjob to run every `time` minutes.
4256
* We use minutes just for development purposes.
43-
* @param endTime Date when saving should end.
57+
* @param endDate Date when saving should end.
4458
* @param time Period between payments (in minutes).
4559
* @param amount Amount in USD to deposit.
4660
* @param address Savings owner address
4761
* @param name Savings name/purpose
4862
*/
49-
exports.start_payment_cron = function (endTime, time, amount, address, name) {
63+
exports.start_payment_cron = function (endDate, time, amount, address, name) {
5064
/*
5165
Should use pollingPolicy?
5266
const pollingPolicy = neo.service.createPollingPolicy(interval);
5367
pollingPolicy.onInterval(function () {
5468
});*/
5569

56-
const endTime = new Date(endTime);
70+
const endTime = new Date(endDate);
5771
const cronTime = `*/${time} * * * *`;
5872

5973
const job = new CronJob({

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
"repository": "git@github.com:BlockSaver/backend.git",
77
"license": "MIT",
88
"dependencies": {
9+
"axios": "^0.17.1",
910
"cron": "^1.3.0",
1011
"neo-api-js": "git+https://github.com/CityOfZion/neo-api-js.git",
11-
"neon-js": "git+https://github.com/CityOfZion/neon-js.git#dev",
12+
"neon-js": "git+https://github.com/CityOfZion/neon-js.git",
1213
"node-restify-validation": "^1.1.1",
1314
"restify": "^6.3.1",
1415
"restify-errors": "^5.0.0",

routes/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const closeSavings = require('../services/savings');
1111
module.exports = function(server) {
1212

1313
server.get('/test', (req, res, next) => {
14-
// paymentController.start_payment_cron("11.8.2017. 00:17", "seconds");
15-
paymentController.open_savings(10);
14+
const endDate = new Date().getTime() + 1000;
15+
paymentController.open_savings(endDate, "Testing bab bam");
16+
// paymentController.start_payment_cron(data.endTime, data.time, data.amount, data.address, data.name);
1617
});
1718

1819
/**

services/blockchain.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
11
const neo = require('neo-api-js');
2+
const Neon = require('neon-js');
23

3-
const node = require("../services/blockchain");
44
const config = require('../config');
5+
const axios = require("axios");
56

67
module.exports = {
78

89
getBalance: (address) => {
9-
const node = neo.node(config.RPCEndpoint);
10-
return node.getBalance(address);
10+
return axios.get(config.RESTEndpoint + '/v2/address/balance/' + address)
11+
.then((res) => {
12+
return res.data
13+
// const neo = res.data.NEO.balance
14+
// const gas = res.data.GAS.balance
15+
// return { Neo: neo, Gas: gas, unspent: { Neo: res.data.NEO.unspent, Gas: res.data.GAS.unspent } }
16+
})
17+
},
18+
19+
getNode: () => {
20+
return neo.node(config.RPCEndpoint)
1121
},
1222

13-
getNode: () => (
14-
neo.node(config.RPCEndpoint)
15-
),
23+
getRPCEndpoint: () => {
24+
return axios.get(config.RESTEndpoint + '/v2/network/best_node').then(function (response) {
25+
return response.data.node;
26+
});
27+
},
28+
29+
queryRPC: (method, params, id = 1) => {
30+
const jsonRequest = axios.create({ headers: { 'Content-Type': 'application/json' } });
31+
const jsonRpcData = { method, params, id, jsonrpc: '2.0' };
32+
return module.exports.getRPCEndpoint().then(function (rpcEndpoint) {
33+
return jsonRequest.post(rpcEndpoint, jsonRpcData).then(function (response) {
34+
return response.data;
35+
});
36+
});
37+
},
1638

1739
execute_transaction: (sb) => {
1840
// Test the script with invokescript
19-
Neon.rpc.Query.invokeScript(sb.str).execute(node.getNode());
41+
const node = neo.node(config.RPCEndpoint);
42+
Neon.rpc.Query.invokeScript(sb.str).execute(node);
2043
// Create InvocationTransaction for real execution
2144
const account = Neon.sc.default.create.account(config.wif);
22-
return Neon.sc.default.create.invocationTx(account.publicKey, {}, {}, sb.str, 0);
45+
const invoke = { operation: 'createSavings', scriptHash: sb.str };
46+
return tx.create.createInvocationTx(account.publicKey, {}, {}, invoke, 0);
2347
}
2448
};

services/savings.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ exports.Savings = (function(){
1515
};
1616
})();
1717

18-
exports.getSavingsState = function(name) {
19-
const name = Neon.u.str2hexstring(name);
18+
exports.getSavingsState = function(savingsName) {
19+
const name = Neon.u.str2hexstring(savingsName);
2020
// Build script
2121
const sb = Neon.sc.default.create.scriptBuilder();
2222
sb.emitAppCall(config.scriptHash, "getSavingsByName", name);
@@ -31,8 +31,8 @@ exports.makeSavingsWithdrawal = function(name, savingsState) {
3131

3232
};
3333

34-
exports.closeSavings = function (name) {
35-
const name = Neon.u.str2hexstring(name);
34+
exports.closeSavings = function (savingsName) {
35+
const name = Neon.u.str2hexstring(savingsName);
3636
// Build script
3737
const sb = Neon.sc.default.create.scriptBuilder();
3838
sb.emitAppCall(config.scriptHash, "closeSavings", name);

util.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = {
2+
ASSETS: {
3+
NEO: 'c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b',
4+
'c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b': 'NEO',
5+
GAS: '602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7',
6+
'602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7': 'GAS'
7+
},
8+
9+
int2hex: (mNumber) => {
10+
const h = mNumber.toString(16);
11+
return h.length % 2 ? '0' + h : h;
12+
},
13+
14+
/**
15+
* Converts a number to a hexstring of a suitable size
16+
* @param {number} num
17+
* @param {number} size - The required size in chars, eg 2 for Uint8, 4 for Uint16. Defaults to 2.
18+
*/
19+
num2hexstring: (num, size = 2) => {
20+
let hexstring = num.toString(16);
21+
return hexstring.length % size === 0 ? hexstring : ('0'.repeat(size) + hexstring).substring(hexstring.length);
22+
},
23+
24+
str2hex: (str) => {
25+
let hex;
26+
let result = "";
27+
for (let i = 0; i < str.length; i++) {
28+
hex = str.charCodeAt(i).toString(16);
29+
result += ("000" + hex).slice(-4);
30+
}
31+
32+
return result;
33+
}
34+
};

yarn.lock

+11-5
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ axios@^0.16.2:
108108
follow-redirects "^1.2.3"
109109
is-buffer "^1.1.5"
110110

111+
axios@^0.17.1:
112+
version "0.17.1"
113+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.17.1.tgz#2d8e3e5d0bdbd7327f91bc814f5c57660f81824d"
114+
dependencies:
115+
follow-redirects "^1.2.5"
116+
is-buffer "^1.1.5"
117+
111118
balanced-match@^1.0.0:
112119
version "1.0.0"
113120
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
@@ -544,7 +551,7 @@ fill-range@^2.1.0:
544551
repeat-element "^1.1.2"
545552
repeat-string "^1.5.2"
546553

547-
follow-redirects@^1.2.3:
554+
follow-redirects@^1.2.3, follow-redirects@^1.2.5:
548555
version "1.2.5"
549556
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.2.5.tgz#ffd3e14cbdd5eaa72f61b6368c1f68516c2a26cc"
550557
dependencies:
@@ -1205,9 +1212,9 @@ negotiator@^0.6.1:
12051212
dependencies:
12061213
axios "^0.16.2"
12071214

1208-
"neon-js@git+https://github.com/CityOfZion/neon-js.git#dev":
1209-
version "1.1.0"
1210-
resolved "git+https://github.com/CityOfZion/neon-js.git#b8e61d43f136de787a99f42192084e9bbc1a2447"
1215+
"neon-js@git+https://github.com/CityOfZion/neon-js.git":
1216+
version "1.1.1"
1217+
resolved "git+https://github.com/CityOfZion/neon-js.git#0ae7ed07bebe5fff325b9530e8e942068ae771c9"
12111218
dependencies:
12121219
axios "^0.16.2"
12131220
base-x "^3.0.2"
@@ -1219,7 +1226,6 @@ negotiator@^0.6.1:
12191226
fs "^0.0.1-security"
12201227
js-scrypt "^0.2.0"
12211228
secure-random "^1.1.1"
1222-
semver "^5.4.1"
12231229
wif "^2.0.6"
12241230

12251231
node-pre-gyp@^0.6.36:

0 commit comments

Comments
 (0)