import logo from './logo.svg';
import './App.css';
import Swap from './swap';
import Transact from './transaction';
import React, { Component } from 'react'
import WalletBalance from './walletbalance';
import Balance from './balance'
import styled from 'styled-components';
const Section = styled.section`
font-size: 12px;
border: 1px solid black;
text-align: center;
padding: 1rem 0 1rem ;
width: 35vh;
font-color: black;
`;
export default class App extends Component {
constructor (props){
super(props);
this.currencies = ["ETH","DAI","MKR","LINK","KNC","LEND","WETH"];
this.state={
confirm: false,
swap: false,
amount: "",
converted: "",
base: "ETH",
other: "DAI",
blockId: "",
timeStamp:"",
price: "",
/*liquidityPool: "",*/
wallets: [
{ticker:"ETH", total: 3000 ,},
{ticker:"WETH", total:0,},
{ticker:"LEND", total:150000,},
{ticker:"LINK", total:10000,},
{ticker:"KNC", total: 0,},
{ticker:"MKR", total:500,},
{ticker:"DAI", total: 500000,} ]
}
};
render() {
/*const timestamp = date.now();
const txId = ""; */
return (
<div>
<div className="App">
<button className="register">SIGN UP</button>
<button className="enter">LOGIN</button>
<header>
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
DRaU
</p>
<Swap amount={this.state.amount} other={this.state.other} converted={this.state.converted} base={this.state.base} makeSelection={this.makeSelection} changeValue={this.changeValue} calculateSwap={this.calculateSwap}
swap={this.state.swap} confirm={this.state.confirm} makeTransaction={this.makeTransaction} resetSwap={this.resetSwap} resetTransaction={this.resetTransaction} showTranId={this.showTranId}/>
<br></br>
<h1 className="details"><Transact sold={this.state.amount} bought={this.state.converted} core={this.state.base} alter={this.state.other} confirm={this.state.confirm} swap={this.state.swap} price={this.state.price}/></h1>
</header>
<Section>
<table>
<thead >
<Balance account= {this.state.totalBalance}/>
</thead>
<tbody>
<tr>
<th>Wallet Balance</th>
</tr>
{this.state.wallets.map((values) =>
<WalletBalance ticker={values.ticker} total={values.total} />
)
}
</tbody>
</table>
</Section>
</header>
</div>
</div>
)
}
makeSelection = (event) => {
this.setState({
[event.target.className]: event.target.value
}, this.calculateSwap);
}
changeValue = (event) => {
this.setState({
amount: event.target.value,
converted: "",
}, this.calculateSwap);
}
calculateSwap = () => {
const isValid = parseFloat(this.state.amount);
if (isNaN(isValid)){
return ;
}
fetch(`https://api.0x.org/swap/v1/quote?buyToken=${this.state.other}&sellToken=${this.state.base}&sellAmount=${this.state.amount}`)
.then(response => response.json())
.then( data => {
var dateTime= new Date();
this.setState({
converted: data.price * isValid,
blockId: data.allowanceTarget,
timeStamp: dateTime,
price: data.price,
/* liquidityPool: data.sources[1].name */
});
});
}
makeTransaction = () => {
const sold= this.state.amount;
const core = this.state.base;
const alter=this.state.other;
const bought= this.state.converted;
if(isNaN(sold)){
alert("unable to process");
return false;
}
else if(isNaN(bought)){
alert("unable to process");
return false;
}
else if(core===alter){
alert("unable to process");
return false;
}
else if(bought===0){
alert("unable to process");
return false;
}
else if(bought===""){
alert("unable to process");
return false;
}
else if(sold===""){
alert("unable to process");
return false;
}
else{
this.setState({
swap: true,
});
}
setTimeout(this.resetSwap, 5000);
}
resetSwap = () => {
this.setState({
swap: false,
confirm: true,
});
setTimeout(this.resetTransaction, 7000);
}
resetTransaction = () =>{
this.setState({
confirm: false,
amount: "",
converted: "",
}, this.showTranId);
}
showTranId = () => {
return (
<>{this.state.timeStamp} {this.state.blockId}</>
);
}
calculateBalance = () =>{
const values = this.state.wallets;
const base = this.state.base;
const other = this.state.other;
const amount = this.state.amount;
const converted = this.state.converted;
for (var n=0; n<values.length; n++){
let balance =values[n].total;
if ((values[n].ticker === base) && (values[n].total !==0)) {
balance = values[n].total - amount;
}
else if (values[n].ticker===other){
balance = values[n].total + converted;
}
else{
balance = 0;
}
this.setstate({}) }
}
}
Hey Carlos, im having trouble with two things.
1)how do i return a value from a function like i have in the āshowTranIDā function and then insert that into the DOM. ie, return a h1 in my App that holds those values generated from the function. I remember in Jquery we would use $ to refer to an element or use document.getelementbyId. i guess im just struggling how to use the jsx syntax within a function call etc.
- (calculateBalance) im having trouble updating the set state array for this.state.wallets with the new wallets.total value. again another syntax or code structuring issue.
i would very much appreciate any assistance in helping me solve these problems. thank you