Skip to content

Commit

Permalink
Merge pull request #38 from studydefi/fixes/supply-token
Browse files Browse the repository at this point in the history
fixed supply bug
  • Loading branch information
kendricktan authored Apr 4, 2020
2 parents e28df6a + 541a109 commit 3d6c7ec
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 72 deletions.
188 changes: 139 additions & 49 deletions packages/frontend/features/position/SupplyCoin.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,163 @@
import { dedgeHelpers } from "../../../smart-contracts/dist/helpers";
import { ethers } from "ethers";
import { legos } from "money-legos/dist";

import { Button, Loader, Box, Flex, Field, Input } from "rimble-ui";

import CompoundPositions from "../../containers/CompoundPositions";
import ContractsContainer from "../../containers/Contracts";
import ConnectionContainer from "../../containers/Connection";
import DACProxyContainer from "../../containers/DACProxy";

import { useState } from "react";

import { dedgeHelpers } from "../../../smart-contracts/dist/helpers";
import { useState, useEffect } from "react";

const SupplyCoin = ({ coin }) => {
const { getBalances } = CompoundPositions.useContainer();
const { contracts } = ContractsContainer.useContainer();
const { proxy } = DACProxyContainer.useContainer();
const { signer, address } = ConnectionContainer.useContainer();
const { hasProxy, proxy, proxyAddress } = DACProxyContainer.useContainer();
const [loading, setLoading] = useState(false);
const [amount, setAmount] = useState("");

const [transferLoading, setTransferLoading] = useState(false);
const [canTransfer, setCanTransfer] = useState(null);

const getCanTransfer = async () => {
if (coin.symbol === "ETH") {
setCanTransfer(true);
return;
}

const tokenContract = new ethers.Contract(
coin.address,
legos.erc20.abi,
signer
);

const allowance = await tokenContract.allowance(address, proxyAddress);

if (allowance.toString() === "0") {
setCanTransfer(false);
return;
}

setCanTransfer(true);
};

useEffect(() => {
if (hasProxy) {
getCanTransfer();
}
}, [proxy]);

return (
<Box>
{/* <Heading.h5 mb="2">Supply {coin.symbol}</Heading.h5> */}
<Box mb="1">
<Field label={`Amount of ${coin.symbol} to supply`}>
<Input
type="number"
required={true}
placeholder="1337"
value={amount}
onChange={(e) => setAmount(e.target.value.toString())}
/>
<Field required={true} label={`Amount of ${coin.symbol} to supply`}>
{canTransfer === false || canTransfer === null ? (
<Input required={true} type="hidden" />
) : (
<Input
type="number"
required={true}
placeholder="1337"
value={amount}
onChange={(e) => setAmount(e.target.value.toString())}
/>
)}
</Field>
</Box>
<Button
ml={3}
disabled={loading}
onClick={async () => {
setLoading(true);

const { dedgeCompoundManager } = contracts;
const tx = await dedgeHelpers.compound.supplyThroughProxy(
proxy,
dedgeCompoundManager.address,
coin.cTokenEquilaventAddress,
ethers.utils.parseUnits(amount, coin.symbol === "USDC" ? 6 : 18)
);
window.toastProvider.addMessage(`Supplying ${coin.symbol}...`, {
secondaryMessage: "Check progress on Etherscan",
actionHref: `https://etherscan.io/tx/${tx.hash}`,
actionText: "Check",
variant: "processing",
});
await tx.wait()

window.toastProvider.addMessage(`Successfully supplied ${coin.symbol}!`, {
variant: "success",
});

setLoading(false);
getBalances();
}}
>
{loading ? (
<Flex alignItems="center">
<span>Supplying...</span> <Loader color="white" ml="2" />
</Flex>
) : (
"Supply"
)}
</Button>
{canTransfer === false || canTransfer === null ? (
<Button
ml={3}
disabled={canTransfer || canTransfer === null}
onClick={async () => {
const maxUINT =
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
setTransferLoading(true);

const tokenContract = new ethers.Contract(
coin.address,
legos.erc20.abi,
signer
);

const tx = await tokenContract.approve(proxyAddress, maxUINT);
window.toastProvider.addMessage(`Approving ${coin.symbol}...`, {
secondaryMessage: "Check progress on Etherscan",
actionHref: `https://etherscan.io/tx/${tx.hash}`,
actionText: "Check",
variant: "processing",
});
await tx.wait();

window.toastProvider.addMessage(
`Successfully approved ${coin.symbol}!`,
{
variant: "success",
}
);

setTransferLoading(false);

getCanTransfer();
}}
>
{canTransfer === null ? (
<Flex alignItems="center">
<span>Checking...</span> <Loader color="white" ml="2" />
</Flex>
) : transferLoading ? (
<Flex alignItems="center">
<span>Approving...</span> <Loader color="white" ml="2" />
</Flex>
) : (
"Approve Transfer"
)}
</Button>
) : (
<Button
ml={3}
disabled={loading || !canTransfer}
onClick={async () => {
setLoading(true);

const { dedgeCompoundManager } = contracts;
const tx = await dedgeHelpers.compound.supplyThroughProxy(
proxy,
dedgeCompoundManager.address,
coin.cTokenEquilaventAddress,
ethers.utils.parseUnits(amount, coin.symbol === "USDC" ? 6 : 18)
);
window.toastProvider.addMessage(`Supplying ${coin.symbol}...`, {
secondaryMessage: "Check progress on Etherscan",
actionHref: `https://etherscan.io/tx/${tx.hash}`,
actionText: "Check",
variant: "processing",
});
await tx.wait();

window.toastProvider.addMessage(
`Successfully supplied ${coin.symbol}!`,
{
variant: "success",
}
);

setLoading(false);
getBalances();
}}
>
{loading ? (
<Flex alignItems="center">
<span>Supplying...</span> <Loader color="white" ml="2" />
</Flex>
) : (
"Supply"
)}
</Button>
)}
</Box>
);
};
Expand Down
115 changes: 115 additions & 0 deletions packages/scripts/supply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { getLegos, networkIds } from "money-legos";
import {
wallet,
newERC20Contract,
getTokenFromUniswapAndApproveProxyTransfer,
} from "dedge-smart-contracts/test/common";
import { dedgeHelpers } from "dedge-smart-contracts/helpers";
import { ethers } from "ethers";

