@drift-labs/sdk 0.2.0-master.10 → 0.2.0-master.11

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.
@@ -1,171 +1,280 @@
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 });
1
+ 'use strict';
2
+ var __createBinding =
3
+ (this && this.__createBinding) ||
4
+ (Object.create
5
+ ? function (o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ Object.defineProperty(o, k2, {
8
+ enumerable: true,
9
+ get: function () {
10
+ return m[k];
11
+ },
12
+ });
13
+ }
14
+ : function (o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ o[k2] = m[k];
17
+ });
18
+ var __setModuleDefault =
19
+ (this && this.__setModuleDefault) ||
20
+ (Object.create
21
+ ? function (o, v) {
22
+ Object.defineProperty(o, 'default', { enumerable: true, value: v });
23
+ }
24
+ : function (o, v) {
25
+ o['default'] = v;
26
+ });
27
+ var __importStar =
28
+ (this && this.__importStar) ||
29
+ function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null)
33
+ for (var k in mod)
34
+ if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
35
+ __createBinding(result, mod, k);
36
+ __setModuleDefault(result, mod);
37
+ return result;
38
+ };
39
+ var __awaiter =
40
+ (this && this.__awaiter) ||
41
+ function (thisArg, _arguments, P, generator) {
42
+ function adopt(value) {
43
+ return value instanceof P
44
+ ? value
45
+ : new P(function (resolve) {
46
+ resolve(value);
47
+ });
48
+ }
49
+ return new (P || (P = Promise))(function (resolve, reject) {
50
+ function fulfilled(value) {
51
+ try {
52
+ step(generator.next(value));
53
+ } catch (e) {
54
+ reject(e);
55
+ }
56
+ }
57
+ function rejected(value) {
58
+ try {
59
+ step(generator['throw'](value));
60
+ } catch (e) {
61
+ reject(e);
62
+ }
63
+ }
64
+ function step(result) {
65
+ result.done
66
+ ? resolve(result.value)
67
+ : adopt(result.value).then(fulfilled, rejected);
68
+ }
69
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
70
+ });
71
+ };
72
+ var __importDefault =
73
+ (this && this.__importDefault) ||
74
+ function (mod) {
75
+ return mod && mod.__esModule ? mod : { default: mod };
76
+ };
77
+ Object.defineProperty(exports, '__esModule', { value: true });
34
78
  exports.MockUSDCFaucet = 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 mock_usdc_faucet_json_1 = __importDefault(require("./idl/mock_usdc_faucet.json"));
