From 9c1baa9b0556252b4b2c0f6163a0566529390793 Mon Sep 17 00:00:00 2001 From: NeOMakinG <14963751+NeOMakinG@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:43:51 +0100 Subject: [PATCH] fix: add address lookup table --- package.json | 2 +- .../src/solana/SolanaChainAdapter.ts | 29 +++++++++++++++++++ packages/chain-adapters/src/solana/types.ts | 1 + .../src/swappers/JupiterSwapper/endpoints.ts | 1 + .../swapperApi/getTradeQuote.ts | 1 + .../swappers/JupiterSwapper/utils/helpers.ts | 3 ++ yarn.lock | 8 ++--- 7 files changed, 40 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index b9215efdac6..facbc73c227 100644 --- a/package.json +++ b/package.json @@ -103,7 +103,7 @@ "@shapeshiftoss/hdwallet-metamask-multichain": "1.55.12", "@shapeshiftoss/hdwallet-native": "1.55.12", "@shapeshiftoss/hdwallet-native-vault": "1.55.12", - "@shapeshiftoss/hdwallet-phantom": "1.55.12", + "@shapeshiftoss/hdwallet-phantom": "../hdwallet/packages/hdwallet-phantom", "@shapeshiftoss/hdwallet-walletconnectv2": "1.55.12", "@shapeshiftoss/swapper": "workspace:^", "@shapeshiftoss/types": "workspace:^", diff --git a/packages/chain-adapters/src/solana/SolanaChainAdapter.ts b/packages/chain-adapters/src/solana/SolanaChainAdapter.ts index a28c666b62f..70296c723ba 100644 --- a/packages/chain-adapters/src/solana/SolanaChainAdapter.ts +++ b/packages/chain-adapters/src/solana/SolanaChainAdapter.ts @@ -22,6 +22,7 @@ import { } from '@solana/spl-token' import type { TransactionInstruction } from '@solana/web3.js' import { + AddressLookupTableAccount, ComputeBudgetProgram, Connection, PublicKey, @@ -236,6 +237,10 @@ export class ChainAdapter implements IChainAdapter ) } + const addressLookupTableAccounts = await this.getAddressLookupTableAccounts( + chainSpecific.addressLookupTableAccounts ?? [], + ) + const txToSign: SignTx = { addressNList: toAddressNList(this.getBIP44Params({ accountNumber })), blockHash: blockhash, @@ -244,6 +249,7 @@ export class ChainAdapter implements IChainAdapter instructions, to: tokenId ? '' : to, value: tokenId ? '' : value, + addressLookupTableAccounts, } return { txToSign } @@ -540,4 +546,27 @@ export class ChainAdapter implements IChainAdapter get httpProvider(): unchained.solana.Api { return this.providers.http } + + public async getAddressLookupTableAccounts( + addresses: string[], + ): Promise { + const addressLookupTableAccountInfos = await this.connection.getMultipleAccountsInfo( + addresses.map(key => new PublicKey(key)), + ) + + return addressLookupTableAccountInfos.reduce((acc, accountInfo, index) => { + const addressLookupTableAddress = addresses[index] + if (accountInfo) { + const addressLookupTableAccount = new AddressLookupTableAccount({ + key: new PublicKey(addressLookupTableAddress), + state: AddressLookupTableAccount.deserialize( + new Uint8Array(Buffer.from(accountInfo.data)), + ), + }) + acc.push(addressLookupTableAccount) + } + + return acc + }, new Array()) + } } diff --git a/packages/chain-adapters/src/solana/types.ts b/packages/chain-adapters/src/solana/types.ts index a898f36d669..e20fae7dbcd 100644 --- a/packages/chain-adapters/src/solana/types.ts +++ b/packages/chain-adapters/src/solana/types.ts @@ -25,6 +25,7 @@ export type BuildTxInput = { computeUnitPrice?: string tokenId?: string instructions?: SolanaTxInstruction[] + addressLookupTableAccounts?: string[] } export type GetFeeDataInput = { diff --git a/packages/swapper/src/swappers/JupiterSwapper/endpoints.ts b/packages/swapper/src/swappers/JupiterSwapper/endpoints.ts index 88a1f6d85f7..f7417542ae2 100644 --- a/packages/swapper/src/swappers/JupiterSwapper/endpoints.ts +++ b/packages/swapper/src/swappers/JupiterSwapper/endpoints.ts @@ -72,6 +72,7 @@ export const jupiterApi: SwapperApi = { value: '0', accountNumber: step.accountNumber, chainSpecific: { + addressLookupTableAccounts: swapResponse.addressLookupTableAddresses, instructions: [...computeBudgetInstructions, ...setupInstructions, swapInstruction], }, } diff --git a/packages/swapper/src/swappers/JupiterSwapper/swapperApi/getTradeQuote.ts b/packages/swapper/src/swappers/JupiterSwapper/swapperApi/getTradeQuote.ts index 4b189175c30..21b151ca549 100644 --- a/packages/swapper/src/swappers/JupiterSwapper/swapperApi/getTradeQuote.ts +++ b/packages/swapper/src/swappers/JupiterSwapper/swapperApi/getTradeQuote.ts @@ -71,6 +71,7 @@ const _getTradeQuote = async ( apiUrl: jupiterUrl, sourceAsset: sellAsset.assetId === solAssetId ? wrappedSolAssetId : sellAsset.assetId, destinationAsset: buyAsset.assetId === solAssetId ? wrappedSolAssetId : buyAsset.assetId, + toAddress: receiveAddress, commissionBps: affiliateBps, amount: sellAmount, slippageBps: convertDecimalPercentageToBasisPoints( diff --git a/packages/swapper/src/swappers/JupiterSwapper/utils/helpers.ts b/packages/swapper/src/swappers/JupiterSwapper/utils/helpers.ts index e95f3737dfe..8de4d0d125a 100644 --- a/packages/swapper/src/swappers/JupiterSwapper/utils/helpers.ts +++ b/packages/swapper/src/swappers/JupiterSwapper/utils/helpers.ts @@ -22,6 +22,7 @@ type GetJupiterQuoteArgs = { commissionBps: string amount: string slippageBps?: string + toAddress: string } type GetJupiterSwapArgs = { @@ -34,6 +35,7 @@ export const getJupiterQuote = ({ apiUrl, sourceAsset, destinationAsset, + toAddress, commissionBps, amount, slippageBps, @@ -41,6 +43,7 @@ export const getJupiterQuote = ({ jupiterService.get( `${apiUrl}/quote` + `?inputMint=${fromAssetId(sourceAsset).assetReference}` + + `&destinationTokenAccount=${toAddress}` + `&outputMint=${fromAssetId(destinationAsset).assetReference}` + `&amount=${amount}` + `&slippageBps=${slippageBps}` + diff --git a/yarn.lock b/yarn.lock index f2b4532b81f..996cd3c8a5c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11451,9 +11451,9 @@ __metadata: languageName: node linkType: hard -"@shapeshiftoss/hdwallet-phantom@npm:1.55.12": +"@shapeshiftoss/hdwallet-phantom@file:../hdwallet/packages/hdwallet-phantom::locator=%40shapeshiftoss%2Fweb%40workspace%3A.": version: 1.55.12 - resolution: "@shapeshiftoss/hdwallet-phantom@npm:1.55.12" + resolution: "@shapeshiftoss/hdwallet-phantom@file:../hdwallet/packages/hdwallet-phantom#../hdwallet/packages/hdwallet-phantom::hash=31dd14&locator=%40shapeshiftoss%2Fweb%40workspace%3A." dependencies: "@shapeshiftoss/bitcoinjs-lib": 5.2.0-shapeshift.2 "@shapeshiftoss/hdwallet-core": 1.55.12 @@ -11462,7 +11462,7 @@ __metadata: bitcoinjs-message: ^2.0.0 ethers: 5.7.2 lodash: ^4.17.21 - checksum: eeaffafd3ccfb06b08349b5f45a28d0ce2b2c3677b625ff2dea29dd148b637074875c523acf7595e353868f18f9425af304ea0643de240682bc8802efdb23911 + checksum: 1e79337defbaee14298d6f51f4d79072881ac59199d07b11943bde1c53fdb579a7fd41bf39c3423e11679386f3228864b7343011ae99feb22de05df9d8367c44 languageName: node linkType: hard @@ -11682,7 +11682,7 @@ __metadata: "@shapeshiftoss/hdwallet-metamask-multichain": 1.55.12 "@shapeshiftoss/hdwallet-native": 1.55.12 "@shapeshiftoss/hdwallet-native-vault": 1.55.12 - "@shapeshiftoss/hdwallet-phantom": 1.55.12 + "@shapeshiftoss/hdwallet-phantom": ../hdwallet/packages/hdwallet-phantom "@shapeshiftoss/hdwallet-walletconnectv2": 1.55.12 "@shapeshiftoss/swapper": "workspace:^" "@shapeshiftoss/types": "workspace:^"