Skip to content

Commit

Permalink
lending: Update JS tests to solana-test-validator (solana-labs#1513)
Browse files Browse the repository at this point in the history
* lending: Update JS tests to solana-test-validator

* Add solana tools install

* Fix oopsie on the path

* Move where deployed programs go
  • Loading branch information
joncinque authored Mar 27, 2021
1 parent d54fe03 commit 34e66c6
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ jobs:
uses: actions/download-artifact@v2
with:
name: programs
path: target/bpfel-unknown-unknown/release
path: target/deploy
- run: ./ci/js-test-token-lending.sh

fuzz:
Expand Down
16 changes: 6 additions & 10 deletions ci/js-test-token-lending.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#!/usr/bin/env bash

set -ex
cd "$(dirname "$0")"
set -e
cd "$(dirname "$0")/.."
source ./ci/solana-version.sh install

(cd ../token/js && npm install)

cd ../token-lending/js
set -x
cd token-lending/js
npm install
npm run lint
npm run build
npm run cluster:localnet
npm run localnet:update
npm run localnet:up
npm run start
npm run localnet:down
npm run start-with-test-validator
1 change: 1 addition & 0 deletions token-lending/js/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ compiled
.rpt2_cache
docs
lib
test-ledger
4 changes: 1 addition & 3 deletions token-lending/js/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
* Exercises the token-lending program
*/

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

async function main() {
// These test cases are designed to run sequentially and in the following order
console.log("Run test: loadPrograms");
await loadPrograms();
console.log("Run test: createLendingMarket");
await createLendingMarket();
console.log("Success\n");
Expand Down
100 changes: 6 additions & 94 deletions token-lending/js/cli/token-lending-test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */

import fs from "mz/fs";
import {
Account,
Connection,
BpfLoader,
PublicKey,
BPF_LOADER_PROGRAM_ID,
} from "@solana/web3.js";
import { Token } from "@solana/spl-token";
import { Account, Connection } from "@solana/web3.js";
import { Token, TOKEN_PROGRAM_ID } from "@solana/spl-token";

import { LendingMarket } from "../client";
import { Store } from "../client/util/store";
import { LENDING_PROGRAM_ID, LendingMarket } from "../client";
import { newAccountWithLamports } from "../client/util/new-account-with-lamports";
import { url } from "../client/util/url";

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

let tokenProgramId: PublicKey;
let tokenLendingProgramId: PublicKey;

export async function loadPrograms(): Promise<void> {
const connection = await getConnection();
({ tokenProgramId, tokenLendingProgramId } = await GetPrograms(connection));

console.log("SPL Token Program ID", tokenProgramId.toString());
console.log("SPL Token Lending Program ID", tokenLendingProgramId.toString());
}

export async function createLendingMarket(): Promise<void> {
const connection = await getConnection();

Expand All @@ -54,86 +35,17 @@ export async function createLendingMarket(): Promise<void> {
quoteMintAuthority.publicKey,
null,
2,
tokenProgramId
TOKEN_PROGRAM_ID
);

const lendingMarketAccount = new Account();
await LendingMarket.create({
connection,
tokenProgramId,
lendingProgramId: tokenLendingProgramId,
tokenProgramId: TOKEN_PROGRAM_ID,
lendingProgramId: LENDING_PROGRAM_ID,
quoteTokenMint: quoteTokenMint.publicKey,
lendingMarketAccount,
lendingMarketOwner: payer.publicKey,
payer,
});
}

async function loadProgram(
connection: Connection,
path: string
): Promise<PublicKey> {
const data = await fs.readFile(path);
const { feeCalculator } = await connection.getRecentBlockhash();

const loaderCost =
feeCalculator.lamportsPerSignature *
BpfLoader.getMinNumSignatures(data.length);
const minAccountBalance = await connection.getMinimumBalanceForRentExemption(
0
);
const minExecutableBalance = await connection.getMinimumBalanceForRentExemption(
data.length
);
const balanceNeeded = minAccountBalance + loaderCost + minExecutableBalance;

const from = await newAccountWithLamports(connection, balanceNeeded);
const program_account = new Account();
console.log("Loading program:", path);
await BpfLoader.load(
connection,
from,
program_account,
data,
BPF_LOADER_PROGRAM_ID
);
return program_account.publicKey;
}

async function GetPrograms(
connection: Connection
): Promise<{
tokenProgramId: PublicKey;
tokenLendingProgramId: PublicKey;
}> {
const store = new Store();
let tokenProgramId = null;
let tokenLendingProgramId = null;
try {
const config = await store.load("config.json");
console.log("Using pre-loaded programs");
console.log(
" Note: To reload programs remove client/util/store/config.json"
);
if ("tokenProgramId" in config && "tokenLendingProgramId" in config) {
tokenProgramId = new PublicKey(config["tokenProgramId"]);
tokenLendingProgramId = new PublicKey(config["tokenLendingProgramId"]);
} else {
throw new Error("Program ids not found");
}
} catch (err) {
tokenProgramId = await loadProgram(
connection,
"../../target/bpfel-unknown-unknown/release/spl_token.so"
);
tokenLendingProgramId = await loadProgram(
connection,
"../../target/bpfel-unknown-unknown/release/spl_token_lending.so"
);
await store.save("config.json", {
tokenProgramId: tokenProgramId.toString(),
tokenLendingProgramId: tokenLendingProgramId.toString(),
});
}
return { tokenProgramId, tokenLendingProgramId };
}
5 changes: 3 additions & 2 deletions token-lending/js/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
SYSVAR_RENT_PUBKEY,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import * as BufferLayout from "buffer-layout";
import * as Layout from "./layout";

const TOKEN_PROGRAM_ID = new PublicKey(
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
export const LENDING_PROGRAM_ID = new PublicKey(
"LendZqTs7gn5CTSJU1jWKhKuVpjJGom45nnwPb2AMTi"
);

/**
Expand Down
27 changes: 0 additions & 27 deletions token-lending/js/client/util/store.ts

This file was deleted.

Loading

0 comments on commit 34e66c6

Please sign in to comment.