79
+ const anchor = __importStar(require('@project-serum/anchor'));
80
+ const anchor_1 = require('@project-serum/anchor');
81
+ const spl_token_1 = require('@solana/spl-token');
82
+ const web3_js_1 = require('@solana/web3.js');
83
+ const mock_usdc_faucet_json_1 = __importDefault(
84
+ require('./idl/token_faucet.json')
85
+ );
40
86
  class MockUSDCFaucet {
41
- constructor(connection, wallet, programId, 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(mock_usdc_faucet_json_1.default, programId, provider);
48
- }
49
- getMockUSDCFaucetStatePublicKeyAndNonce() {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('mock_usdc_faucet'))], this.program.programId);
52
- });
53
- }
54
- getMockUSDCFaucetStatePublicKey() {
55
- return __awaiter(this, void 0, void 0, function* () {
56
- if (this.mockUSDCFaucetStatePublicKey) {
57
- return this.mockUSDCFaucetStatePublicKey;
58
- }
59
- this.mockUSDCFaucetStatePublicKey = (yield this.getMockUSDCFaucetStatePublicKeyAndNonce())[0];
60
- return this.mockUSDCFaucetStatePublicKey;
61
- });
62
- }
63
- initialize() {
64
- return __awaiter(this, void 0, void 0, function* () {
65
- const stateAccountRPCResponse = yield this.connection.getParsedAccountInfo(yield this.getMockUSDCFaucetStatePublicKey());
66
- if (stateAccountRPCResponse.value !== null) {
67
- throw new Error('Faucet already initialized');
68
- }
69
- const fakeUSDCMint = anchor.web3.Keypair.generate();
70
- const createUSDCMintAccountIx = web3_js_1.SystemProgram.createAccount({
71
- fromPubkey: this.wallet.publicKey,
72
- newAccountPubkey: fakeUSDCMint.publicKey,
73
- lamports: yield spl_token_1.Token.getMinBalanceRentForExemptMint(this.connection),
74
- space: spl_token_1.MintLayout.span,
75
- programId: spl_token_1.TOKEN_PROGRAM_ID,
76
- });
77
- const [mintAuthority, _mintAuthorityNonce] = yield web3_js_1.PublicKey.findProgramAddress([fakeUSDCMint.publicKey.toBuffer()], this.program.programId);
78
- const initUSDCMintIx = spl_token_1.Token.createInitMintInstruction(spl_token_1.TOKEN_PROGRAM_ID, fakeUSDCMint.publicKey, 6, mintAuthority, null);
79
- const [mockUSDCFaucetStatePublicKey, mockUSDCFaucetStateNonce] = yield this.getMockUSDCFaucetStatePublicKeyAndNonce();
80
- return yield this.program.rpc.initialize(mockUSDCFaucetStateNonce, {
81
- accounts: {
82
- mockUsdcFaucetState: mockUSDCFaucetStatePublicKey,
83
- admin: this.wallet.publicKey,
84
- mintAccount: fakeUSDCMint.publicKey,
85
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
86
- systemProgram: anchor.web3.SystemProgram.programId,
87
- },
88
- instructions: [createUSDCMintAccountIx, initUSDCMintIx],
89
- signers: [fakeUSDCMint],
90
- });
91
- });
92
- }
93
- fetchState() {
94
- return __awaiter(this, void 0, void 0, function* () {
95
- return yield this.program.account.mockUsdcFaucetState.fetch(yield this.getMockUSDCFaucetStatePublicKey());
96
- });
97
- }
98
- mintToUser(userTokenAccount, amount) {
99
- return __awaiter(this, void 0, void 0, function* () {
100
- const state = yield this.fetchState();
101
- return yield this.program.rpc.mintToUser(amount, {
102
- accounts: {
103
- mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
104
- mintAccount: state.mint,
105
- userTokenAccount,
106
- mintAuthority: state.mintAuthority,
107
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
108
- },
109
- });
110
- });
111
- }
112
- createAssociatedTokenAccountAndMintTo(userPublicKey, amount) {
113
- return __awaiter(this, void 0, void 0, function* () {
114
- const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] = yield this.createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount);
115
- const tx = new web3_js_1.Transaction().add(createAssociatedAccountIx).add(mintToTx);
116
- const txSig = yield this.program.provider.sendAndConfirm(tx, [], this.opts);
117
- return [associatedTokenPublicKey, txSig];
118
- });
119
- }
120
- createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount) {
121
- return __awaiter(this, void 0, void 0, function* () {
122
- const state = yield this.fetchState();
123
- const associateTokenPublicKey = yield this.getAssosciatedMockUSDMintAddress({ userPubKey: userPublicKey });
124
- 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);
125
- const mintToIx = yield this.program.instruction.mintToUser(amount, {
126
- accounts: {
127
- mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
128
- mintAccount: state.mint,
129
- userTokenAccount: associateTokenPublicKey,
130
- mintAuthority: state.mintAuthority,
131
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
132
- },
133
- });
134
- return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
135
- });
136
- }
137
- getAssosciatedMockUSDMintAddress(props) {
138
- return __awaiter(this, void 0, void 0, function* () {
139
- const state = yield this.fetchState();
140
- return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, props.userPubKey);
141
- });
142
- }
143
- getTokenAccountInfo(props) {
144
- return __awaiter(this, void 0, void 0, function* () {
145
- const assosciatedKey = yield this.getAssosciatedMockUSDMintAddress(props);
146
- const state = yield this.fetchState();
147
- const token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
148
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
149
- // @ts-ignore
150
- this.provider.payer);
151
- return yield token.getAccountInfo(assosciatedKey);
152
- });
153
- }
154
- subscribeToTokenAccount(props) {
155
- return __awaiter(this, void 0, void 0, function* () {
156
- try {
157
- const tokenAccountKey = yield this.getAssosciatedMockUSDMintAddress(props);
158
- props.callback(yield this.getTokenAccountInfo(props));
159
- // Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
160
- this.connection.onAccountChange(tokenAccountKey, (_accountInfo /* accountInfo is a buffer which we don't know how to deserialize */) => __awaiter(this, void 0, void 0, function* () {
161
- props.callback(yield this.getTokenAccountInfo(props));
162
- }));
163
- return true;
164
- }
165
- catch (e) {
166
- return false;
167
- }
168
- });
169
- }
87
+ constructor(connection, wallet, programId, opts) {
88
+ this.connection = connection;
89
+ this.wallet = wallet;
90
+ this.opts = opts || anchor_1.AnchorProvider.defaultOptions();
91
+ const provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
92
+ this.provider = provider;
93
+ this.program = new anchor_1.Program(
94
+ mock_usdc_faucet_json_1.default,
95
+ programId,
96
+ provider
97
+ );
98
+ }
99
+ getMockUSDCFaucetStatePublicKeyAndNonce() {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ return anchor.web3.PublicKey.findProgramAddress(
102
+ [Buffer.from(anchor.utils.bytes.utf8.encode('mock_usdc_faucet'))],
103
+ this.program.programId
104
+ );
105
+ });
106
+ }
107
+ getMockUSDCFaucetStatePublicKey() {
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ if (this.mockUSDCFaucetStatePublicKey) {
110
+ return this.mockUSDCFaucetStatePublicKey;
111
+ }
112
+ this.mockUSDCFaucetStatePublicKey =
113
+ (yield this.getMockUSDCFaucetStatePublicKeyAndNonce())[0];
114
+ return this.mockUSDCFaucetStatePublicKey;
115
+ });
116
+ }
117
+ initialize() {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ const stateAccountRPCResponse =
120
+ yield this.connection.getParsedAccountInfo(
121
+ yield this.getMockUSDCFaucetStatePublicKey()
122
+ );
123
+ if (stateAccountRPCResponse.value !== null) {
124
+ throw new Error('Faucet already initialized');
125
+ }
126
+ const fakeUSDCMint = anchor.web3.Keypair.generate();
127
+ const createUSDCMintAccountIx = web3_js_1.SystemProgram.createAccount({
128
+ fromPubkey: this.wallet.publicKey,
129
+ newAccountPubkey: fakeUSDCMint.publicKey,
130
+ lamports: yield spl_token_1.Token.getMinBalanceRentForExemptMint(
131
+ this.connection
132
+ ),
133
+ space: spl_token_1.MintLayout.span,
134
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
135
+ });
136
+ const [mintAuthority, _mintAuthorityNonce] =
137
+ yield web3_js_1.PublicKey.findProgramAddress(
138
+ [fakeUSDCMint.publicKey.toBuffer()],
139
+ this.program.programId
140
+ );
141
+ const initUSDCMintIx = spl_token_1.Token.createInitMintInstruction(
142
+ spl_token_1.TOKEN_PROGRAM_ID,
143
+ fakeUSDCMint.publicKey,
144
+ 6,
145
+ mintAuthority,
146
+ null
147
+ );
148
+ const [mockUSDCFaucetStatePublicKey, mockUSDCFaucetStateNonce] =
149
+ yield this.getMockUSDCFaucetStatePublicKeyAndNonce();
150
+ return yield this.program.rpc.initialize(mockUSDCFaucetStateNonce, {
151
+ accounts: {
152
+ mockUsdcFaucetState: mockUSDCFaucetStatePublicKey,
153
+ admin: this.wallet.publicKey,
154
+ mintAccount: fakeUSDCMint.publicKey,
155
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
156
+ systemProgram: anchor.web3.SystemProgram.programId,
157
+ },
158
+ instructions: [createUSDCMintAccountIx, initUSDCMintIx],
159
+ signers: [fakeUSDCMint],
160
+ });
161
+ });
162
+ }
163
+ fetchState() {
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ return yield this.program.account.mockUsdcFaucetState.fetch(
166
+ yield this.getMockUSDCFaucetStatePublicKey()
167
+ );
168
+ });
169
+ }
170
+ mintToUser(userTokenAccount, amount) {
171
+ return __awaiter(this, void 0, void 0, function* () {
172
+ const state = yield this.fetchState();
173
+ return yield this.program.rpc.mintToUser(amount, {
174
+ accounts: {
175
+ mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
176
+ mintAccount: state.mint,
177
+ userTokenAccount,
178
+ mintAuthority: state.mintAuthority,
179
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
180
+ },
181
+ });
182
+ });
183
+ }
184
+ createAssociatedTokenAccountAndMintTo(userPublicKey, amount) {
185
+ return __awaiter(this, void 0, void 0, function* () {
186
+ const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] =
187
+ yield this.createAssociatedTokenAccountAndMintToInstructions(
188
+ userPublicKey,
189
+ amount
190
+ );
191
+ const tx = new web3_js_1.Transaction()
192
+ .add(createAssociatedAccountIx)
193
+ .add(mintToTx);
194
+ const txSig = yield this.program.provider.sendAndConfirm(
195
+ tx,
196
+ [],
197
+ this.opts
198
+ );
199
+ return [associatedTokenPublicKey, txSig];
200
+ });
201
+ }
202
+ createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount) {
203
+ return __awaiter(this, void 0, void 0, function* () {
204
+ const state = yield this.fetchState();
205
+ const associateTokenPublicKey =
206
+ yield this.getAssosciatedMockUSDMintAddress({
207
+ userPubKey: userPublicKey,
208
+ });
209
+ const createAssociatedAccountIx =
210
+ spl_token_1.Token.createAssociatedTokenAccountInstruction(
211
+ spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
212
+ spl_token_1.TOKEN_PROGRAM_ID,
213
+ state.mint,
214
+ associateTokenPublicKey,
215
+ userPublicKey,
216
+ this.wallet.publicKey
217
+ );
218
+ const mintToIx = yield this.program.instruction.mintToUser(amount, {
219
+ accounts: {
220
+ mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
221
+ mintAccount: state.mint,
222
+ userTokenAccount: associateTokenPublicKey,
223
+ mintAuthority: state.mintAuthority,
224
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
225
+ },
226
+ });
227
+ return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
228
+ });
229
+ }
230
+ getAssosciatedMockUSDMintAddress(props) {
231
+ return __awaiter(this, void 0, void 0, function* () {
232
+ const state = yield this.fetchState();
233
+ return spl_token_1.Token.getAssociatedTokenAddress(
234
+ spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
235
+ spl_token_1.TOKEN_PROGRAM_ID,
236
+ state.mint,
237
+ props.userPubKey
238
+ );
239
+ });
240
+ }
241
+ getTokenAccountInfo(props) {
242
+ return __awaiter(this, void 0, void 0, function* () {
243
+ const assosciatedKey = yield this.getAssosciatedMockUSDMintAddress(props);
244
+ const state = yield this.fetchState();
245
+ const token = new spl_token_1.Token(
246
+ this.connection,
247
+ state.mint,
248
+ spl_token_1.TOKEN_PROGRAM_ID,
249
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
250
+ // @ts-ignore
251
+ this.provider.payer
252
+ );
253
+ return yield token.getAccountInfo(assosciatedKey);
254
+ });
255
+ }
256
+ subscribeToTokenAccount(props) {
257
+ return __awaiter(this, void 0, void 0, function* () {
258
+ try {
259
+ const tokenAccountKey = yield this.getAssosciatedMockUSDMintAddress(
260
+ props
261
+ );
262
+ props.callback(yield this.getTokenAccountInfo(props));
263
+ // Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
264
+ this.connection.onAccountChange(
265
+ tokenAccountKey,
266
+ (
267
+ _accountInfo /* accountInfo is a buffer which we don't know how to deserialize */
268
+ ) =>
269
+ __awaiter(this, void 0, void 0, function* () {
270
+ props.callback(yield this.getTokenAccountInfo(props));
271
+ })
272
+ );
273
+ return true;
274
+ } catch (e) {
275
+ return false;
276
+ }
277
+ });
278
+ }
170
279
  }
