|
| 1 | +import { dedgeHelpers } from "../../../smart-contracts/dist/helpers"; |
1 | 2 | import { ethers } from "ethers";
|
| 3 | +import { legos } from "money-legos/dist"; |
2 | 4 |
|
3 | 5 | import { Button, Loader, Box, Flex, Field, Input } from "rimble-ui";
|
4 | 6 |
|
5 | 7 | import CompoundPositions from "../../containers/CompoundPositions";
|
6 | 8 | import ContractsContainer from "../../containers/Contracts";
|
| 9 | +import ConnectionContainer from "../../containers/Connection"; |
7 | 10 | import DACProxyContainer from "../../containers/DACProxy";
|
8 | 11 |
|
9 |
| -import { useState } from "react"; |
10 |
| - |
11 |
| -import { dedgeHelpers } from "../../../smart-contracts/dist/helpers"; |
| 12 | +import { useState, useEffect } from "react"; |
12 | 13 |
|
13 | 14 | const SupplyCoin = ({ coin }) => {
|
14 | 15 | const { getBalances } = CompoundPositions.useContainer();
|
15 | 16 | const { contracts } = ContractsContainer.useContainer();
|
16 |
| - const { proxy } = DACProxyContainer.useContainer(); |
| 17 | + const { signer, address } = ConnectionContainer.useContainer(); |
| 18 | + const { hasProxy, proxy, proxyAddress } = DACProxyContainer.useContainer(); |
17 | 19 | const [loading, setLoading] = useState(false);
|
18 | 20 | const [amount, setAmount] = useState("");
|
19 | 21 |
|
| 22 | + const [transferLoading, setTransferLoading] = useState(false); |
| 23 | + const [canTransfer, setCanTransfer] = useState(null); |
| 24 | + |
| 25 | + const getCanTransfer = async () => { |
| 26 | + if (coin.symbol === "ETH") { |
| 27 | + setCanTransfer(true); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const tokenContract = new ethers.Contract( |
| 32 | + coin.address, |
| 33 | + legos.erc20.abi, |
| 34 | + signer |
| 35 | + ); |
| 36 | + |
| 37 | + const allowance = await tokenContract.allowance(address, proxyAddress); |
| 38 | + |
| 39 | + if (allowance.toString() === "0") { |
| 40 | + setCanTransfer(false); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + setCanTransfer(true); |
| 45 | + }; |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (hasProxy) { |
| 49 | + getCanTransfer(); |
| 50 | + } |
| 51 | + }, [proxy]); |
| 52 | + |
20 | 53 | return (
|
21 | 54 | <Box>
|
22 | 55 | {/* <Heading.h5 mb="2">Supply {coin.symbol}</Heading.h5> */}
|
23 | 56 | <Box mb="1">
|
24 |
| - <Field label={`Amount of ${coin.symbol} to supply`}> |
25 |
| - <Input |
26 |
| - type="number" |
27 |
| - required={true} |
28 |
| - placeholder="1337" |
29 |
| - value={amount} |
30 |
| - onChange={(e) => setAmount(e.target.value.toString())} |
31 |
| - /> |
| 57 | + <Field required={true} label={`Amount of ${coin.symbol} to supply`}> |
| 58 | + {canTransfer === false || canTransfer === null ? ( |
| 59 | + <Input required={true} type="hidden" /> |
| 60 | + ) : ( |
| 61 | + <Input |
| 62 | + type="number" |
| 63 | + required={true} |
| 64 | + placeholder="1337" |
| 65 | + value={amount} |
| 66 | + onChange={(e) => setAmount(e.target.value.toString())} |
| 67 | + /> |
| 68 | + )} |
32 | 69 | </Field>
|
33 | 70 | </Box>
|
34 |
| - <Button |
35 |
| - ml={3} |
36 |
| - disabled={loading} |
37 |
| - onClick={async () => { |
38 |
| - setLoading(true); |
39 |
| - |
40 |
| - const { dedgeCompoundManager } = contracts; |
41 |
| - const tx = await dedgeHelpers.compound.supplyThroughProxy( |
42 |
| - proxy, |
43 |
| - dedgeCompoundManager.address, |
44 |
| - coin.cTokenEquilaventAddress, |
45 |
| - ethers.utils.parseUnits(amount, coin.symbol === "USDC" ? 6 : 18) |
46 |
| - ); |
47 |
| - window.toastProvider.addMessage(`Supplying ${coin.symbol}...`, { |
48 |
| - secondaryMessage: "Check progress on Etherscan", |
49 |
| - actionHref: `https://etherscan.io/tx/${tx.hash}`, |
50 |
| - actionText: "Check", |
51 |
| - variant: "processing", |
52 |
| - }); |
53 |
| - await tx.wait() |
54 |
| - |
55 |
| - window.toastProvider.addMessage(`Successfully supplied ${coin.symbol}!`, { |
56 |
| - variant: "success", |
57 |
| - }); |
58 |
| - |
59 |
| - setLoading(false); |
60 |
| - getBalances(); |
61 |
| - }} |
62 |
| - > |
63 |
| - {loading ? ( |
64 |
| - <Flex alignItems="center"> |
65 |
| - <span>Supplying...</span> <Loader color="white" ml="2" /> |
66 |
| - </Flex> |
67 |
| - ) : ( |
68 |
| - "Supply" |
69 |
| - )} |
70 |
| - </Button> |
| 71 | + {canTransfer === false || canTransfer === null ? ( |
| 72 | + <Button |
| 73 | + ml={3} |
| 74 | + disabled={canTransfer || canTransfer === null} |
| 75 | + onClick={async () => { |
| 76 | + const maxUINT = |
| 77 | + "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; |
| 78 | + setTransferLoading(true); |
| 79 | + |
| 80 | + const tokenContract = new ethers.Contract( |
| 81 | + coin.address, |
| 82 | + legos.erc20.abi, |
| 83 | + signer |
| 84 | + ); |
| 85 | + |
| 86 | + const tx = await tokenContract.approve(proxyAddress, maxUINT); |
| 87 | + window.toastProvider.addMessage(`Approving ${coin.symbol}...`, { |
| 88 | + secondaryMessage: "Check progress on Etherscan", |
| 89 | + actionHref: `https://etherscan.io/tx/${tx.hash}`, |
| 90 | + actionText: "Check", |
| 91 | + variant: "processing", |
| 92 | + }); |
| 93 | + await tx.wait(); |
| 94 | + |
| 95 | + window.toastProvider.addMessage( |
| 96 | + `Successfully approved ${coin.symbol}!`, |
| 97 | + { |
| 98 | + variant: "success", |
| 99 | + } |
| 100 | + ); |
| 101 | + |
| 102 | + setTransferLoading(false); |
| 103 | + |
| 104 | + getCanTransfer(); |
| 105 | + }} |
| 106 | + > |
| 107 | + {canTransfer === null ? ( |
| 108 | + <Flex alignItems="center"> |
| 109 | + <span>Checking...</span> <Loader color="white" ml="2" /> |
| 110 | + </Flex> |
| 111 | + ) : transferLoading ? ( |
| 112 | + <Flex alignItems="center"> |
| 113 | + <span>Approving...</span> <Loader color="white" ml="2" /> |
| 114 | + </Flex> |
| 115 | + ) : ( |
| 116 | + "Approve Transfer" |
| 117 | + )} |
| 118 | + </Button> |
| 119 | + ) : ( |
| 120 | + <Button |
| 121 | + ml={3} |
| 122 | + disabled={loading || !canTransfer} |
| 123 | + onClick={async () => { |
| 124 | + setLoading(true); |
| 125 | + |
| 126 | + const { dedgeCompoundManager } = contracts; |
| 127 | + const tx = await dedgeHelpers.compound.supplyThroughProxy( |
| 128 | + proxy, |
| 129 | + dedgeCompoundManager.address, |
| 130 | + coin.cTokenEquilaventAddress, |
| 131 | + ethers.utils.parseUnits(amount, coin.symbol === "USDC" ? 6 : 18) |
| 132 | + ); |
| 133 | + window.toastProvider.addMessage(`Supplying ${coin.symbol}...`, { |
| 134 | + secondaryMessage: "Check progress on Etherscan", |
| 135 | + actionHref: `https://etherscan.io/tx/${tx.hash}`, |
| 136 | + actionText: "Check", |
| 137 | + variant: "processing", |
| 138 | + }); |
| 139 | + await tx.wait(); |
| 140 | + |
| 141 | + window.toastProvider.addMessage( |
| 142 | + `Successfully supplied ${coin.symbol}!`, |
| 143 | + { |
| 144 | + variant: "success", |
| 145 | + } |
| 146 | + ); |
| 147 | + |
| 148 | + setLoading(false); |
| 149 | + getBalances(); |
| 150 | + }} |
| 151 | + > |
| 152 | + {loading ? ( |
| 153 | + <Flex alignItems="center"> |
| 154 | + <span>Supplying...</span> <Loader color="white" ml="2" /> |
| 155 | + </Flex> |
| 156 | + ) : ( |
| 157 | + "Supply" |
| 158 | + )} |
| 159 | + </Button> |
| 160 | + )} |
71 | 161 | </Box>
|
72 | 162 | );
|
73 | 163 | };
|
|
0 commit comments