Skip to content

Commit

Permalink
fix(transaction execute): fix error with signer verif (#998)
Browse files Browse the repository at this point in the history
* Fix/verif (#4)

* fix

* fixes

* hotfix (#5)
  • Loading branch information
EvgeniiVoznyuk committed Aug 30, 2022
1 parent 114cede commit ee795dc
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 110 deletions.
33 changes: 22 additions & 11 deletions Strategies/components/EverlendModalContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ButtonGroup from '@components/ButtonGroup'
import { useEffect, useState } from 'react'
import { TreasuryStrategy } from 'Strategies/types/types'
import {
calcUserTokenBalanceByPoolToken,
CreateEverlendProposal,
lamportsToSol,
} from 'Strategies/protocols/everlend/tools'
Expand All @@ -18,7 +19,12 @@ enum Tabs {
}

interface IProps {
proposedInvestment: TreasuryStrategy & { poolMint: string }
proposedInvestment: TreasuryStrategy & {
poolMint: string
rateEToken: number
decimals: number
poolPubKey: string
}
handledMint: string
createProposalFcn: CreateEverlendProposal
governedTokenAccount: AssetAccount
Expand All @@ -41,40 +47,45 @@ const EverlendModalContent = ({
? governedTokenAccount!.pubkey
: governedTokenAccount!.extensions!.token!.account.owner

console.log('proposed', governedTokenAccount!.pubkey.toString())

useEffect(() => {
const loadMaxAmount = async () => {
const tokenMintATA = await findAssociatedTokenAccount(
console.log('owner', owner.toString())
const poolMintATA = await findAssociatedTokenAccount(
owner,
new PublicKey(proposedInvestment.poolMint)
)
console.log('tokenmintata', poolMintATA.toString())
let poolMintATABalance = 0
let tokenMintATABalance = 0
try {
const fetchedTokenMintATABalance = await connection.current.getTokenAccountBalance(
tokenMintATA
poolMintATA
)

poolMintATABalance = calcUserTokenBalanceByPoolToken(
Number(fetchedTokenMintATABalance.value.uiAmount),
proposedInvestment.decimals,
proposedInvestment.rateEToken,
false
)
tokenMintATABalance = Number(fetchedTokenMintATABalance.value.uiAmount)
} catch (e) {
console.log(e)
}
try {
if (isSol) {
const fetchedBalance = await connection.current.getBalance(owner)
poolMintATABalance = lamportsToSol(fetchedBalance)
console.log(owner.toString())
tokenMintATABalance = lamportsToSol(fetchedBalance)
} else {
const fetchedBalance = await connection.current.getTokenAccountBalance(
governedTokenAccount!.pubkey
)
poolMintATABalance = Number(fetchedBalance.value.uiAmount)
tokenMintATABalance = Number(fetchedBalance.value.uiAmount)
}
} catch (e) {
console.log(e)
}
setDepositedAmount(tokenMintATABalance)
setMaxDepositAmount(poolMintATABalance)
setDepositedAmount(poolMintATABalance)
setMaxDepositAmount(tokenMintATABalance)
}
loadMaxAmount()
}, [proposedInvestment, handledMint])
Expand Down
2 changes: 1 addition & 1 deletion Strategies/components/everlend/EverlendDeposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const EverlendDeposit = ({
description: proposalInfo.description,
amountFmt: String(amount),
bnAmount: getMintNaturalAmountFromDecimalAsBN(
amount as number,
amount,
governedTokenAccount.extensions.mint!.account.decimals
),
action: 'Deposit',
Expand Down
18 changes: 15 additions & 3 deletions Strategies/components/everlend/EverlendWithdraw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ import { precision } from '@utils/formatting'
import { validateInstruction } from '@utils/instructionTools'
import useGovernanceAssets from '@hooks/useGovernanceAssets'
import Loading from '@components/Loading'
import { TreasuryStrategy } from '../../types/types'

interface IProps {
proposedInvestment
proposedInvestment: TreasuryStrategy & {
poolMint: string
rateEToken: number
decimals: number
poolPubKey: string
}
handledMint: string
createProposalFcn: CreateEverlendProposal
governedTokenAccount: AssetAccount
Expand Down Expand Up @@ -108,14 +114,20 @@ const EverlendWithdraw = ({
? realm!.account.config.councilMint
: undefined

const amountToRate = Number(
(amount * proposedInvestment.rateEToken).toFixed(
proposedInvestment.decimals
)
)

const proposalAddress = await createProposalFcn(
rpcContext,
{
title: proposalInfo.title || proposalTitle,
description: proposalInfo.description,
amountFmt: String(amount),
bnAmount: getMintNaturalAmountFromDecimalAsBN(
amount as number,
amountToRate,
governedTokenAccount.extensions.mint!.account.decimals
),
action: 'Withdraw',
Expand Down Expand Up @@ -207,7 +219,7 @@ const EverlendWithdraw = ({
</span>
</div>
<div className="flex justify-between">
<span className="text-fgd-3">Proposed Deposit</span>
<span className="text-fgd-3">Proposed Withdraw</span>
<span className="font-bold text-fgd-1">
{amount?.toLocaleString() || (
<span className="font-normal text-red">Enter an amount</span>
Expand Down
74 changes: 32 additions & 42 deletions Strategies/protocols/everlend/depositTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
} from './constants'
import { getInitMiningTx } from './useRewardProgram'
import { SignerWalletAdapter } from '@solana/wallet-adapter-base'
import { syncNative } from '@solendprotocol/solend-sdk'

export type ActionOptions = {
/** the JSON RPC connection instance. */
Expand All @@ -59,6 +60,7 @@ export type ActionOptions = {
export type ActionResult = {
/** the prepared transaction, ready for signing and sending. */
tx: Transaction
setupTx: Transaction
/** the additional key pairs which may be needed for signing and sending transactions. */
keypairs?: Record<string, Keypair>
}
Expand All @@ -84,6 +86,7 @@ export const prepareSolDepositTx = async (
])

const tx = new Transaction()
const setupTx = new Transaction()
const registryPoolConfig = await findRegistryPoolConfigAccount(registry, pool)

// Wrapping SOL
Expand All @@ -98,7 +101,7 @@ export const prepareSolDepositTx = async (
payerPublicKey,
payerPublicKey
)
tx.add(createAtaInst)
setupTx.add(createAtaInst)
}

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

tx.add(transferLamportsIx)
setupTx.add(transferLamportsIx)

// const syncIx = syncNative(source)
// tx.add(syncIx)
const syncIx = syncNative(source)
tx.add(syncIx)

// Create destination account for pool mint if doesn't exist
destination =
destination ?? (await findAssociatedTokenAccount(payerPublicKey, poolMint))
!(await connection.getAccountInfo(destination)) &&
tx.add(
setupTx.add(
new CreateAssociatedTokenAccount(
{ feePayer: payerPublicKey },
{
Expand Down Expand Up @@ -152,7 +155,7 @@ export const prepareSolDepositTx = async (
)
)

return { tx }
return { tx, setupTx }
}

export async function handleEverlendAction(
Expand Down Expand Up @@ -246,7 +249,7 @@ export async function handleEverlendAction(
})
})

if (isSol && initMiningTx) {
if (initMiningTx.instructions.length) {
initMiningTx.instructions.map((instruction) => {
setupInsts.push({
data: getInstructionDataFromBase64(
Expand Down Expand Up @@ -289,37 +292,23 @@ export async function handleEverlendAction(

const proposalsAdresses: PublicKey[] = []

if (setupInsts.length) {
const setupProposalAddress = await createProposal(
rpcContext,
realm,
matchedTreasury.governance!.pubkey,
tokenOwnerRecord,
'Setup init mining for Everlend SOL pool',
'You need to sign init mining tx before you create deposit proposal',
governingTokenMint,
proposalIndex,
[...setupInsts],
isDraft,
client
)
proposalsAdresses.push(setupProposalAddress)
} else {
const proposalAddress = await createProposal(
rpcContext,
realm,
matchedTreasury.governance!.pubkey,
tokenOwnerRecord,
form.title,
form.description,
governingTokenMint,
proposalIndex,
[...insts, ...cleanupInsts],
isDraft,
client
)
proposalsAdresses.push(proposalAddress)
}
// eslint-disable-next-line no-debugger
debugger

const proposalAddress = await createProposal(
rpcContext,
realm,
matchedTreasury.governance!.pubkey,
tokenOwnerRecord,
form.title,
form.description,
governingTokenMint,
proposalIndex,
[...setupInsts, ...insts, ...cleanupInsts],
isDraft,
client
)
proposalsAdresses.push(proposalAddress)

return proposalsAdresses
}
Expand Down Expand Up @@ -353,13 +342,14 @@ export async function handleEverlendDeposit(
wallet.publicKey!,
owner,
connection,
wallet
wallet,
CONFIG
)

initMiningTx.add(initTx)
}
if (isSol) {
const { tx: depositTx } = await prepareSolDepositTx(
const { tx: depositTx, setupTx } = await prepareSolDepositTx(
{ connection: connection.current, payerPublicKey: owner },
new PublicKey(poolPubKey),
REGISTRY,
Expand All @@ -372,6 +362,7 @@ export async function handleEverlendDeposit(
destination
)
actionTx.add(depositTx)
initMiningTx.add(setupTx)
} else {
const { tx: depositTx } = await prepareDepositTx(
{ connection: connection.current, payerPublicKey: owner },
Expand All @@ -384,11 +375,10 @@ export async function handleEverlendDeposit(
rewardAccount,
source
)
actionTx.add(initMiningTx)
actionTx.add(depositTx)
}

return isSol ? { actionTx, initMiningTx } : { actionTx }
return { actionTx, initMiningTx }
}

export async function handleEverlendWithdraw(
Expand Down
Loading

1 comment on commit ee795dc

@vercel
Copy link

@vercel vercel bot commented on ee795dc Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.