Skip to content

Commit

Permalink
added transaction handler
Browse files Browse the repository at this point in the history
  • Loading branch information
devintegral4 committed May 17, 2020
1 parent 0849ad7 commit 28e3207
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
11 changes: 11 additions & 0 deletions oracleApp/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ const config = {
weight = 100
}
]
},
oracleContract = {
contractAddress = "0xfc00face00000000000000000000000000000000",
defaultHost = "localhost",
defaultPort = "18545",

sender = {
"keyObject": {"address":"95f30ec61da14a8b3abc24bb11eebcf58f9f0cf3","crypto":{"cipher":"aes-128-ctr","ciphertext":"30f6235d561c44538eb6c234b1e429c56a3650a3b04c4b0aa5905533d7c82922","cipherparams":{"iv":"4ab0baf8eef69a38b2f5069b4b0582c0"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"a09cc7d83a86b42087e7cfe34566206daf5956f8cffee2803175739f2790cef3"},"mac":"f8b64f2824acc98f2e551de0e696717e5dcf55bedf105269c022bbf0281c82b5"},"id":"abbeda74-c324-4120-9d99-efa3236ead00", "version":3},
"privateKey": "343ba3ed7da835da945f9cbd784e62e1c329737c1b33b262e38a53501021af44",
"address": "0x95f30ec61da14a8b3abc24bb11eebcf58f9f0cf3"
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions oracleApp/contractHandler/oracleContractHandler.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
const Web3 = require('web3');
const config = require('../config');
const oracleAbi = require('../public/contractAbis/abis');
const TransactionHandler = require('./transactionsHandler');

class ContractHandler {
constructor(endpoint, contractAddress, oracleAbi) {
this.endpoint = config.oracleContract.defaultHost + ":" + config.oracleContract.defaultPort;
this.contractAddress = config.oracleContract.contractAddress;
this.web3 = new Web3(new Web3.providers.HttpProvider(endpoint));
this._oracle = new this.web3.eth.Contract(oracleAbi, this.contractAddress); // abi
this.transactions = new TransactionHandler(this.web3, this._oracle, this.contractAddress, this.txStorage);
}

updatePairPrice(pair1, pair2, price) {
this.transactions.proposePriceForPair(pair1, pair2, price);
}
}
73 changes: 73 additions & 0 deletions oracleApp/contractHandler/transactionsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const config = require("../config");


class TransactionHandler {
constructor(web3, oracle, contractAddr) {
this.web3 = web3;
this.oracle = oracle;
this.contractAddr = contractAddr;
this.from = config.address;
// this.txStorage = txStorage;
}

proposePriceForPair(pair1, pair2, price) {
return this.newSignedTx({
from: from,
to: this.contractAddr,
value: "0",
memo: this.oracle.methods.proposePriceForPair(pair1, pair2, price).encodeABI(),
gasLimit: 200000,
web3Delegate: this.web3
});
};

async estimateGas(rawTx) {
let estimateGas;
await this.web3.eth.estimateGas(rawTx, (err, gas) => {
if (err)
throw(err);

estimateGas = gas;
});

return estimateGas;
};

async newSignedTx({
accountFrom,
to,
value,
memo = '',
web3Delegate = '',
turnLogsOff = false,
}) {
const useWeb3 = web3Delegate || this.web3;
const nonce = await useWeb3.eth.getTransactionCount(accountFrom.address);
const gasPrice = await useWeb3.eth.getGasPrice();
// const txName

const rawTx = {
from: accountFrom.address,
to,
value: this.web3.utils.toHex(this.web3.utils.toWei(value, 'ether')),
gasPrice: this.web3.utils.toHex(gasPrice),
nonce: this.web3.utils.toHex(nonce),
data: memo
};

let estimatedGas = await this.estimateGas(rawTx);
rawTx.gasLimit = this.web3.utils.toHex(estimatedGas);

let tx = await this.web3.eth.accounts.signTransaction(rawTx, accountFrom.privateKey);
return this.web3.eth.sendSignedTransaction(tx.rawTransaction, function(err, hash) {
if (err)
throw(err);

if (!turnLogsOff)
console.log("tx sendSignedTransaction hash:", hash);
});
};

}

module.exports = TransactionHandler;
3 changes: 3 additions & 0 deletions oracleApp/public/contractAbis/abis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const oracleAbi = "";

module.exports = oracleAbi;

0 comments on commit 28e3207

Please sign in to comment.