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