Skip to content

Commit

Permalink
send nft to vaults (#1894)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrzezinski94 authored Oct 27, 2023
1 parent 6a1c4cf commit fa694ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ import EmptyWallet from '@utils/Mango/listingTools'
import { Keypair, PublicKey, TransactionInstruction } from '@solana/web3.js'
import { tryGetTokenAccount } from '@utils/tokens'
import Button from '@components/Button'
import { TOKEN_PROGRAM_ID, Token, u64 } from '@solana/spl-token'
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
Token,
u64,
} from '@solana/spl-token'
import Input from '@components/inputs/Input'
import { parseMintNaturalAmountFromDecimal } from '@tools/sdk/units'
import { validateInstruction } from '@utils/instructionTools'
import useGovernanceNfts from '@components/treasuryV2/WalletList/WalletListItem/AssetList/useGovernanceNfts'

interface FillVaultsForm {
governedAccount: AssetAccount | null
Expand All @@ -37,6 +43,7 @@ type Vault = {
amount: bigint
mintIndex: number
mint: PublicKey
type: string
}

type Transfer = {
Expand Down Expand Up @@ -69,6 +76,7 @@ const FillVaults = ({
const [vaults, setVaults] = useState<{ [pubkey: string]: Vault }>()
const [formErrors, setFormErrors] = useState({})
const { handleSetInstructions } = useContext(NewProposalContext)
const nfts = useGovernanceNfts(form.governedAccount?.governance.pubkey)

const schema = useMemo(
() =>
Expand Down Expand Up @@ -131,6 +139,8 @@ const FillVaults = ({
const v: any = {}
for (let i = 0; i < distribution.metadata!.mints.length; i++) {
const mint = distribution.metadata!.mints[i]
const type = distribution.metadata!.mints[i].properties.type

const vaultAddress = distribution.findVaultAddress(
new PublicKey(mint.address)
)
Expand All @@ -145,13 +155,15 @@ const FillVaults = ({
amount: tokenAccount?.account.amount,
mint: tokenAccount?.account.mint,
mintIndex: i,
type: type,
}
} catch {
v[vaultAddress.toString()] = { amount: -1, mintIndex: i }
}
}
setVaults(v)
}

useEffect(() => {
if (distribution) {
fetchVaults()
Expand All @@ -170,22 +182,35 @@ const FillVaults = ({
useEffect(() => {
if (vaults && form.governedAccount) {
const trans = Object.values(vaults).map((v) => {
const from = assetAccounts.find(
const isToken = v.type.toLowerCase() === 'token'
const fromToken = assetAccounts.find(
(assetAccount) =>
assetAccount.isToken &&
assetAccount.extensions.mint?.publicKey.equals(v.mint) &&
assetAccount.extensions.token?.account.owner.equals(
form.governedAccount!.extensions.transferAddress!
)
)
if (!from) {
const fromNft = nfts?.find((x) => x.id === v.mint.toBase58())

if (!fromToken && !fromNft) {
return undefined
}

return {
from: from!.pubkey,
from: isToken
? fromToken!.pubkey
: PublicKey.findProgramAddressSync(
[
new PublicKey(fromNft!.ownership.owner).toBuffer(),
TOKEN_PROGRAM_ID.toBuffer(),
new PublicKey(fromNft!.id).toBuffer(),
],
ASSOCIATED_TOKEN_PROGRAM_ID
)[0],
to: v.publicKey,
amount: '',
decimals: from!.extensions.mint!.account.decimals,
decimals: isToken ? fromToken!.extensions.mint!.account.decimals : 0,
mintIndex: v.mintIndex,
}
})
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6313,12 +6313,12 @@ ansi-escapes@^4.2.1:
dependencies:
type-fest "^0.21.3"

[email protected]:
[email protected], ansi-regex@^2.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-3.0.1.tgz#123d6479e92ad45ad897d4054e3c7ca7db4944e1"
integrity sha512-+O9Jct8wf++lXxxFc4hc8LsjaSq0HFzzL7cVsw8pRDIPdjKD2mT4ytDZlLuSBZ4cLKZFXIrMGO7DbQCtMJJMKw==

"ansi-regex@>=3.0.1 <=5.0.1", ansi-regex@^2.0.0, ansi-regex@^3.0.0, ansi-regex@^4.1.0, ansi-regex@^5.0.0, ansi-regex@^5.0.1:
"ansi-regex@>=3.0.1 <=5.0.1", ansi-regex@^3.0.0, ansi-regex@^4.1.0, ansi-regex@^5.0.0, ansi-regex@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304"
integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==
Expand Down

1 comment on commit fa694ca

@vercel
Copy link

@vercel vercel bot commented on fa694ca Oct 27, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

governance-ui – ./

governance-ui-solana-labs.vercel.app
governance-ui-git-main-solana-labs.vercel.app
app.realms.today

Please sign in to comment.