feat: Unwrap sollet USDC and USDT

This commit is contained in:
secretshardul 2021-09-28 14:49:34 +05:30
parent 80c61a46c8
commit 8ee9412636
3 changed files with 115 additions and 11 deletions

View File

@ -6,6 +6,7 @@ import {
SystemProgram,
Signer,
SYSVAR_RENT_PUBKEY,
TransactionInstruction,
} from "@solana/web3.js";
import {
u64,
@ -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,
@ -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) => ({
@ -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
@ -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.
@ -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");
@ -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 ? (
@ -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"

View File

@ -14,6 +14,8 @@ import {
USDT_MINT,
SOL_MINT,
WRAPPED_SOL_MINT,
SOLLET_USDT_MINT,
SOLLET_USDC_MINT,
} from "../utils/pubkeys";
import {
useFairRoute,
@ -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
@ -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();

View File

@ -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"
);
@ -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"
);