Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add swap and fix donate #72

Merged
merged 21 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
08d3d50
updated useContractCalls and much of DonateComponent
krisbitney Dec 29, 2023
9abd3c4
added useApproveSwapTokenCallback and useSwapRoute
krisbitney Dec 29, 2023
4e02064
draft swap integration
krisbitney Dec 30, 2023
31ff9ae
added index to hooks/useContractCalls
krisbitney Dec 30, 2023
5d33d59
prevented swap from G$ to G$
krisbitney Dec 30, 2023
56cd466
fixed formatting and some swap bugs
krisbitney Dec 30, 2023
5404b97
removed unused function from lib
krisbitney Dec 30, 2023
1e2603e
Merge remote-tracking branch 'origin/kris/realtime-donation-flow' int…
krisbitney Dec 30, 2023
aafd807
Merge remote-tracking branch 'origin/kris/realtime-donation-flow' int…
krisbitney Jan 4, 2024
b9bbeb5
now using celo token list
krisbitney Jan 4, 2024
cb1e9aa
removed unused import
krisbitney Jan 4, 2024
c4b8201
made dropdown menu scrollable
krisbitney Jan 4, 2024
4e85141
Merge remote-tracking branch 'origin/web3Integrations' into kris/inte…
krisbitney Jan 4, 2024
e24a890
swap works!
krisbitney Jan 5, 2024
2fb34b3
adjustments to reflect uniswap default slippage tolerance
krisbitney Jan 5, 2024
b3ad4e1
fixed jsbi via vite configuration
krisbitney Jan 6, 2024
72e8905
changed priceImpact to number
krisbitney Jan 7, 2024
cf67b5d
removed unused `WalletConnectionProvider` context
krisbitney Jan 8, 2024
f7cfdcf
by request, handleApproveToken is no longer memoized
krisbitney Jan 8, 2024
09dbb70
fixed GD formatting in Donor List
krisbitney Jan 9, 2024
4ee4e32
changed acceptable price impact percent to 5% and moved it to constan…
krisbitney Jan 9, 2024
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
3 changes: 2 additions & 1 deletion packages/app/src/components/DonateComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ function DonateComponent({ collective }: DonateComponentProps) {

const isInsufficientBalance = donorCurrencyBalance ? totalDecimalDonation > donorCurrencyBalance : true;
const isInsufficientLiquidity = currency !== 'G$' && swapRouteStatus !== SwapRouteState.READY;
const isUnacceptablePriceImpact = currency !== 'G$' && priceImpact ? priceImpact.gte(0.1) : false;
// TODO: what is an acceptable price impact?
const isUnacceptablePriceImpact = currency !== 'G$' && priceImpact ? priceImpact.gte(10) : false;
Copy link
Contributor

Choose a reason for hiding this comment

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

i think 5%.
What does the UI specifications says? is there an alert?
@patpedrosa @decentralauren

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the example says 36%, but i don't think that's intended as the cutoff

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it to 5% for now, and also moved it to models/constants.ts so it is easier to find if you want to change it later


const { price } = useGetTokenPrice(currency);
const usdValue = price ? formatFiatCurrency(decimalDonationAmount * price) : undefined;
Expand Down
24 changes: 20 additions & 4 deletions packages/app/src/hooks/useGetTokenPrice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,32 @@ export const useGetTokenPrice = (currency: string): { price?: number; isLoading:
return { price, isLoading };
};

const getTokenPrice = (currency: string, token: Token): Promise<number | undefined> => {
const getTokenPrice = async (currency: string, token: Token): Promise<number | undefined> => {
let tokenAddress = coingeckoTokenMapping[currency] ?? token.address;
const url = `https://api.coingecko.com/api/v3/simple/token_price/celo?contract_addresses=${tokenAddress}&vs_currencies=usd`;
return axios
.get(url)
const priceByContractUrl = `https://api.coingecko.com/api/v3/simple/token_price/celo?contract_addresses=${tokenAddress}&vs_currencies=usd`;
const priceByContract: number | undefined = await axios
.get(priceByContractUrl)
.then((res) => {
return res.data[tokenAddress.toLowerCase()]?.usd;
})
.catch((err) => {
console.error(err);
return undefined;
});

if (priceByContract !== undefined) {
return priceByContract;
}

// fallback
const priceBySymbolUrl = `https://api.coingecko.com/api/v3/simple/price?ids=${currency}&vs_currencies=usd`;
return await axios
.get(priceBySymbolUrl)
.then((res) => {
return res.data[currency.toLowerCase()]?.usd;
})
.catch((err) => {
console.error(err);
return undefined;
});
};
24 changes: 10 additions & 14 deletions packages/app/src/hooks/useSwapRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AlphaRouter, SwapRoute, V3Route } from '@uniswap/smart-order-router';
import { AlphaRouter, SwapRoute, SwapType, V3Route } from '@uniswap/smart-order-router';
import { CurrencyAmount, Percent, TradeType } from '@uniswap/sdk-core';
import { useAccount, useNetwork } from 'wagmi';
import { GDToken } from '../models/constants';
Expand Down Expand Up @@ -55,14 +55,12 @@ export function useSwapRoute(
inputAmount,
GDToken,
TradeType.EXACT_INPUT,
// TODO: use SwapConfig when https://github.com/Uniswap/sdk-core/issues/20 is resolved by https://github.com/Uniswap/sdk-core/pull/69
// {
// type: SwapType.SWAP_ROUTER_02,
// recipient: address,
// slippageTolerance: slippageTolerance,
// deadline: Math.floor(Date.now() / 1000 + 1800),
// },
undefined,
{
type: SwapType.SWAP_ROUTER_02,
recipient: address,
slippageTolerance: slippageTolerance,
deadline: Math.floor(Date.now() / 1000 + 1800),
},
{
protocols: [Protocol.V3],
}
Expand All @@ -76,16 +74,14 @@ export function useSwapRoute(
});
}, [address, chain?.id, signer?.provider, tokenIn, decimalAmountIn, duration, slippageTolerance]);

