From 3a58375616ad27c2f011410d6b8aa0c4db3ce664 Mon Sep 17 00:00:00 2001 From: armaniferrante Date: Tue, 18 May 2021 01:26:03 -0700 Subject: [PATCH] Add USDC and USDT referrals --- package.json | 4 +-- src/App.tsx | 1 - src/swap/components/Swap.tsx | 4 ++- src/swap/context/Swap.tsx | 48 +++++++++++++++++++++++++++++++++--- src/swap/index.tsx | 3 +++ yarn.lock | 8 +++--- 6 files changed, 57 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index e2513f5..7955b21 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/App.tsx b/src/App.tsx index 668ec6c..7247434 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -146,7 +146,6 @@ class NotifyingProvider extends Provider { opts?: ConfirmOptions ): Promise { try { - // A production implementation should handle error notifications as well. const txSig = await super.send(tx, signers, opts); this.onTransaction(txSig); return txSig; diff --git a/src/swap/components/Swap.tsx b/src/swap/components/Swap.tsx index 78e2bdd..b1fca77 100644 --- a/src/swap/components/Swap.tsx +++ b/src/swap/components/Swap.tsx @@ -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, diff --git a/src/swap/context/Swap.tsx b/src/swap/context/Swap.tsx index 1e342e5..dc429a7 100644 --- a/src/swap/context/Swap.tsx +++ b/src/swap/context/Swap.tsx @@ -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(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; +} diff --git a/src/swap/index.tsx b/src/swap/index.tsx index 3967cae..44a1414 100644 --- a/src/swap/index.tsx +++ b/src/swap/index.tsx @@ -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} > diff --git a/yarn.lock b/yarn.lock index 1917c41..b3c0240 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"