Skip to content

Commit

Permalink
lending: js fixes (solana-labs#2012)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordaaash authored Jul 2, 2021
1 parent fe57f29 commit ba0c0e0
Show file tree
Hide file tree
Showing 22 changed files with 76 additions and 61 deletions.
5 changes: 5 additions & 0 deletions token-lending/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Please note that only the lending program deployed to devnet is currently operat
| Testnet | [`6TvznH3B2e3p2mbhufNBpgSrLx6UkgvxtVQvopEZ2kuH`](https://explorer.solana.com/address/6TvznH3B2e3p2mbhufNBpgSrLx6UkgvxtVQvopEZ2kuH?cluster=testnet) |
| Devnet | [`6TvznH3B2e3p2mbhufNBpgSrLx6UkgvxtVQvopEZ2kuH`](https://explorer.solana.com/address/6TvznH3B2e3p2mbhufNBpgSrLx6UkgvxtVQvopEZ2kuH?cluster=devnet) |

### Documentation

- [CLI docs](https://github.com/solana-labs/solana-program-library/tree/master/token-lending/cli)
- [Client library docs](https://solana-labs.github.io/solana-program-library/token-lending/)

### Deploy a lending program (optional)

This is optional! You can skip these steps and use the [Token Lending CLI](./cli/README.md) with one of the on-chain programs listed above to create a lending market and add reserves to it.
Expand Down
6 changes: 3 additions & 3 deletions token-lending/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solana/spl-token-lending",
"version": "0.3.0",
"version": "0.3.3",
"description": "SPL Token Lending JavaScript API",
"license": "MIT",
"author": "Solana Maintainers <[email protected]>",
Expand All @@ -17,12 +17,12 @@
},
"main": "lib/index.cjs.js",
"module": "lib/index.esm.js",
"types": "lib/index.d.ts",
"types": "lib/src/index.d.ts",
"files": [
"lib"
],
"scripts": {
"build": "rollup -c",
"build": "rm -rf lib/* && tsc && rollup -c && rm lib/rollup.config.d.ts",
"deploy": "yarn docs && gh-pages -d docs -e token-lending",
"docs": "rm -rf docs/* && typedoc",
"lint": "eslint . --ext .ts && prettier --check '**/*.{ts,js,json}'",
Expand Down
2 changes: 1 addition & 1 deletion token-lending/js/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export default {
watch: {
include: 'src/**',
},
plugins: [typescript({ exclude: ['rollup.config.ts'] }), commonjs(), nodeResolve()],
plugins: [typescript(), commonjs(), nodeResolve()],
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface Data {
liquidityAmount: bigint;
}

const DataLayout = struct<Data>([u8('instruction'), u64('liquidityAmount')]);

export const borrowObligationLiquidityInstruction = (
liquidityAmount: number | bigint,
sourceLiquidity: PublicKey,
Expand All @@ -22,10 +24,8 @@ export const borrowObligationLiquidityInstruction = (
obligationOwner: PublicKey,
hostFeeReceiver?: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction'), u64('liquidityAmount')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: LendingInstruction.BorrowObligationLiquidity,
liquidityAmount: BigInt(liquidityAmount),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface Data {
collateralAmount: bigint;
}

const DataLayout = struct<Data>([u8('instruction'), u64('collateralAmount')]);

export const depositObligationCollateralInstruction = (
collateralAmount: number | bigint,
sourceCollateral: PublicKey,
Expand All @@ -21,10 +23,8 @@ export const depositObligationCollateralInstruction = (
obligationOwner: PublicKey,
transferAuthority: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction'), u64('collateralAmount')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: LendingInstruction.DepositObligationCollateral,
collateralAmount: BigInt(collateralAmount),
Expand Down
8 changes: 4 additions & 4 deletions token-lending/js/src/instructions/depositReserveLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface Data {
liquidityAmount: bigint;
}

const DataLayout = struct<Data>([u8('instruction'), u64('liquidityAmount')]);

export const depositReserveLiquidityInstruction = (
liquidityAmount: number | bigint,
sourceLiquidity: PublicKey,
Expand All @@ -21,10 +23,8 @@ export const depositReserveLiquidityInstruction = (
lendingMarketAuthority: PublicKey,
transferAuthority: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction'), u64('liquidityAmount')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: LendingInstruction.DepositReserveLiquidity,
liquidityAmount: BigInt(liquidityAmount),
Expand Down
8 changes: 4 additions & 4 deletions token-lending/js/src/instructions/initLendingMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ interface Data {
quoteCurrency: Buffer;
}

const DataLayout = struct<Data>([u8('instruction'), publicKey('owner'), blob(32, 'quoteCurrency')]);

export const initLendingMarketInstruction = (
owner: PublicKey,
quoteCurrency: Buffer,
lendingMarket: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction'), publicKey('owner'), blob(32, 'quoteCurrency')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: LendingInstruction.InitLendingMarket,
owner,
Expand Down
8 changes: 4 additions & 4 deletions token-lending/js/src/instructions/initObligation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ interface Data {
instruction: number;
}

const DataLayout = struct<Data>([u8('instruction')]);

export const initObligationInstruction = (
obligation: PublicKey,
lendingMarket: PublicKey,
obligationOwner: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode({ instruction: LendingInstruction.InitObligation }, data);
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode({ instruction: LendingInstruction.InitObligation }, data);

const keys = [
{ pubkey: obligation, isSigner: false, isWritable: true },
Expand Down
8 changes: 4 additions & 4 deletions token-lending/js/src/instructions/initReserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface Data {
config: ReserveConfig;
}

const DataLayout = struct<Data>([u8('instruction'), u64('liquidityAmount'), ReserveConfigLayout]);

export const initReserveInstruction = (
liquidityAmount: number | bigint,
config: ReserveConfig,
Expand All @@ -30,10 +32,8 @@ export const initReserveInstruction = (
lendingMarketOwner: PublicKey,
transferAuthority: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction'), u64('liquidityAmount'), ReserveConfigLayout]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: LendingInstruction.InitReserve,
liquidityAmount: BigInt(liquidityAmount),
Expand Down
8 changes: 4 additions & 4 deletions token-lending/js/src/instructions/liquidateObligation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface Data {
liquidityAmount: bigint;
}

const DataLayout = struct<Data>([u8('instruction'), u64('liquidityAmount')]);

export const liquidateObligationInstruction = (
liquidityAmount: number | bigint,
sourceLiquidity: PublicKey,
Expand All @@ -23,10 +25,8 @@ export const liquidateObligationInstruction = (
lendingMarketAuthority: PublicKey,
transferAuthority: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction'), u64('liquidityAmount')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: LendingInstruction.LiquidateObligation,
liquidityAmount: BigInt(liquidityAmount),
Expand Down
8 changes: 4 additions & 4 deletions token-lending/js/src/instructions/redeemReserveCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface Data {
collateralAmount: bigint;
}

const DataLayout = struct<Data>([u8('instruction'), u64('collateralAmount')]);

export const redeemReserveCollateralInstruction = (
collateralAmount: number | bigint,
sourceCollateral: PublicKey,
Expand All @@ -21,10 +23,8 @@ export const redeemReserveCollateralInstruction = (
lendingMarketAuthority: PublicKey,
transferAuthority: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction'), u64('collateralAmount')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: LendingInstruction.RedeemReserveCollateral,
collateralAmount: BigInt(collateralAmount),
Expand Down
8 changes: 4 additions & 4 deletions token-lending/js/src/instructions/refreshObligation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ interface Data {
instruction: number;
}

const DataLayout = struct<Data>([u8('instruction')]);

export const refreshObligationInstruction = (
obligation: PublicKey,
depositReserves: PublicKey[],
borrowReserves: PublicKey[]
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode({ instruction: LendingInstruction.RefreshObligation }, data);
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode({ instruction: LendingInstruction.RefreshObligation }, data);

const keys = [
{ pubkey: obligation, isSigner: false, isWritable: true },
Expand Down
8 changes: 4 additions & 4 deletions token-lending/js/src/instructions/refreshReserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ interface Data {
instruction: number;
}

export const refreshReserveInstruction = (reserve: PublicKey, oracle: PublicKey): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction')]);
const DataLayout = struct<Data>([u8('instruction')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode({ instruction: LendingInstruction.RefreshReserve }, data);
export const refreshReserveInstruction = (reserve: PublicKey, oracle: PublicKey): TransactionInstruction => {
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode({ instruction: LendingInstruction.RefreshReserve }, data);

const keys = [
{ pubkey: reserve, isSigner: false, isWritable: true },
Expand Down
8 changes: 4 additions & 4 deletions token-lending/js/src/instructions/repayObligationLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface Data {
liquidityAmount: bigint;
}

const DataLayout = struct<Data>([u8('instruction'), u64('liquidityAmount')]);

export const repayObligationLiquidityInstruction = (
liquidityAmount: number | bigint,
sourceLiquidity: PublicKey,
Expand All @@ -19,10 +21,8 @@ export const repayObligationLiquidityInstruction = (
lendingMarket: PublicKey,
transferAuthority: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction'), u64('liquidityAmount')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: LendingInstruction.RepayObligationLiquidity,
liquidityAmount: BigInt(liquidityAmount),
Expand Down
8 changes: 4 additions & 4 deletions token-lending/js/src/instructions/setLendingMarketOwner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ interface Data {
newOwner: PublicKey;
}

const DataLayout = struct<Data>([u8('instruction'), publicKey('newOwner')]);

export const setLendingMarketOwnerInstruction = (
newOwner: PublicKey,
lendingMarket: PublicKey,
currentOwner: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction'), publicKey('newOwner')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: LendingInstruction.SetLendingMarketOwner,
newOwner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface Data {
collateralAmount: bigint;
}

const DataLayout = struct<Data>([u8('instruction'), u64('collateralAmount')]);

export const withdrawObligationCollateralInstruction = (
collateralAmount: number | bigint,
sourceCollateral: PublicKey,
Expand All @@ -20,10 +22,8 @@ export const withdrawObligationCollateralInstruction = (
lendingMarketAuthority: PublicKey,
obligationOwner: PublicKey
): TransactionInstruction => {
const dataLayout = struct<Data>([u8('instruction'), u64('collateralAmount')]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: LendingInstruction.WithdrawObligationCollateral,
collateralAmount: BigInt(collateralAmount),
Expand Down
1 change: 1 addition & 0 deletions token-lending/js/src/state/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './lastUpdate';
export * from './lendingMarket';
export * from './reserve';
export * from './obligation';
4 changes: 3 additions & 1 deletion token-lending/js/src/state/lendingMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ export const LendingMarketLayout = struct<LendingMarket>(
'lendingMarket'
);

export const LENDING_MARKET_SIZE = LendingMarketLayout.span;

export const isLendingMarket = (info: AccountInfo<Buffer>): boolean => {
return info.data.length === LendingMarketLayout.span;
return info.data.length === LENDING_MARKET_SIZE;
};

export const parseLendingMarket: Parser<LendingMarket> = (pubkey: PublicKey, info: AccountInfo<Buffer>) => {
Expand Down
4 changes: 3 additions & 1 deletion token-lending/js/src/state/obligation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ export const ObligationLayout = struct<ObligationDataFlat>(
'obligation'
);

export const OBLIGATION_SIZE = ObligationLayout.span;

export const isObligation = (info: AccountInfo<Buffer>): boolean => {
return info.data.length === ObligationLayout.span;
return info.data.length === OBLIGATION_SIZE;
};

export const parseObligation: Parser<Obligation> = (pubkey: PublicKey, info: AccountInfo<Buffer>) => {
Expand Down
4 changes: 3 additions & 1 deletion token-lending/js/src/state/reserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ export const ReserveLayout = struct<Reserve>([
blob(248, 'padding'),
]);

export const RESERVE_SIZE = ReserveLayout.span;

export const isReserve = (info: AccountInfo<Buffer>): boolean => {
return info.data.length === ReserveLayout.span;
return info.data.length === RESERVE_SIZE;
};

export const parseReserve: Parser<Reserve> = (pubkey: PublicKey, info: AccountInfo<Buffer>) => {
Expand Down
3 changes: 1 addition & 2 deletions token-lending/js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"compilerOptions": {
"target": "esnext",
"noEmit": true,
"noEmitOnError": true,
"emitDeclarationOnly": true,
"outDir": "lib",
"typeRoots": ["node_modules/@types", "types"],
"module": "esnext",
Expand Down
4 changes: 4 additions & 0 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ fn process_init_reserve(
msg!("Borrow fee must be in range [0, 1_000_000_000_000_000_000)");
return Err(LendingError::InvalidConfig.into());
}
if config.fees.flash_loan_fee_wad >= WAD {
msg!("Flash loan fee must be in range [0, 1_000_000_000_000_000_000)");
return Err(LendingError::InvalidConfig.into());
}
if config.fees.host_fee_percentage > 100 {
msg!("Host fee percentage must be in range [0, 100]");
return Err(LendingError::InvalidConfig.into());
Expand Down

0 comments on commit ba0c0e0

Please sign in to comment.