From 81d045e4e66e720bbc331d428b93bc0fee14e026 Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Mon, 12 Jul 2021 21:11:25 -0700 Subject: [PATCH] Send to ata when available (#335) --- package.json | 2 +- src/utils/tokens/index.js | 34 +++++++++++++++------------------- yarn.lock | 14 ++++---------- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 2b156b3..9b81c44 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "@material-ui/lab": "^4.0.0-alpha.58", "@project-serum/anchor": "^0.7.0", "@project-serum/serum": "^0.13.33", - "@project-serum/swap-ui": "^0.1.11", + "@project-serum/swap-ui": "^0.2.1", "@project-serum/token": "^0.0.1-alpha.3", "@solana/spl-token-registry": "^0.2.102", "@solana/web3.js": "^1.17.0", diff --git a/src/utils/tokens/index.js b/src/utils/tokens/index.js index e2b05ea..11acd61 100644 --- a/src/utils/tokens/index.js +++ b/src/utils/tokens/index.js @@ -16,12 +16,7 @@ import { TOKEN_PROGRAM_ID, transferChecked, } from './instructions'; -import { - ACCOUNT_LAYOUT, - getOwnedAccountsFilters, - MINT_LAYOUT, - parseTokenAccountData, -} from './data'; +import { ACCOUNT_LAYOUT, getOwnedAccountsFilters, MINT_LAYOUT } from './data'; import bs58 from 'bs58'; export async function getOwnedTokenAccounts(connection, publicKey) { @@ -303,7 +298,7 @@ export async function transferTokens({ decimals, overrideDestinationCheck, }) { - const destinationAccountInfo = await connection.getAccountInfo( + let destinationAccountInfo = await connection.getAccountInfo( destinationPublicKey, ); if ( @@ -328,24 +323,25 @@ export async function transferTokens({ ) { throw new Error('Cannot send to address with zero SOL balances'); } - const destinationSplTokenAccount = ( - await getOwnedTokenAccounts(connection, destinationPublicKey) - ) - .map(({ publicKey, accountInfo }) => { - return { publicKey, parsed: parseTokenAccountData(accountInfo.data) }; - }) - .filter(({ parsed }) => parsed.mint.equals(mint)) - .sort((a, b) => { - return b.parsed.amount - a.parsed.amount; - })[0]; - if (destinationSplTokenAccount) { + + const destinationAssociatedTokenAddress = await findAssociatedTokenAddress( + destinationPublicKey, + mint, + ); + destinationAccountInfo = await connection.getAccountInfo( + destinationAssociatedTokenAddress, + ); + if ( + !!destinationAccountInfo && + destinationAccountInfo.owner.equals(TOKEN_PROGRAM_ID) + ) { return await transferBetweenSplTokenAccounts({ connection, owner, mint, decimals, sourcePublicKey, - destinationPublicKey: destinationSplTokenAccount.publicKey, + destinationPublicKey: destinationAssociatedTokenAddress, amount, memo, }); diff --git a/yarn.lock b/yarn.lock index 20e8a02..59e6445 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1519,11 +1519,6 @@ "@ethersproject/rlp" "^5.0.3" "@ethersproject/signing-key" "^5.0.4" -"@fontsource/roboto@^4.3.0": - version "4.3.0" - resolved "https://registry.yarnpkg.com/@fontsource/roboto/-/roboto-4.3.0.tgz#00f1cceca43eff85bb0e1d424311751ee41f6aa6" - integrity sha512-WeFWCWYutLWyEtRmBhn+bLbW4/km0l+HhTpR8wWDxJLiGiMOhVLO/Z0q5w6l20ZOkWnf6Z1rN3o3W2HjvYN6Rg== - "@hapi/address@2.x.x": version "2.1.4" resolved "https://registry.yarnpkg.com/@hapi/address/-/address-2.1.4.tgz#5d67ed43f3fd41a69d4b9ff7b56e7c0d1d0a81e5" @@ -1965,12 +1960,11 @@ bn.js "^5.1.2" buffer-layout "^1.2.0" -"@project-serum/swap-ui@^0.1.11": - version "0.1.11" - resolved "https://registry.yarnpkg.com/@project-serum/swap-ui/-/swap-ui-0.1.11.tgz#026f28dc87c36aee6bb1b9be924342b66d7f11de" - integrity sha512-irhexVN319gzHBVO6ordDafEOdnOuSJsy+Fi+5CSOV4XbqkD+iUm/bJdSw1iszwjXYQcyMR8qvONbsnEDS0lRA== +"@project-serum/swap-ui@^0.2.1": + version "0.2.1" + resolved "https://registry.yarnpkg.com/@project-serum/swap-ui/-/swap-ui-0.2.1.tgz#eb6f5f4b58db747e0f9e5362d2b95b43fe0e7c16" + integrity sha512-xINb2LIFKTDdZsYfbRTUfbp6CyUe7CIzf2w08EPfVwsd669fentdQ53DIfDXjS552f4EoaEeJh9oZgcK8CMXwA== dependencies: - "@fontsource/roboto" "^4.3.0" "@project-serum/serum" "^0.13.34" "@project-serum/swap" "^0.1.0-alpha.31" "@solana/spl-token" "^0.1.4"