Skip to content

Instantly share code, notes, and snippets.

@shingonu
Created August 3, 2018 06:48
Show Gist options
  • Select an option

  • Save shingonu/f124b7e7f51d6f78df5a13bd4711efc2 to your computer and use it in GitHub Desktop.

Select an option

Save shingonu/f124b7e7f51d6f78df5a13bd4711efc2 to your computer and use it in GitHub Desktop.
import { Router } from 'express';
import path from 'path';
import fs from 'fs';
import _ from 'lodash';
import Token from '../../models/token';
import Tx from 'ethereumjs-tx';
require('dotenv').config();
export default () => {
const api = Router();
api.post('/token/deploy', async (req, res, next) => {
const query = await Token.findOne({
token_id: req.body.token_id,
}).exec();
if (!_.isNull(query)) {
return res.status(400).json({
code: 1,
message: 'token ID already exists.',
});
}
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HanwhaToken.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi);
const tokenId = req.body.token_id;
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const bytecode = JSON.parse(fs.readFileSync(abiPath).toString()).bytecode;
const params = {};
const values = Object.values(Object.assign(params, req.body.params));
const data = contract.deploy({
data: bytecode,
arguments: values,
})
.encodeABI();
let gas;
try {
gas = await contract.deploy({
data: bytecode,
arguments: values,
})
.estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
data: data,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', async receipt => {
if (_.isNull(receipt.contractAddress)) {
return res.status(400).json({
code: 1,
message: 'it is not deploy message call',
});
}
const block = await web3.eth.getBlock(receipt.blockNumber, true);
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HanwhaToken.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, receipt.contractAddress);
const name = await getValue(web3, contract, 'name');
const symbol = await getValue(web3, contract, 'symbol');
const decimals = await getValue(web3, contract, 'decimals');
const expireDates = await getValue(web3, contract, 'getExpireDates');
Token.create({
token_id: tokenId,
token_address: receipt.contractAddress,
timestamp: block.timestamp,
name: name,
symbol: symbol,
decimals: decimals,
expire_dates: expireDates
}, (err, token) => {
if (err) return res.status(500).json({
code: 1,
message: err.message,
});
return res.status(200).json({
code: 0,
message: 'success',
response: {
token_id: tokenId,
contract_address: receipt.contractAddress,
}
});
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/swapper/deploy', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HanwhaTokenSwapper.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const bytecode = JSON.parse(fs.readFileSync(abiPath).toString()).bytecode;
const params = {};
const values = Object.values(Object.assign(params, req.body.params));
const data = contract.deploy({
data: bytecode,
arguments: values,
})
.encodeABI();
let gas;
try {
gas = await contract.deploy({
data: bytecode,
arguments: values,
})
.estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
swapper_address: receipt.contractAddress,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/oneid/deploy', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'OneId.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const bytecode = JSON.parse(fs.readFileSync(abiPath).toString()).bytecode;
const params = {};
const values = Object.values(Object.assign(params, req.body.params));
const data = contract.deploy({
data: bytecode,
arguments: values,
})
.encodeABI();
let gas;
try {
gas = await contract.deploy({
data: bytecode,
arguments: values,
})
.estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
oneid_address: receipt.contractAddress,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/flight-delay/deploy', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HonechainFlightDelayProcess.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const bytecode = JSON.parse(fs.readFileSync(abiPath).toString()).bytecode;
const params = {};
const values = Object.values(Object.assign(params, req.body.params));
const data = contract.deploy({
data: bytecode,
arguments: values,
})
.encodeABI();
let gas;
try {
gas = await contract.deploy({
data: bytecode,
arguments: values,
})
.estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
contract_address: receipt.contractAddress,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/p2pLoan/deploy', async (req, res, next) => {
const web3 = global.web3;
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'P2PLoan.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const bytecode = JSON.parse(fs.readFileSync(abiPath).toString()).bytecode;
const params = Object.values(req.body.params);
const data = contract.deploy({
data: bytecode,
arguments: params,
})
.encodeABI();
let gas;
try {
gas = await contract.deploy({
data: data,
arguments: params,
})
.estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
data: data,
gasPrice: '22e9',
gas: parseInt(gas * 1.2)
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
contract_address: receipt.contractAddress,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message
});
}
});
api.post('/token/:token_id', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HanwhaToken.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const token_id = req.params.token_id;
const query = await Token.findOne({
token_id: req.params.token_id
}).exec();
if(_.isNull(query))
return res.status(500).json({
code: 1,
message: 'The token id does not exist.'
});
const tokenAddress = query.token_address;
const contract = new web3.eth.Contract(abi, tokenAddress);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const method = req.body.method;
const bytecode = getBytecode(web3, abi, method, req.body.params);
const values = Object.values(req.body.params);
let gas;
try {
gas =
await contract.methods[method](...values).estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
to: tokenAddress,
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/swapper/:address', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HanwhaTokenSwapper.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const method = req.body.method;
const bytecode = getBytecode(web3, abi, method, req.body.params);
const values = Object.values(req.body.params);
let gas;
try {
gas =
await contract.methods[method](...values).estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
to: req.params.address,
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/swapper/:address/swap', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HanwhaTokenSwapper.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const method = req.body.method;
const params = {};
const values = Object.values(Object.assign(params, req.body.params));
for (let i = 0; i < 2; i++) {
let query = await Token.findOne({
token_id: values[i],
}).exec();
if(_.isNull(query))
return res.status(500).json({
code: 1,
message: 'The token id does not exist.'
});
values[i] = query.token_address;
}
const bytecode = getBytecode(web3, abi, method, values);
let gas;
try {
gas =
await contract.methods[method](...values).estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
to: req.params.address,
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/oneid/:address', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'OneId.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const method = req.body.method;
let bytecode;
try {
bytecode = getBytecode(web3, abi, method, req.body.params);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const values = Object.values(req.body.params);
let gas;
try {
gas =
await contract.methods[method](...values).estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
to: req.params.address,
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/flight-delay/:address', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HonechainFlightDelayProcess.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const method = req.body.method;
const bytecode = getBytecode(web3, abi, method, req.body.params);
const values = Object.values(req.body.params);
const estimateGasOptions = {};
estimateGasOptions.from = from;
if (!_.isUndefined(req.body.value)) estimateGasOptions.value = req.body.value;
let gas;
try {
gas =
await contract.methods[method](...values).estimateGas(estimateGasOptions);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
to: req.params.address,
data: bytecode,
value: req.body.value,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
error
});
});
} catch (err) {
return res.status(400).json({
code: 2,
message: err.message,
error
});
}
});
api.post('/p2pLoan/:address', async (req, res, next) => {
const web3 = global.web3;
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'P2PLoan.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const method = req.body.method;
const bytecode = getBytecode(web3, abi, method, req.body.params);
const values = Object.values(req.body.params);
const estimateGasOptions = {};
estimateGasOptions.from = from;
if (!_.isUndefined(req.body.value)) estimateGasOptions.value = req.body.value;
let gas;
try {
gas =
await contract.methods[method](...values).estimateGas(estimateGasOptions);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
to: req.params.address,
data: bytecode,
value: req.body.value,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
error
});
});
} catch (err) {
return res.status(400).json({
code: 2,
message: err.message,
error
});
}
});
api.post('/reg-asset/deploy', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'RegisterAsset.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const bytecode = JSON.parse(fs.readFileSync(abiPath).toString()).bytecode;
const params = {};
const values = Object.values(Object.assign(params, req.body.params));
const data = contract.deploy({
data: bytecode,
arguments: values,
})
.encodeABI();
let gas;
try {
gas = await contract.deploy({
data: bytecode,
arguments: values,
})
.estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 5.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const seirializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + seirializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
address: receipt.contractAddress,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/reg-asset/:address', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'RegisterAsset.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const method = req.body.method;
const bytecode = getBytecode(web3, abi, method, req.body.params);
const values = Object.values(req.body.params);
const estimateGasOptions = {};
estimateGasOptions.from = from;
if (!_.isUndefined(req.body.value)) estimateGasOptions.value = req.body.value;
let gas;
try {
gas =
await contract.methods[method](...values).estimateGas(estimateGasOptions);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
to: req.params.address,
data: bytecode,
value: req.body.value,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const seirializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + seirializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
error
});
});
} catch (err) {
return res.status(400).json({
code: 2,
message: err.message,
error
});
}
});
api.post('/reg-asset/:address/call', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'RegisterAsset.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const params = Object.values(req.body.params);
const result = await contract.methods[req.body.method](...params).call();
return res.status(200).json({
code: 0,
message: 'success',
response: result,
});
});
api.post('/htoken/deploy', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HToken.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const bytecode = JSON.parse(fs.readFileSync(abiPath).toString()).bytecode;
const params = {};
const values = Object.values(Object.assign(params, req.body.params));
const data = contract.deploy({
data: bytecode,
arguments: values,
})
.encodeABI();
let gas;
try {
gas = await contract.deploy({
data: bytecode,
arguments: values,
})
.estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const seirializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + seirializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
address: receipt.contractAddress,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/htoken/:address', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HToken.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const method = req.body.method;
const bytecode = getBytecode(web3, abi, method, req.body.params);
const values = Object.values(req.body.params);
const estimateGasOptions = {};
estimateGasOptions.from = from;
if (!_.isUndefined(req.body.value)) estimateGasOptions.value = req.body.value;
let gas;
try {
gas =
await contract.methods[method](...values).estimateGas(estimateGasOptions);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
to: req.params.address,
data: bytecode,
value: req.body.value,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const seirializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + seirializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
error
});
});
} catch (err) {
return res.status(400).json({
code: 2,
message: err.message,
error
});
}
});
api.post('/htoken/:address/call', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HToken.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const params = Object.values(req.body.params);
const result = await contract.methods[req.body.method](...params).call();
return res.status(200).json({
code: 0,
message: 'success',
response: result,
});
});
api.post('/at-crowdsale/deploy', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'AssetTokenCrowdsale.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const bytecode = JSON.parse(fs.readFileSync(abiPath).toString()).bytecode;
const params = {};
const values = Object.values(Object.assign(params, req.body.params));
const data = contract.deploy({
data: bytecode,
arguments: values,
})
.encodeABI();
let gas;
try {
gas = await contract.deploy({
data: bytecode,
arguments: values,
})
.estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 1.8),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const seirializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + seirializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
address: receipt.contractAddress,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/at-crowdsale/:address', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'AssetTokenCrowdsale.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const method = req.body.method;
const bytecode = getBytecode(web3, abi, method, req.body.params);
const values = Object.values(req.body.params);
const estimateGasOptions = {};
estimateGasOptions.from = from;
if (!_.isUndefined(req.body.value)) estimateGasOptions.value = req.body.value;
// let gas;
// try {
// gas =
// await contract.methods[method](...values).estimateGas(estimateGasOptions);
// } catch (err) {
// return res.status(400).json({
// code: 1,
// message: err.message,
// });
// }
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
to: req.params.address,
data: bytecode,
value: req.body.value,
gasPrice: '22e9',
gas: 791977,
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const seirializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + seirializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
error
});
});
} catch (err) {
return res.status(400).json({
code: 2,
message: err.message,
error
});
}
});
api.post('/at-crowdsale/:address/call', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'AssetTokenCrowdsale.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const params = Object.values(req.body.params);
const result = await contract.methods[req.body.method](...params).call();
return res.status(200).json({
code: 0,
message: 'success',
response: result,
});
});
api.post('/asset-token/deploy', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'AssetToken.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const bytecode = JSON.parse(fs.readFileSync(abiPath).toString()).bytecode;
const params = {};
const values = Object.values(Object.assign(params, req.body.params));
const data = contract.deploy({
data: bytecode,
arguments: values,
})
.encodeABI();
let gas;
try {
gas = await contract.deploy({
data: bytecode,
arguments: values,
})
.estimateGas({
from: from,
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
data: bytecode,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const seirializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + seirializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
address: receipt.contractAddress,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/asset-token/:address', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'AssetToken.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const from = process.env.OPERATOR_ADDRESS;
let nonce;
try {
nonce = await web3.eth.getTransactionCount(from);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const privateKey = new Buffer(process.env.OPERATOR_PRIVATE_KEY, 'hex');
const method = req.body.method;
const bytecode = getBytecode(web3, abi, method, req.body.params);
const values = Object.values(req.body.params);
const estimateGasOptions = {};
estimateGasOptions.from = from;
if (!_.isUndefined(req.body.value)) estimateGasOptions.value = req.body.value;
let gas;
try {
gas =
await contract.methods[method](...values).estimateGas(estimateGasOptions);
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
const rawTx = {
nonce: nonce,
chainId: await web3.eth.net.getId(),
to: req.params.address,
data: bytecode,
value: req.body.value,
gasPrice: '22e9',
gas: parseInt(gas * 1.2),
};
const tx = new Tx(rawTx);
tx.sign(privateKey);
const seirializedTx = tx.serialize().toString('hex');
try {
web3.eth.sendSignedTransaction('0x' + seirializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
error
});
});
} catch (err) {
return res.status(400).json({
code: 2,
message: err.message,
error
});
}
});
api.post('/asset-token/:address/call', async (req, res, next) => {
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'AssetToken.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, req.params.address);
const params = Object.values(req.body.params);
const result = await contract.methods[req.body.method](...params).call();
return res.status(200).json({
code: 0,
message: 'success',
response: result,
});
});
return api;
};
function getBytecode(web3, abi, methodName, params) {
let method;
for (let i = 0; i < abi.length; i++) {
if (abi[i].name == methodName) {
method = abi[i];
break;
}
}
const functionSelector = web3.eth.abi.encodeFunctionSignature(method);
const types = [];
for (let i = 0; i < method.inputs.length; i++) {
types.push(method.inputs[i].type);
}
const values = Object.values(params);
const encodeParamters = web3.eth.abi.encodeParameters(types, values).slice(2);
const bytecode = functionSelector.concat(encodeParamters);
return bytecode;
}
async function getValue(web3, contract, name) {
const result = await contract.methods[name]().call();
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment