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

Integrate alex swaps with stacks transaction sponsor #515

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
31f9223
feat(Dashboard): update new action button style and add swap button
zhigang1992 Apr 3, 2023
0f0b865
feat(Swap): add swap screen placeholder
zhigang1992 Apr 3, 2023
5eaebb3
feat(swap): add swap basic UI
zhigang1992 Apr 15, 2023
bccda0b
feat(swap): add more UI components
zhigang1992 Apr 15, 2023
99f1fcc
feat(swap): add slippage
zhigang1992 Apr 15, 2023
2c08d75
feat(swap): make BitCoin and Stacks option in the coin select modal
zhigang1992 Apr 15, 2023
8acb918
feat(swap): add token selection logic
zhigang1992 Apr 15, 2023
accf1a8
feat: add swap data connection
zhigang1992 Apr 15, 2023
358f98f
feat(swap): add runSwap
zhigang1992 Apr 15, 2023
9a9daef
chore(swap): bump alex-sdk version
zhigang1992 Apr 15, 2023
c115639
fix(swap): slippage setting
zhigang1992 Apr 15, 2023
558e1c1
feat: swap confirm layout
zhigang1992 Apr 20, 2023
e5f5e0d
feat: add data bindings
zhigang1992 Apr 20, 2023
d71dc7f
fix: function name and copy address
zhigang1992 Apr 21, 2023
084ab9f
feat: add advanced settings to the confirmation page
zhigang1992 Apr 21, 2023
2a2afa7
feat: bump alex-sdk to latest and fix the issue where from is the sam…
zhigang1992 Apr 26, 2023
7c2243c
feat: add math.floor before converting to bigInt
zhigang1992 Apr 26, 2023
7b44f27
fix: skip action when input is invalid
zhigang1992 Apr 26, 2023
d8152ac
chore: update package.lock
zhigang1992 May 5, 2023
f3318b3
feat(swap): update to support amm v1_1 pools
zhigang1992 May 7, 2023
9e44686
chore: fix lint warnings
teebszet Jun 23, 2023
b06df1d
fix: update swap svg stroke and add missing translation
teebszet Jun 23, 2023
89961fd
fix: add key to fix react warnings
teebszet Jun 27, 2023
23b118a
chore: fix lint warnings
teebszet Jun 27, 2023
49b6127
fix: add error 700 border to swap token card
teebszet Jun 27, 2023
81c5dbf
fix: update swap error message and restrict input to number
teebszet Jun 27, 2023
ea886a3
chore: prettier
teebszet Jun 27, 2023
ed6ae75
style: add a not-allowed cursor to disabled buttons
teebszet Jun 27, 2023
8686541
fix: update translations
teebszet Jun 27, 2023
c969917
fix: use background elevation_1
teebszet Jun 27, 2023
e7f56f0
style: update swap details spacing
teebszet Jun 27, 2023
5a5540e
style: use unicode arrow right for swap route details
teebszet Jun 27, 2023
6592b2c
feat: implement toggle tokens button in swaps
teebszet Jun 27, 2023
f4b601b
feat: add reset slippage button to swaps
teebszet Jun 27, 2023
9421f7d
fix: refactor select currency logic to use reducer and unit test
teebszet Jun 28, 2023
51b9360
chore: commit package-lock
teebszet Jun 28, 2023
e8d8c38
fix: disable button when insufficient balance
teebszet Jun 28, 2023
054c2e8
fix: add token name to min received
teebszet Jun 28, 2023
51da5e8
fix: advanced settings modal on swaps
teebszet Jun 28, 2023
a4bd16b
fix: use 4dp rounding on exchange rate
teebszet Jun 28, 2023
844fa75
fix: toggle button should also toggle amounts
teebszet Jun 29, 2023
0f5a100
fix: function font style should be body_medium_m
teebszet Jun 29, 2023
44c5e23
feat: make swap confirmation buttons sticky bottom
teebszet Jun 29, 2023
79b36d4
fix: standardise button hover, active, disabled css
teebszet Jun 29, 2023
dcf4f8c
feat: add error state to slippage input
teebszet Jun 29, 2023
07bf741
fix: use intended params for swap advance settings on apply handler
teebszet Jun 29, 2023
4305af5
fix: padding on swap confirm buttons
teebszet Jul 7, 2023
47069e6
fix: no exponents in fees block of swap confirmation
teebszet Jul 7, 2023
dab6139
feat: add sponsor swap transaction UI and add a hook with placeholder
teebszet Jul 12, 2023
92fb649
feat: bump xverse-core dep version and use sponsor2 url
teebszet Jul 17, 2023
3654327
fix: bump xverse-core version and handle sponsor transaction error
teebszet Jul 18, 2023
c85ee22
Merge branch 'feat/swap' into tim/eng-2377-integrate-alex-swaps-with-…
teebszet Jul 18, 2023
6f9476e
style: prettier
teebszet Jul 18, 2023
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
670 changes: 659 additions & 11 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"private": true,
"dependencies": {
"@react-spring/web": "^9.6.1",
"@secretkeylabs/xverse-core": "1.1.2",
"@secretkeylabs/xverse-core": "1.4.0-1004b69",
"@stacks/connect": "^6.10.2",
"@stacks/encryption": "4.3.5",
"@stacks/stacks-blockchain-api-types": "^6.1.1",
"@stacks/transactions": "^4.3.8",
"@tanstack/query-sync-storage-persister": "^4.29.1",
"@tanstack/react-query": "^4.29.3",
"@tanstack/react-query-devtools": "^4.29.3",
"@tanstack/react-query-persist-client": "^4.29.3",
"@stacks/transactions": "^4.3.8",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
Expand Down
11 changes: 8 additions & 3 deletions src/app/components/tokenImage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ const TickerIconText = styled.h1((props) => ({
fontSize: 13,
}));

export default function TokenImage(props: TokenImageProps) {
const { token, loading, fungibleToken, round, size, loaderSize } = props;

export default function TokenImage({
token,
loading,
fungibleToken,
size,
loaderSize,
round,
}: TokenImageProps) {
const getCoinIcon = useCallback(() => {
if (token === 'STX') {
return IconStacks;
Expand Down
32 changes: 32 additions & 0 deletions src/app/hooks/useSponsoredTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { getSponsorInfo } from '@secretkeylabs/xverse-core/api';

export const useSponsorInfoQuery = (sponsorUrl?: string) =>
useQuery({
queryKey: ['sponsorInfo'],
queryFn: async () => {
try {
return await getSponsorInfo(sponsorUrl);
} catch (e: any) {
return Promise.reject(e);
}
},
});

export const useSponsoredTransaction = (sponsorUrl?: string) => {
const [isSponsored, setIsSponsored] = useState(false);

const { error, data: isActive } = useSponsorInfoQuery(sponsorUrl);
useEffect(() => {
if (!error) {
setIsSponsored(!!isActive);
}
}, [isActive, error]);

return {
isSponsored,
};
};

export default useSponsoredTransaction;
1 change: 1 addition & 0 deletions src/app/screens/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const RowButtonContainer = styled.div((props) => ({
flexDirection: 'row',
marginTop: props.theme.spacing(11),
columnGap: props.theme.spacing(11),

}));

const TokenListButtonContainer = styled.div((props) => ({
Expand Down
28 changes: 26 additions & 2 deletions src/app/screens/swap/swapConfirmation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { useCallback, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { useConfirmSwap } from '@screens/swap/swapConfirmation/useConfirmSwap';
import { AdvanceSettings } from '@screens/swap/swapConfirmation/advanceSettings';
import { useSponsoredTransaction } from '@hooks/useSponsoredTransaction';
import SponsoredTransactionIcon from '@assets/img/transactions/CircleWavyCheck.svg';

const TitleText = styled.div((props) => ({
fontSize: 21,
Expand All @@ -29,19 +31,34 @@ export const ButtonContainer = styled.div((props) => ({
position: 'sticky',
bottom: 0,
background: props.theme.colors.background.elevation0,
padding: `${props.theme.spacing(12)}px 0`
padding: `${props.theme.spacing(12)}px 0`,
}));

export const ActionButtonWrap = styled.div((props) => ({
marginRight: props.theme.spacing(8),
width: '100%',
}));

const SponsoredTransactionText = styled.div((props) => ({
...props.theme.body_m,
color: props.theme.colors.white[200],
marginTop: props.theme.spacing(10),
display: 'flex',
gap: props.theme.spacing(4),
}));

const Icon = styled.img((props) => ({
marginTop: props.theme.spacing(1),
width: 24,
height: 24,
}));

export default function SwapConfirmation() {
const { t } = useTranslation('translation', { keyPrefix: 'SWAP_CONFIRM_SCREEN' });
const location = useLocation();
const navigate = useNavigate();
const swap = useConfirmSwap(location.state);
const { isSponsored } = useSponsoredTransaction();

const onCancel = useCallback(() => {
navigate('/swap');
Expand Down Expand Up @@ -69,7 +86,14 @@ export default function SwapConfirmation() {
lpFeeFiatAmount={swap.lpFeeFiatAmount}
currency={swap.fromToken.name}
/>
<AdvanceSettings swap={swap} />
{isSponsored ? (
<SponsoredTransactionText>
<Icon src={SponsoredTransactionIcon} />
{t('THIS_IS_A_SPONSORED_TRANSACTION')}
</SponsoredTransactionText>
) : (
<AdvanceSettings swap={swap} />
)}
<ButtonContainer>
<ActionButtonWrap>
<ActionButton text={t('CANCEL')} transparent onPress={onCancel} />
Expand Down
33 changes: 21 additions & 12 deletions src/app/screens/swap/swapConfirmation/useConfirmSwap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { SwapToken } from '@screens/swap/useSwap';
import { ReactNode } from 'react';
import { Currency } from 'alex-sdk';
import useWalletSelector from '@hooks/useWalletSelector';
import { broadcastSignedTransaction, signTransaction } from '@secretkeylabs/xverse-core';
import {
makeUnsignedContractCall,
AnchorMode,
PostConditionMode,
StacksTransaction,
} from '@stacks/transactions';
import type { TxToBroadCast } from 'alex-sdk/dist/helpers/SwapHelper';
broadcastSignedTransaction,
signTransaction,
sponsorTransaction,
} from '@secretkeylabs/xverse-core';
import { StacksTransaction } from '@stacks/transactions';
import useNetworkSelector from '@hooks/useNetwork';
import { useNavigate } from 'react-router-dom';
import useSponsoredTransaction from '@hooks/useSponsoredTransaction';
import { ApiResponseError } from '@secretkeylabs/xverse-core/types';

export type SwapConfirmationInput = {
from: Currency;
Expand All @@ -28,11 +28,14 @@ export type SwapConfirmationInput = {
functionName: string;
};

const XVERSE_SPONSOR_2_URL = 'https://sponsor2.xverse.app';

export function useConfirmSwap(
input: SwapConfirmationInput
input: SwapConfirmationInput,
): SwapConfirmationInput & { onConfirm: () => Promise<void> } {
const { selectedAccount, seedPhrase, stxPublicKey } = useWalletSelector();
const { selectedAccount, seedPhrase } = useWalletSelector();
const selectedNetwork = useNetworkSelector();
const { isSponsored } = useSponsoredTransaction(XVERSE_SPONSOR_2_URL);
const navigate = useNavigate();
return {
...input,
Expand All @@ -41,10 +44,15 @@ export function useConfirmSwap(
input.unsignedTx,
seedPhrase,
selectedAccount?.id ?? 0,
selectedNetwork
selectedNetwork,
);
try {
const broadcastResult: string = await broadcastSignedTransaction(signed, selectedNetwork);
let broadcastResult: string | null;
if (isSponsored) {
broadcastResult = await sponsorTransaction(signed, XVERSE_SPONSOR_2_URL);
} else {
broadcastResult = await broadcastSignedTransaction(signed, selectedNetwork);
}
if (broadcastResult) {
navigate('/tx-status', {
state: {
Expand All @@ -61,7 +69,8 @@ export function useConfirmSwap(
state: {
txid: '',
currency: 'STX',
error: e.message,
error: e instanceof ApiResponseError ? e.data.message : e.message,
sponsored: isSponsored,
browserTx: true,
},
});
Expand Down
6 changes: 6 additions & 0 deletions src/assets/img/transactions/CircleWavyCheck.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@
"COPIED": "Copied",
"COPY_YOUR_ADDRESS": "copy your address",
"ADVANCED_SETTING": "Advanced settings",
"ROUTE_DESC": "For the transaction to proceed, your BTC will be swapped for xBTC and STX. There is no additional cost for you and no STX will be added to your wallet."
"ROUTE_DESC": "For the transaction to proceed, your BTC will be swapped for xBTC and STX. There is no additional cost for you and no STX will be added to your wallet.",
"THIS_IS_A_SPONSORED_TRANSACTION": "This is a sponsored transaction, no transaction fees will be deducted from your account."
}
}