github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/examples/xc/qtum/dapp/server.js (about) 1 const express = require('express'); 2 const app = express(); 3 const bodyParser = require('body-parser'); 4 const ora = require("ora") 5 // const parseArgs = require("minimist") 6 7 const { 8 Qtum 9 } = require("qtumjs") 10 11 const repoData = require("../solar.development") 12 const qtum = new Qtum("http://qtum:test@localhost:3889", repoData) 13 const contracts = { 14 "INK": "contracts/INK.sol", 15 "XC": "contracts/XC.sol", 16 "XCPlugin": "contracts/XCPlugin.sol", 17 } 18 19 const XCPlugin = repoData.contracts[contracts["XCPlugin"]] 20 const INK = repoData.contracts[contracts["INK"]] 21 const XC = repoData.contracts[contracts["XC"]] 22 23 // application/x-www-form-urlencoded 24 const urlencodedParser = bodyParser.urlencoded({extended: false}) 25 26 app.use(express.static('public')); 27 28 async function call(contract,method,params,fromAddr) { 29 var name = contracts[contract]; 30 var abi = repoData.contracts[name].abi 31 for (var i=0; i < abi.length; i++) { 32 var a = abi[i] 33 if (a.name == method && a.type == "function") { 34 if (a.constant){ 35 var result = await qtum.contract(name).call(method,params) 36 console.log("log:", result.outputs[0]) 37 return result.outputs 38 }else{ 39 const tx = await qtum.contract(name).send(method,params, { 40 senderAddress: fromAddr, 41 gasLimit: 40000000, 42 }) 43 console.log("transfer tx:", tx.txid) 44 console.log(tx) 45 // or: await tx.confirm(1) 46 const confirmation = tx.confirm(1) 47 ora.promise(confirmation, "confirm transfer") 48 await confirmation 49 } 50 } 51 } 52 } 53 54 app.post('/xc/init/', urlencodedParser,async function (req, res) { 55 56 var result = await call("XCPlugin","addPlatform",[req.body.platform], XCPlugin.sender) 57 console.log("XCPlugin addPlatform:",result) 58 59 result = await call("XCPlugin","addPublicKey",[req.body.platform,req.body.publicKey], XCPlugin.sender) 60 console.log("XCPlugin addPublicKey:",result) 61 62 result = await call("XCPlugin","addPublicKey",[req.body.platform,req.body.publicKey2], XCPlugin.sender) 63 console.log("XCPlugin addPublicKey2:",result) 64 65 result = await call("XCPlugin","addCaller",[XC.address], XCPlugin.sender) 66 console.log("XCPlugin addCaller:",result) 67 68 // XC 69 result = await call("XC","setINK",[INK.address], XC.sender) 70 console.log("XC setINK:",result) 71 72 result = await call("XC","setXCPlugin",[XCPlugin.address], XC.sender) 73 console.log("XC setXCPlugin:",result) 74 75 // start 76 result = await call("XCPlugin","start",[], XCPlugin.sender) 77 console.log("XCPlugin start:",result) 78 79 result = await call("XC","setStatus",[3], XC.sender) 80 console.log("XC start:",result) 81 var response = { 82 "ok":"ok" 83 }; 84 res.end(JSON.stringify(response)); 85 }); 86 87 app.post('/xc/out/', urlencodedParser,async function (req, res) { 88 89 console.log(JSON.stringify(req.body)) 90 91 var result = await call("INK","approve",[XC.address,req.body.value],INK.sender) 92 console.log("approve:",result) 93 var result2 = await call("INK","allowance",[INK.senderHex,XC.address]) 94 console.log("allowance:",result2[0].toString()) 95 96 var result3 = await call("XC","lock",[req.body.toPlatform,req.body.toAccount,req.body.value],INK.sender) 97 console.log("lock:",result3) 98 99 var result4 = await call("XC","lockBalance") 100 console.log("lock:",result4) 101 102 var response = { 103 "lockBalance":result4.toString() 104 }; 105 console.log(response); 106 res.end(JSON.stringify(response)); 107 }); 108 109 app.post('/xc/in/', urlencodedParser,async function (req, res) { 110 111 var sign = req.body.sign; 112 var r = sign.substr(0,64) 113 var s = sign.substr(64,64) 114 var v = sign.substr(128,2) 115 var _v = 27; 116 if ( v == '00' || v == '1b') { 117 _v = 27 118 } else if ( v == '01' || v == '1c') { 119 _v = 28 120 } 121 console.log(req.body.fromPlatform, req.body.fromAccount, req.body.toAccount, req.body.value, req.body.txid,r,s,_v, XCPlugin.sender) 122 // return 123 124 var result = await call("XCPlugin","voteProposal",[req.body.fromPlatform, req.body.fromAccount, req.body.toAccount, req.body.value, req.body.txid,r,s,_v], XCPlugin.sender) 125 console.log("voter:",result) 126 // var success = await call("XCPlugin","verifyProposal",[req.body.fromPlatform, req.body.fromAccount, req.body.toAccount, req.body.value, req.body.txid]) 127 // console.log("verifyProposal:",success) 128 129 // return 130 result = await call("XC","unlock",[req.body.txid, req.body.fromPlatform, req.body.fromAccount, req.body.toAccount, req.body.value],XCPlugin.sender) 131 console.log("unlock:",result) 132 133 result = await call("XC","lockBalance") 134 console.log("lockBalance:",result) 135 136 var response = { 137 "lockBalance":result.toString() 138 }; 139 console.log(response); 140 res.end(JSON.stringify(response)); 141 }); 142 143 app.post('/xc/voter/', urlencodedParser,async function (req, res) { 144 var sign = req.body.sign; 145 var r = sign.substr(0,64) 146 var s = sign.substr(64,64) 147 var v = sign.substr(128,2) 148 var _v = 27; 149 if ( v == '00' || v == '1b') { 150 _v = 27 151 } else if ( v == '01' || v == '1c') { 152 _v = 28 153 } 154 console.log(req.body.fromPlatform, req.body.fromAccount, req.body.toAccount, req.body.value, req.body.txid,r,s,_v, XCPlugin.sender) 155 156 var result = await call("XCPlugin","voteProposal",[req.body.fromPlatform, req.body.fromAccount, req.body.toAccount, req.body.value, req.body.txid,r,s,_v], XCPlugin.sender) 157 console.log("voter:",result) 158 159 var response = { 160 "voterProposal": "ok" 161 }; 162 console.log(response); 163 res.end(JSON.stringify(response)); 164 }); 165 166 app.post('/xc/verifyProposal/', urlencodedParser,async function (req, res) { 167 168 console.log(req.body.fromPlatform, req.body.fromAccount, req.body.toAccount, req.body.value, req.body.txid,req.body.sign, XCPlugin.sender) 169 170 var result = await call("XCPlugin","verifyProposal",[req.body.fromPlatform, req.body.fromAccount, req.body.toAccount, req.body.value, req.body.txid]) 171 console.log("verifyProposal:",result) 172 173 var response = { 174 "verifyProposal":result 175 }; 176 console.log(response); 177 res.end(JSON.stringify(response)); 178 }); 179 180 app.post('/xc/unlock/', urlencodedParser,async function (req, res) { 181 182 console.log(req.body.fromPlatform, req.body.fromAccount, req.body.toAccount, req.body.value, req.body.txid,req.body.sign, XCPlugin.sender) 183 184 // return 185 result = await call("XC","unlock",[req.body.txid, req.body.fromPlatform, req.body.fromAccount, req.body.toAccount, req.body.value],XCPlugin.sender) 186 console.log("unlock:",result) 187 188 result = await call("XC","lockBalance") 189 console.log("lockBalance:",result) 190 191 var response = { 192 "lockBalance":result.toString() 193 }; 194 console.log(response); 195 res.end(JSON.stringify(response)); 196 }); 197 198 app.post('/xc/lockBalance/', urlencodedParser,async function (req, res) { 199 var result = await call("XC","lockBalance") 200 console.log("lockBalance:",result) 201 202 var response = { 203 "lockBalance":result.toString() 204 }; 205 console.log(response); 206 res.end(JSON.stringify(response)); 207 }); 208 209 210 app.post('/xc/balanceOf/', urlencodedParser,async function (req, res) { 211 212 var result = await call("INK","balanceOf",[req.body.account]) 213 console.log("balanceOf:",result) 214 215 var response = { 216 "balanceOf":result.toString() 217 }; 218 console.log(response); 219 res.end(JSON.stringify(response)); 220 }); 221 222 app.post('/xc/setWeight/', urlencodedParser,async function (req, res) { 223 224 console.log(req.body.platformName, req.body.weight); 225 var result = await call("XCPlugin","setWeight",[req.body.platformName, req.body.weight],XCPlugin.sender) 226 console.log("setWeight:",result) 227 228 var response = { 229 "setWeight":result 230 }; 231 console.log(response); 232 res.end(JSON.stringify(response)); 233 }); 234 235 app.post('/xc/getWeight/', urlencodedParser,async function (req, res) { 236 237 var result = await call("XCPlugin","getWeight",[req.body.platformName]) 238 console.log("getWeight:",result) 239 240 var response = { 241 "getWeight":result 242 }; 243 console.log(response); 244 res.end(JSON.stringify(response)); 245 }); 246 247 248 249 var server = app.listen(8888, function () { 250 var host = server.address().address 251 var port = server.address().port 252 console.log("http://%s:%s", host, port) 253 });