Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions example-web/pages/components/DaoBreakdown/ApproveDonate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,74 @@ const ApproveDonate = () => {
const [approvedAmount, setApprovedAmount] = useState(false);
const [donationIsLoading, setDonationIsLoading] = useState(false);
const [donationAmount, setDonationAmount] = useState('');

const [communityAddress, setCommunityAddress] = useState('');

const approveDonation = async () => {
setDonationIsLoading(true);

const response = await donationMiner.approve(donationAmount);
let response;
if (communityAddress.length > 0) {
response = await donationMiner.approve(donationAmount, communityAddress);
} else {
response = await donationMiner.approve(donationAmount);
}

setDonationIsLoading(false);

if (response?.status) {
setApprovedAmount(true);
}
}
};

const executeDonation = async () => {
setDonationIsLoading(true);

const response = await donationMiner.donateToTreasury(donationAmount);
let response;
if (communityAddress.length > 0) {
response = await donationMiner.donateToCommunity(communityAddress, donationAmount);
} else {
response = await donationMiner.donateToTreasury(donationAmount);
}

setDonationIsLoading(false);

if (response?.status) {
setApprovedAmount(false);
}
}
};

return (
<>
<h3>Approve & Donate</h3>
<div style={{ marginTop: 8 }}>
<input disabled={donationIsLoading} onChange={event => setDonationAmount(event.target.value)} type="number" value={donationAmount || ''} />
<button disabled={donationIsLoading || approvedAmount} onClick={approveDonation}>Approve</button>
<button disabled={donationIsLoading || !approvedAmount} onClick={executeDonation}>Donate</button>
<input
placeholder="amount"
disabled={donationIsLoading}
onChange={event => setDonationAmount(event.target.value)}
type="number"
value={donationAmount || ''}
/>
<br />
<input
placeholder="community address (optional)"
disabled={donationIsLoading}
onChange={event => setCommunityAddress(event.target.value)}
value={communityAddress || ''}
style={{ width: 400 }}
/>
<br />
<button disabled={donationIsLoading || approvedAmount} onClick={approveDonation}>
Approve
</button>
<br />
<button disabled={donationIsLoading || !approvedAmount} onClick={executeDonation}>
Donate
</button>
<br />
{donationIsLoading && <span> Loading...</span>}
</div>
</>
)
}
);
};

export default ApproveDonate;
8 changes: 6 additions & 2 deletions src/internalUseTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export const internalUseTransaction = () => {
throw new Error('no valid address connected');
}

if (signer) {
return tx;
}

const [_nonce, _gasPrice, _gasLimit, _value] = await Promise.all([
apiGetAccountNonce(jsonRpcUrl, tx.from || address),
apiGetGasPrice(jsonRpcUrl, defaultFeeCurrency),
Expand All @@ -62,7 +66,7 @@ export const internalUseTransaction = () => {
...tx,
account: (tx.from || address) as `0x${string}`,
gas: BigInt(_gasLimit.toNumber() * 2),
gasPrice: BigInt(_gasPrice),
gasPrice: BigInt(parseInt(_gasPrice, 16) * 2),
nonce: _nonce,
value: BigInt(_value)
};
Expand Down Expand Up @@ -92,7 +96,7 @@ export const internalUseTransaction = () => {
const txResponse = await connection.sendTransaction({
data: tx.data,
from: tx.from || address,
gasPrice: await apiGetGasPrice(jsonRpcUrl, defaultFeeCurrency),
gasPrice: parseInt(await apiGetGasPrice(jsonRpcUrl, defaultFeeCurrency), 16) * 2,
to: tx.to
});

Expand Down
2 changes: 2 additions & 0 deletions src/useBorrower.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export const useBorrower = () => {

if (loanId !== -1) {
userLoans(address, loanId).then(() => setIsReady(true));
} else {
setIsReady(true);
}
};

Expand Down
38 changes: 16 additions & 22 deletions src/useDonationMiner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,27 @@ export const useDonationMiner = () => {
const executeTransaction = internalUseTransaction();

const approve = async (value: string | number, to?: string) => {
try {
const { cusd, donationMiner } = getContracts(provider, networkId);
const amount = toToken(value, { EXPONENTIAL_AT: 29 });

if (to === undefined) {
to = donationMiner.address;
}
if (!address || !donationMiner?.provider || !cusd?.provider || !amount) {
return;
}
const { cusd, donationMiner } = getContracts(provider, networkId);
const amount = toToken(value, { EXPONENTIAL_AT: 29 });

const cUSDAllowance = await cusd.allowance(address, to);
const cusdAllowance = toNumber(cUSDAllowance);
const allowance = cusdAllowance || 0;
if (to === undefined) {
to = donationMiner.address;
}
if (!address || !donationMiner?.provider || !cusd?.provider || !amount) {
return;
}

if (allowance >= Number(value)) {
return { status: true };
}
const cUSDAllowance = await cusd.allowance(address, to);
const cusdAllowance = toNumber(cUSDAllowance);
const allowance = cusdAllowance || 0;

const tx = await cusd.populateTransaction.approve(to, amount);
if (allowance >= Number(value)) {
return { status: true };
}

return await executeTransaction(tx);
} catch (error) {
console.log('Error approving amount: \n', error);
const tx = await cusd.populateTransaction.approve(to, amount);

return { status: false };
}
return await executeTransaction(tx);
};

const donateToTreasury = async (value: string | number) => {
Expand Down