Skip to content

Commit

Permalink
add token ref account creation to listing (solana-labs#1943)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrzezinski94 authored Nov 22, 2023
1 parent 63ae8d2 commit 7d224d0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"@blockworks-foundation/mango-mints-redemption": "0.0.10",
"@blockworks-foundation/mango-v4": "0.20.3",
"@jup-ag/referral-sdk": "0.1.5",
"@blockworks-foundation/mango-v4-settings": "0.2.18",
"@blockworks-foundation/mangolana": "0.0.1-beta.15",
"@bonfida/spl-name-service": "0.1.47",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import UseMangoV4 from '../../../../../../../../hooks/useMangoV4'
import { toNative } from '@blockworks-foundation/mango-v4'
import { BN } from '@coral-xyz/anchor'
import useWalletOnePointOh from '@hooks/useWalletOnePointOh'
import { ReferralProvider } from '@jup-ag/referral-sdk'
import { JUPITER_REFERRAL_PK } from '@tools/constants'
import useLegacyConnectionContext from '@hooks/useLegacyConnectionContext'

const REDUCE_ONLY_OPTIONS = [
{ value: 0, name: 'Disabled' },
Expand Down Expand Up @@ -68,6 +71,8 @@ const TokenRegister = ({
const wallet = useWalletOnePointOh()
const { mangoClient, mangoGroup, getAdditionalLabelInfo } = UseMangoV4()
const { assetAccounts } = useGovernanceAssets()
const connection = useLegacyConnectionContext()

const solAccounts = assetAccounts.filter(
(x) =>
x.type === AccountType.SOL &&
Expand Down Expand Up @@ -121,6 +126,7 @@ const TokenRegister = ({
async function getInstruction(): Promise<UiInstruction> {
const isValid = await validateInstruction()
let serializedInstruction = ''
const additionalSerializedInstructions: string[] = []
if (
isValid &&
form.governedAccount?.governance?.account &&
Expand Down Expand Up @@ -175,6 +181,22 @@ const TokenRegister = ({
})
.instruction()

const rp = new ReferralProvider(connection.current)

const tx = await rp.initializeReferralTokenAccount({
payerPubKey: form.governedAccount.extensions.transferAddress!,
referralAccountPubKey: JUPITER_REFERRAL_PK,
mint: new PublicKey(form.mintPk),
})
const isExistingAccount =
(await connection.current.getBalance(tx.referralTokenAccountPubKey)) > 1

if (!isExistingAccount) {
additionalSerializedInstructions.push(
...tx.tx.instructions.map((x) => serializeInstructionToBase64(x))
)
}

serializedInstruction = serializeInstructionToBase64(ix)
}
const obj: UiInstruction = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import InstructionForm, { InstructionInput } from '../../FormCreator'
import { InstructionInputType } from '../../inputInstructionType'
import UseMangoV4 from '../../../../../../../../hooks/useMangoV4'
import useWalletOnePointOh from '@hooks/useWalletOnePointOh'
import { ReferralProvider } from '@jup-ag/referral-sdk'
import useLegacyConnectionContext from '@hooks/useLegacyConnectionContext'
import { JUPITER_REFERRAL_PK } from '@tools/constants'

interface TokenRegisterTrustlessForm {
governedAccount: AssetAccount | null
Expand Down Expand Up @@ -42,6 +45,7 @@ const TokenRegisterTrustless = ({
(mangoGroup?.admin &&
x.extensions.transferAddress?.equals(mangoGroup?.admin)))
)
const connection = useLegacyConnectionContext()
const shouldBeGoverned = !!(index !== 0 && governance)
const [form, setForm] = useState<TokenRegisterTrustlessForm>({
governedAccount: null,
Expand All @@ -62,6 +66,7 @@ const TokenRegisterTrustless = ({
async function getInstruction(): Promise<UiInstruction> {
const isValid = await validateInstruction()
let serializedInstruction = ''
const additionalSerializedInstructions: string[] = []
if (
isValid &&
form.governedAccount?.governance?.account &&
Expand All @@ -80,10 +85,27 @@ const TokenRegisterTrustless = ({
})
.instruction()

const rp = new ReferralProvider(connection.current)

const tx = await rp.initializeReferralTokenAccount({
payerPubKey: form.governedAccount.extensions.transferAddress!,
referralAccountPubKey: JUPITER_REFERRAL_PK,
mint: new PublicKey(form.mintPk),
})
const isExistingAccount =
(await connection.current.getBalance(tx.referralTokenAccountPubKey)) > 1

if (!isExistingAccount) {
additionalSerializedInstructions.push(
...tx.tx.instructions.map((x) => serializeInstructionToBase64(x))
)
}

serializedInstruction = serializeInstructionToBase64(ix)
}
const obj: UiInstruction = {
serializedInstruction: serializedInstruction,
additionalSerializedInstructions,
isValid,
governance: form.governedAccount?.governance,
customHoldUpTime: form.holdupTime,
Expand Down
5 changes: 5 additions & 0 deletions tools/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PublicKey } from '@solana/web3.js'
import BN from 'bn.js'

/**
Expand All @@ -16,3 +17,7 @@ export const DEFAULT_NFT_VOTER_PLUGIN =

export const DEFAULT_NFT_VOTER_PLUGIN_V2 =
'GnftVc21v2BRchsRa9dGdrVmJPLZiRHe9j2offnFTZFg'

export const JUPITER_REFERRAL_PK = new PublicKey(
'EV4qhLE2yPKdUPdQ74EWJUn21xT3eGQxG3DRR1g9NNFc'
)
38 changes: 24 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,13 @@
superstruct "^0.15.4"
toml "^3.0.0"

"@coral-xyz/anchor@^0.28.0":
version "0.28.0"
resolved "https://registry.yarnpkg.com/@coral-xyz/anchor/-/anchor-0.28.0.tgz#8345c3c9186a91f095f704d7b90cd256f7e8b2dc"
integrity sha512-kQ02Hv2ZqxtWP30WN1d4xxT4QqlOXYDxmEd3k/bbneqhV3X5QMO4LAtoUFs7otxyivOgoqam5Il5qx81FuI4vw==
"@coral-xyz/[email protected].1-beta.2", "@coral-xyz/anchor@^0.28.1-beta.2":
version "0.28.1-beta.2"
resolved "https://registry.yarnpkg.com/@coral-xyz/anchor/-/anchor-0.28.1-beta.2.tgz#4ddd4b2b66af04407be47cf9524147793ec514a0"
integrity sha512-xreUcOFF8+IQKWOBUrDKJbIw2ftpRVybFlEPVrbSlOBCbreCWrQ5754Gt9cHIcuBDAzearCDiBqzsGQdNgPJiw==
dependencies:
"@coral-xyz/borsh" "^0.28.0"
"@noble/hashes" "^1.3.1"
"@solana/web3.js" "^1.68.0"
base64-js "^1.5.1"
bn.js "^5.1.2"
Expand All @@ -736,19 +737,17 @@
cross-fetch "^3.1.5"
crypto-hash "^1.3.0"
eventemitter3 "^4.0.7"
js-sha256 "^0.9.0"
pako "^2.0.3"
snake-case "^3.0.4"
superstruct "^0.15.4"
toml "^3.0.0"

"@coral-xyz/anchor@^0.28.1-beta.2":
version "0.28.1-beta.2"
resolved "https://registry.yarnpkg.com/@coral-xyz/anchor/-/anchor-0.28.1-beta.2.tgz#4ddd4b2b66af04407be47cf9524147793ec514a0"
integrity sha512-xreUcOFF8+IQKWOBUrDKJbIw2ftpRVybFlEPVrbSlOBCbreCWrQ5754Gt9cHIcuBDAzearCDiBqzsGQdNgPJiw==
"@coral-xyz/anchor@^0.28.0":
version "0.28.0"
resolved "https://registry.yarnpkg.com/@coral-xyz/anchor/-/anchor-0.28.0.tgz#8345c3c9186a91f095f704d7b90cd256f7e8b2dc"
integrity sha512-kQ02Hv2ZqxtWP30WN1d4xxT4QqlOXYDxmEd3k/bbneqhV3X5QMO4LAtoUFs7otxyivOgoqam5Il5qx81FuI4vw==
dependencies:
"@coral-xyz/borsh" "^0.28.0"
"@noble/hashes" "^1.3.1"
"@solana/web3.js" "^1.68.0"
base64-js "^1.5.1"
bn.js "^5.1.2"
Expand All @@ -758,6 +757,7 @@
cross-fetch "^3.1.5"
crypto-hash "^1.3.0"
eventemitter3 "^4.0.7"
js-sha256 "^0.9.0"
pako "^2.0.3"
snake-case "^3.0.4"
superstruct "^0.15.4"
Expand Down Expand Up @@ -1939,6 +1939,16 @@
"@json-rpc-tools/types" "^1.7.6"
"@pedrouid/environment" "^1.0.1"

"@jup-ag/[email protected]":
version "0.1.5"
resolved "https://registry.yarnpkg.com/@jup-ag/referral-sdk/-/referral-sdk-0.1.5.tgz#02a8bc901154659e75a5f31ae9a3e3edd28cd6cd"
integrity sha512-0R/NuBkIj2otpLnatacZqIwVAwL+OIheSlT+a2atAaNuf2WceL4lKZ/EZWm1AVTFhmFd/07HHN9PJ2fma9AMPA==
dependencies:
"@coral-xyz/anchor" "0.28.1-beta.2"
"@solana/spl-token" "0.3.8"
"@solana/web3.js" "^1.77.3"
lodash "^4.17.21"

"@keystonehq/bc-ur-registry-sol@^0.3.1":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@keystonehq/bc-ur-registry-sol/-/bc-ur-registry-sol-0.3.1.tgz#5319c7c4a22cc83bbacfa6fe09aaa6fb21363f24"
Expand Down Expand Up @@ -4140,7 +4150,7 @@
"@solana/buffer-layout-utils" "^0.2.0"
buffer "^6.0.3"

"@solana/spl-token@^0.3.5", "@solana/spl-token@^0.3.6", "@solana/spl-token@^0.3.8":
"@solana/spl-token@0.3.8", "@solana/spl-token@^0.3.5", "@solana/spl-token@^0.3.6", "@solana/spl-token@^0.3.8":
version "0.3.8"
resolved "https://registry.yarnpkg.com/@solana/spl-token/-/spl-token-0.3.8.tgz#8e9515ea876e40a4cc1040af865f61fc51d27edf"
integrity sha512-ogwGDcunP9Lkj+9CODOWMiVJEdRtqHAtX2rWF62KxnnSWtMZtV9rDhTrZFshiyJmxDnRL/1nKE1yJHg4jjs3gg==
Expand Down Expand Up @@ -4608,7 +4618,7 @@
"@wallet-standard/app" "^1.0.1"
"@wallet-standard/base" "^1.0.1"

"@solana/[email protected]", "@solana/[email protected]", "@solana/web3.js@^1.17.0", "@solana/web3.js@^1.21.0", "@solana/web3.js@^1.22.0", "@solana/web3.js@^1.30.2", "@solana/web3.js@^1.31.0", "@solana/web3.js@^1.32.0", "@solana/web3.js@^1.35.1", "@solana/web3.js@^1.36.0", "@solana/web3.js@^1.37.1", "@solana/web3.js@^1.43.4", "@solana/web3.js@^1.44.3", "@solana/web3.js@^1.48.0", "@solana/web3.js@^1.50.1", "@solana/web3.js@^1.56.2", "@solana/web3.js@^1.59.1", "@solana/web3.js@^1.63.0", "@solana/web3.js@^1.63.1", "@solana/web3.js@^1.66.2", "@solana/web3.js@^1.68.0", "@solana/web3.js@^1.73.0", "@solana/web3.js@^1.73.2", "@solana/web3.js@^1.78.2", "@solana/web3.js@^1.78.3":
"@solana/[email protected]", "@solana/[email protected]", "@solana/web3.js@^1.17.0", "@solana/web3.js@^1.21.0", "@solana/web3.js@^1.22.0", "@solana/web3.js@^1.30.2", "@solana/web3.js@^1.31.0", "@solana/web3.js@^1.32.0", "@solana/web3.js@^1.35.1", "@solana/web3.js@^1.36.0", "@solana/web3.js@^1.37.1", "@solana/web3.js@^1.43.4", "@solana/web3.js@^1.44.3", "@solana/web3.js@^1.48.0", "@solana/web3.js@^1.50.1", "@solana/web3.js@^1.56.2", "@solana/web3.js@^1.59.1", "@solana/web3.js@^1.63.0", "@solana/web3.js@^1.63.1", "@solana/web3.js@^1.66.2", "@solana/web3.js@^1.68.0", "@solana/web3.js@^1.73.0", "@solana/web3.js@^1.73.2", "@solana/web3.js@^1.77.3", "@solana/web3.js@^1.78.2", "@solana/web3.js@^1.78.3":
version "1.73.3"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.73.3.tgz#60e6bd68f6f364d4be360b1e0a03a0a68468a029"
integrity sha512-vHRMo589XEIpoujpE2sZZ1aMZvfA1ImKfNxobzEFyMb+H5j6mRRUXfdgWD0qJ0sm11e5BcBC7HPeRXJB+7f3Lg==
Expand Down Expand Up @@ -6186,12 +6196,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

0 comments on commit 7d224d0

Please sign in to comment.