@drift-labs/sdk 0.2.0-master.2 → 0.2.0-master.22

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.
Files changed (149) hide show
  1. package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
  2. package/lib/accounts/bulkUserStatsSubscription.js +21 -0
  3. package/lib/accounts/bulkUserSubscription.js +0 -1
  4. package/lib/accounts/fetch.d.ts +2 -1
  5. package/lib/accounts/fetch.js +9 -1
  6. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  7. package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
  8. package/lib/accounts/types.d.ts +14 -1
  9. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  10. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  11. package/lib/addresses/pda.d.ts +1 -0
  12. package/lib/addresses/pda.js +8 -1
  13. package/lib/admin.d.ts +9 -5
  14. package/lib/admin.js +52 -11
  15. package/lib/clearingHouse.d.ts +52 -23
  16. package/lib/clearingHouse.js +736 -200
  17. package/lib/clearingHouseConfig.d.ts +1 -0
  18. package/lib/clearingHouseUser.d.ts +24 -16
  19. package/lib/clearingHouseUser.js +223 -101
  20. package/lib/clearingHouseUserStats.d.ts +18 -0
  21. package/lib/clearingHouseUserStats.js +49 -0
  22. package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
  23. package/lib/clearingHouseUserStatsConfig.js +2 -0
  24. package/lib/config.js +1 -1
  25. package/lib/constants/banks.d.ts +2 -2
  26. package/lib/constants/banks.js +12 -4
  27. package/lib/constants/numericConstants.d.ts +5 -0
  28. package/lib/constants/numericConstants.js +8 -3
  29. package/lib/events/eventList.js +3 -0
  30. package/lib/events/types.d.ts +3 -1
  31. package/lib/events/types.js +2 -0
  32. package/lib/factory/bigNum.d.ts +1 -0
  33. package/lib/factory/bigNum.js +34 -10
  34. package/lib/idl/clearing_house.json +1603 -377
  35. package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  36. package/lib/index.d.ts +9 -3
  37. package/lib/index.js +13 -3
  38. package/lib/math/amm.d.ts +1 -0
  39. package/lib/math/amm.js +22 -38
  40. package/lib/math/auction.js +4 -1
  41. package/lib/math/bankBalance.d.ts +7 -1
  42. package/lib/math/bankBalance.js +77 -2
  43. package/lib/math/margin.d.ts +11 -0
  44. package/lib/math/margin.js +72 -0
  45. package/lib/math/market.d.ts +4 -1
  46. package/lib/math/market.js +35 -1
  47. package/lib/math/oracles.d.ts +3 -0
  48. package/lib/math/oracles.js +25 -5
  49. package/lib/math/orders.d.ts +6 -2
  50. package/lib/math/orders.js +62 -12
  51. package/lib/math/position.d.ts +8 -0
  52. package/lib/math/position.js +45 -12
  53. package/lib/math/trade.d.ts +1 -1
  54. package/lib/math/trade.js +7 -10
  55. package/lib/orderParams.d.ts +14 -5
  56. package/lib/orderParams.js +8 -96
  57. package/lib/slot/SlotSubscriber.d.ts +7 -0
  58. package/lib/slot/SlotSubscriber.js +3 -0
  59. package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
  60. package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
  61. package/lib/tx/retryTxSender.js +9 -2
  62. package/lib/tx/utils.js +1 -1
  63. package/lib/types.d.ts +236 -26
  64. package/lib/types.js +64 -1
  65. package/lib/util/computeUnits.js +1 -1
  66. package/lib/util/getTokenAddress.d.ts +2 -0
  67. package/lib/util/getTokenAddress.js +9 -0
  68. package/package.json +3 -3
  69. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  70. package/src/accounts/bulkUserSubscription.ts +0 -1
  71. package/src/accounts/fetch.ts +27 -2
  72. package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
  73. package/src/accounts/types.ts +18 -0
  74. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  75. package/src/addresses/marketAddresses.js +26 -0
  76. package/src/addresses/pda.ts +13 -0
  77. package/src/admin.ts +82 -15
  78. package/src/assert/assert.js +9 -0
  79. package/src/clearingHouse.ts +1232 -323
  80. package/src/clearingHouseConfig.ts +1 -0
  81. package/src/clearingHouseUser.ts +343 -155
  82. package/src/clearingHouseUserStats.ts +75 -0
  83. package/src/clearingHouseUserStatsConfig.ts +18 -0
  84. package/src/config.ts +1 -1
  85. package/src/constants/banks.js +42 -0
  86. package/src/constants/banks.ts +14 -4
  87. package/src/constants/markets.js +42 -0
  88. package/src/constants/numericConstants.js +41 -0
  89. package/src/constants/numericConstants.ts +14 -2
  90. package/src/events/eventList.js +77 -0
  91. package/src/events/eventList.ts +3 -0
  92. package/src/events/eventSubscriber.js +139 -0
  93. package/src/events/sort.js +44 -0
  94. package/src/events/txEventCache.js +71 -0
  95. package/src/events/types.ts +6 -0
  96. package/src/examples/makeTradeExample.js +80 -0
  97. package/src/factory/bigNum.js +390 -0
  98. package/src/factory/bigNum.ts +42 -13
  99. package/src/factory/oracleClient.js +20 -0
  100. package/src/idl/clearing_house.json +1603 -377
  101. package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  102. package/src/index.ts +9 -3
  103. package/src/math/amm.ts +54 -55
  104. package/src/math/auction.js +42 -0
  105. package/src/math/auction.ts +5 -1
  106. package/src/math/bankBalance.ts +148 -2
  107. package/src/math/conversion.js +11 -0
  108. package/src/math/funding.js +248 -0
  109. package/src/math/margin.ts +124 -0
  110. package/src/math/market.ts +66 -1
  111. package/src/math/oracles.js +26 -0
  112. package/src/math/oracles.ts +42 -5
  113. package/src/math/orders.ts +129 -13
  114. package/src/math/position.ts +64 -9
  115. package/src/math/repeg.js +128 -0
  116. package/src/math/state.js +15 -0
  117. package/src/math/trade.js +253 -0
  118. package/src/math/trade.ts +23 -25
  119. package/src/math/utils.js +0 -1
  120. package/src/oracles/oracleClientCache.js +19 -0
  121. package/src/oracles/pythClient.js +46 -0
  122. package/src/oracles/quoteAssetOracleClient.js +32 -0
  123. package/src/oracles/switchboardClient.js +69 -0
  124. package/src/oracles/types.js +2 -0
  125. package/src/orderParams.js +20 -0
  126. package/src/orderParams.ts +20 -141
  127. package/src/slot/SlotSubscriber.js +39 -0
  128. package/src/slot/SlotSubscriber.ts +11 -1
  129. package/src/token/index.js +38 -0
  130. package/src/tokenFaucet.js +189 -0
  131. package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
  132. package/src/tx/retryTxSender.ts +11 -3
  133. package/src/tx/types.js +2 -0
  134. package/src/tx/utils.js +17 -0
  135. package/src/tx/utils.ts +1 -1
  136. package/src/types.ts +239 -27
  137. package/src/userName.js +20 -0
  138. package/src/util/computeUnits.js +21 -11
  139. package/src/util/computeUnits.ts +1 -1
  140. package/src/util/getTokenAddress.js +9 -0
  141. package/src/util/getTokenAddress.ts +18 -0
  142. package/src/util/promiseTimeout.js +14 -0
  143. package/src/util/tps.js +27 -0
  144. package/src/wallet.js +35 -0
  145. package/tests/bn/test.ts +10 -0
  146. package/lib/orders.d.ts +0 -8
  147. package/lib/orders.js +0 -142
  148. package/src/orders.ts +0 -251
  149. package/src/util/computeUnits.js.map +0 -1
