Skip to content

Commit

Permalink
feat: Unwrap sollet USDC and USDT
Browse files Browse the repository at this point in the history
  • Loading branch information
secretshardul committed Sep 28, 2021
1 parent 80c61a4 commit 8ee9412
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 11 deletions.
101 changes: 91 additions & 10 deletions src/components/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SystemProgram,
Signer,
SYSVAR_RENT_PUBKEY,
TransactionInstruction,
} from "@solana/web3.js";
import {
u64,
Expand All @@ -24,7 +25,14 @@ import {
useTheme,
} from "@material-ui/core";
import { ExpandMore, ImportExportRounded } from "@material-ui/icons";
import { useCanCreateAccounts, useCanWrapOrUnwrap, useSwapContext, useSwapFair } from "../context/Swap";
import {
useIsUnwrapSollet,
useCanCreateAccounts,
useCanWrapOrUnwrap,
useSwapContext,
useSwapFair,
} from "../context/Swap";
// import { useIsUnwrapSolletUsdt, useSwapContext, useSwapFair } from "../context/Swap";
import {
useDexContext,
useRouteVerbose,
Expand All @@ -44,7 +52,13 @@ import { useCanSwap, useReferral, useIsWrapSol } from "../context/Swap";
import TokenDialog from "./TokenDialog";
import { SettingsButton } from "./Settings";
import { InfoLabel } from "./Info";
import { SOL_MINT, WRAPPED_SOL_MINT, DEX_PID } from "../utils/pubkeys";
import {
SOL_MINT,
WRAPPED_SOL_MINT,
DEX_PID,
MEMO_PROGRAM_ID,
SOLLET_USDT_MINT,
} from "../utils/pubkeys";
import { getTokenAddrressAndCreateIx } from "../utils/tokens";

const useStyles = makeStyles((theme) => ({
Expand Down Expand Up @@ -378,6 +392,7 @@ export function SwapButton() {
const fair = useSwapFair();

const { isWrapSol, isUnwrapSol } = useIsWrapSol(fromMint, toMint);
const isUnwrapSollet = useIsUnwrapSollet(fromMint, toMint);

const fromOpenOrders = useMemo(() => {
return fromMarket
Expand All @@ -391,11 +406,14 @@ export function SwapButton() {

const disconnected = !swapClient.program.provider.wallet.publicKey;

const insufficientBalance = fromAmount * Math.pow(10, fromMintInfo?.decimals ?? 0)
> (fromWallet?.account.amount.toNumber() ?? 0);
const insufficientBalance =
fromAmount == 0 ||
fromAmount * Math.pow(10, fromMintInfo?.decimals ?? 0) >
(fromWallet?.account.amount.toNumber() ?? 0);

const needsCreateAccounts =
!toWallet || !fromOpenOrders || (toMarket && !toOpenOrders);
!toWallet ||
(!isUnwrapSollet && (!fromOpenOrders || (toMarket && !toOpenOrders)));

// Click handlers.

Expand Down Expand Up @@ -639,6 +657,64 @@ export function SwapButton() {
await swapClient.program.provider.send(tx, signers);
};

const sendUnwrapSolletTransaction = async () => {
interface SolletBody {
address: string;
blockchain: string;
coin: string;
size: number;
wusdtToUsdt?: boolean;
wusdcToUsdc?: boolean;
}
const solletReqBody: SolletBody = {
address: toWallet!.publicKey.toString(),
blockchain: "sol",
coin: toMint.toString(),
size: 1,
};
if (fromMint.equals(SOLLET_USDT_MINT)) {
solletReqBody.wusdtToUsdt = true;
} else {
solletReqBody.wusdcToUsdc = true;
}
const solletRes = await fetch("https://swap.sollet.io/api/swap_to", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(solletReqBody),
});

const { address: bridgeAddr, maxSize } = (await solletRes.json())
.result as {
address: string;
maxSize: number;
};

const tx = new Transaction();
const amount = new u64(fromAmount * 10 ** fromMintInfo!.decimals);
tx.add(
Token.createTransferInstruction(
TOKEN_PROGRAM_ID,
fromWallet!.publicKey,
new PublicKey(bridgeAddr),
swapClient.program.provider.wallet.publicKey,
[],
amount
)
);
tx.add(
new TransactionInstruction({
keys: [],
data: Buffer.from(toWallet!.publicKey.toString(), "utf-8"),
programId: MEMO_PROGRAM_ID,
})
);

await swapClient.program.provider.send(tx);
};

const sendSwapTransaction = async () => {
if (!fromMintInfo || !toMintInfo) {
throw new Error("Unable to calculate mint decimals");
Expand Down Expand Up @@ -758,11 +834,7 @@ export function SwapButton() {
}

return !fromWallet || insufficientBalance ? (
<Button
variant="contained"
className={styles.swapButton}
disabled={true}
>
<Button variant="contained" className={styles.swapButton} disabled={true}>
Insufficient balance
</Button>
) : needsCreateAccounts ? (
Expand Down Expand Up @@ -792,6 +864,15 @@ export function SwapButton() {
>
Unwrap SOL
</Button>
) : isUnwrapSollet ? (
<Button
variant="contained"
className={styles.swapButton}
onClick={sendUnwrapSolletTransaction}
disabled={fromAmount <= 0}
>
Unwrap
</Button>
) : (
<Button
variant="contained"
Expand Down
13 changes: 12 additions & 1 deletion src/context/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
USDT_MINT,
SOL_MINT,
WRAPPED_SOL_MINT,
SOLLET_USDT_MINT,
SOLLET_USDC_MINT,
} from "../utils/pubkeys";
import {
useFairRoute,
Expand Down Expand Up @@ -233,7 +235,6 @@ export function useCanCreateAccounts(): boolean {
fromMint.equals(toMint) === false &&
// Wallet is connected.
swapClient.program.provider.wallet.publicKey !== null &&

// Trade route exists.
route !== null &&
// Wormhole <-> native markets must have the wormhole token as the
Expand Down Expand Up @@ -268,6 +269,16 @@ export function useCanWrapOrUnwrap(): boolean {
);
}

export function useIsUnwrapSollet(
fromMint: PublicKey,
toMint: PublicKey
): boolean {
return (
(fromMint.equals(SOLLET_USDT_MINT) && toMint.equals(USDT_MINT)) ||
(fromMint.equals(SOLLET_USDC_MINT) && toMint.equals(USDC_MINT))
);
}

// Returns true if the user can swap with the current context.
export function useCanSwap(): boolean {
const { fromMint, toMint, fromAmount, toAmount } = useSwapContext();
Expand Down
12 changes: 12 additions & 0 deletions src/utils/pubkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export const DEX_PID = new PublicKey(
"9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin"
);

export const MEMO_PROGRAM_ID = new PublicKey(
"MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"
);

export const SRM_MINT = new PublicKey(
"SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"
);
Expand Down Expand Up @@ -44,3 +48,11 @@ export const WORM_USDT_MINT = new PublicKey(
export const WORM_USDT_MARKET = new PublicKey(
"4v6e6vNXAaEunrvbqkYnKwbaWfck8a2KVR4uRAVXxVwC"
);

export const SOLLET_USDT_MINT = new PublicKey(
"BQcdHdAQW1hczDbBi9hiegXAR7A98Q9jx3X3iBBBDiq4"
);

export const SOLLET_USDC_MINT = new PublicKey(
"BXXkv6z8ykpG1yuvUDPgh732wzVHB69RnB9YgSYh3itW"
);

0 comments on commit 8ee9412

Please sign in to comment.