Add USDC and USDT referrals

This commit is contained in:
armaniferrante 2021-05-18 01:26:03 -07:00
parent 2b361b2459
commit 3a58375616
No known key found for this signature in database
GPG Key ID: 58BEF301E91F7828
6 changed files with 57 additions and 11 deletions

View File

@ -1,13 +1,13 @@
{
"name": "@project-serum/swap-ui",
"version": "0.1.0-alpha.8",
"version": "0.1.0-alpha.9",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"homepage": ".",
"dependencies": {
"react-async-hook": "^3.6.2",
"@project-serum/serum": "^0.13.34",
"@project-serum/swap": "^0.1.0-alpha.8",
"@project-serum/swap": "^0.1.0-alpha.9",
"@solana/spl-token": "^0.1.4"
},
"peerDependencies": {

View File

@ -146,7 +146,6 @@ class NotifyingProvider extends Provider {
opts?: ConfirmOptions
): Promise<TransactionSignature> {
try {
// A production implementation should handle error notifications as well.
const txSig = await super.send(tx, signers, opts);
this.onTransaction(txSig);
return txSig;

View File

@ -19,7 +19,7 @@ import {
} from "../context/Dex";
import { useTokenMap } from "../context/TokenList";
import { useMint, useOwnedTokenAccount } from "../context/Token";
import { useCanSwap } from "../context/Swap";
import { useCanSwap, useReferral } from "../context/Swap";
import TokenDialog from "./TokenDialog";
import { SettingsButton } from "./Settings";
import { InfoLabel } from "./Info";
@ -249,6 +249,7 @@ function SwapButton() {
route && route.markets ? route.markets[1] : undefined
);
const canSwap = useCanSwap();
const referral = useReferral(fromMarket);
// Click handler.
const sendSwapTransaction = async () => {
@ -273,6 +274,7 @@ function SwapButton() {
toMint,
amount,
minExpectedSwapAmount,
referral,
// Pass in the below parameters so that the client doesn't perform
// wasteful network requests when we already have the data.
fromMarket,

View File

@ -1,7 +1,14 @@
import React, { useContext, useState } from "react";
import * as assert from "assert";
import React, { useContext, useState } from "react";
import { useAsync } from "react-async-hook";
import { PublicKey } from "@solana/web3.js";
import { SRM_MINT, USDC_MINT } from "../utils/pubkeys";
import {
Token,
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
} from "@solana/spl-token";
import { Market } from "@project-serum/serum";
import { SRM_MINT, USDC_MINT, USDT_MINT } from "../utils/pubkeys";
import { useFairRoute, useRouteVerbose, useDexContext } from "./Dex";
import {
useTokenListContext,
@ -42,6 +49,10 @@ export type SwapContext = {
fairOverride: number | null;
setFairOverride: (n: number | null) => void;
// The referral *owner* address. Associated token accounts must be created,
// first, for this to be used.
referral?: PublicKey;
// True if all newly created market accounts should be closed in the
// same user flow (ideally in the same transaction).
isClosingNewAccounts: boolean;
@ -55,10 +66,10 @@ export function SwapContextProvider(props: any) {
const [fromAmount, _setFromAmount] = useState(props.fromAmount ?? 0);
const [toAmount, _setToAmount] = useState(props.toAmount ?? 0);
const [isClosingNewAccounts, setIsClosingNewAccounts] = useState(false);
// Percent units.
const [slippage, setSlippage] = useState(DEFAULT_SLIPPAGE_PERCENT);
const [fairOverride, setFairOverride] = useState<number | null>(null);
const fair = _useSwapFair(fromMint, toMint, fairOverride);
const referral = props.referral;
assert.ok(slippage >= 0);
@ -107,6 +118,7 @@ export function SwapContextProvider(props: any) {
setFairOverride,
isClosingNewAccounts,
setIsClosingNewAccounts,
referral,
}}
>
{props.children}
@ -171,3 +183,33 @@ export function useCanSwap(): boolean {
?.tags?.includes(SPL_REGISTRY_SOLLET_TAG) !== undefined)
);
}
export function useReferral(fromMarket?: Market): PublicKey | undefined {
const { referral } = useSwapContext();
const asyncReferral = useAsync(async () => {
if (!referral) {
return undefined;
}
if (!fromMarket) {
return undefined;
}
if (
!fromMarket.quoteMintAddress.equals(USDC_MINT) &&
!fromMarket.quoteMintAddress.equals(USDT_MINT)
) {
return undefined;
}
return Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
fromMarket.quoteMintAddress,
referral
);
}, [fromMarket]);
if (!asyncReferral.result) {
return undefined;
}
return asyncReferral.result;
}

View File

@ -18,6 +18,7 @@ export default function Swap({
toMint,
fromAmount,
toAmount,
referral,
}: {
provider: Provider;
tokenList: TokenListContainer;
@ -25,6 +26,7 @@ export default function Swap({
toMint?: PublicKey;
fromAmount?: number;
toAmount?: number;
referral?: PublicKey;
style?: any;
}) {
const swapClient = new SwapClient(provider, tokenList);
@ -37,6 +39,7 @@ export default function Swap({
toMint={toMint}
fromAmount={fromAmount}
toAmount={toAmount}
referral={referral}
>
<SwapCard style={style} />
</SwapContextProvider>

View File

@ -1588,10 +1588,10 @@
bs58 "^4.0.1"
eventemitter3 "^4.0.4"
"@project-serum/swap@^0.1.0-alpha.8":
version "0.1.0-alpha.8"
resolved "https://registry.yarnpkg.com/@project-serum/swap/-/swap-0.1.0-alpha.8.tgz#1df677bc4dc1c0293bb759179f126e1b8ca87ea4"
integrity sha512-NWHFewsGgNpA+AN+FsLyZ74Ir5eoYvzvPPKytTEwDR5Q5Q8lxWlBDfpYB3tyio/36VmYdyImb+FAvU8pREmofg==
"@project-serum/swap@^0.1.0-alpha.9":
version "0.1.0-alpha.9"
resolved "https://registry.yarnpkg.com/@project-serum/swap/-/swap-0.1.0-alpha.9.tgz#bd3b7a48426a9dbeaea457375b9ffe9b6d0272ee"
integrity sha512-jrKpot+WPNBPTNcDoPD09XLFkr68wDVtob3vVx1eEcgN2xHtfjg0EKh42BM2BYasPsi7VSmS4yGwXui8j84jBw==
dependencies:
"@project-serum/anchor" "^0.5.1-beta.2"
"@project-serum/serum" "^0.13.34"