@@ -0,0 +1,189 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ var __importDefault = (this && this.__importDefault) || function (mod) {
31
+ return (mod && mod.__esModule) ? mod : { "default": mod };
32
+ };
33
+ Object.defineProperty(exports, "__esModule", { value: true });
34
+ exports.TokenFaucet = void 0;
35
+ const anchor = __importStar(require("@project-serum/anchor"));
36
+ const anchor_1 = require("@project-serum/anchor");
37
+ const spl_token_1 = require("@solana/spl-token");
38
+ const web3_js_1 = require("@solana/web3.js");
39
+ const token_faucet_json_1 = __importDefault(require("./idl/token_faucet.json"));
40
+ class TokenFaucet {
41
+ constructor(connection, wallet, programId, mint, opts) {
42
+ this.connection = connection;
43
+ this.wallet = wallet;
44
+ this.opts = opts || anchor_1.AnchorProvider.defaultOptions();
45
+ const provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
46
+ this.provider = provider;
47
+ this.program = new anchor_1.Program(token_faucet_json_1.default, programId, provider);
48
+ this.mint = mint;
49
+ }
50
+ getFaucetConfigPublicKeyAndNonce() {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ return anchor.web3.PublicKey.findProgramAddress([
53
+ Buffer.from(anchor.utils.bytes.utf8.encode('faucet_config')),
54
+ this.mint.toBuffer(),
55
+ ], this.program.programId);
56
+ });
57
+ }
58
+ getMintAuthority() {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ return (yield anchor.web3.PublicKey.findProgramAddress([
61
+ Buffer.from(anchor.utils.bytes.utf8.encode('mint_authority')),
62
+ this.mint.toBuffer(),
63
+ ], this.program.programId))[0];
64
+ });
65
+ }
66
+ getFaucetConfigPublicKey() {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ return (yield this.getFaucetConfigPublicKeyAndNonce())[0];
69
+ });
70
+ }
71
+ initialize() {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ const [faucetConfigPublicKey] = yield this.getFaucetConfigPublicKeyAndNonce();
74
+ return yield this.program.rpc.initialize({
75
+ accounts: {
76
+ faucetConfig: faucetConfigPublicKey,
77
+ admin: this.wallet.publicKey,
78
+ mintAccount: this.mint,
79
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
80
+ systemProgram: anchor.web3.SystemProgram.programId,
81
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
82
+ },
83
+ });
84
+ });
85
+ }
86
+ fetchState() {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ return yield this.program.account.faucetConfig.fetch(yield this.getFaucetConfigPublicKey());
89
+ });
90
+ }
91
+ mintToUserIx(userTokenAccount, amount) {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ return this.program.instruction.mintToUser(amount, {
94
+ accounts: {
95
+ faucetConfig: yield this.getFaucetConfigPublicKey(),
96
+ mintAccount: this.mint,
97
+ userTokenAccount,
98
+ mintAuthority: yield this.getMintAuthority(),
99
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
100
+ },
101
+ });
102
+ });
103
+ }
104
+ mintToUser(userTokenAccount, amount) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ const mintIx = yield this.mintToUserIx(userTokenAccount, amount);
107
+ const tx = new web3_js_1.Transaction().add(mintIx);
108
+ const txSig = yield this.program.provider.sendAndConfirm(tx, [], this.opts);
109
+ return txSig;
110
+ });
111
+ }
112
+ transferMintAuthority() {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ return yield this.program.rpc.transferMintAuthority({
115
+ accounts: {
116
+ faucetConfig: yield this.getFaucetConfigPublicKey(),
117
+ mintAccount: this.mint,
118
+ mintAuthority: yield this.getMintAuthority(),
119
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
120
+ admin: this.wallet.publicKey,
121
+ },
122
+ });
123
+ });
124
+ }
125
+ createAssociatedTokenAccountAndMintTo(userPublicKey, amount) {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ const tx = new web3_js_1.Transaction();
128
+ const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] = yield this.createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount);
129
+ let associatedTokenAccountExists = false;
130
+ try {
131
+ const assosciatedTokenAccount = yield this.connection.getAccountInfo(associatedTokenPublicKey);
132
+ associatedTokenAccountExists = !!assosciatedTokenAccount;
133
+ }
134
+ catch (e) {
135
+ // token account doesn't exist
136
+ associatedTokenAccountExists = false;
137
+ }
138
+ const skipAccountCreation = associatedTokenAccountExists;
139
+ if (!skipAccountCreation)
140
+ tx.add(createAssociatedAccountIx);
141
+ tx.add(mintToTx);
142
+ const txSig = yield this.program.provider.sendAndConfirm(tx, [], this.opts);
143
+ return [associatedTokenPublicKey, txSig];
144
+ });
145
+ }
146
+ createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount) {
147
+ return __awaiter(this, void 0, void 0, function* () {
148
+ const state = yield this.fetchState();
149
+ const associateTokenPublicKey = yield this.getAssosciatedMockUSDMintAddress({ userPubKey: userPublicKey });
150
+ const createAssociatedAccountIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, associateTokenPublicKey, userPublicKey, this.wallet.publicKey);
151
+ const mintToIx = yield this.mintToUserIx(associateTokenPublicKey, amount);
152
+ return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
153
+ });
154
+ }
155
+ getAssosciatedMockUSDMintAddress(props) {
156
+ return __awaiter(this, void 0, void 0, function* () {
157
+ const state = yield this.fetchState();
158
+ return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, props.userPubKey);
159
+ });
160
+ }
161
+ getTokenAccountInfo(props) {
162
+ return __awaiter(this, void 0, void 0, function* () {
163
+ const assosciatedKey = yield this.getAssosciatedMockUSDMintAddress(props);
164
+ const state = yield this.fetchState();
165
+ const token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
166
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
167
+ // @ts-ignore
168
+ this.provider.payer);
169
+ return yield token.getAccountInfo(assosciatedKey);
170
+ });
171
+ }
172
+ subscribeToTokenAccount(props) {
173
+ return __awaiter(this, void 0, void 0, function* () {
174
+ try {
175
+ const tokenAccountKey = yield this.getAssosciatedMockUSDMintAddress(props);
176
+ props.callback(yield this.getTokenAccountInfo(props));
177
+ // Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
178
+ this.connection.onAccountChange(tokenAccountKey, (_accountInfo /* accountInfo is a buffer which we don't know how to deserialize */) => __awaiter(this, void 0, void 0, function* () {
179
+ props.callback(yield this.getTokenAccountInfo(props));
180
+ }));
181
+ return true;
182
+ }
183
+ catch (e) {
184
+ return false;
185
+ }
186
+ });
187
+ }
188
+ }
189
+ exports.TokenFaucet = TokenFaucet;
@@ -3,7 +3,6 @@ import { AnchorProvider, Idl, Program } from '@project-serum/anchor';
3
3
  import {
4
4
  AccountInfo,
5
5
  ASSOCIATED_TOKEN_PROGRAM_ID,
6
- MintLayout,
7
6
  Token,
8
7
  TOKEN_PROGRAM_ID,
9
8
  } from '@solana/spl-token';
