1
1
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
2
2
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
3
3
4
- import fs from "mz/fs" ;
5
- import {
6
- Account ,
7
- Connection ,
8
- BpfLoader ,
9
- PublicKey ,
10
- BPF_LOADER_PROGRAM_ID ,
11
- } from "@solana/web3.js" ;
12
- import { Token } from "@solana/spl-token" ;
4
+ import { Account , Connection } from "@solana/web3.js" ;
5
+ import { Token , TOKEN_PROGRAM_ID } from "@solana/spl-token" ;
13
6
14
- import { LendingMarket } from "../client" ;
15
- import { Store } from "../client/util/store" ;
7
+ import { LENDING_PROGRAM_ID , LendingMarket } from "../client" ;
16
8
import { newAccountWithLamports } from "../client/util/new-account-with-lamports" ;
17
9
import { url } from "../client/util/url" ;
18
10
@@ -27,17 +19,6 @@ async function getConnection(): Promise<Connection> {
27
19
return connection ;
28
20
}
29
21
30
- let tokenProgramId : PublicKey ;
31
- let tokenLendingProgramId : PublicKey ;
32
-
33
- export async function loadPrograms ( ) : Promise < void > {
34
- const connection = await getConnection ( ) ;
35
- ( { tokenProgramId, tokenLendingProgramId } = await GetPrograms ( connection ) ) ;
36
-
37
- console . log ( "SPL Token Program ID" , tokenProgramId . toString ( ) ) ;
38
- console . log ( "SPL Token Lending Program ID" , tokenLendingProgramId . toString ( ) ) ;
39
- }
40
-
41
22
export async function createLendingMarket ( ) : Promise < void > {
42
23
const connection = await getConnection ( ) ;
43
24
@@ -54,86 +35,17 @@ export async function createLendingMarket(): Promise<void> {
54
35
quoteMintAuthority . publicKey ,
55
36
null ,
56
37
2 ,
57
- tokenProgramId
38
+ TOKEN_PROGRAM_ID
58
39
) ;
59
40
60
41
const lendingMarketAccount = new Account ( ) ;
61
42
await LendingMarket . create ( {
62
43
connection,
63
- tokenProgramId,
64
- lendingProgramId : tokenLendingProgramId ,
44
+ tokenProgramId : TOKEN_PROGRAM_ID ,
45
+ lendingProgramId : LENDING_PROGRAM_ID ,
65
46
quoteTokenMint : quoteTokenMint . publicKey ,
66
47
lendingMarketAccount,
67
48
lendingMarketOwner : payer . publicKey ,
68
49
payer,
69
50
} ) ;
70
51
}
71
-
72
- async function loadProgram (
73
- connection : Connection ,
74
- path : string
75
- ) : Promise < PublicKey > {
76
- const data = await fs . readFile ( path ) ;
77
- const { feeCalculator } = await connection . getRecentBlockhash ( ) ;
78
-
79
- const loaderCost =
80
- feeCalculator . lamportsPerSignature *
81
- BpfLoader . getMinNumSignatures ( data . length ) ;
82
- const minAccountBalance = await connection . getMinimumBalanceForRentExemption (
83
- 0
84
- ) ;
85
- const minExecutableBalance = await connection . getMinimumBalanceForRentExemption (
86
- data . length
87
- ) ;
88
- const balanceNeeded = minAccountBalance + loaderCost + minExecutableBalance ;
89
-
90
- const from = await newAccountWithLamports ( connection , balanceNeeded ) ;
91
- const program_account = new Account ( ) ;
92
- console . log ( "Loading program:" , path ) ;
93
- await BpfLoader . load (
94
- connection ,
95
- from ,
96
- program_account ,
97
- data ,
98
- BPF_LOADER_PROGRAM_ID
99
- ) ;
100
- return program_account . publicKey ;
101
- }
102
-
103
- async function GetPrograms (
104
- connection : Connection
105
- ) : Promise < {
106
- tokenProgramId : PublicKey ;
107
- tokenLendingProgramId : PublicKey ;
108
- } > {
109
- const store = new Store ( ) ;
110
- let tokenProgramId = null ;
111
- let tokenLendingProgramId = null ;
112
- try {
113
- const config = await store . load ( "config.json" ) ;
114
- console . log ( "Using pre-loaded programs" ) ;
115
- console . log (
116
- " Note: To reload programs remove client/util/store/config.json"
117
- ) ;
118
- if ( "tokenProgramId" in config && "tokenLendingProgramId" in config ) {
119
- tokenProgramId = new PublicKey ( config [ "tokenProgramId" ] ) ;
120
- tokenLendingProgramId = new PublicKey ( config [ "tokenLendingProgramId" ] ) ;
121
- } else {
122
- throw new Error ( "Program ids not found" ) ;
123
- }
124
- } catch ( err ) {
125
- tokenProgramId = await loadProgram (
126
- connection ,
127
- "../../target/bpfel-unknown-unknown/release/spl_token.so"
128
- ) ;
129
- tokenLendingProgramId = await loadProgram (
130
- connection ,
131
- "../../target/bpfel-unknown-unknown/release/spl_token_lending.so"
132
- ) ;
133
- await store . save ( "config.json" , {
134
- tokenProgramId : tokenProgramId . toString ( ) ,
135
- tokenLendingProgramId : tokenLendingProgramId . toString ( ) ,
136
- } ) ;
137
- }
138
- return { tokenProgramId, tokenLendingProgramId } ;
139
- }
0 commit comments