diff --git a/.npmignore b/.npmignore index 93ddc08..332eef1 100644 --- a/.npmignore +++ b/.npmignore @@ -3,7 +3,7 @@ node_modules /dist /docs /src - +/dist/src .env # local env files .env.local diff --git a/package.json b/package.json index 165d51d..6e2772c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@pontem/liquidswap-widget", - "version": "0.2.12", + "version": "0.3.0", "homepage": "https://github.com/pontem-network/liquidswap-widget#readme", "description": "LiquidSwap widget as custom web component", "files": [ @@ -49,7 +49,7 @@ "dependencies": { "@metamask/jazzicon": "2.0.0", "@pontem/aptos-wallet-adapter": "0.7.2", - "@pontem/coins-registry": "2.1.8", + "@pontem/coins-registry": "2.1.19", "@pontem/liquidswap-sdk": "0.6.1", "@tsconfig/recommended": "1.0.2", "@types/lodash": "4.14.182", diff --git a/src/Swap/SwapContainer.vue b/src/Swap/SwapContainer.vue index d71ffd6..c5a5af9 100644 --- a/src/Swap/SwapContainer.vue +++ b/src/Swap/SwapContainer.vue @@ -41,20 +41,21 @@
+ v-if="tokensChosen && !fullCurveOfDefaultPool" + class="swap__row" + :class="[mainStore.insideNativeWallet.value && 'swap__row--extra-padding']" + > Caution: make sure the pair you are trading should be stable or - uncorrelated. i.e USDC/USDT is stable and USDC/BTC is - uncorrelatedCaution: make sure the pair you are trading should be stable or uncorrelated. i.e USDC/USDT + is stable and USDC/BTC is uncorrelated
- + v-show="tokensChosen && !fullCurveOfDefaultPool" + class="swap__row" + :class="[mainStore.insideNativeWallet.value && 'swap__row--extra-padding']" + > +
- +
- + Connect Wallet
-

- More features in full-size version -

+

More features in full-size version

Go to the web dApp to add liquidity or stake LP tokens in farms