171
280
  exports.MockUSDCFaucet = MockUSDCFaucet;
@@ -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,78 +39,56 @@ 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
 
@@ -118,18 +96,29 @@ export class MockUSDCFaucet {
118
96
  userTokenAccount: PublicKey,
119
97
  amount: BN
120
98
  ): Promise<TransactionSignature> {
121
- const state: any = await this.fetchState();
122
99
  return await this.program.rpc.mintToUser(amount, {
123
100
  accounts: {
124
- mockUsdcFaucetState: await this.getMockUSDCFaucetStatePublicKey(),
125
- mintAccount: state.mint,
101
+ faucetConfig: await this.getFaucetConfigPublicKey(),
102
+ mintAccount: this.mint,
126
103
  userTokenAccount,
127
- mintAuthority: state.mintAuthority,
104
+ mintAuthority: await this.getMintAuthority(),
128
105
  tokenProgram: TOKEN_PROGRAM_ID,
129
106
  },
130
107
  });
131
108
  }
132
109
 
110
+ public async transferMintAuthority(): Promise<TransactionSignature> {
111
+ return await this.program.rpc.transferMintAuthority({
112
+ accounts: {
113
+ faucetConfig: await this.getFaucetConfigPublicKey(),
114
+ mintAccount: this.mint,
115
+ mintAuthority: await this.getMintAuthority(),
116
+ tokenProgram: TOKEN_PROGRAM_ID,
117
+ admin: this.wallet.publicKey,
118
+ },
119
+ });
120
+ }
121
+
133
122
  public async createAssociatedTokenAccountAndMintTo(
134
123
  userPublicKey: PublicKey,
135
124
  amount: BN
@@ -166,7 +155,7 @@ export class MockUSDCFaucet {
166
155
 
167
156
  const mintToIx = await this.program.instruction.mintToUser(amount, {
168
157
  accounts: {
169
- mockUsdcFaucetState: await this.getMockUSDCFaucetStatePublicKey(),
158
+ faucetConfig: await this.getFaucetConfigPublicKey(),
170
159
  mintAccount: state.mint,
171
160
  userTokenAccount: associateTokenPublicKey,
172
161
  mintAuthority: state.mintAuthority,