@tetherto/wdk-wallet-solana-gasless 1.0.0-beta.2 → 1.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tetherto/wdk-wallet-solana-gasless",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.3",
4
4
  "description": "A simple package to manage BIP-32 wallets for the solana blockchain, which implement the gasless functionality.",
5
5
  "keywords": [
6
6
  "wdk",
@@ -25,7 +25,7 @@ import { appendTransactionMessageInstruction, appendTransactionMessageInstructio
25
25
  import { KoraClient } from '@solana/kora'
26
26
  import { compileTransaction, getBase64EncodedWireTransaction } from '@solana/transactions'
27
27
  import { findAssociatedTokenPda, getCreateAssociatedTokenIdempotentInstruction, getTransferInstruction, TOKEN_PROGRAM_ADDRESS } from '@solana-program/token'
28
- import { AccountRole, blockhash, createNoopSigner, pipe } from '@solana/kit'
28
+ import { AccountRole, blockhash, createNoopSigner, getU64Decoder, pipe } from '@solana/kit'
29
29
  import { getTransferSolInstruction } from '@solana-program/system'
30
30
 
31
31
  import { ConfigurationError } from './errors.js'
@@ -446,6 +446,7 @@ export default class WalletAccountReadOnlySolanaGasless extends WalletAccountRea
446
446
  * @param {TransactionMessage} transactionMessage - The transaction message to fetch the payment info.
447
447
  * @param {SolanaGaslessWalletPaymasterConfigOverrides} [config] - If set, overrides the given configuration options.
448
448
  * @returns {Promise<GetPaymentInstructionResponse>} The payment info.
449
+ * @throws {Error} If the paymaster payment instruction is not a recognized SPL transfer to the paymaster token account.
449
450
  */
450
451
  async _getTransactionPaymentInfo (transactionMessage, config = {}) {
451
452
  const mergedConfig = { ...this._config, ...config }
@@ -470,6 +471,60 @@ export default class WalletAccountReadOnlySolanaGasless extends WalletAccountRea
470
471
  })
471
472
  }
472
473
 
473
- return { ...payment, payment_instruction: upgradedPaymentInstruction }
474
+ const paymasterTokenAccount = await this._getPaymasterAssociatedTokenAccount(mergedConfig.paymasterToken.address)
475
+ const paymentAmount = this._getPaymentInstructionAmount(upgradedPaymentInstruction, paymasterTokenAccount)
476
+
477
+ return {
478
+ ...payment,
479
+ payment_amount: Number(paymentAmount),
480
+ payment_instruction: upgradedPaymentInstruction
481
+ }
482
+ }
483
+
484
+ /**
485
+ * @protected
486
+ * @param {string} [paymasterTokenAddress] - The paymaster fee token mint.
487
+ * @returns {Promise<string>} The paymaster's associated token account for that mint.
488
+ */
489
+ async _getPaymasterAssociatedTokenAccount (paymasterTokenAddress = this._config.paymasterToken.address) {
490
+ const [paymasterTokenAccount] = await findAssociatedTokenPda({
491
+ mint: address(paymasterTokenAddress),
492
+ owner: address(this._config.paymasterAddress),
493
+ tokenProgram: TOKEN_PROGRAM_ADDRESS
494
+ })
495
+
496
+ return paymasterTokenAccount
497
+ }
498
+
499
+ /**
500
+ * @protected
501
+ * @param {object} instruction - A candidate SPL token instruction.
502
+ * @param {string} paymasterTokenAccount - The paymaster associated token account.
503
+ * @returns {boolean} True if the instruction is an SPL transfer whose destination is the paymaster token account.
504
+ */
505
+ _isPaymentInstruction (instruction, paymasterTokenAccount) {
506
+ const discriminator = instruction.data?.[0]
507
+ if (instruction.programAddress !== TOKEN_PROGRAM_ADDRESS || (discriminator !== 3 && discriminator !== 12)) {
508
+ return false
509
+ }
510
+
511
+ const destinationIndex = discriminator === 12 ? 2 : 1
512
+
513
+ return instruction.accounts?.[destinationIndex]?.address === paymasterTokenAccount
514
+ }
515
+
516
+ /**
517
+ * @protected
518
+ * @param {object} paymentInstruction - The paymaster payment instruction.
519
+ * @param {string} paymasterTokenAccount - The paymaster associated token account.
520
+ * @returns {bigint} The transfer amount encoded in the instruction.
521
+ * @throws {Error} If the instruction is not a recognized SPL transfer to the paymaster token account.
522
+ */
523
+ _getPaymentInstructionAmount (paymentInstruction, paymasterTokenAccount) {
524
+ if (!this._isPaymentInstruction(paymentInstruction, paymasterTokenAccount)) {
525
+ throw new Error('Invalid payment instruction from paymaster.')
526
+ }
527
+
528
+ return BigInt(getU64Decoder().decode(paymentInstruction.data, 1))
474
529
  }
475
530
  }
