feat: revoke instruction

This commit is contained in:
bartosz-lipinski 2020-12-26 16:36:35 -06:00
parent 29387bb974
commit 51862fcbde
6 changed files with 77 additions and 58 deletions

View File

@ -25,6 +25,7 @@ import {
LendingMarket,
BorrowAmountType,
LendingObligation,
approve,
} from "../models";
import { toLamports } from "../utils/utils";
@ -161,15 +162,13 @@ export const borrow = async (
);
// create approval for transfer transactions
instructions.push(
Token.createApproveInstruction(
TOKEN_PROGRAM_ID,
fromAccount,
authority,
wallet.publicKey,
[],
fromLamports
)
approve(
instructions,
cleanupInstructions,
fromAccount,
authority,
wallet.publicKey,
fromLamports
);
const dexMarketAddress = borrowReserve.info.dexMarketOption

View File

@ -11,14 +11,14 @@ import {
initReserveInstruction,
LendingReserve,
} from "./../models/lending";
import { AccountLayout, Token } from "@solana/spl-token";
import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../constants/ids";
import { AccountLayout } from "@solana/spl-token";
import { LENDING_PROGRAM_ID } from "../constants/ids";
import {
createUninitializedAccount,
ensureSplAccount,
findOrCreateAccountByMint,
} from "./account";
import { TokenAccount } from "../models";
import { approve, TokenAccount } from "../models";
export const deposit = async (
from: TokenAccount,
@ -60,15 +60,13 @@ export const deposit = async (
);
// create approval for transfer transactions
instructions.push(
Token.createApproveInstruction(
TOKEN_PROGRAM_ID,
fromAccount,
authority,
wallet.publicKey,
[],
amountLamports
)
approve(
instructions,
cleanupInstructions,
fromAccount,
authority,
wallet.publicKey,
amountLamports
);
let toAccount: PublicKey;

View File

@ -11,7 +11,7 @@ import { liquidateInstruction } from "./../models/lending/liquidate";
import { AccountLayout, Token } from "@solana/spl-token";
import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../constants/ids";
import { createTempMemoryAccount, ensureSplAccount, findOrCreateAccountByMint } from "./account";
import { LendingMarket, LendingObligation, TokenAccount } from "../models";
import { approve, LendingMarket, LendingObligation, TokenAccount } from "../models";
import { cache, ParsedAccount } from "../contexts/accounts";
export const liquidate = async (
@ -58,15 +58,13 @@ export const liquidate = async (
);
// create approval for transfer transactions
instructions.push(
Token.createApproveInstruction(
TOKEN_PROGRAM_ID,
fromAccount,
authority,
wallet.publicKey,
[],
amountLamports
)
approve(
instructions,
cleanupInstructions,
fromAccount,
authority,
wallet.publicKey,
amountLamports
);
// get destination account

View File

@ -11,7 +11,7 @@ import { repayInstruction } from "./../models/lending/repay";
import { AccountLayout, Token } from "@solana/spl-token";
import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../constants/ids";
import { findOrCreateAccountByMint } from "./account";
import { LendingObligation, TokenAccount } from "../models";
import { approve, LendingObligation, TokenAccount } from "../models";
import { ParsedAccount } from "../contexts/accounts";
export const repay = async (
@ -53,15 +53,13 @@ export const repay = async (
const fromAccount = from.pubkey;
// create approval for transfer transactions
instructions.push(
Token.createApproveInstruction(
TOKEN_PROGRAM_ID,
fromAccount,
authority,
wallet.publicKey,
[],
amountLamports
)
approve(
instructions,
cleanupInstructions,
fromAccount,
authority,
wallet.publicKey,
amountLamports
);
// get destination account
@ -76,15 +74,13 @@ export const repay = async (
);
// create approval for transfer transactions
instructions.push(
Token.createApproveInstruction(
TOKEN_PROGRAM_ID,
obligationToken.pubkey,
authority,
wallet.publicKey,
[],
obligationToken.info.amount.toNumber()
)
approve(
instructions,
cleanupInstructions,
obligationToken.pubkey,
authority,
wallet.publicKey,
obligationToken.info.amount.toNumber()
);
// TODO: add obligation

View File

@ -10,7 +10,7 @@ import { LendingReserve, withdrawInstruction } from "./../models/lending";
import { AccountLayout, Token } from "@solana/spl-token";
import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../constants/ids";
import { findOrCreateAccountByMint } from "./account";
import { TokenAccount } from "../models";
import { approve, TokenAccount } from "../models";
export const withdraw = async (
from: TokenAccount, // CollateralAccount
@ -43,15 +43,13 @@ export const withdraw = async (
const fromAccount = from.pubkey;
// create approval for transfer transactions
instructions.push(
Token.createApproveInstruction(
TOKEN_PROGRAM_ID,
approve(
instructions,
cleanupInstructions,
fromAccount,
authority,
wallet.publicKey,
[],
amountLamports
)
);
// get destination account

View File

@ -1,9 +1,39 @@
import { AccountInfo, PublicKey } from "@solana/web3.js";
import { AccountInfo, PublicKey, TransactionInstruction } from "@solana/web3.js";
import { AccountInfo as TokenAccountInfo } from "@solana/spl-token";
import { AccountInfo as TokenAccountInfo, Token } from "@solana/spl-token";
import { TOKEN_PROGRAM_ID } from "../constants";
export interface TokenAccount {
pubkey: PublicKey;
account: AccountInfo<Buffer>;
info: TokenAccountInfo;
}
export function approve(
instructions: TransactionInstruction[],
cleanupInstructions: TransactionInstruction[],
account: PublicKey,
delegate: PublicKey,
owner: PublicKey,
amount: number,
): void {
const tokenProgram = TOKEN_PROGRAM_ID;
instructions.push(
Token.createApproveInstruction(
tokenProgram,
account,
delegate,
owner,
[],
amount
)
);
cleanupInstructions.push(
Token.createRevokeInstruction(
tokenProgram,
account,
owner,
[]),
);
}