if (!route) {
if (!route || !route.methodParameters) {
return { status: SwapRouteState.NO_ROUTE };
} else {
// This typecast is safe because Uniswap v2 is not deployed on Celo
const path = encodeRouteToPath(route.route[0].route as V3Route, false);
const quote = new Decimal(route.quote.toFixed(18));
// TODO: use commented out values when https://github.com/Uniswap/sdk-core/issues/20 is resolved by https://github.com/Uniswap/sdk-core/pull/69
const rawMinimumAmountOut =
route.quoteGasAndPortionAdjusted?.numerator.toString() ?? route.quoteGasAdjusted.numerator.toString(); // route.trade.minimumAmountOut(slippageTolerance).numerator.toString();
const priceImpact = new Decimal(0.005); // new Decimal(route.trade.priceImpact.toFixed(4));
const rawMinimumAmountOut = route.trade.minimumAmountOut(slippageTolerance).numerator.toString();
const priceImpact = new Decimal(route.trade.priceImpact.toFixed(4));
return { path, quote, rawMinimumAmountOut, priceImpact, status: SwapRouteState.READY };
Copy link
Contributor

Choose a reason for hiding this comment

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

why is price impact converted here? to Decimal?

Copy link
Contributor Author

@krisbitney krisbitney Jan 7, 2024

Choose a reason for hiding this comment

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

It was so it could be compared to a number, and also to remove trailing zeroes. It could be number instead of Decimal. I just changed it in a new commit.

}
}
2 changes: 2 additions & 0 deletions packages/app/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import react from '@vitejs/plugin-react';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import dynamicImports from 'vite-plugin-dynamic-import';
import viteTsconfigPaths from 'vite-tsconfig-paths';
import * as path from 'path';

// https://vitejs.dev/config/
export default defineConfig({
Expand All @@ -12,6 +13,7 @@ export default defineConfig({
'react-native': 'react-native-web',
'react-native-svg': 'react-native-svg-web',
'react-native-webview': 'react-native-web-webview',
jsbi: path.resolve(__dirname, '..', 'node_modules', 'jsbi', 'dist', 'jsbi-cjs.js'),
},
dedupe: ['react', 'ethers', 'react-dom', 'native-base'],
},
Expand Down
Loading