@drift-labs/vaults-sdk 0.1.531 → 0.1.533

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts CHANGED
@@ -1,10 +1,14 @@
1
1
  import { AnchorProvider } from '@coral-xyz/anchor';
2
2
  import { DriftClient, IWallet } from '@drift-labs/sdk';
3
- import { Connection } from '@solana/web3.js';
3
+ import { Connection, PublicKey, TransactionInstruction } from '@solana/web3.js';
4
4
  import { DriftVaults, IDL } from './types/drift_vaults';
5
5
  import { VaultClient } from './vaultClient';
6
6
  import * as anchor from '@coral-xyz/anchor';
7
7
  import { VAULT_PROGRAM_ID } from './types/types';
8
+ import {
9
+ createAssociatedTokenAccountInstruction,
10
+ getAssociatedTokenAddress,
11
+ } from '@solana/spl-token';
8
12
 
9
13
  export const getDriftVaultProgram = (
10
14
  connection: Connection,
@@ -31,3 +35,35 @@ export const getVaultClient = (
31
35
 
32
36
  return vaultClient;
33
37
  };
38
+
39
+ export const getOrCreateATAInstruction = async (
40
+ tokenMint: PublicKey,
41
+ owner: PublicKey,
42
+ connection: Connection,
43
+ allowOwnerOffCurve = true,
44
+ payer = owner
45
+ ): Promise<[PublicKey, TransactionInstruction?]> => {
46
+ let toAccount;
47
+ try {
48
+ toAccount = await getAssociatedTokenAddress(
49
+ tokenMint,
50
+ owner,
51
+ allowOwnerOffCurve
52
+ );
53
+ const account = await connection.getAccountInfo(toAccount);
54
+ if (!account) {
55
+ const ix = createAssociatedTokenAccountInstruction(
56
+ payer,
57
+ toAccount,
58
+ owner,
59
+ tokenMint
60
+ );
61
+ return [toAccount, ix];
62
+ }
63
+ return [toAccount, undefined];
64
+ } catch (e) {
65
+ /* handle error */
66
+ console.error('Error::getOrCreateATAInstruction', e);
67
+ throw e;
68
+ }
69
+ };