Created
June 1, 2018 15:40
-
-
Save ohadnav/80ee5650f50768043a9a40ec051da55a to your computer and use it in GitHub Desktop.
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
import alt from '../common/alt' | |
import AlertActions from './AlertActions' | |
import ERC20_abi from 'human-standard-token-abi' | |
import {Contract, QtumRPC} from "qtumjs" | |
import exchange from '../build/contracts/Exchange.json' | |
import erc20Token from '../build/contracts/ERC20Token.json' | |
var Web3 = require('web3') | |
const qtumUsername = "qtum" | |
const qtumPassword = "test" | |
const qtumPort = 3889 | |
const contractRelativePath = "contracts/Exchange.sol" | |
// Load deployment information generated by solar | |
const solarContractRepo = require("./solar.json") | |
// Initialize qtum varialbes | |
const qtumRpc = new QtumRPC("http://" + qtumUsername + ":" | |
+ qtumPassword + "@localhost:" + qtumPort) | |
const qtumContract = new Contract(qtumRpc, | |
solarContractRepo.contracts[contractRelativePath]) | |
class DataActions { | |
constructor() { | |
this.generateActions( | |
'setMedia', | |
'getAccount', | |
'getEth', | |
'addERC20Token', | |
'toggleTransferDialog', | |
'toggleAllowanceDialog', | |
'toggleLendDialog', | |
'toggleBorrowDialog', | |
'toggleCollectDialog', | |
'toggleMarginCallDialog', | |
'getTransInfo', | |
'getLoanInfo' | |
); | |
this.web3 = null | |
this.exchange_abi = exchange.abi | |
this.erc20_abi = erc20Token.abi | |
this.exchangeContract = null | |
// const exchange_address = '0x27EFa06154CF02Bd5f404B06b87224B38795e09c' | |
// const exchange_address = '0x8BAA570dcDaEC0747099Fc7aF7b1e0735e153086' | |
this.exchange_address = '0x345ca3e014aaf5dca488057592ee47305d9b3e10' | |
this.gasLimit = 6654750 | |
this.useQtum = false | |
} | |
loadAccount(done) { | |
const self = this | |
try { | |
if (!web3) { | |
throw new Error( | |
"Web browser wallet plugin is not running. Please enable the wallet extension such as MetaMask.") | |
} | |
if (!web3.currentProvider) { | |
throw new Error("Please login the web browser wallet such as MetaMask.") | |
} | |
if (!web3.isConnected()) { | |
throw new Error("Network is not connected") | |
} | |
self.web3 = new Web3(web3.currentProvider) | |
self.web3.eth.getAccounts((error, result) => { | |
try { | |
if (error) { | |
throw error | |
} | |
else { | |
if (!result || result.length == 0) { | |
throw new Error( | |
"Please login the web browser wallet such as MetaMask.") | |
} | |
self.getAccount(result[0]) | |
done(result[0]) | |
} | |
} | |
catch (error) { | |
AlertActions.show( | |
error.message ? error.message : JSON.stringify(error)); | |
console.error(error) | |
} | |
}) | |
} | |
catch (error) { | |
AlertActions.show( | |
"Web browser wallet plugin is not running. Please enable the wallet extension such as MetaMask."); | |
console.error(error) | |
} | |
} | |
loadEth(address) { | |
const self = this | |
try { | |
self.web3.eth.getBalance(address, (error, result) => { | |
if (error) { | |
throw new Error(error) | |
} | |
const eth = web3.fromWei(result, 'ether') | |
self.getEth(eth) | |
}) | |
} | |
catch (error) { | |
AlertActions.show(error.message ? error.message : JSON.stringify(error)); | |
console.error(error) | |
} | |
} | |
loadERC20Token(accountAddress, tokenAddress) { | |
const self = this | |
try { | |
const token = self.web3.eth.contract(ERC20_abi).at(tokenAddress) | |
const tokenInfo = {address: tokenAddress} | |
token.name((err, name) => { | |
if (err) { | |
console.error(err.toString()); | |
return; | |
} | |
if (name) { | |
tokenInfo.name = name | |
} | |
token.symbol((err, symbol) => { | |
if (err) { | |
console.error(err.toString()) | |
return; | |
} | |
if (symbol) { | |
tokenInfo.symbol = symbol | |
} | |
token.decimals((err, decimals) => { | |
if (err) { | |
console.error(err.toString()) | |
} | |
if (decimals) { | |
tokenInfo.decimals = decimals | |
tokenInfo.decimal_expand = 10 ** decimals | |
} | |
token.totalSupply((err, totalSupply) => { | |
if (err) { | |
console.error(err.toString()) | |
} | |
if (totalSupply) { | |
tokenInfo.totalSupply = totalSupply | |
} | |
token.balanceOf(accountAddress, (err, balance) => { | |
if (err) { | |
console.error(err.toString()) | |
} | |
if (balance) { | |
tokenInfo.balance = balance | |
} | |
token.allowance(accountAddress, self.exchange_address, | |
(err, allowance) => { | |
if (err) { | |
console.error(err.toString()) | |
} | |
if (allowance) { | |
tokenInfo.allowance = allowance | |
} | |
self.addERC20Token(tokenInfo) | |
}) | |
}) | |
}) | |
}) | |
}) | |
}) | |
} | |
catch (error) { | |
AlertActions.show(error.message ? error.message : JSON.stringify(error)); | |
console.error(error) | |
} | |
} | |
loadExchangeContract(done) { | |
const self = this | |
try { | |
self.exchangeContract = web3.eth.contract(self.exchange_abi).at( | |
self.exchange_address) | |
// done() | |
} | |
catch (error) { | |
AlertActions.show(error.message ? error.message : JSON.stringify(error)); | |
console.error(error) | |
} | |
} | |
loadLoans() { | |
const self = this | |
try { | |
if (this.useQtum) { | |
qtumContract.rawCall("getLoanCount") | |
.then(result => { | |
const numOfLoans = result | |
for (let i = 1; i <= numOfLoans; i++) { | |
qtumContract.call("getLoanInfo", [i]) | |
.then(result => { | |
self.getLoanInfo(result) | |
}) | |
.catch(err => { | |
console.err(err) | |
}) | |
} | |
}) | |
.catch(err => { | |
AlertActions.show(err.toString()) | |
}) | |
} else { | |
self.exchangeContract.getLoanCount((err, result) => { | |
if (err) { | |
AlertActions.show(err.toString()) | |
} | |
else { | |
const numOfLoans = result.toNumber() | |
for (let i = 1; i <= numOfLoans; i++) { | |
self.exchangeContract.getLoanInfo(i, (err, result) => { | |
if (err) { | |
console.err(err) | |
} | |
else { | |
self.getLoanInfo(result) | |
} | |
}) | |
} | |
} | |
}) | |
} | |
} | |
catch (error) { | |
AlertActions.show(error.message ? error.message : JSON.stringify(error)); | |
console.error(error) | |
} | |
} | |
submitLend(lendInfo) { | |
const self = this | |
try { | |
console.log(lendInfo) | |
// self.exchangeContract.greet((err, result)=>{ | |
// self.getGreet(result) | |
// }) | |
self.getTransInfo(lendInfo) | |
} | |
catch (error) { | |
AlertActions.show(error.message ? error.message : JSON.stringify(error)); | |
console.error(error) | |
} | |
} | |
submitTransfer(transferInfo) { | |
const self = this | |
try { | |
const token = self.web3.eth.contract(ERC20_abi).at( | |
transferInfo.tokenAddress) | |
token.transfer(transferInfo.toAddress, transferInfo.amount * 10 ** 18, | |
{from: web3.eth.defaultAccount, gas: self.gasLimit}, | |
(err, result) => { | |
if (err) { | |
AlertActions.show(err.toString()) | |
} | |
else { | |
AlertActions.show(result) | |
} | |
}) | |
} | |
catch (error) { | |
AlertActions.show(error.message ? error.message : JSON.stringify(error)); | |
console.error(error) | |
} | |
} | |
submitAllowance(allowanceInfo) { | |
const self = this | |
try { | |
const token = self.web3.eth.contract(ERC20_abi).at( | |
allowanceInfo.tokenAddress) | |
token.approve(self.exchangeContract.address, allowanceInfo.amount * 10 | |
** 18, {from: web3.eth.defaultAccount, gas: self.gasLimit}, | |
(err, result) => { | |
if (err) { | |
AlertActions.show(err.toString()) | |
} | |
else { | |
AlertActions.show(result) | |
} | |
}) | |
} | |
catch (error) { | |
AlertActions.show(error.message ? error.message : JSON.stringify(error)); | |
console.error(error) | |
} | |
} | |
submitBorrow(borrowInfo) { | |
const self = this | |
try { | |
self.exchangeContract.createByBorrower( | |
borrowInfo.collectTokenAddress, | |
borrowInfo.collectAmount * 10 ** 18, | |
borrowInfo.loanTokenAddress, | |
borrowInfo.loanAmount * 10 ** 18, | |
{from: web3.eth.defaultAccount, gas: self.gasLimit}, | |
(err, result) => { | |
if (err) { | |
AlertActions.show(err.toString()) | |
} | |
else { | |
AlertActions.show(result) | |
} | |
}) | |
} | |
catch (error) { | |
AlertActions.show(error.message ? error.message : JSON.stringify(error)); | |
console.error(error) | |
} | |
} | |
fulfillOrder(loanInfo) { | |
const self = this | |
try { | |
if (loanInfo[1] != '0x0000000000000000000000000000000000000000') { | |
self.exchangeContract.fulfillByLender(loanInfo[0].toNumber(), | |
{ | |
from: web3.eth.defaultAccount, | |
gas : self.gasLimit | |
}, | |
(err, result) => { | |
if (err) { | |
AlertActions.show( | |
err.toString()) | |
} | |
else { | |
AlertActions.show(result) | |
} | |
}) | |
} | |
else if (loanInfo[2] != '0x0000000000000000000000000000000000000000') { | |
self.exchangeContract.fulfillByBorrower(loanInfo[0].toNumber(), | |
{ | |
from: web3.eth.defaultAccount, | |
gas : self.gasLimit | |
}, | |
(err, result) => { | |
if (err) { | |
AlertActions.show( | |
err.toString()) | |
} | |
else { | |
AlertActions.show(result) | |
} | |
}) | |
} | |
} | |
catch (error) { | |
AlertActions.show(error.message ? error.message : JSON.stringify(error)); | |
console.error(error) | |
} | |
} | |
} | |
export default alt.createActions(DataActions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment