Send to ata when available (#335)
This commit is contained in:
parent
5d72776e98
commit
81d045e4e6
|
@ -10,7 +10,7 @@
|
||||||
"@material-ui/lab": "^4.0.0-alpha.58",
|
"@material-ui/lab": "^4.0.0-alpha.58",
|
||||||
"@project-serum/anchor": "^0.7.0",
|
"@project-serum/anchor": "^0.7.0",
|
||||||
"@project-serum/serum": "^0.13.33",
|
"@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",
|
"@project-serum/token": "^0.0.1-alpha.3",
|
||||||
"@solana/spl-token-registry": "^0.2.102",
|
"@solana/spl-token-registry": "^0.2.102",
|
||||||
"@solana/web3.js": "^1.17.0",
|
"@solana/web3.js": "^1.17.0",
|
||||||
|
|
|
@ -16,12 +16,7 @@ import {
|
||||||
TOKEN_PROGRAM_ID,
|
TOKEN_PROGRAM_ID,
|
||||||
transferChecked,
|
transferChecked,
|
||||||
} from './instructions';
|
} from './instructions';
|
||||||
import {
|
import { ACCOUNT_LAYOUT, getOwnedAccountsFilters, MINT_LAYOUT } from './data';
|
||||||
ACCOUNT_LAYOUT,
|
|
||||||
getOwnedAccountsFilters,
|
|
||||||
MINT_LAYOUT,
|
|
||||||
parseTokenAccountData,
|
|
||||||
} from './data';
|
|
||||||
import bs58 from 'bs58';
|
import bs58 from 'bs58';
|
||||||
|
|
||||||
export async function getOwnedTokenAccounts(connection, publicKey) {
|
export async function getOwnedTokenAccounts(connection, publicKey) {
|
||||||
|
@ -303,7 +298,7 @@ export async function transferTokens({
|
||||||
decimals,
|
decimals,
|
||||||
overrideDestinationCheck,
|
overrideDestinationCheck,
|
||||||
}) {
|
}) {
|
||||||
const destinationAccountInfo = await connection.getAccountInfo(
|
let destinationAccountInfo = await connection.getAccountInfo(
|
||||||
destinationPublicKey,
|
destinationPublicKey,
|
||||||
);
|
);
|
||||||
if (
|
if (
|
||||||
|
@ -328,24 +323,25 @@ export async function transferTokens({
|
||||||
) {
|
) {
|
||||||
throw new Error('Cannot send to address with zero SOL balances');
|
throw new Error('Cannot send to address with zero SOL balances');
|
||||||
}
|
}
|
||||||
const destinationSplTokenAccount = (
|
|
||||||
await getOwnedTokenAccounts(connection, destinationPublicKey)
|
const destinationAssociatedTokenAddress = await findAssociatedTokenAddress(
|
||||||
)
|
destinationPublicKey,
|
||||||
.map(({ publicKey, accountInfo }) => {
|
mint,
|
||||||
return { publicKey, parsed: parseTokenAccountData(accountInfo.data) };
|
);
|
||||||
})
|
destinationAccountInfo = await connection.getAccountInfo(
|
||||||
.filter(({ parsed }) => parsed.mint.equals(mint))
|
destinationAssociatedTokenAddress,
|
||||||
.sort((a, b) => {
|
);
|
||||||
return b.parsed.amount - a.parsed.amount;
|
if (
|
||||||
})[0];
|
!!destinationAccountInfo &&
|
||||||
if (destinationSplTokenAccount) {
|
destinationAccountInfo.owner.equals(TOKEN_PROGRAM_ID)
|
||||||
|
) {
|
||||||
return await transferBetweenSplTokenAccounts({
|
return await transferBetweenSplTokenAccounts({
|
||||||
connection,
|
connection,
|
||||||
owner,
|
owner,
|
||||||
mint,
|
mint,
|
||||||
decimals,
|
decimals,
|
||||||
sourcePublicKey,
|
sourcePublicKey,
|
||||||
destinationPublicKey: destinationSplTokenAccount.publicKey,
|
destinationPublicKey: destinationAssociatedTokenAddress,
|
||||||
amount,
|
amount,
|
||||||
memo,
|
memo,
|
||||||
});
|
});
|
||||||
|
|
14
yarn.lock
14
yarn.lock
|
@ -1519,11 +1519,6 @@
|
||||||
"@ethersproject/rlp" "^5.0.3"
|
"@ethersproject/rlp" "^5.0.3"
|
||||||
"@ethersproject/signing-key" "^5.0.4"
|
"@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":
|
"@hapi/address@2.x.x":
|
||||||
version "2.1.4"
|
version "2.1.4"
|
||||||
resolved "https://registry.yarnpkg.com/@hapi/address/-/address-2.1.4.tgz#5d67ed43f3fd41a69d4b9ff7b56e7c0d1d0a81e5"
|
resolved "https://registry.yarnpkg.com/@hapi/address/-/address-2.1.4.tgz#5d67ed43f3fd41a69d4b9ff7b56e7c0d1d0a81e5"
|
||||||
|
@ -1965,12 +1960,11 @@
|
||||||
bn.js "^5.1.2"
|
bn.js "^5.1.2"
|
||||||
buffer-layout "^1.2.0"
|
buffer-layout "^1.2.0"
|
||||||
|
|
||||||
"@project-serum/swap-ui@^0.1.11":
|
"@project-serum/swap-ui@^0.2.1":
|
||||||
version "0.1.11"
|
version "0.2.1"
|
||||||
resolved "https://registry.yarnpkg.com/@project-serum/swap-ui/-/swap-ui-0.1.11.tgz#026f28dc87c36aee6bb1b9be924342b66d7f11de"
|
resolved "https://registry.yarnpkg.com/@project-serum/swap-ui/-/swap-ui-0.2.1.tgz#eb6f5f4b58db747e0f9e5362d2b95b43fe0e7c16"
|
||||||
integrity sha512-irhexVN319gzHBVO6ordDafEOdnOuSJsy+Fi+5CSOV4XbqkD+iUm/bJdSw1iszwjXYQcyMR8qvONbsnEDS0lRA==
|
integrity sha512-xINb2LIFKTDdZsYfbRTUfbp6CyUe7CIzf2w08EPfVwsd669fentdQ53DIfDXjS552f4EoaEeJh9oZgcK8CMXwA==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@fontsource/roboto" "^4.3.0"
|
|
||||||
"@project-serum/serum" "^0.13.34"
|
"@project-serum/serum" "^0.13.34"
|
||||||
"@project-serum/swap" "^0.1.0-alpha.31"
|
"@project-serum/swap" "^0.1.0-alpha.31"
|
||||||
"@solana/spl-token" "^0.1.4"
|
"@solana/spl-token" "^0.1.4"
|
||||||
|
|
Loading…
Reference in New Issue