Skip to content

Commit

Permalink
Everlend fixes (solana-labs#1013)
Browse files Browse the repository at this point in the history
* Fix/verif (solana-labs#4)

* fix

* fixes

* hotfix (solana-labs#5)

* update general pool package

* fixes (solana-labs#6)
  • Loading branch information
EvgeniiVoznyuk authored Sep 9, 2022
1 parent 3100cc1 commit 8c88f8c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 71 deletions.
55 changes: 27 additions & 28 deletions Strategies/protocols/everlend/depositTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { GeneralPoolsProgram } from '@everlend/general-pool'
import {
CreateAssociatedTokenAccount,
findAssociatedTokenAccount,
findRegistryPoolConfigAccount,
} from '@everlend/common'
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
Expand Down Expand Up @@ -60,7 +59,7 @@ export type ActionOptions = {
export type ActionResult = {
/** the prepared transaction, ready for signing and sending. */
tx: Transaction
setupTx: Transaction
setupIxs: TransactionInstruction[]
/** the additional key pairs which may be needed for signing and sending transactions. */
keypairs?: Record<string, Keypair>
}
Expand All @@ -86,8 +85,11 @@ export const prepareSolDepositTx = async (
])

const tx = new Transaction()
const setupTx = new Transaction()
const registryPoolConfig = await findRegistryPoolConfigAccount(registry, pool)
const setupIxs: TransactionInstruction[] = []
const poolConfig = await GeneralPoolsProgram.findProgramAddress([
Buffer.from('config'),
pool.toBuffer(),
])

// Wrapping SOL
const depositAccountInfo = await connection.getAccountInfo(source)
Expand All @@ -101,7 +103,7 @@ export const prepareSolDepositTx = async (
payerPublicKey,
payerPublicKey
)
setupTx.add(createAtaInst)
setupIxs.push(createAtaInst)
}

const userWSOLAccountInfo = await connection.getAccountInfo(destination)
Expand All @@ -114,7 +116,7 @@ export const prepareSolDepositTx = async (
lamports: (userWSOLAccountInfo ? 0 : rentExempt) + amount.toNumber(),
})

setupTx.add(transferLamportsIx)
setupIxs.push(transferLamportsIx)

const syncIx = syncNative(source)
tx.add(syncIx)
Expand All @@ -123,21 +125,21 @@ export const prepareSolDepositTx = async (
destination =
destination ?? (await findAssociatedTokenAccount(payerPublicKey, poolMint))
!(await connection.getAccountInfo(destination)) &&
setupTx.add(
setupIxs.push(
new CreateAssociatedTokenAccount(
{ feePayer: payerPublicKey },
{
associatedTokenAddress: destination,
tokenMint: poolMint,
}
)
).instructions[0]
)

tx.add(
new DepositTx(
{ feePayer: payerPublicKey },
{
poolConfig: registryPoolConfig,
poolConfig,
poolMarket,
pool,
source,
Expand All @@ -154,7 +156,7 @@ export const prepareSolDepositTx = async (
)
)

return { tx, setupTx }
return { tx, setupIxs }
}

export async function handleEverlendAction(
Expand Down Expand Up @@ -187,7 +189,7 @@ export async function handleEverlendAction(
const REGISTRY = new PublicKey(isMainnet ? REGISTRY_MAIN : REGISTRY_DEV)
const CONFIG = new PublicKey(isMainnet ? CONFIG_MAINNET : CONFIG_DEVNET)

const ctokenATA = isSol
const liquidityATA = isSol
? await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
Expand All @@ -197,7 +199,7 @@ export async function handleEverlendAction(
)
: matchedTreasury.pubkey

const liquidityATA = await Token.getAssociatedTokenAddress(
const ctokenATA = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
new PublicKey(form.poolMint),
Expand All @@ -222,7 +224,7 @@ export async function handleEverlendAction(
const cleanupInsts: InstructionDataWithHoldUpTime[] = []

if (form.action === 'Deposit') {
const { actionTx, initMiningTx } = await handleEverlendDeposit(
const { actionTx, prerequisiteInstructions } = await handleEverlendDeposit(
wallet!,
Boolean(isSol),
connection,
Expand All @@ -233,8 +235,8 @@ export async function handleEverlendAction(
rewardAccount,
form.poolPubKey,
form.bnAmount,
ctokenATA,
liquidityATA
liquidityATA,
ctokenATA
)
actionTx.instructions.map((instruction) => {
insts.push({
Expand All @@ -248,8 +250,8 @@ export async function handleEverlendAction(
})
})

if (initMiningTx.instructions.length) {
initMiningTx.instructions.map((instruction) => {
if (prerequisiteInstructions) {
prerequisiteInstructions.map((instruction) => {
setupInsts.push({
data: getInstructionDataFromBase64(
serializeInstructionToBase64(instruction)
Expand All @@ -272,8 +274,8 @@ export async function handleEverlendAction(
rewardAccount,
form.poolPubKey,
form.bnAmount,
liquidityATA,
ctokenATA
ctokenATA,
liquidityATA
)

withdrawTx.instructions.map((instruction) => {
Expand All @@ -291,9 +293,6 @@ export async function handleEverlendAction(

const proposalsAdresses: PublicKey[] = []

// eslint-disable-next-line no-debugger
debugger

const proposalAddress = await createProposal(
rpcContext,
realm,
Expand Down Expand Up @@ -328,7 +327,7 @@ export async function handleEverlendDeposit(
) {
const actionTx = new Transaction()
const initMiningTx = new Transaction()

const prerequisiteInstructions: TransactionInstruction[] = []
const rewardPoolInfo = await connection.current.getAccountInfo(rewardPool)
const rewardAccountInfo = await connection.current.getAccountInfo(
rewardAccount
Expand All @@ -344,11 +343,11 @@ export async function handleEverlendDeposit(
wallet,
CONFIG
)

initMiningTx.add(initTx)
prerequisiteInstructions.push(...initTx.instructions)
}
if (isSol) {
const { tx: depositTx, setupTx } = await prepareSolDepositTx(
const { tx: depositTx, setupIxs } = await prepareSolDepositTx(
{ connection: connection.current, payerPublicKey: owner },
new PublicKey(poolPubKey),
REGISTRY,
Expand All @@ -361,7 +360,7 @@ export async function handleEverlendDeposit(
destination
)
actionTx.add(depositTx)
initMiningTx.add(setupTx)
prerequisiteInstructions.push(...setupIxs)
} else {
const { tx: depositTx } = await prepareDepositTx(
{ connection: connection.current, payerPublicKey: owner },
Expand All @@ -376,7 +375,7 @@ export async function handleEverlendDeposit(
actionTx.add(depositTx)
}

return { actionTx, initMiningTx }
return { actionTx, initMiningTx, prerequisiteInstructions }
}

export async function handleEverlendWithdraw(
Expand Down Expand Up @@ -404,7 +403,7 @@ export async function handleEverlendWithdraw(
rewardPool,
rewardAccount,
source,
isSol ? owner : undefined
isSol ? owner : destination
)
const withdrawTx = withdrawslTx
let closeIx: TransactionInstruction | undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import useGovernanceAssets from '@hooks/useGovernanceAssets'
import { handleEverlendDeposit } from 'Strategies/protocols/everlend/depositTools'
import { getEverlendStrategies } from 'Strategies/protocols/everlend/tools'
import {
CONFIG_DEVNET,
CONFIG_MAINNET,
REGISTRY_DEV,
REGISTRY_MAIN,
REWARD_PROGRAM_ID,
} from 'Strategies/protocols/everlend/constants'
Expand All @@ -46,10 +48,6 @@ const DepositForm = ({
const [stratagies, setStratagies] = useState<any>([])

const { assetAccounts } = useGovernanceAssets()
// Hardcoded gate used to be clear about what cluster is supported for now
if (connection.cluster !== 'mainnet') {
return <>This instruction does not support {connection.cluster}</>
}

useEffect(() => {
const fetchStratagies = async () => {
Expand Down Expand Up @@ -99,14 +97,15 @@ const DepositForm = ({
governance: form.governedAccount?.governance,
}
}

const owner = form.governedAccount.governance.owner
const isSol = form.governedAccount.isSol
const owner = isSol
? form.governedAccount.pubkey
: form.governedAccount.extensions!.token!.account.owner

const REGISTRY = new PublicKey(REGISTRY_MAIN)
const CONFIG = new PublicKey(CONFIG_MAINNET)
const isDev = connection.cluster === 'devnet'

console.log(stratagies)
const REGISTRY = new PublicKey(isDev ? REGISTRY_DEV : REGISTRY_MAIN)
const CONFIG = new PublicKey(isDev ? CONFIG_DEVNET : CONFIG_MAINNET)

const matchedStratagie = stratagies.find(
(el) =>
Expand All @@ -129,23 +128,28 @@ const DepositForm = ({
REWARD_PROGRAM_ID
)

const ctokenATA = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
new PublicKey(matchedStratagie.handledMint),
owner,
true
)
const liquidityATA = isSol
? await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
new PublicKey(matchedStratagie.handledMint),
owner,
true
)
: form.governedAccount.extensions!.token!.account.address

const liquidityATA = await Token.getAssociatedTokenAddress(
const ctokenATA = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
new PublicKey(matchedStratagie.poolMint),
owner,
true
)

const { actionTx: tx } = await handleEverlendDeposit(
const {
actionTx: tx,
prerequisiteInstructions,
} = await handleEverlendDeposit(
wallet,
Boolean(isSol),
connection,
Expand All @@ -159,14 +163,28 @@ const DepositForm = ({
+form.uiAmount as number,
form.governedAccount.extensions.mint!.account.decimals
),
ctokenATA,
liquidityATA
liquidityATA,
ctokenATA
)

tx.instructions.forEach((inst, index) => {
if (index < tx.instructions.length - 1) {
prerequisiteInstructions.push(inst)
}
})

const additionalSerializedIxs = prerequisiteInstructions.map((inst) =>
serializeInstructionToBase64(inst)
)

return {
serializedInstruction: serializeInstructionToBase64(tx.instructions[0]),
serializedInstruction: serializeInstructionToBase64(
tx.instructions[tx.instructions.length - 1]
),
additionalSerializedInstructions: additionalSerializedIxs,
isValid: true,
governance: form.governedAccount.governance,
shouldSplitIntoSeparateTxs: true,
}
}

Expand Down
Loading

0 comments on commit 8c88f8c

Please sign in to comment.