import {
dacProxyFactoryAddress,
dedgeCompoundManagerAddress,
} from "dedge-smart-contracts/build/DeployedAddresses.json";

import dacProxyDef from "dedge-smart-contracts/build/DACProxy.json";
import dacProxyFactoryDef from "dedge-smart-contracts/build/DACProxyFactory.json";

const legos = getLegos(networkIds.mainnet);
const erc20Tokens = Object.keys(legos.erc20).filter((x) => x !== "abi");
const cTokens = Object.keys(legos.compound).filter(
(x) => x !== "cTokenAbi" && x !== "comptroller" && x[0] === "c"
);

if (process.argv.length !== 5) {
console.log(
`ts-node supply.ts [${erc20Tokens.join("|")}] <amount> [${cTokens.join(
"|"
)}]`
);
process.exit(1);
}

if (!erc20Tokens.includes(process.argv[2])) {
console.log(
`ts-node supply.ts ${erc20Tokens.join("|")}] <amount> [${cTokens.join(
"|"
)}]`
);
process.exit(1);
}

if (!cTokens.includes(process.argv[4])) {
console.log(
`ts-node supply.ts ${erc20Tokens.join("|")}] <amount> [${cTokens.join(
"|"
)}]`
);
process.exit(1);
}

const token = process.argv[2];
const ctoken = process.argv[4];
const amount = process.argv[3];
const tokenAddress = legos.erc20[token].address;
const cTokenEquilavent = legos.compound[ctoken].address;

const dacProxyFactoryContract = new ethers.Contract(
dacProxyFactoryAddress,
dacProxyFactoryDef.abi,
wallet
);

const main = async () => {
console.log("Supplying....");
let dacProxyAddress = await dacProxyFactoryContract.proxies(wallet.address);
if (dacProxyAddress === "0x0000000000000000000000000000000000000000") {
const cTokensToEnter = [
legos.compound.cEther.address,
legos.compound.cSAI.address,
legos.compound.cDAI.address,
legos.compound.cREP.address,
legos.compound.cUSDC.address,
legos.compound.cBAT.address,
legos.compound.cZRX.address,
legos.compound.cWBTC.address,
];

console.log("Creating proxy address and entering market....");
await dedgeHelpers.proxyFactory.buildAndEnterMarkets(
dacProxyFactoryContract,
dedgeCompoundManagerAddress,
cTokensToEnter
);
dacProxyAddress = await dacProxyFactoryContract.proxies(wallet.address);
}
const dacProxyContract = new ethers.Contract(
dacProxyAddress,
dacProxyDef.abi,
wallet
);
console.log("Getting tokens from uniswap");
await getTokenFromUniswapAndApproveProxyTransfer(
dacProxyAddress,
tokenAddress,
1
);
const amountWei = ethers.utils.parseUnits(amount, token === "usdc" ? 6 : 18);
console.log("Approving erc20 transferFrom....");
await newERC20Contract(tokenAddress).approve(
dacProxyAddress,
amountWei.toString(),
{
gasLimit: 4000000,
}
);
console.log("supplying...");
await dedgeHelpers.compound.supplyThroughProxy(
dacProxyContract,
dedgeCompoundManagerAddress,
cTokenEquilavent,
amountWei.toString()
);
};

main();
4 changes: 2 additions & 2 deletions packages/smart-contracts/artifacts/DedgeCompoundManager.json
Original file line number Diff line number Diff line change
Expand Up @@ -20401,8 +20401,8 @@
"1": {
"events": {},
"links": {},
"address": "0x16493dfB0281bA27C8B712f6Eb8eC32bcc50EAEc",
"transactionHash": "0x98046a27999965dfff9390719dec08d335b3f480fafa14249a17956afb392b36"
"address": "0xd219e1cad8cde8c46f2d923ace35a8e5b729a683",
"transactionHash": "0xfcdabce2ffc0b4b892e7338140eff3da4e6575bdfd2e8375f7064434767cedd4"
}
},
"schemaVersion": "3.0.23",
Expand Down
2 changes: 1 addition & 1 deletion packages/smart-contracts/artifacts/DeployedAddresses.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dacProxyFactoryAddress": "0x572bfabaeb0f7F5d65EEa04D85595010756Ba986",
"dedgeCompoundManagerAddress": "0x16493dfB0281bA27C8B712f6Eb8eC32bcc50EAEc",
"dedgeCompoundManagerAddress": "0xd219e1cad8cde8c46f2d923ace35a8e5b729a683",
"dedgeMakerManagerAddress": "0x1C9c3ED6De13F423a4457aCCfa4e09bc436D5e89",
"dedgeExitManagerAddress": "0x81BFDCF729B5cAC3c4f1F906b63F26E5865b615B",
"dedgeGeneralManagerAddress": "0x40b4C8B5EE1FDd5259E40907E257f11819681b3d",
Expand Down
Loading

1 comment on commit 3d6c7ec

@vercel
Copy link

@vercel vercel bot commented on 3d6c7ec Apr 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.