-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
devintegral4
committed
May 17, 2020
1 parent
0849ad7
commit 28e3207
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const oracleAbi = ""; | ||
|
||
module.exports = oracleAbi; |