@@ -11,27 +10,28 @@ import {
11
10
  ConfirmOptions,
12
11
  Connection,
13
12
  PublicKey,
14
- SystemProgram,
15
13
  SYSVAR_RENT_PUBKEY,
16
14
  Transaction,
17
15
  TransactionInstruction,
18
16
  TransactionSignature,
19
17
  } from '@solana/web3.js';
20
18
  import { BN } from '.';
21
- import mockUSDCFaucetIDL from './idl/mock_usdc_faucet.json';
19
+ import tokenFaucet from './idl/token_faucet.json';
22
20
  import { IWallet } from './types';
23
21
 
24
- export class MockUSDCFaucet {
22
+ export class TokenFaucet {
25
23
  connection: Connection;
26
24
  wallet: IWallet;
27
25
  public program: Program;
28
26
  provider: AnchorProvider;
27
+ mint: PublicKey;
29
28
  opts?: ConfirmOptions;
30
29
 
31
30
  public constructor(
32
31
  connection: Connection,
33
32
  wallet: IWallet,
34
33
  programId: PublicKey,
34
+ mint: PublicKey,
35
35
  opts?: ConfirmOptions
36
36
  ) {
37
37
  this.connection = connection;
@@ -39,93 +39,92 @@ export class MockUSDCFaucet {
39
39
  this.opts = opts || AnchorProvider.defaultOptions();
40
40
  const provider = new AnchorProvider(connection, wallet, this.opts);
41
41
  this.provider = provider;
42
- this.program = new Program(mockUSDCFaucetIDL as Idl, programId, provider);
42
+ this.program = new Program(tokenFaucet as Idl, programId, provider);
43
+ this.mint = mint;
43
44
  }
44
45
 
45
- public async getMockUSDCFaucetStatePublicKeyAndNonce(): Promise<
46
+ public async getFaucetConfigPublicKeyAndNonce(): Promise<
46
47
  [PublicKey, number]
47
48
  > {
48
49
  return anchor.web3.PublicKey.findProgramAddress(
49
- [Buffer.from(anchor.utils.bytes.utf8.encode('mock_usdc_faucet'))],
50
+ [
51
+ Buffer.from(anchor.utils.bytes.utf8.encode('faucet_config')),
52
+ this.mint.toBuffer(),
53
+ ],
50
54
  this.program.programId
51
55
  );
52
56
  }
53
57
 
54
- mockUSDCFaucetStatePublicKey?: PublicKey;
55
- public async getMockUSDCFaucetStatePublicKey(): Promise<PublicKey> {
56
- if (this.mockUSDCFaucetStatePublicKey) {
57
- return this.mockUSDCFaucetStatePublicKey;
58
- }
59
- this.mockUSDCFaucetStatePublicKey = (
60
- await this.getMockUSDCFaucetStatePublicKeyAndNonce()
58
+ public async getMintAuthority(): Promise<PublicKey> {
59
+ return (
60
+ await anchor.web3.PublicKey.findProgramAddress(
61
+ [
62
+ Buffer.from(anchor.utils.bytes.utf8.encode('mint_authority')),
63
+ this.mint.toBuffer(),
64
+ ],
65
+ this.program.programId
66
+ )
61
67
  )[0];
62
- return this.mockUSDCFaucetStatePublicKey;
63
68
  }
64
69
 
65
- public async initialize(): Promise<TransactionSignature> {
66
- const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
67
- await this.getMockUSDCFaucetStatePublicKey()
68
- );
69
- if (stateAccountRPCResponse.value !== null) {
70
- throw new Error('Faucet already initialized');
71
- }
72
-
73
- const fakeUSDCMint = anchor.web3.Keypair.generate();
74
- const createUSDCMintAccountIx = SystemProgram.createAccount({
75
- fromPubkey: this.wallet.publicKey,
76
- newAccountPubkey: fakeUSDCMint.publicKey,
77
- lamports: await Token.getMinBalanceRentForExemptMint(this.connection),
78
- space: MintLayout.span,
79
- programId: TOKEN_PROGRAM_ID,
80
- });
81
-
82
- const [mintAuthority, _mintAuthorityNonce] =
83
- await PublicKey.findProgramAddress(
84
- [fakeUSDCMint.publicKey.toBuffer()],
85
- this.program.programId
86
- );
87
-
88
- const initUSDCMintIx = Token.createInitMintInstruction(
89
- TOKEN_PROGRAM_ID,
90
- fakeUSDCMint.publicKey,
91
- 6,
92
- mintAuthority,
93
- null
94
- );
70
+ public async getFaucetConfigPublicKey(): Promise<PublicKey> {
71
+ return (await this.getFaucetConfigPublicKeyAndNonce())[0];
72
+ }
95
73
 
96
- const [mockUSDCFaucetStatePublicKey, mockUSDCFaucetStateNonce] =
97
- await this.getMockUSDCFaucetStatePublicKeyAndNonce();
98
- return await this.program.rpc.initialize(mockUSDCFaucetStateNonce, {
74
+ public async initialize(): Promise<TransactionSignature> {
75
+ const [faucetConfigPublicKey] =
76
+ await this.getFaucetConfigPublicKeyAndNonce();
77
+ return await this.program.rpc.initialize({
99
78
  accounts: {
100
- mockUsdcFaucetState: mockUSDCFaucetStatePublicKey,
79
+ faucetConfig: faucetConfigPublicKey,
101
80
  admin: this.wallet.publicKey,
102
- mintAccount: fakeUSDCMint.publicKey,
81
+ mintAccount: this.mint,
103
82
  rent: SYSVAR_RENT_PUBKEY,
104
83
  systemProgram: anchor.web3.SystemProgram.programId,
84
+ tokenProgram: TOKEN_PROGRAM_ID,
105
85
  },
106
- instructions: [createUSDCMintAccountIx, initUSDCMintIx],
107
- signers: [fakeUSDCMint],
108
86
  });
109
87
  }
110
88
 
111
89
  public async fetchState(): Promise<any> {
112
- return await this.program.account.mockUsdcFaucetState.fetch(
113
- await this.getMockUSDCFaucetStatePublicKey()
90
+ return await this.program.account.faucetConfig.fetch(
91
+ await this.getFaucetConfigPublicKey()
114
92
  );
115
93
  }
116
94
 
95
+ private async mintToUserIx(userTokenAccount: PublicKey, amount: BN) {
96
+ return this.program.instruction.mintToUser(amount, {
97
+ accounts: {
98
+ faucetConfig: await this.getFaucetConfigPublicKey(),
99
+ mintAccount: this.mint,
100
+ userTokenAccount,
101
+ mintAuthority: await this.getMintAuthority(),
102
+ tokenProgram: TOKEN_PROGRAM_ID,
103
+ },
104
+ });
105
+ }
106
+
117
107
  public async mintToUser(
118
108
  userTokenAccount: PublicKey,
119
109
  amount: BN
120
110
  ): Promise<TransactionSignature> {
121
- const state: any = await this.fetchState();
122
- return await this.program.rpc.mintToUser(amount, {
111
+ const mintIx = await this.mintToUserIx(userTokenAccount, amount);
112
+
113
+ const tx = new Transaction().add(mintIx);
114
+
115
+ const txSig = await this.program.provider.sendAndConfirm(tx, [], this.opts);
116
+
117
+ return txSig;
118
+ }
119
+
120
+ public async transferMintAuthority(): Promise<TransactionSignature> {
121
+ return await this.program.rpc.transferMintAuthority({
123
122
  accounts: {
124
- mockUsdcFaucetState: await this.getMockUSDCFaucetStatePublicKey(),
125
- mintAccount: state.mint,
126
- userTokenAccount,
127
- mintAuthority: state.mintAuthority,
123
+ faucetConfig: await this.getFaucetConfigPublicKey(),
124
+ mintAccount: this.mint,
125
+ mintAuthority: await this.getMintAuthority(),
128
126
  tokenProgram: TOKEN_PROGRAM_ID,
127
+ admin: this.wallet.publicKey,
129
128
  },
130
129
  });
131
130
  }
@@ -134,12 +133,33 @@ export class MockUSDCFaucet {
134
133
  userPublicKey: PublicKey,
135
134
  amount: BN
136
135
  ): Promise<[PublicKey, TransactionSignature]> {
136
+ const tx = new Transaction();
137
+
137
138
  const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] =
138
139
  await this.createAssociatedTokenAccountAndMintToInstructions(
139
140
  userPublicKey,
140
141
  amount
141
142
  );
142
- const tx = new Transaction().add(createAssociatedAccountIx).add(mintToTx);
143
+
144
+ let associatedTokenAccountExists = false;
145
+
146
+ try {
147
+ const assosciatedTokenAccount = await this.connection.getAccountInfo(
148
+ associatedTokenPublicKey
149
+ );
150
+
151
+ associatedTokenAccountExists = !!assosciatedTokenAccount;
152
+ } catch (e) {
153
+ // token account doesn't exist
154
+ associatedTokenAccountExists = false;
155
+ }
156
+
157
+ const skipAccountCreation = associatedTokenAccountExists;
158
+
159
+ if (!skipAccountCreation) tx.add(createAssociatedAccountIx);
160
+
161
+ tx.add(mintToTx);
162
+
143
163
  const txSig = await this.program.provider.sendAndConfirm(tx, [], this.opts);
144
164
  return [associatedTokenPublicKey, txSig];
145
165
  }
@@ -164,15 +184,7 @@ export class MockUSDCFaucet {
164
184
  this.wallet.publicKey
165
185
  );
166
186
 
167
- const mintToIx = await this.program.instruction.mintToUser(amount, {
168
- accounts: {
169
- mockUsdcFaucetState: await this.getMockUSDCFaucetStatePublicKey(),
170
- mintAccount: state.mint,
171
- userTokenAccount: associateTokenPublicKey,
172
- mintAuthority: state.mintAuthority,
173
- tokenProgram: TOKEN_PROGRAM_ID,
174
- },
175
- });
187
+ const mintToIx = await this.mintToUserIx(associateTokenPublicKey, amount);
176
188
 
177
189
  return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
178
190
  }
@@ -56,9 +56,17 @@ export class RetryTxSender implements TxSender {
56
56
  const rawTransaction = tx.serialize();
57
57
  const startTime = this.getTimestamp();
58
58
 
59
- const txid: TransactionSignature =
60
- await this.provider.connection.sendRawTransaction(rawTransaction, opts);
61
- this.sendToAdditionalConnections(rawTransaction, opts);
59
+ let txid: TransactionSignature;
60
+ try {
61
+ txid = await this.provider.connection.sendRawTransaction(
62
+ rawTransaction,
63
+ opts
64
+ );
65
+ this.sendToAdditionalConnections(rawTransaction, opts);
66
+ } catch (e) {
67
+ console.error(e);
68
+ throw e;
69
+ }
62
70
 
63
71
  let done = false;
64
72
  const resolveReference: ResolveReference = {
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wrapInTx = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ const COMPUTE_UNITS_DEFAULT = 200000;
6
+ function wrapInTx(instruction, computeUnits = 600000 // TODO, requires less code change
7
+ ) {
8
+ const tx = new web3_js_1.Transaction();
9
+ if (computeUnits != COMPUTE_UNITS_DEFAULT) {
10
+ tx.add(web3_js_1.ComputeBudgetProgram.requestUnits({
11
+ units: computeUnits,
12
+ additionalFee: 0,
13
+ }));
14
+ }
15
+ return tx.add(instruction);
16
+ }
17
+ exports.wrapInTx = wrapInTx;
package/src/tx/utils.ts CHANGED
@@ -8,7 +8,7 @@ const COMPUTE_UNITS_DEFAULT = 200_000;
8
8
 
9
9
  export function wrapInTx(
10
10
  instruction: TransactionInstruction,
11
- computeUnits = 500_000 // TODO, requires less code change
11
+ computeUnits = 600_000 // TODO, requires less code change
12
12
  ): Transaction {
13
13
  const tx = new Transaction();
14
14
  if (computeUnits != COMPUTE_UNITS_DEFAULT) {