@solana/subscriptions 0.2.0 → 0.3.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/index.js CHANGED
@@ -227,14 +227,16 @@ function getCreateFixedDelegationDataEncoder() {
227
227
  return getStructEncoder2([
228
228
  ["nonce", getU64Encoder4()],
229
229
  ["amount", getU64Encoder4()],
230
- ["expiryTs", getI64Encoder()]
230
+ ["expiryTs", getI64Encoder()],
231
+ ["expectedSubscriptionAuthorityInitId", getI64Encoder()]
231
232
  ]);
232
233
  }
233
234
  function getCreateFixedDelegationDataDecoder() {
234
235
  return getStructDecoder2([
235
236
  ["nonce", getU64Decoder()],
236
237
  ["amount", getU64Decoder()],
237
- ["expiryTs", getI64Decoder()]
238
+ ["expiryTs", getI64Decoder()],
239
+ ["expectedSubscriptionAuthorityInitId", getI64Decoder()]
238
240
  ]);
239
241
  }
240
242
  function getCreateFixedDelegationDataCodec() {
@@ -257,7 +259,8 @@ function getCreateRecurringDelegationDataEncoder() {
257
259
  ["amountPerPeriod", getU64Encoder5()],
258
260
  ["periodLengthS", getU64Encoder5()],
259
261
  ["startTs", getI64Encoder2()],
260
- ["expiryTs", getI64Encoder2()]
262
+ ["expiryTs", getI64Encoder2()],
263
+ ["expectedSubscriptionAuthorityInitId", getI64Encoder2()]
261
264
  ]);
262
265
  }
263
266
  function getCreateRecurringDelegationDataDecoder() {
@@ -266,7 +269,8 @@ function getCreateRecurringDelegationDataDecoder() {
266
269
  ["amountPerPeriod", getU64Decoder2()],
267
270
  ["periodLengthS", getU64Decoder2()],
268
271
  ["startTs", getI64Decoder2()],
269
- ["expiryTs", getI64Decoder2()]
272
+ ["expiryTs", getI64Decoder2()],
273
+ ["expectedSubscriptionAuthorityInitId", getI64Decoder2()]
270
274
  ]);
271
275
  }
272
276
  function getCreateRecurringDelegationDataCodec() {
@@ -425,7 +429,8 @@ function getSubscribeDataEncoder() {
425
429
  ["expectedMint", getAddressEncoder8()],
426
430
  ["expectedAmount", getU64Encoder8()],
427
431
  ["expectedPeriodHours", getU64Encoder8()],
428
- ["expectedCreatedAt", getI64Encoder6()]
432
+ ["expectedCreatedAt", getI64Encoder6()],
433
+ ["expectedSubscriptionAuthorityInitId", getI64Encoder6()]
429
434
  ]);
430
435
  }
431
436
  function getSubscribeDataDecoder() {
@@ -435,7 +440,8 @@ function getSubscribeDataDecoder() {
435
440
  ["expectedMint", getAddressDecoder3()],
436
441
  ["expectedAmount", getU64Decoder5()],
437
442
  ["expectedPeriodHours", getU64Decoder5()],
438
- ["expectedCreatedAt", getI64Decoder6()]
443
+ ["expectedCreatedAt", getI64Decoder6()],
444
+ ["expectedSubscriptionAuthorityInitId", getI64Decoder6()]
439
445
  ]);
440
446
  }
