@solana-program/token-wrap 1.0.0 → 2.0.0

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.
@@ -1,7 +1,7 @@
1
- import { getProgramDerivedAddress, getUtf8Encoder, getAddressEncoder, getStructEncoder, getStructDecoder, getAddressDecoder, combineCodec, decodeAccount, assertAccountExists, fetchEncodedAccount, assertAccountsExist, fetchEncodedAccounts, containsBytes, getU8Encoder, isProgramError, transformEncoder, getBooleanEncoder, getU8Decoder, getBooleanDecoder, getU64Encoder, getU64Decoder, AccountRole, pipe, createTransactionMessage, setTransactionMessageFeePayerSigner, setTransactionMessageLifetimeUsingBlockhash, appendTransactionMessageInstructions, assertTransactionIsFullySigned, upgradeRoleToSigner, generateKeyPairSigner, isTransactionSigner as isTransactionSigner$1 } from '@solana/kit';
2
- import { getMintSize, findAssociatedTokenPda, getTokenDecoder, TOKEN_2022_PROGRAM_ADDRESS, getInitializeAccountInstruction as getInitializeAccountInstruction$1 } from '@solana-program/token-2022';
3
- import { getTransferSolInstruction, getCreateAccountInstruction } from '@solana-program/system';
4
- import { TOKEN_PROGRAM_ADDRESS, getInitializeAccountInstruction } from '@solana-program/token';
1
+ import { getProgramDerivedAddress, getUtf8Encoder, getAddressEncoder, getStructEncoder, getStructDecoder, getAddressDecoder, combineCodec, decodeAccount, assertAccountExists, fetchEncodedAccount, assertAccountsExist, fetchEncodedAccounts, containsBytes, getU8Encoder, isProgramError, transformEncoder, getBooleanEncoder, getU8Decoder, getBooleanDecoder, getU64Encoder, getU64Decoder, AccountRole, assertTransactionIsFullySigned, pipe, createTransactionMessage, setTransactionMessageFeePayerSigner, setTransactionMessageLifetimeUsingBlockhash, appendTransactionMessageInstructions, upgradeRoleToSigner, isTransactionSigner as isTransactionSigner$1 } from '@solana/kit';
2
+ import { getMintSize, findAssociatedTokenPda, fetchMaybeToken, getCreateAssociatedTokenInstruction, getTokenDecoder } from '@solana-program/token-2022';
3
+ import { getTransferSolInstruction } from '@solana-program/system';
4
+ import '@solana-program/token';
5
5
 
6
6
  // src/generated/accounts/backpointer.ts
