Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 34e66c6

Browse files
authored
lending: Update JS tests to solana-test-validator (#1513)
* lending: Update JS tests to solana-test-validator * Add solana tools install * Fix oopsie on the path * Move where deployed programs go
1 parent d54fe03 commit 34e66c6

File tree

10 files changed

+342
-144
lines changed

10 files changed

+342
-144
lines changed

.github/workflows/pull-request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ jobs:
205205
uses: actions/download-artifact@v2
206206
with:
207207
name: programs
208-
path: target/bpfel-unknown-unknown/release
208+
path: target/deploy
209209
- run: ./ci/js-test-token-lending.sh
210210

211211
fuzz:

ci/js-test-token-lending.sh

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
#!/usr/bin/env bash
22

3-
set -ex
4-
cd "$(dirname "$0")"
3+
set -e
4+
cd "$(dirname "$0")/.."
5+
source ./ci/solana-version.sh install
56

6-
(cd ../token/js && npm install)
7-
8-
cd ../token-lending/js
7+
set -x
8+
cd token-lending/js
99
npm install
1010
npm run lint
1111
npm run build
12-
npm run cluster:localnet
13-
npm run localnet:update
14-
npm run localnet:up
15-
npm run start
16-
npm run localnet:down
12+
npm run start-with-test-validator

token-lending/js/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ compiled
1111
.rpt2_cache
1212
docs
1313
lib
14+
test-ledger

token-lending/js/cli/main.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
* Exercises the token-lending program
33
*/
44

5-
import { loadPrograms, createLendingMarket } from "./token-lending-test";
5+
import { createLendingMarket } from "./token-lending-test";
66

77
async function main() {
88
// These test cases are designed to run sequentially and in the following order
9-
console.log("Run test: loadPrograms");
10-
await loadPrograms();
119
console.log("Run test: createLendingMarket");
1210
await createLendingMarket();
1311
console.log("Success\n");
Lines changed: 6 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
22
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
33

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";
136

14-
import { LendingMarket } from "../client";
15-
import { Store } from "../client/util/store";
7+
import { LENDING_PROGRAM_ID, LendingMarket } from "../client";
168
import { newAccountWithLamports } from "../client/util/new-account-with-lamports";
179
import { url } from "../client/util/url";
1810

@@ -27,17 +19,6 @@ async function getConnection(): Promise<Connection> {
2719
return connection;
2820
}
2921

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-
4122
export async function createLendingMarket(): Promise<void> {
4223
const connection = await getConnection();
4324

@@ -54,86 +35,17 @@ export async function createLendingMarket(): Promise<void> {
5435
quoteMintAuthority.publicKey,
5536
null,
5637
2,
57-
tokenProgramId
38+
TOKEN_PROGRAM_ID
5839
);
5940

6041
const lendingMarketAccount = new Account();
6142
await LendingMarket.create({
6243
connection,
63-
tokenProgramId,
64-
lendingProgramId: tokenLendingProgramId,
44+
tokenProgramId: TOKEN_PROGRAM_ID,
45+
lendingProgramId: LENDING_PROGRAM_ID,
6546
quoteTokenMint: quoteTokenMint.publicKey,
6647
lendingMarketAccount,
6748
lendingMarketOwner: payer.publicKey,
6849
payer,
6950
});
7051
}
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-
}

token-lending/js/client/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import {
1212
SYSVAR_RENT_PUBKEY,
1313
sendAndConfirmTransaction,
1414
} from "@solana/web3.js";
15+
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
1516
import * as BufferLayout from "buffer-layout";
1617
import * as Layout from "./layout";
1718

18-
const TOKEN_PROGRAM_ID = new PublicKey(
19-
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
19+
export const LENDING_PROGRAM_ID = new PublicKey(
20+
"LendZqTs7gn5CTSJU1jWKhKuVpjJGom45nnwPb2AMTi"
2021
);
2122

2223
/**

token-lending/js/client/util/store.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)