441
447
  function getSubscribeDataCodec() {
@@ -1889,6 +1895,7 @@ function getTransferFixedInstruction(input, config) {
1889
1895
  subscriptionAuthority: { value: input.subscriptionAuthority ?? null, isWritable: false },
1890
1896
  delegatorAta: { value: input.delegatorAta ?? null, isWritable: true },
1891
1897
  receiverAta: { value: input.receiverAta ?? null, isWritable: true },
1898
+ tokenMint: { value: input.tokenMint ?? null, isWritable: false },
1892
1899
  tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
1893
1900
  delegatee: { value: input.delegatee ?? null, isWritable: false },
1894
1901
  eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
@@ -1909,6 +1916,7 @@ function getTransferFixedInstruction(input, config) {
1909
1916
  getAccountMeta("subscriptionAuthority", accounts.subscriptionAuthority),
1910
1917
  getAccountMeta("delegatorAta", accounts.delegatorAta),
1911
1918
  getAccountMeta("receiverAta", accounts.receiverAta),
1919
+ getAccountMeta("tokenMint", accounts.tokenMint),
1912
1920
  getAccountMeta("tokenProgram", accounts.tokenProgram),
1913
1921
  getAccountMeta("delegatee", accounts.delegatee),
1914
1922
  getAccountMeta("eventAuthority", accounts.eventAuthority),
@@ -1919,10 +1927,10 @@ function getTransferFixedInstruction(input, config) {
1919
1927
  });
1920
1928
  }
1921
1929
  function parseTransferFixedInstruction(instruction) {
1922
- if (instruction.accounts.length < 8) {
1930
+ if (instruction.accounts.length < 9) {
1923
1931
  throw new SolanaError11(SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS11, {
1924
1932
  actualAccountMetas: instruction.accounts.length,
1925
- expectedAccountMetas: 8
1933
+ expectedAccountMetas: 9
1926
1934
  });
1927
1935
  }
1928
1936
  let accountIndex = 0;
@@ -1938,6 +1946,7 @@ function parseTransferFixedInstruction(instruction) {
1938
1946
  subscriptionAuthority: getNextAccount(),
1939
1947
  delegatorAta: getNextAccount(),
1940
1948
  receiverAta: getNextAccount(),
1949
+ tokenMint: getNextAccount(),
1941
1950
  tokenProgram: getNextAccount(),
1942
1951
  delegatee: getNextAccount(),
1943
1952
  eventAuthority: getNextAccount(),
@@ -1988,6 +1997,7 @@ function getTransferRecurringInstruction(input, config) {
1988
1997
  subscriptionAuthority: { value: input.subscriptionAuthority ?? null, isWritable: false },
1989
1998
  delegatorAta: { value: input.delegatorAta ?? null, isWritable: true },
1990
1999
  receiverAta: { value: input.receiverAta ?? null, isWritable: true },
2000
+ tokenMint: { value: input.tokenMint ?? null, isWritable: false },
1991
2001
  tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
1992
2002
  delegatee: { value: input.delegatee ?? null, isWritable: false },
1993
2003
  eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
@@ -2008,6 +2018,7 @@ function getTransferRecurringInstruction(input, config) {
2008
2018
  getAccountMeta("subscriptionAuthority", accounts.subscriptionAuthority),
2009
2019
  getAccountMeta("delegatorAta", accounts.delegatorAta),
2010
2020
  getAccountMeta("receiverAta", accounts.receiverAta),
2021
+ getAccountMeta("tokenMint", accounts.tokenMint),
2011
2022
  getAccountMeta("tokenProgram", accounts.tokenProgram),
2012
2023
  getAccountMeta("delegatee", accounts.delegatee),
2013
2024
  getAccountMeta("eventAuthority", accounts.eventAuthority),
@@ -2018,10 +2029,10 @@ function getTransferRecurringInstruction(input, config) {
2018
2029
  });
2019
2030
  }
2020
2031
  function parseTransferRecurringInstruction(instruction) {
2021
- if (instruction.accounts.length < 8) {
2032
+ if (instruction.accounts.length < 9) {
2022
2033
  throw new SolanaError12(SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS12, {
2023
2034
  actualAccountMetas: instruction.accounts.length,
2024
- expectedAccountMetas: 8
2035
+ expectedAccountMetas: 9
2025
2036
  });
2026
2037
  }
2027
2038
  let accountIndex = 0;
@@ -2037,6 +2048,7 @@ function parseTransferRecurringInstruction(instruction) {
2037
2048
  subscriptionAuthority: getNextAccount(),
2038
2049
  delegatorAta: getNextAccount(),
2039
2050
  receiverAta: getNextAccount(),
2051
+ tokenMint: getNextAccount(),
2040
2052
  tokenProgram: getNextAccount(),
2041
2053
  delegatee: getNextAccount(),
2042
2054
  eventAuthority: getNextAccount(),
@@ -2092,6 +2104,7 @@ function getTransferSubscriptionInstruction(input, config) {
2092
2104
  delegatorAta: { value: input.delegatorAta ?? null, isWritable: true },
2093
2105
  receiverAta: { value: input.receiverAta ?? null, isWritable: true },
2094
2106
  caller: { value: input.caller ?? null, isWritable: false },
2107
+ tokenMint: { value: input.tokenMint ?? null, isWritable: false },
2095
2108
  tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
2096
2109
  eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
2097
2110
  selfProgram: { value: input.selfProgram ?? null, isWritable: false }
@@ -2113,6 +2126,7 @@ function getTransferSubscriptionInstruction(input, config) {
2113
2126
  getAccountMeta("delegatorAta", accounts.delegatorAta),
2114
2127
  getAccountMeta("receiverAta", accounts.receiverAta),
2115
2128
  getAccountMeta("caller", accounts.caller),
2129
+ getAccountMeta("tokenMint", accounts.tokenMint),
2116
2130
  getAccountMeta("tokenProgram", accounts.tokenProgram),
2117
2131
  getAccountMeta("eventAuthority", accounts.eventAuthority),
2118
2132
  getAccountMeta("selfProgram", accounts.selfProgram)
@@ -2122,10 +2136,10 @@ function getTransferSubscriptionInstruction(input, config) {
2122
2136
  });
2123
2137
  }
2124
2138
  function parseTransferSubscriptionInstruction(instruction) {
2125
- if (instruction.accounts.length < 9) {
2139
+ if (instruction.accounts.length < 10) {
2126
2140
  throw new SolanaError13(SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS13, {
2127
2141
  actualAccountMetas: instruction.accounts.length,
2128
- expectedAccountMetas: 9
2142
+ expectedAccountMetas: 10
2129
2143
  });
2130
2144
  }
2131
2145
  let accountIndex = 0;
@@ -2143,6 +2157,7 @@ function parseTransferSubscriptionInstruction(instruction) {
2143
2157
  delegatorAta: getNextAccount(),
2144
2158
  receiverAta: getNextAccount(),
2145
2159
  caller: getNextAccount(),
2160
+ tokenMint: getNextAccount(),
2146
2161
  tokenProgram: getNextAccount(),
2147
2162
  eventAuthority: getNextAccount(),
2148
2163
  selfProgram: getNextAccount()
@@ -2822,6 +2837,11 @@ async function getCloseSubscriptionAuthorityOverlayInstructionAsync(input) {
2822
2837
  }
2823
2838
  async function getCreateFixedDelegationOverlayInstructionAsync(input) {
2824
2839
  assertPositive(input.amount, "amount");
2840
+ if (input.expectedSubscriptionAuthorityInitId === void 0) {
2841
+ throw new Error(
2842
+ "getCreateFixedDelegationOverlayInstructionAsync requires expectedSubscriptionAuthorityInitId. Use the plugin client `subscriptions.instructions.createFixedDelegation(...)` to auto-fetch from the live authority."
2843
+ );
2844
+ }
2825
2845
  const [subscriptionAuthority] = await findSubscriptionAuthorityPda(
2826
2846
  { tokenMint: input.tokenMint, user: input.delegator.address },
2827
2847
  pdaConfig(input.programAddress)
@@ -2843,6 +2863,7 @@ async function getCreateFixedDelegationOverlayInstructionAsync(input) {
2843
2863
  delegator: input.delegator,
2844
2864
  fixedDelegation: {
2845
2865
  amount: input.amount,
2866
+ expectedSubscriptionAuthorityInitId: input.expectedSubscriptionAuthorityInitId,
2846
2867
  expiryTs: input.expiryTs,
2847
2868
  nonce: input.nonce
2848
2869
  },
@@ -2856,6 +2877,11 @@ async function getCreateFixedDelegationOverlayInstructionAsync(input) {
2856
2877
  async function getCreateRecurringDelegationOverlayInstructionAsync(input) {
2857
2878
  assertPositive(input.amountPerPeriod, "amountPerPeriod");
2858
2879
  assertPositive(input.periodLengthS, "periodLengthS");
2880
+ if (input.expectedSubscriptionAuthorityInitId === void 0) {
2881
+ throw new Error(
2882
+ "getCreateRecurringDelegationOverlayInstructionAsync requires expectedSubscriptionAuthorityInitId. Use the plugin client `subscriptions.instructions.createRecurringDelegation(...)` to auto-fetch from the live authority."
2883
+ );
2884
+ }
2859
2885
  const [subscriptionAuthority] = await findSubscriptionAuthorityPda(
2860
2886
  { tokenMint: input.tokenMint, user: input.delegator.address },
2861
2887
  pdaConfig(input.programAddress)
@@ -2877,6 +2903,7 @@ async function getCreateRecurringDelegationOverlayInstructionAsync(input) {
2877
2903
  delegator: input.delegator,
2878
2904
  recurringDelegation: {
2879
2905
  amountPerPeriod: input.amountPerPeriod,
2906
+ expectedSubscriptionAuthorityInitId: input.expectedSubscriptionAuthorityInitId,
2880
2907
  expiryTs: input.expiryTs,
2881
2908
  nonce: input.nonce,
2882
2909
  periodLengthS: input.periodLengthS,
@@ -2931,6 +2958,7 @@ async function getTransferDelegationOverlayInstructionAsync(input, getInstructio
2931
2958
  delegatorAta: input.delegatorAta,
2932
2959
  receiverAta: input.receiverAta,
2933
2960
  subscriptionAuthority,
2961
+ tokenMint: input.tokenMint,
2934
2962
  tokenProgram: input.tokenProgram,
2935
2963
  transferData: {
2936
2964
  amount: input.amount,
@@ -2966,6 +2994,7 @@ async function getTransferSubscriptionOverlayInstructionAsync(input) {
2966
2994
  receiverAta: input.receiverAta,
2967
2995
  subscriptionAuthority,
2968
2996
  subscriptionPda: input.subscriptionPda,
2997
+ tokenMint: input.tokenMint,
2969
2998
  tokenProgram: input.tokenProgram,
2970
2999
  transferData: {
2971
3000
  amount: input.amount,
@@ -3030,9 +3059,9 @@ function getDeletePlanOverlayInstruction(input) {
3030
3059
  return getDeletePlanInstruction({ owner: input.owner, planPda: input.planPda }, pdaConfig(input.programAddress));
3031
3060
  }
3032
3061
  async function getSubscribeOverlayInstructionAsync(input) {
3033
- if (input.expectedAmount === void 0 || input.expectedPeriodHours === void 0 || input.expectedCreatedAt === void 0) {
3062
+ if (input.expectedAmount === void 0 || input.expectedPeriodHours === void 0 || input.expectedCreatedAt === void 0 || input.expectedSubscriptionAuthorityInitId === void 0) {
3034
3063
  throw new Error(
3035
- "getSubscribeOverlayInstructionAsync requires expectedAmount, expectedPeriodHours, and expectedCreatedAt. Use the plugin client `subscriptions.instructions.subscribe(...)` to auto-fetch from the live plan."
3064
+ "getSubscribeOverlayInstructionAsync requires expectedAmount, expectedPeriodHours, expectedCreatedAt, and expectedSubscriptionAuthorityInitId. Use the plugin client `subscriptions.instructions.subscribe(...)` to auto-fetch from the live plan and authority."
3036
3065
  );
3037
3066
  }
3038
3067
  const [planPda, planBump] = await findPlanPda(
@@ -3053,6 +3082,7 @@ async function getSubscribeOverlayInstructionAsync(input) {
3053
3082
  expectedCreatedAt: input.expectedCreatedAt,
3054
3083
  expectedMint: input.tokenMint,
3055
3084
  expectedPeriodHours: input.expectedPeriodHours,
3085
+ expectedSubscriptionAuthorityInitId: input.expectedSubscriptionAuthorityInitId,
3056
3086
  planBump,
3057
3087
  planId: input.planId
3058
3088
  },
@@ -3109,6 +3139,20 @@ function subscriptionsProgram2() {
3109
3139
  },
3110
3140
  plansForOwner: (owner) => fetchPlansForOwner(c.rpc, owner)
3111
3141
  };
3142
+ const resolveExpectedSubscriptionAuthorityInitId = async (tokenMint, user, programAddress, expectedSubscriptionAuthorityInitId) => {
3143
+ if (expectedSubscriptionAuthorityInitId !== void 0) {
3144
+ return expectedSubscriptionAuthorityInitId;
3145
+ }
3146
+ const [subscriptionAuthorityPda] = await findSubscriptionAuthorityPda(
3147
+ { tokenMint, user },
3148
+ pdaConfig(programAddress)
3149
+ );
3150
+ const subscriptionAuthority = await fetchMaybeSubscriptionAuthority(c.rpc, subscriptionAuthorityPda);
3151
+ if (!subscriptionAuthority.exists) {
3152
+ throw new Error("SubscriptionAuthority is not initialized for this delegator and token mint.");
3153
+ }
3154
+ return subscriptionAuthority.data.initId;
3155
+ };
3112
3156
  const instructions = {
3113
3157
  cancelSubscription: (input) => addSelfPlanAndSendFunctions2(
3114
3158
  client,
@@ -3126,11 +3170,21 @@ function subscriptionsProgram2() {
3126
3170
  ),
3127
3171
  createFixedDelegation: (input) => addSelfPlanAndSendFunctions2(
3128
3172
  client,
3129
- getCreateFixedDelegationOverlayInstructionAsync({
3130
- ...input,
3131
- delegator: input.delegator ?? client.identity,
3132
- payer: input.payer ?? (client.payer === client.identity ? void 0 : client.payer)
3133
- })
3173
+ (async () => {
3174
+ const delegator = input.delegator ?? client.identity;
3175
+ const expectedSubscriptionAuthorityInitId = await resolveExpectedSubscriptionAuthorityInitId(
3176
+ input.tokenMint,
3177
+ delegator.address,
3178
+ input.programAddress,
3179
+ input.expectedSubscriptionAuthorityInitId
3180
+ );
3181
+ return await getCreateFixedDelegationOverlayInstructionAsync({
3182
+ ...input,
3183
+ delegator,
3184
+ expectedSubscriptionAuthorityInitId,
3185
+ payer: input.payer ?? (client.payer === client.identity ? void 0 : client.payer)
3186
+ });
3187
+ })()
3134
3188
  ),
3135
3189
  createPlan: (input) => addSelfPlanAndSendFunctions2(
3136
3190
  client,
@@ -3141,11 +3195,21 @@ function subscriptionsProgram2() {
3141
3195
  ),
3142
3196
  createRecurringDelegation: (input) => addSelfPlanAndSendFunctions2(
3143
3197
  client,
3144
- getCreateRecurringDelegationOverlayInstructionAsync({
3145
- ...input,
3146
- delegator: input.delegator ?? client.identity,
3147
- payer: input.payer ?? (client.payer === client.identity ? void 0 : client.payer)
3148
- })
3198
+ (async () => {
3199
+ const delegator = input.delegator ?? client.identity;
3200
+ const expectedSubscriptionAuthorityInitId = await resolveExpectedSubscriptionAuthorityInitId(
3201
+ input.tokenMint,
3202
+ delegator.address,
3203
+ input.programAddress,
3204
+ input.expectedSubscriptionAuthorityInitId
3205
+ );
3206
+ return await getCreateRecurringDelegationOverlayInstructionAsync({
3207
+ ...input,
3208
+ delegator,
3209
+ expectedSubscriptionAuthorityInitId,
3210
+ payer: input.payer ?? (client.payer === client.identity ? void 0 : client.payer)
3211
+ });
3212
+ })()
3149
3213
  ),
3150
3214
  deletePlan: (input) => addSelfPlanAndSendFunctions2(
3151
3215
  client,
@@ -3186,9 +3250,14 @@ function subscriptionsProgram2() {
3186
3250
  subscribe: (input) => addSelfPlanAndSendFunctions2(
3187
3251
  client,
3188
3252
  (async () => {
3189
- let { expectedAmount, expectedPeriodHours, expectedCreatedAt } = input;
3253
+ const subscriber = input.subscriber ?? client.identity;
3254
+ let {
3255
+ expectedAmount,
3256
+ expectedCreatedAt,
3257
+ expectedPeriodHours,
3258
+ expectedSubscriptionAuthorityInitId
3259
+ } = input;
3190
3260
  if (expectedAmount === void 0 || expectedPeriodHours === void 0 || expectedCreatedAt === void 0) {
3191
- const subscriber = input.subscriber ?? client.identity;
3192
3261
  const [planPda] = await findPlanPda(
3193
3262
  { owner: input.merchant, planId: input.planId },
3194
3263
  pdaConfig(input.programAddress)
@@ -3197,22 +3266,31 @@ function subscriptionsProgram2() {
3197
3266
  expectedAmount = expectedAmount ?? plan.data.data.terms.amount;
3198
3267
  expectedPeriodHours = expectedPeriodHours ?? plan.data.data.terms.periodHours;
3199
3268
  expectedCreatedAt = expectedCreatedAt ?? plan.data.data.terms.createdAt;
3200
- return await getSubscribeOverlayInstructionAsync({
3201
- ...input,
3202
- expectedAmount,
3203
- expectedCreatedAt,
3204
- expectedPeriodHours,
3205
- payer: input.payer ?? (client.payer === client.identity ? void 0 : client.payer),
3206
- subscriber
3207
- });
3269
+ }
3270
+ if (expectedSubscriptionAuthorityInitId === void 0) {
3271
+ const [subscriptionAuthorityPda] = await findSubscriptionAuthorityPda(
3272
+ { tokenMint: input.tokenMint, user: subscriber.address },
3273
+ pdaConfig(input.programAddress)
3274
+ );
3275
+ const subscriptionAuthority = await fetchMaybeSubscriptionAuthority(
3276
+ c.rpc,
3277
+ subscriptionAuthorityPda
3278
+ );
3279
+ if (!subscriptionAuthority.exists) {
3280
+ throw new Error(
3281
+ "SubscriptionAuthority is not initialized for this subscriber and token mint."
3282
+ );
3283
+ }
3284
+ expectedSubscriptionAuthorityInitId = subscriptionAuthority.data.initId;
3208
3285
  }
3209
3286
  return await getSubscribeOverlayInstructionAsync({
3210
3287
  ...input,
3211
3288
  expectedAmount,
3212
3289
  expectedCreatedAt,
3213
3290
  expectedPeriodHours,
3291
+ expectedSubscriptionAuthorityInitId,
3214
3292
  payer: input.payer ?? (client.payer === client.identity ? void 0 : client.payer),
3215
- subscriber: input.subscriber ?? client.identity
3293
+ subscriber
3216
3294
  });
3217
3295
  })()
3218
3296
  ),