From 7d224d0848aa86b2bc8f4f5954954f425e200b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Brzezi=C5=84ski?= Date: Wed, 22 Nov 2023 22:27:33 +0100 Subject: [PATCH] add token ref account creation to listing (#1943) --- package.json | 1 + .../Mango/MangoV4/TokenRegister.tsx | 22 +++++++++++ .../Mango/MangoV4/TokenRegisterTrustless.tsx | 22 +++++++++++ tools/constants.ts | 5 +++ yarn.lock | 38 ++++++++++++------- 5 files changed, 74 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index a5239c35e2..94a55949bb 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pages/dao/[symbol]/proposal/components/instructions/Mango/MangoV4/TokenRegister.tsx b/pages/dao/[symbol]/proposal/components/instructions/Mango/MangoV4/TokenRegister.tsx index 95c31328ae..affbd62584 100644 --- a/pages/dao/[symbol]/proposal/components/instructions/Mango/MangoV4/TokenRegister.tsx +++ b/pages/dao/[symbol]/proposal/components/instructions/Mango/MangoV4/TokenRegister.tsx @@ -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' }, @@ -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 && @@ -121,6 +126,7 @@ const TokenRegister = ({ async function getInstruction(): Promise { const isValid = await validateInstruction() let serializedInstruction = '' + const additionalSerializedInstructions: string[] = [] if ( isValid && form.governedAccount?.governance?.account && @@ -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 = { diff --git a/pages/dao/[symbol]/proposal/components/instructions/Mango/MangoV4/TokenRegisterTrustless.tsx b/pages/dao/[symbol]/proposal/components/instructions/Mango/MangoV4/TokenRegisterTrustless.tsx index ca37222482..b8ea0de3ea 100644 --- a/pages/dao/[symbol]/proposal/components/instructions/Mango/MangoV4/TokenRegisterTrustless.tsx +++ b/pages/dao/[symbol]/proposal/components/instructions/Mango/MangoV4/TokenRegisterTrustless.tsx @@ -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 @@ -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({ governedAccount: null, @@ -62,6 +66,7 @@ const TokenRegisterTrustless = ({ async function getInstruction(): Promise { const isValid = await validateInstruction() let serializedInstruction = '' + const additionalSerializedInstructions: string[] = [] if ( isValid && form.governedAccount?.governance?.account && @@ -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, diff --git a/tools/constants.ts b/tools/constants.ts index a5aaae979f..225e808265 100644 --- a/tools/constants.ts +++ b/tools/constants.ts @@ -1,3 +1,4 @@ +import { PublicKey } from '@solana/web3.js' import BN from 'bn.js' /** @@ -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' +) diff --git a/yarn.lock b/yarn.lock index 9783320a9c..842c6d71d5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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/anchor@0.28.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" @@ -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" @@ -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" @@ -1939,6 +1939,16 @@ "@json-rpc-tools/types" "^1.7.6" "@pedrouid/environment" "^1.0.1" +"@jup-ag/referral-sdk@0.1.5": + 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" @@ -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== @@ -4608,7 +4618,7 @@ "@wallet-standard/app" "^1.0.1" "@wallet-standard/base" "^1.0.1" -"@solana/web3.js@1.56.0", "@solana/web3.js@1.73.3", "@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/web3.js@1.56.0", "@solana/web3.js@1.73.3", "@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== @@ -6186,12 +6196,12 @@ ansi-escapes@^4.2.1: dependencies: type-fest "^0.21.3" -ansi-regex@3.0.1: +ansi-regex@3.0.1, 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==