- + liquidswap.com
@@ -115,7 +109,7 @@ :to-token="swapStore.toCurrency.token" :from-token="swapStore.fromCurrency.token" /> - +
@@ -131,7 +125,7 @@ import { ReservesContainer } from '@/components/ReservesContainer'; import { TxSettingsDialog } from '@/components/TxSettingsDialog'; import { ContractSwitch } from '@/components/ContractSwitch'; import { useCurrentAccountBalance } from '@/composables/useAccountBalance'; -import { useStore, useSwapStore, useTokensStore, usePoolsStore } from '@/store'; +import { useStore, useSwapStore, usePoolsStore } from '@/store'; import { d } from '@/utils/utils'; import SwapInfo from './SwapInfo.vue'; import SwapInput from './SwapInput.vue'; @@ -141,22 +135,21 @@ import { VERSION_0, VERSION_0_5, CURVE_UNCORRELATED_V05, - CURVE_UNCORRELATED + CURVE_UNCORRELATED, } from '@/constants/constants'; import { getCurve, getShortCurveFromFull } from '@/utils/contracts'; -import { TVersionType } from "@/types"; +import { TCurveType, TVersionType } from '@/types'; +import { IPersistedPool } from '@/types/pools'; const mainStore = useStore(); const poolsStore = usePoolsStore(); const swapStore = useSwapStore(); -const tokensStore = useTokensStore(); const { account } = mainStore; const version = computed(() => swapStore.version); -const insideNativeWallet = computed(() => mainStore.insideNativeWallet.value); const stableCurve = computed(() => getCurve('stable', version.value)); -const unstableCurve = computed(() => getCurve('uncorrelated', version.value)); +const unstableCurve = computed(() => getCurve('unstable', version.value)); const connected = computed(() => Boolean(account.value)); @@ -175,46 +168,43 @@ const curveType = computed(() => const fullCurveOfDefaultPool = computed(() => { // Now all known pools have version 0 const isExistsDefaultPoolV0 = poolsStore.getCurveType( - swapStore.fromCurrency?.token, - swapStore.toCurrency?.token, - VERSION_0, + swapStore.fromCurrency?.token, + swapStore.toCurrency?.token, + VERSION_0, ); const isExistsDefaultPoolV05 = poolsStore.getCurveType( - swapStore.fromCurrency?.token, - swapStore.toCurrency?.token, - VERSION_0_5, + swapStore.fromCurrency?.token, + swapStore.toCurrency?.token, + VERSION_0_5, ); const hasDefaultPoolStableCurveV0 = [CURVE_STABLE, CURVE_STABLE_V05].includes( - '' + isExistsDefaultPoolV0, + '' + isExistsDefaultPoolV0, ); - const hasDefaultPoolStableCurveV05 = [ - CURVE_STABLE, - CURVE_STABLE_V05, - ].includes('' + isExistsDefaultPoolV05); + const hasDefaultPoolStableCurveV05 = [CURVE_STABLE, CURVE_STABLE_V05].includes( + '' + isExistsDefaultPoolV05, + ); - const hasDefaultPoolUncorrelatedCurveV0 = [ - CURVE_UNCORRELATED, - CURVE_UNCORRELATED_V05, - ].includes('' + isExistsDefaultPoolV0); + const hasDefaultPoolUncorrelatedCurveV0 = [CURVE_UNCORRELATED, CURVE_UNCORRELATED_V05].includes( + '' + isExistsDefaultPoolV0, + ); - const hasDefaultPoolUncorrelatedCurveV05 = [ - CURVE_UNCORRELATED, - CURVE_UNCORRELATED_V05, - ].includes('' + isExistsDefaultPoolV05); + const hasDefaultPoolUncorrelatedCurveV05 = [CURVE_UNCORRELATED, CURVE_UNCORRELATED_V05].includes( + '' + isExistsDefaultPoolV05, + ); if ( - (isExistsDefaultPoolV0 || isExistsDefaultPoolV05) && - (hasDefaultPoolStableCurveV0 || hasDefaultPoolStableCurveV05) + (isExistsDefaultPoolV0 || isExistsDefaultPoolV05) && + (hasDefaultPoolStableCurveV0 || hasDefaultPoolStableCurveV05) ) { return version.value === VERSION_0_5 ? CURVE_STABLE_V05 : CURVE_STABLE; } if ( - (isExistsDefaultPoolV0 || isExistsDefaultPoolV05) && - (hasDefaultPoolUncorrelatedCurveV0 || hasDefaultPoolUncorrelatedCurveV05) + (isExistsDefaultPoolV0 || isExistsDefaultPoolV05) && + (hasDefaultPoolUncorrelatedCurveV0 || hasDefaultPoolUncorrelatedCurveV05) ) { return version.value === VERSION_0_5 ? CURVE_UNCORRELATED_V05 : CURVE_UNCORRELATED; } @@ -223,41 +213,81 @@ const fullCurveOfDefaultPool = computed(() => { }); watch([curveType, stableCurve, unstableCurve], () => { - if (curveType.value) { - swapStore.curve = - curveType.value === stableCurve.value || curveType.value === 'stable' - ? stableCurve.value - : unstableCurve.value; + if (curveType.value) { + swapStore.curve = + curveType.value === stableCurve.value || curveType.value === 'stable' + ? stableCurve.value + : unstableCurve.value; + } else { + const shortName = getShortCurveFromFull(swapStore.curve); + const adaptedShoerName = shortName === 'uncorrelated' ? 'unstable' : 'stable'; + swapStore.curve = getCurve(adaptedShoerName, version.value); + } +}); + +watch( + () => [swapStore.fromCurrency.token, swapStore.toCurrency.token], + async ([newFrom, newTo], [oldFrom, oldTo]) => { + if (!newFrom || !newTo) return; + + // If the pair of tokens is the same, we don't need to set the curve + if (oldFrom === newTo && oldTo == newFrom) { + return; } - else { - const shortName = getShortCurveFromFull(swapStore.curve); - swapStore.curve = getCurve(shortName, version.value); + + // need to reset the version when changing a pair of tokens + swapStore.version = VERSION_0; + + let resultCurve: string | undefined; + let resultVersion: number = VERSION_0; + let resultPool: IPersistedPool | undefined; + + for (const curveType of ['stable', 'unstable'] as TCurveType[]) { + for (const version of [VERSION_0_5, VERSION_0]) { + const curve = getCurve(curveType, version); + + let pool = undefined; + try { + pool = await poolsStore.getPool(newFrom, newTo, curve, version); + } catch (error) { + console.error('SwapContainer: getPool', error); + } + + if ( + !resultPool || + ((pool?.reserveX ?? 0) > resultPool.reserveX && (pool?.reserveY ?? 0) > resultPool.reserveY) + ) { + resultPool = pool; + resultCurve = curve; + resultVersion = version; + } + } } - }, -) -const fromBalance = useCurrentAccountBalance( - computed(() => swapStore.fromCurrency?.token), -); -const toBalance = useCurrentAccountBalance( - computed(() => swapStore.toCurrency?.token), + /** + * Set uncorrelated pool version 0 as the default value. + * In this case, the user chooses the curve and version himself. + */ + swapStore.curve = resultCurve; + swapStore.version = resultVersion; + }, ); + +const fromBalance = useCurrentAccountBalance(computed(() => swapStore.fromCurrency?.token)); +const toBalance = useCurrentAccountBalance(computed(() => swapStore.toCurrency?.token)); const txSettingsDialog = ref(); const overlayAnchor = ref(); -const tokensChosen = computed( - () => !!swapStore.fromCurrency.token && !!swapStore.toCurrency.token, -); +const tokensChosen = computed(() => !!swapStore.fromCurrency.token && !!swapStore.toCurrency.token); const canSwitchContract = computed(() => { const isToTokenChosen = swapStore.toCurrency?.token; const isDefaultPool = !!fullCurveOfDefaultPool.value; const hasDefaultPoolStableCurve = [CURVE_STABLE_V05, CURVE_STABLE].includes( - '' + fullCurveOfDefaultPool.value, + '' + fullCurveOfDefaultPool.value, ); const isPoolUnknown = fullCurveOfDefaultPool.value === false; - const canSwitch = - (isDefaultPool && hasDefaultPoolStableCurve) || isPoolUnknown; + const canSwitch = (isDefaultPool && hasDefaultPoolStableCurve) || isPoolUnknown; return isToTokenChosen && canSwitch; }); @@ -304,10 +334,7 @@ const buttonState = computed(() => { }; } - if ( - !toBalance.isExists.value && - !(toBalance.isFetching.value && toBalance.isFirstFetching.value) - ) { + if (!toBalance.isExists.value && !(toBalance.isFetching.value && toBalance.isFirstFetching.value)) { return { disabled: false, text: `Register ${toBalance.symbol.value} and Swap`, @@ -322,18 +349,16 @@ const buttonState = computed(() => { const priceImpactClass = computed(() => { return swapStore.priceImpactState === 'normal' - ? 'p-button-primary' - : swapStore.priceImpactState === 'warning' - ? 'p-button-warning_custom' - : 'p-button-alert'; + ? 'p-button-primary' + : swapStore.priceImpactState === 'warning' + ? 'p-button-warning_custom' + : 'p-button-alert'; }); function submitForm(e: Event) { const isNextButton = (e.target as HTMLElement)?.enterKeyHint === 'next'; const isSubmitDisabled = buttonState.value.disabled; - const cancelEvent = Boolean( - (isNextButton || isSubmitDisabled) && connected.value, - ); + const cancelEvent = Boolean((isNextButton || isSubmitDisabled) && connected.value); if (cancelEvent) return; const handler = !connected.value ? onConnectWallet : showSwapDialog; @@ -350,8 +375,7 @@ function toggleSwap() { function showSwapDialog() { const isShowSwapWarningDialog = - swapStore.priceImpactState === 'warning' || - swapStore.priceImpactState === 'alert'; + swapStore.priceImpactState === 'warning' || swapStore.priceImpactState === 'alert'; if (isShowSwapWarningDialog) { mainStore.showDialog('priceImpact'); diff --git a/src/Swap/SwapInfo.vue b/src/Swap/SwapInfo.vue index 56eaaa3..27a79a4 100644 --- a/src/Swap/SwapInfo.vue +++ b/src/Swap/SwapInfo.vue @@ -110,7 +110,7 @@ const slippageAmount = computed(() => swap.slippageAmount); const hasSlippage = computed( () => (version.value === VERSION_0 && - swap.curve === getCurve('uncorrelated', version.value)) || + swap.curve === getCurve('unstable', version.value)) || version.value === VERSION_0_5, ); diff --git a/src/components/ButtonToken/buttonToken.scss b/src/components/ButtonToken/buttonToken.scss index 9c41302..b4228ba 100644 --- a/src/components/ButtonToken/buttonToken.scss +++ b/src/components/ButtonToken/buttonToken.scss @@ -10,7 +10,7 @@ color: white; &:hover { - background: linear-gradient(90deg, #015c68 0%, #04a57d 100%); + background: linear-gradient(90deg, #015c68 0%, #04a57d 100%) !important; color: white; } diff --git a/src/components/ContractSwitch/ContractSwitch.vue b/src/components/ContractSwitch/ContractSwitch.vue index 4ca02aa..e88f987 100644 --- a/src/components/ContractSwitch/ContractSwitch.vue +++ b/src/components/ContractSwitch/ContractSwitch.vue @@ -3,9 +3,9 @@
Pool Version @@ -14,9 +14,11 @@ :options="poolVersionOptions" option-disabled="disabled" option-value="value" + data-tid="pool-version-switch-container" option-label="label" class="ml-auto no-a11y" :unselectable="false" + :disabled="!canSwitchVersion" >