7
7
  async function findBackpointerPda(seeds, config = {}) {
@@ -124,10 +124,12 @@ var TOKEN_WRAP_ERROR__MINT_AUTHORITY_MISMATCH = 3;
124
124
  var TOKEN_WRAP_ERROR__ESCROW_OWNER_MISMATCH = 4;
125
125
  var TOKEN_WRAP_ERROR__INVALID_WRAPPED_MINT_OWNER = 5;
126
126
  var TOKEN_WRAP_ERROR__INVALID_BACKPOINTER_OWNER = 6;
127
+ var TOKEN_WRAP_ERROR__ESCROW_MISMATCH = 7;
127
128
  var tokenWrapErrorMessages;
128
129
  if (process.env.NODE_ENV !== "production") {
129
130
  tokenWrapErrorMessages = {
130
131
  [TOKEN_WRAP_ERROR__BACKPOINTER_MISMATCH]: `Wrapped backpointer account address does not match expected PDA`,
132
+ [TOKEN_WRAP_ERROR__ESCROW_MISMATCH]: `Escrow account address does not match expected ATA`,
131
133
  [TOKEN_WRAP_ERROR__ESCROW_OWNER_MISMATCH]: `Unwrapped escrow token owner is not set to expected PDA`,
132
134
  [TOKEN_WRAP_ERROR__INVALID_BACKPOINTER_OWNER]: `Wrapped backpointer account owner is not the expected token wrap program`,
133
135
  [TOKEN_WRAP_ERROR__INVALID_WRAPPED_MINT_OWNER]: `Wrapped mint account owner is not the expected token program`,
@@ -495,9 +497,8 @@ function parseWrapInstruction(instruction) {
495
497
  data: getWrapInstructionDataDecoder().decode(instruction.data)
496
498
  };
497
499
  }
498
- async function createMintTx({
500
+ async function createMint({
499
501
  rpc,
500
- blockhash,
501
502
  unwrappedMint,
502
503
  wrappedTokenProgram,
503
504
  payer,
@@ -552,63 +553,16 @@ async function createMintTx({
552
553
  idempotent
553
554
  })
554
555
  );
555
- const tx = pipe(
556
- createTransactionMessage({ version: 0 }),
557
- (tx2) => setTransactionMessageFeePayerSigner(payer, tx2),
558
- (tx2) => setTransactionMessageLifetimeUsingBlockhash(blockhash, tx2),
559
- (tx2) => appendTransactionMessageInstructions(instructions, tx2)
560
- );
561
556
  return {
562
557
  wrappedMint,
563
558
  backpointer,
564
- tx,
559
+ ixs: instructions,
565
560
  fundedWrappedMintLamports,
566
561
  fundedBackpointerLamports
567
562
  };
568
563
  }
569
- function getInitializeTokenFn(tokenProgram) {
570
- if (tokenProgram === TOKEN_PROGRAM_ADDRESS) return getInitializeAccountInstruction;
571
- if (tokenProgram === TOKEN_2022_PROGRAM_ADDRESS) return getInitializeAccountInstruction$1;
572
- throw new Error(`${tokenProgram} is not a valid token program.`);
573
- }
574
- async function createTokenAccountTx({
575
- rpc,
576
- blockhash,
577
- payer,
578
- mint,
579
- owner,
580
- tokenProgram
581
- }) {
582
- const [keyPair, lamports] = await Promise.all([
583
- generateKeyPairSigner(),
584
- rpc.getMinimumBalanceForRentExemption(165n).send()
585
- ]);
586
- const createAccountIx = getCreateAccountInstruction({
587
- payer,
588
- newAccount: keyPair,
589
- lamports,
590
- space: 165,
591
- programAddress: tokenProgram
592
- });
593
- const initializeAccountIx = getInitializeTokenFn(tokenProgram)({
594
- account: keyPair.address,
595
- mint,
596
- owner
597
- });
598
- const tx = pipe(
599
- createTransactionMessage({ version: 0 }),
600
- (tx2) => setTransactionMessageFeePayerSigner(payer, tx2),
601
- (tx2) => setTransactionMessageLifetimeUsingBlockhash(blockhash, tx2),
602
- (tx2) => appendTransactionMessageInstructions([createAccountIx, initializeAccountIx], tx2)
603
- );
604
- return {
605
- tx,
606
- keyPair
607
- };
608
- }
609
- async function createEscrowAccountTx({
564
+ async function createEscrowAccount({
610
565
  rpc,
611
- blockhash,
612
566
  payer,
613
567
  unwrappedMint,
614
568
  wrappedTokenProgram
@@ -616,14 +570,23 @@ async function createEscrowAccountTx({
616
570
  const [wrappedMint] = await findWrappedMintPda({ unwrappedMint, wrappedTokenProgram });
617
571
  const [wrappedMintAuthority] = await findWrappedMintAuthorityPda({ wrappedMint });
618
572
  const unwrappedTokenProgram = await getOwnerFromAccount(rpc, unwrappedMint);
619
- return createTokenAccountTx({
620
- rpc,
621
- blockhash,
622
- payer,
573
+ const [escrowAta] = await findAssociatedTokenPda({
574
+ owner: wrappedMintAuthority,
623
575
  mint: unwrappedMint,
576
+ tokenProgram: unwrappedTokenProgram
577
+ });
578
+ const escrowResult = await fetchMaybeToken(rpc, escrowAta);
579
+ if (escrowResult.exists) {
580
+ return { kind: "already_exists", account: escrowResult };
581
+ }
582
+ const ix = getCreateAssociatedTokenInstruction({
583
+ payer,
624
584
  owner: wrappedMintAuthority,
585
+ mint: unwrappedMint,
586
+ ata: escrowAta,
625
587
  tokenProgram: unwrappedTokenProgram
626
588
  });
589
+ return { address: escrowAta, ixs: [ix], kind: "instructions_to_create" };
627
590
  }
628
591
  async function getOwnerFromAccount(rpc, accountAddress) {
629
592
  const accountInfo = await rpc.getAccountInfo(accountAddress, { encoding: "base64" }).send();
@@ -691,15 +654,19 @@ function combinedMultisigTx({
691
654
  assertTransactionIsFullySigned(tx);
692
655
  return tx;
693
656
  }
694
- function multisigOfflineSignWrapTx(args) {
695
- return buildWrapTransaction(args);
657
+ async function multisigOfflineSignWrap(args) {
658
+ const wrapIx = await buildWrapIx(args);
659
+ return pipe(
660
+ createTransactionMessage({ version: 0 }),
661
+ (tx) => setTransactionMessageFeePayerSigner(args.payer, tx),
662
+ (tx) => setTransactionMessageLifetimeUsingBlockhash(args.blockhash, tx),
663
+ (tx) => appendTransactionMessageInstructions([wrapIx], tx)
664
+ );
696
665
  }
697
- async function singleSignerWrapTx({
666
+ async function singleSignerWrap({
698
667
  rpc,
699
- blockhash,
700
668
  payer,
701
669
  unwrappedTokenAccount,
702
- escrowAccount,
703
670
  wrappedTokenProgram,
704
671
  amount,
705
672
  transferAuthority: inputTransferAuthority,
@@ -713,7 +680,8 @@ async function singleSignerWrapTx({
713
680
  wrappedMint,
714
681
  wrappedMintAuthority,
715
682
  recipientWrappedTokenAccount,
716
- transferAuthority
683
+ transferAuthority,
684
+ unwrappedEscrow
717
685
  } = await resolveAddrs({
718
686
  rpc,
719
687
  payer,
@@ -724,11 +692,8 @@ async function singleSignerWrapTx({
724
692
  wrappedTokenProgram,
725
693
  inputRecipientTokenAccount
726
694
  });
727
- const tx = buildWrapTransaction({
728
- blockhash,
729
- payer,
695
+ const ix = await buildWrapIx({
730
696
  unwrappedTokenAccount,
731
- escrowAccount,
732
697
  wrappedTokenProgram,
733
698
  amount,
734
699
  transferAuthority,
@@ -739,9 +704,9 @@ async function singleSignerWrapTx({
739
704
  unwrappedTokenProgram
740
705
  });
741
706
  return {
742
- tx,
707
+ ixs: [ix],
743
708
  recipientWrappedTokenAccount,
744
- escrowAccount,
709
+ escrowAccount: unwrappedEscrow,
745
710
  amount: BigInt(amount)
746
711
  };
747
712
  }
@@ -764,8 +729,14 @@ async function resolveAddrs({
764
729
  mint: wrappedMint,
765
730
  tokenProgram: wrappedTokenProgram
766
731
  }))[0];
732
+ const [unwrappedEscrow] = await findAssociatedTokenPda({
733
+ owner: wrappedMintAuthority,
734
+ mint: unwrappedMint,
735
+ tokenProgram: unwrappedTokenProgram
736
+ });
767
737
  const transferAuthority = inputTransferAuthority ?? payer;
768
738
  return {
739
+ unwrappedEscrow,
769
740
  transferAuthority,
770
741
  unwrappedMint,
771
742
  unwrappedTokenProgram,
@@ -774,10 +745,8 @@ async function resolveAddrs({
774
745
  recipientWrappedTokenAccount
775
746
  };
776
747
  }
777
- function buildWrapTransaction({
778
- payer,
748
+ async function buildWrapIx({
779
749
  unwrappedTokenAccount,
780
- escrowAccount,
781
750
  wrappedTokenProgram,
782
751
  amount,
783
752
  transferAuthority,
@@ -786,9 +755,13 @@ function buildWrapTransaction({
786
755
  unwrappedTokenProgram,
787
756
  wrappedMint,
788
757
  wrappedMintAuthority,
789
- blockhash,
790
758
  multiSigners = []
791
759
  }) {
760
+ const [unwrappedEscrow] = await findAssociatedTokenPda({
761
+ owner: wrappedMintAuthority,
762
+ mint: unwrappedMint,
763
+ tokenProgram: unwrappedTokenProgram
764
+ });
792
765
  const wrapInstructionInput = {
793
766
  recipientWrappedTokenAccount,
794
767
  wrappedMint,
@@ -797,32 +770,26 @@ function buildWrapTransaction({
797
770
  wrappedTokenProgram,
798
771
  unwrappedTokenAccount,
799
772
  unwrappedMint,
800
- unwrappedEscrow: escrowAccount,
773
+ unwrappedEscrow,
801
774
  transferAuthority,
802
775
  amount: BigInt(amount),
803
776
  multiSigners
804
777
  };
805
- const wrapInstruction = getWrapInstruction(wrapInstructionInput);
806
- return pipe(
807
- createTransactionMessage({ version: 0 }),
808
- (tx) => setTransactionMessageFeePayerSigner(payer, tx),
809
- (tx) => setTransactionMessageLifetimeUsingBlockhash(blockhash, tx),
810
- (tx) => appendTransactionMessageInstructions([wrapInstruction], tx)
811
- );
778
+ return getWrapInstruction(wrapInstructionInput);
812
779
  }
813
780
  async function resolveUnwrapAddrs({
814
781
  rpc,
815
782
  payer,
816
783
  wrappedTokenAccount,
817
- unwrappedEscrow,
784
+ recipientUnwrappedToken,
818
785
  inputUnwrappedMint,
819
786
  inputTransferAuthority,
820
787
  inputWrappedTokenProgram,
821
788
  inputUnwrappedTokenProgram
822
789
  }) {
823
790
  const wrappedTokenProgram = inputWrappedTokenProgram ?? await getOwnerFromAccount(rpc, wrappedTokenAccount);
824
- const unwrappedTokenProgram = inputUnwrappedTokenProgram ?? await getOwnerFromAccount(rpc, unwrappedEscrow);
825
- const unwrappedMint = inputUnwrappedMint ?? await getMintFromTokenAccount(rpc, unwrappedEscrow);
791
+ const unwrappedTokenProgram = inputUnwrappedTokenProgram ?? await getOwnerFromAccount(rpc, recipientUnwrappedToken);
792
+ const unwrappedMint = inputUnwrappedMint ?? await getMintFromTokenAccount(rpc, recipientUnwrappedToken);
826
793
  const wrappedAccountInfo = await fetchEncodedAccount(rpc, wrappedTokenAccount);
827
794
  if (!wrappedAccountInfo.exists) {
828
795
  throw new Error(`Wrapped token account ${wrappedTokenAccount} not found.`);
@@ -839,9 +806,7 @@ async function resolveUnwrapAddrs({
839
806
  unwrappedTokenProgram
840
807
  };
841
808
  }
842
- function buildUnwrapTransaction({
843
- payer,
844
- unwrappedEscrow,
809
+ async function buildUnwrapTransaction({
845
810
  recipientUnwrappedToken,
846
811
  wrappedMintAuthority,
847
812
  unwrappedMint,
@@ -851,9 +816,13 @@ function buildUnwrapTransaction({
851
816
  wrappedMint,
852
817
  transferAuthority,
853
818
  amount,
854
- blockhash,
855
819
  multiSigners = []
856
820
  }) {
821
+ const [unwrappedEscrow] = await findAssociatedTokenPda({
822
+ owner: wrappedMintAuthority,
823
+ mint: unwrappedMint,
824
+ tokenProgram: unwrappedTokenProgram
825
+ });
857
826
  const unwrapInstructionInput = {
858
827
  unwrappedEscrow,
859
828
  recipientUnwrappedToken,
@@ -867,20 +836,12 @@ function buildUnwrapTransaction({
867
836
  amount: BigInt(amount),
868
837
  multiSigners
869
838
  };
870
- const unwrapInstruction = getUnwrapInstruction(unwrapInstructionInput);
871
- return pipe(
872
- createTransactionMessage({ version: 0 }),
873
- (tx) => setTransactionMessageFeePayerSigner(payer, tx),
874
- (tx) => setTransactionMessageLifetimeUsingBlockhash(blockhash, tx),
875
- (tx) => appendTransactionMessageInstructions([unwrapInstruction], tx)
876
- );
839
+ return getUnwrapInstruction(unwrapInstructionInput);
877
840
  }
878
- async function singleSignerUnwrapTx({
841
+ async function singleSignerUnwrap({
879
842
  rpc,
880
- blockhash,
881
843
  payer,
882
844
  wrappedTokenAccount,
883
- unwrappedEscrow,
884
845
  amount,
885
846
  recipientUnwrappedToken,
886
847
  transferAuthority: inputTransferAuthority,
@@ -899,15 +860,13 @@ async function singleSignerUnwrapTx({
899
860
  rpc,
900
861
  payer,
901
862
  wrappedTokenAccount,
902
- unwrappedEscrow,
863
+ recipientUnwrappedToken,
903
864
  inputUnwrappedMint,
904
865
  inputTransferAuthority,
905
866
  inputWrappedTokenProgram,
906
867
  inputUnwrappedTokenProgram
907
868
  });
908
- const tx = buildUnwrapTransaction({
909
- payer,
910
- unwrappedEscrow,
869
+ const ix = await buildUnwrapTransaction({
911
870
  recipientUnwrappedToken,
912
871
  wrappedMintAuthority,
913
872
  unwrappedMint,
@@ -916,19 +875,24 @@ async function singleSignerUnwrapTx({
916
875
  wrappedTokenAccount,
917
876
  wrappedMint,
918
877
  transferAuthority,
919
- amount,
920
- blockhash
878
+ amount
921
879
  });
922
880
  return {
923
881
  recipientUnwrappedToken,
924
882
  amount: BigInt(amount),
925
- tx
883
+ ixs: [ix]
926
884
  };
927
885
  }
928
- function multisigOfflineSignUnwrap(args) {
929
- return buildUnwrapTransaction(args);
886
+ async function multisigOfflineSignUnwrap(args) {
887
+ const unwrapIx = await buildUnwrapTransaction(args);
888
+ return pipe(
889
+ createTransactionMessage({ version: 0 }),
890
+ (tx) => setTransactionMessageFeePayerSigner(args.payer, tx),
891
+ (tx) => setTransactionMessageLifetimeUsingBlockhash(args.blockhash, tx),
892
+ (tx) => appendTransactionMessageInstructions([unwrapIx], tx)
893
+ );
930
894
  }
931
895
 
932
- export { CREATE_MINT_DISCRIMINATOR, TOKEN_WRAP_ERROR__BACKPOINTER_MISMATCH, TOKEN_WRAP_ERROR__ESCROW_OWNER_MISMATCH, TOKEN_WRAP_ERROR__INVALID_BACKPOINTER_OWNER, TOKEN_WRAP_ERROR__INVALID_WRAPPED_MINT_OWNER, TOKEN_WRAP_ERROR__MINT_AUTHORITY_MISMATCH, TOKEN_WRAP_ERROR__WRAPPED_MINT_MISMATCH, TOKEN_WRAP_ERROR__ZERO_WRAP_AMOUNT, TOKEN_WRAP_PROGRAM_ADDRESS, TokenWrapAccount, TokenWrapInstruction, UNWRAP_DISCRIMINATOR, WRAP_DISCRIMINATOR, combinedMultisigTx, createEscrowAccountTx, createMintTx, decodeBackpointer, fetchAllBackpointer, fetchAllMaybeBackpointer, fetchBackpointer, fetchBackpointerFromSeeds, fetchMaybeBackpointer, fetchMaybeBackpointerFromSeeds, findBackpointerPda, findWrappedMintAuthorityPda, findWrappedMintPda, getBackpointerCodec, getBackpointerDecoder, getBackpointerEncoder, getBackpointerSize, getCreateMintDiscriminatorBytes, getCreateMintInstruction, getCreateMintInstructionDataCodec, getCreateMintInstructionDataDecoder, getCreateMintInstructionDataEncoder, getTokenWrapErrorMessage, getUnwrapDiscriminatorBytes, getUnwrapInstruction, getUnwrapInstructionDataCodec, getUnwrapInstructionDataDecoder, getUnwrapInstructionDataEncoder, getWrapDiscriminatorBytes, getWrapInstruction, getWrapInstructionDataCodec, getWrapInstructionDataDecoder, getWrapInstructionDataEncoder, identifyTokenWrapInstruction, isTokenWrapError, multisigOfflineSignUnwrap, multisigOfflineSignWrapTx, parseCreateMintInstruction, parseUnwrapInstruction, parseWrapInstruction, singleSignerUnwrapTx, singleSignerWrapTx };
896
+ export { CREATE_MINT_DISCRIMINATOR, TOKEN_WRAP_ERROR__BACKPOINTER_MISMATCH, TOKEN_WRAP_ERROR__ESCROW_MISMATCH, TOKEN_WRAP_ERROR__ESCROW_OWNER_MISMATCH, TOKEN_WRAP_ERROR__INVALID_BACKPOINTER_OWNER, TOKEN_WRAP_ERROR__INVALID_WRAPPED_MINT_OWNER, TOKEN_WRAP_ERROR__MINT_AUTHORITY_MISMATCH, TOKEN_WRAP_ERROR__WRAPPED_MINT_MISMATCH, TOKEN_WRAP_ERROR__ZERO_WRAP_AMOUNT, TOKEN_WRAP_PROGRAM_ADDRESS, TokenWrapAccount, TokenWrapInstruction, UNWRAP_DISCRIMINATOR, WRAP_DISCRIMINATOR, combinedMultisigTx, createEscrowAccount, createMint, decodeBackpointer, fetchAllBackpointer, fetchAllMaybeBackpointer, fetchBackpointer, fetchBackpointerFromSeeds, fetchMaybeBackpointer, fetchMaybeBackpointerFromSeeds, findBackpointerPda, findWrappedMintAuthorityPda, findWrappedMintPda, getBackpointerCodec, getBackpointerDecoder, getBackpointerEncoder, getBackpointerSize, getCreateMintDiscriminatorBytes, getCreateMintInstruction, getCreateMintInstructionDataCodec, getCreateMintInstructionDataDecoder, getCreateMintInstructionDataEncoder, getTokenWrapErrorMessage, getUnwrapDiscriminatorBytes, getUnwrapInstruction, getUnwrapInstructionDataCodec, getUnwrapInstructionDataDecoder, getUnwrapInstructionDataEncoder, getWrapDiscriminatorBytes, getWrapInstruction, getWrapInstructionDataCodec, getWrapInstructionDataDecoder, getWrapInstructionDataEncoder, identifyTokenWrapInstruction, isTokenWrapError, multisigOfflineSignUnwrap, multisigOfflineSignWrap, parseCreateMintInstruction, parseUnwrapInstruction, parseWrapInstruction, singleSignerUnwrap, singleSignerWrap };
933
897
  //# sourceMappingURL=index.mjs.map
934
898
  //# sourceMappingURL=index.mjs.map