@@ -20,8 +20,7 @@ import { createKeyPairSignerFromPrivateKeyBytes, partiallySignTransactionMessage
20
20
  import { assertIsFullySignedTransaction, getBase64EncodedWireTransaction, getTransactionDecoder } from '@solana/transactions'
21
21
  import { appendTransactionMessageInstruction, setTransactionMessageFeePayer } from '@solana/transaction-messages'
22
22
  import { address } from '@solana/addresses'
23
- import { AccountRole, decompileTransactionMessageFetchingLookupTables, getBase64Encoder, getCompiledTransactionMessageDecoder, getU64Decoder, pipe } from '@solana/kit'
24
- import { findAssociatedTokenPda, TOKEN_PROGRAM_ADDRESS } from '@solana-program/token'
23
+ import { AccountRole, decompileTransactionMessageFetchingLookupTables, getBase64Encoder, getCompiledTransactionMessageDecoder, pipe } from '@solana/kit'
25
24
 
26
25
  import WalletAccountReadOnlySolanaGasless from './wallet-account-read-only-solana-gasless.js'
27
26
 
@@ -333,22 +332,14 @@ export default class WalletAccountSolanaGasless extends WalletAccountReadOnlySol
333
332
 
334
333
  const { instructions } = await decompileTransactionMessageFetchingLookupTables(compiledTransactionMessage, this._rpc)
335
334
 
336
- const [paymasterTokenAccount] = await findAssociatedTokenPda({
337
- mint: address(this._config.paymasterToken.address),
338
- owner: address(this._config.paymasterAddress),
339
- tokenProgram: TOKEN_PROGRAM_ADDRESS
340
- })
341
-
342
- const paymentInstruction = instructions.find((instruction) => {
343
- const discriminator = instruction.data?.[0]
344
- const isTokenTransfer = instruction.programAddress === TOKEN_PROGRAM_ADDRESS && (discriminator === 3 || discriminator === 12)
345
-
346
- return isTokenTransfer && instruction.accounts?.some((account) => account.address === paymasterTokenAccount)
347
- })
335
+ const paymasterTokenAccount = await this._getPaymasterAssociatedTokenAccount()
348
336
 
349
- const paymentAmount = paymentInstruction && getU64Decoder().decode(paymentInstruction.data, 1)
337
+ const paymentInstruction = instructions.find((instruction) =>
338
+ this._isPaymentInstruction(instruction, paymasterTokenAccount))
350
339
 
351
- return BigInt(paymentAmount || 0n)
340
+ return paymentInstruction
341
+ ? this._getPaymentInstructionAmount(paymentInstruction, paymasterTokenAccount)
342
+ : 0n
352
343
  }
353
344
 
354
345
  /**
@@ -161,8 +161,30 @@ export default class WalletAccountReadOnlySolanaGasless extends WalletAccountRea
161
161
  * @param {TransactionMessage} transactionMessage - The transaction message to fetch the payment info.
162
162
  * @param {SolanaGaslessWalletPaymasterConfigOverrides} [config] - If set, overrides the given configuration options.
163
163
  * @returns {Promise<GetPaymentInstructionResponse>} The payment info.
164
+ * @throws {Error} If the paymaster payment instruction is not a recognized SPL transfer to the paymaster token account.
164
165
  */
165
166
  protected _getTransactionPaymentInfo(transactionMessage: TransactionMessage, config?: SolanaGaslessWalletPaymasterConfigOverrides): Promise<GetPaymentInstructionResponse>;
167
+ /**
168
+ * @protected
169
+ * @param {string} [paymasterTokenAddress] - The paymaster fee token mint.
170
+ * @returns {Promise<string>} The paymaster's associated token account for that mint.
171
+ */
172
+ protected _getPaymasterAssociatedTokenAccount(paymasterTokenAddress?: string): Promise<string>;
173
+ /**
174
+ * @protected
175
+ * @param {object} instruction - A candidate SPL token instruction.
176
+ * @param {string} paymasterTokenAccount - The paymaster associated token account.
177
+ * @returns {boolean} True if the instruction is an SPL transfer whose destination is the paymaster token account.
178
+ */
179
+ protected _isPaymentInstruction(instruction: object, paymasterTokenAccount: string): boolean;
180
+ /**
181
+ * @protected
182
+ * @param {object} paymentInstruction - The paymaster payment instruction.
183
+ * @param {string} paymasterTokenAccount - The paymaster associated token account.
184
+ * @returns {bigint} The transfer amount encoded in the instruction.
185
+ * @throws {Error} If the instruction is not a recognized SPL transfer to the paymaster token account.
186
+ */
187
+ protected _getPaymentInstructionAmount(paymentInstruction: object, paymasterTokenAccount: string): bigint;
166
188
  }
167
189
  export type TransactionResult = import("@tetherto/wdk-wallet").TransactionResult;
168
190
  export type TransactionMessage = import("@solana/transaction-messages").TransactionMessage;