Skip to content

Commit

Permalink
state update working
Browse files Browse the repository at this point in the history
  • Loading branch information
filvecchiato committed Jul 8, 2021
1 parent b333783 commit 3f8e3cb
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 114 deletions.
18 changes: 11 additions & 7 deletions client/src/components/AssetBalance.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ const AssetBalance = ({
asset,
clickAction,
swapAmount,
balances,
setSwapAmount,
direction,
provider
}) => {
const currBalance = balances.length && balances.find(inv => inv.id === asset.id);
const assetBalance = currBalance?.balance || asset.balance;

return (
<AssetSt>
<div>
Expand All @@ -84,7 +88,7 @@ const AssetBalance = ({
onChange={(e) => setSwapAmount(e.target.value)}
value={swapAmount || '0.0'}
min="0.00"
max={asset.balance}
max={assetBalance}
placeholder="0.0"
>
</AmountSwap>
Expand All @@ -94,19 +98,19 @@ const AssetBalance = ({
<BalanceSelector>
{direction !== 'to' ?
<>
<p>Balance: {Number(asset.balance).toFixed(4)|| '0.0'} {asset.symbol}</p>
<p>Balance: {Number(assetBalance).toFixed(4)|| '0.0'} {asset.symbol}</p>
<div>
<button onClick={() => setSwapAmount(Number(asset.balance * 0.25).toFixed(4)) }>25%</button>
<button onClick={() => setSwapAmount(Number(assetBalance * 0.25).toFixed(4)) }>25%</button>
<p>|</p>
<button onClick={() => setSwapAmount(Number(asset.balance * 0.5).toFixed(4)) }>50%</button>
<button onClick={() => setSwapAmount(Number(assetBalance * 0.5).toFixed(4)) }>50%</button>
<p>|</p>
<button onClick={() => setSwapAmount(Number(asset.balance * 0.75).toFixed(4)) }>75%</button>
<button onClick={() => setSwapAmount(Number(assetBalance * 0.75).toFixed(4)) }>75%</button>
<p>|</p>
<button onClick={() => setSwapAmount(Number(asset.balance * 0.98).toFixed(4)) }>max</button>
<button onClick={() => setSwapAmount(Number(assetBalance * 0.98).toFixed(4)) }>max</button>
</div>
</>
:
<p> Current Balance: {Number(asset.balance).toFixed(4) || '0.0'} {asset.symbol}</p>
<p> Current Balance: {Number(assetBalance).toFixed(4) || '0.0'} {asset.symbol}</p>
}
</BalanceSelector>
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@ export const NETWORK = chainId => {
}
};

export const TESSER_CONTRACT = "0xD1760AA0FCD9e64bA4ea43399Ad789CFd63C7809";
export const TESSER_CONTRACT = "0x6f2E42BB4176e9A7352a8bF8886255Be9F3D2d13";
109 changes: 42 additions & 67 deletions client/src/containers/Dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
} from './dashboard.style';
import { ethers } from 'ethers';
import IERC20 from '../../utils/helpers/IERC20.json'
import CURVE_ABI from '../../utils/helpers/curveGaugeAbi.json'
import { TESSER_CONTRACT } from '../../constants.js';

const initState = {
Expand All @@ -40,6 +39,12 @@ const isMarket = (asset) => {
return (asset.type === 'LP' || asset.type === 'Farm')
}

const getRandomIntInclusive = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};

const approveConn = async (token, user, amount, provider) => {
const erc20 = new ethers.Contract(token, IERC20, provider);
await erc20.approve(user, amount);
Expand All @@ -55,31 +60,27 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
const TesserController = new ethers.Contract(TESSER_CONTRACT, TesserContract, userSigner)

const getBalances = async (data, type) => {
const assets = [...data];
for (let asset of assets) {
for (let asset of data) {
if (asset.address === '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0') {
const balance = await provider.getBalance(address)
const formatBalance = ethers.utils.formatUnits(balance, asset.decimals);
asset['balance'] = formatBalance
} else if (asset.address === '0x19793B454D3AfC7b454F206Ffe95aDE26cA6912c') {
const contractAddress = asset.address;
const contract = new ethers.Contract(contractAddress, CURVE_ABI, userSigner);
const balance = await contract.balanceOf(address);
const formatBalance = ethers.utils.formatUnits(balance, asset.decimals);
asset['balance'] = formatBalance || 0.0;
} else if (!asset.balance) {
asset['balance'] = formatBalance;
} else {
const contractAddress = asset.outputToken || asset.address;
const contract = new ethers.Contract(contractAddress, IERC20, provider);
const balance = await contract.balanceOf(address);
const formatBalance = ethers.utils.formatUnits(balance, asset.decimals);
let formatBalance = asset.balanceStored;
if (!asset.balanceStored) {
const balance = await contract.balanceOf(address);
formatBalance = ethers.utils.formatUnits(balance, asset.decimals);
}
asset['balance'] = formatBalance;
}
}

if (type === 'tokens') {
setTokens(assets);
setTokens((prevState) => (data));
} else {
setInvestments(assets);
setInvestments((prevState) => (data));
}
}

Expand All @@ -88,8 +89,8 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
if (address && !!provider) {
const tokens = [...investmentsData.tokens];
const investments = [...investmentsData.investments];
getBalances(tokens, 'tokens')
getBalances(investments, 'investments')
getBalances(tokens, 'tokens');
getBalances(investments, 'investments');
}
}, [address, provider]);

Expand Down Expand Up @@ -134,9 +135,6 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
const toSelected = swapSelection.to.asset !== {}
const buttonDisabled = !(fromSelected && toSelected);


// migrate from prot to prot but not
// deposit
const tesserInvestments = async () => {
let transaction
setTesser(!tesserStarted)
Expand All @@ -152,7 +150,7 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
console.log('migrate')
if (swapSelection.from.asset.address === "0x68456B298c230415E2DE7aD4897A79Ee3f1A965a") fromAddress = "0x445FE580eF8d70FF569aB36e80c647af338db351"
const adapterAddressFrom = await TesserController.getAdapterAddressForMarket(fromAddress);
const bigInt = '' + (swapSelection.from.value * (10 ** swapSelection.from.asset.decimals))
const bigInt = '' + (parseInt('' + swapSelection.from.value * (10 ** swapSelection.from.asset.decimals)));
await approveConn(fromAddress, adapterAddressFrom, bigInt, userSigner);

transaction = await TesserController.migrate(
Expand All @@ -167,11 +165,11 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
//withdraw
const adapterAddressFrom = await TesserController.getAdapterAddressForMarket(fromAddress);
const toAddress = swapSelection.from.asset.outputToken || swapSelection.from.asset.address;
const bigInt = '' + (swapSelection.from.value * (10 ** swapSelection.from.asset.decimals))
const bigInt = '' + (parseInt('' + swapSelection.from.value * (10 ** swapSelection.from.asset.decimals)));
await approveConn(toAddress, adapterAddressFrom, bigInt, userSigner)
transaction = await TesserController.withdraw(toAddress, [bigInt], false)
} else if (swapSelection.from.asset.address === "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0"){
const bigInt = '' + (swapSelection.from.value * (10 ** swapSelection.from.asset.decimals))
} else if (swapSelection.from.asset.address === "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0") {
const bigInt = '' + (parseInt('' + swapSelection.from.value * (10 ** swapSelection.from.asset.decimals)));
transaction = await TesserController.depositETH(
toAddress,
false,
Expand All @@ -183,11 +181,11 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
console.log('deposit')
// deposit or depositOtherTokens
if (swapSelection.to.asset.inputTokens.includes(fromAddress)) {
const bigInt = '' + (swapSelection.from.value * (10 ** swapSelection.from.asset.decimals))
const bigInt = '' + (parseInt('' + swapSelection.from.value * (10 ** swapSelection.from.asset.decimals)));
const adapterAddressTo = await TesserController.getAdapterAddressForMarket(swapSelection.to.asset.address);
await approveConn(fromAddress, adapterAddressTo, bigInt, userSigner)
transaction = await TesserController.deposit(
fromAddress,
toAddress,
[bigInt],
false
)
Expand All @@ -207,43 +205,25 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
// withdraw
const adapterAddressFrom = await TesserController.getAdapterAddressForMarket(fromAddress);
const toAddress = swapSelection.from.asset.outputToken || swapSelection.from.asset.address;
const bigInt = '' + (swapSelection.from.value * (10 ** swapSelection.from.asset.decimals))
const bigInt = '' + (parseInt('' + swapSelection.from.value * (10 ** swapSelection.from.asset.decimals)));
await approveConn(toAddress, adapterAddressFrom, bigInt, userSigner)
transaction = await TesserController.withdraw(toAddress, [bigInt], false)
}

const receipt = await transaction.wait()
console.log(receipt)
await getBalances(tokens, 'tokens')
await getBalances(investments, 'investments')
if (receipt.status === 1) {
setTimeout(() => {
getBalances(tokens, 'tokens')
getBalances(investments, 'investments')
setSwapSelection(initState)
}, 2000);

setTimeout(() => {
setTesser(false)
setTimeout(async () => {
setSwapValue(0.0, 'from')
setModal({ state: false })
}, 1000);
// refresh data

setTesser(false)
}, 1000);
} else {
getBalances(tokens, 'tokens')
getBalances(investments, 'investments')
setTesser(false)
setModal({ state: false })
// refresh data
setTimeout(() => {
setSwapSelection(initState)

}, 500);
setTesser(false)
}
}

const getRandomIntInclusive = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};

const tesseringModal = () => {
Expand Down Expand Up @@ -279,27 +259,20 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
</TesserModalSt>
);
};
const filteredData = showStakeDao
? {
tokens: [...tokens],
protocols: [...investmentsData.protocols].filter(prot => prot.address !== "0x361a5a4993493ce00f61c32d4ecca5512b82ce90"),
investments: [...investments]
}
:
{
tokens: [...tokens],
protocols: [...investmentsData.protocols],
investments: [...investments]
}

const selectionModal = () => {
return <SelectionModal
investmentsData={filteredData}
investmentsData={{
tokens: tokens,
protocols: showStakeDao
? [...investmentsData.protocols]
: [...investmentsData.protocols].filter(prot => prot.address !== "0x361a5a4993493ce00f61c32d4ecca5512b82ce90"),
investments: investments
}}
setSwapAsset={(asset) => setSwapAsset(asset, modal.direction)}
/>
}


const ModalContent = tesserStarted
? tesseringModal()
: selectionModal()
Expand All @@ -310,7 +283,7 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
}
}

return (
return (
<ContainerSt>
{modal.state &&
<Modal
Expand All @@ -323,6 +296,7 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
<TitleSt> Tesser </TitleSt>
<AssetBalance
asset={swapSelection.from.asset}
balances={[...tokens, ...investments]}
clickAction={() => openSelector('from')}
swapAmount={swapSelection.from.value}
setSwapAmount={(value) => setSwapValue(value, 'from')}
Expand All @@ -332,6 +306,7 @@ const Dashboard = ({ address, provider, userSigner, loadWeb3Modal, showStakeDao
<img src={SwitchIcon} alt=""/>
<AssetBalance
asset={swapSelection.to.asset}
balances={[...tokens, ...investments]}
clickAction={() => openSelector('to')}
direction={'to'}
provider={provider}
Expand Down
34 changes: 0 additions & 34 deletions client/src/containers/Landing/index.js

This file was deleted.

Empty file.
Loading

0 comments on commit 3f8e3cb

Please sign in to comment.