Skip to content

Commit

Permalink
Change to Neon dev and adjust invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
morrigan committed Nov 11, 2017
1 parent 75633a5 commit 11b3eb2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 39 deletions.
40 changes: 18 additions & 22 deletions controllers/payment.controller.js
Expand Up @@ -5,29 +5,25 @@ const Neon = require('neon-js');
const config = require('../config');
const NodeService = require("../services/node");

exports.open_savings = function (amount) {
/**
* Create new savings in smart contract.
* @param deadline - end time for saving in unixtime
* @param name - name/purpose for saving
*/
exports.open_savings = function (deadline, name) {
const name = Neon.u.str2hexstring(name);
const scriptHash = Neon.wallet.getScriptHashFromAddress(config.contractAddress);
// Build script
const sb = Neon.sc.default.create.scriptBuilder();
sb.emitAppCall(scriptHash, "create", [deadline, name]);

// Test the script with invokescript
Neon.rpc.Query.invokeScript(sb.str).execute(NodeService.getNode());

// Create InvocationTransaction for real execution
const account = Neon.getAccountFromWIFKey(config.wif);
const scriptHash = Neon.getScriptHashFromAddress(config.contractAddress);
const invoke = { operation: 'create', scriptHash };
const intents = [{ assetId: Neon.ASSETS['NEO'], value: amount, scriptHash }];
const gasCost = 0.5;
let signedTx;
console.log("HERE");
NodeService.getBalance(config.contractAddress).then((response) => {
// const balance = response.result.balance;
console.log("Balance is:", response);
const unsignedTx = Neon.create.invocation(account.publicKey, response, intents, invoke, gasCost, { version: 1 });
signedTx = Neon.signTransaction(unsignedTx, account.privateKey);
const hexTx = Neon.serializeTransaction(signedTx);
return Neon.queryRPC(net, 'sendrawtransaction', [hexTx], 4);
}).catch(function(error) {
console.log(error);
}).then((res) => {
if (res.result === true) {
res.txid = Neon.getTransactionHash(signedTx)
}
return res
})
const tx = Neon.sc.default.create.invocationTx(account.publicKey, {}, {}, sb.str, 0);
console.log(tx);
};

exports.start_payment_cron = function (until, period) {
Expand Down
Empty file removed controllers/savings.controller.js
Empty file.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -8,7 +8,7 @@
"dependencies": {
"cron": "^1.3.0",
"neo-api-js": "git+https://github.com/CityOfZion/neo-api-js.git",
"neon-js": "git+https://github.com/CityOfZion/neon-js.git",
"neon-js": "git+https://github.com/CityOfZion/neon-js.git#dev",
"node-restify-validation": "^1.1.1",
"restify": "^6.3.1",
"restify-errors": "^5.0.0",
Expand Down
8 changes: 5 additions & 3 deletions routes/index.js
Expand Up @@ -21,8 +21,10 @@ module.exports = function(server) {
content : {
address: { isRequired: true },
startTime: { isRequired: true },
quantity: { isRequired: true, isNatural: true },
amount: { isRequired: true }
endTime: { isRequired: true },
time: { isRequired: true, isNatural: true },
amount: { isRequired: true },
name: { isRequired: true },
}
}}, (req, res, next) => {
if (!req.is('application/json')) {
Expand All @@ -32,7 +34,7 @@ module.exports = function(server) {
}

const data = req.body;
paymentController.open_savings(data);
paymentController.open_savings(data.endTime, data.name);
});

/**
Expand Down
13 changes: 3 additions & 10 deletions services/node.js
Expand Up @@ -6,17 +6,10 @@ module.exports = {

getBalance: (address) => {
const node = neo.node(config.RPCEndpoint);
console.log(address);
return node.getBalance(address);
},

queryRPC: (node, method, params) => {
const jsonRequest = _axios2.default.create({ headers: { 'Content-Type': 'application/json' } });
const jsonRpcData = { method: method, params: params, id: id, jsonrpc: '2.0' };
return getRPCEndpoint(net).then(function (rpcEndpoint) {
return jsonRequest.post(rpcEndpoint, jsonRpcData).then(function (response) {
return response.data;
});
});
}
getNode: () => (
neo.node(config.RPCEndpoint)
)
};
2 changes: 2 additions & 0 deletions services/savings.js
@@ -0,0 +1,2 @@
module.exports = {
};
7 changes: 4 additions & 3 deletions yarn.lock
Expand Up @@ -1205,9 +1205,9 @@ negotiator@^0.6.1:
dependencies:
axios "^0.16.2"

"neon-js@git+https://github.com/CityOfZion/neon-js.git":
version "1.1.1"
resolved "git+https://github.com/CityOfZion/neon-js.git#0ae7ed07bebe5fff325b9530e8e942068ae771c9"
"neon-js@git+https://github.com/CityOfZion/neon-js.git#dev":
version "1.1.0"
resolved "git+https://github.com/CityOfZion/neon-js.git#b8e61d43f136de787a99f42192084e9bbc1a2447"
dependencies:
axios "^0.16.2"
base-x "^3.0.2"
Expand All @@ -1219,6 +1219,7 @@ negotiator@^0.6.1:
fs "^0.0.1-security"
js-scrypt "^0.2.0"
secure-random "^1.1.1"
semver "^5.4.1"
wif "^2.0.6"

node-pre-gyp@^0.6.36:
Expand Down

0 comments on commit 11b3eb2

Please sign in to comment.