Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

A puller transfers against a subscription

Source: subscriptions/tests/test_transfer_subscription.rs L199

  • When: the user initializes a subscription authority
  • Then: init authority succeeds ✓
  • When: the merchant creates plan 1
  • Then: create plan succeeds ✓
  • When: the subscriber subscribes
  • Then: subscribe succeeds ✓
  • Then: the merchant ATA starts empty ✓
  • When: a puller transfers against the subscription
  • Then: the merchant received 10 tokens ✓
  • Then: the pulled-in-period is 10 tokens ✓

Result: ✓ success — 5973 CU · claims 6/6 ✓

sequenceDiagram
    participant p0 as merchant
    participant p1 as Subscriptions
    participant p2 as token
    p0->>p1: transferSubscription
    activate p1
    p1->>p2: transferChecked
    activate p2
    p2-->>p1: ✓ 113cu
    deactivate p2
    p1->>p1: emit:subscriptionTransferEvent
    activate p1
    p1-->>p1: ✓ 137cu
    deactivate p1
    p1-->>p0: ✓ 5973cu
    deactivate p1

Authority

flowchart LR
    Subscriptions["Subscriptions"]:::program
    Subscription[("Subscription")]:::state
    aliceATAUSDCmint[("alice/ATA(USDC mint)")]:::state
    merchantATAUSDCmint[("merchant/ATA(USDC mint)")]:::state
    merchant(["merchant"]):::signer
    token["token"]:::program
    SAaliceUSDCmint(["SA(alice, USDC mint)"]):::signer
    eventAuthority(["eventAuthority"]):::signer
    Subscriptions -->|writes| Subscription
    Subscriptions -->|writes| aliceATAUSDCmint
    Subscriptions -->|writes| merchantATAUSDCmint
    merchant -->|signs| Subscriptions
    token -->|writes| aliceATAUSDCmint
    token -->|writes| merchantATAUSDCmint
    SAaliceUSDCmint -->|signs| token
    eventAuthority -->|signs| Subscriptions
    classDef program fill:#dae8fc,stroke:#6c8ebf;
    classDef signer fill:#d5e8d4,stroke:#82b366;
    classDef state fill:#ffe6cc,stroke:#d79b00;

Ownership

flowchart LR
    Subscriptions["Subscriptions"]:::program
    Subscription[("Subscription")]:::state
    token["token"]:::program
    aliceATAUSDCmint[("alice/ATA(USDC mint)")]:::state
    merchantATAUSDCmint[("merchant/ATA(USDC mint)")]:::state
    system["system"]:::program
    merchant[("merchant")]:::state
    Subscriptions -->|owns| Subscription
    token -->|owns| aliceATAUSDCmint
    token -->|owns| merchantATAUSDCmint
    system -->|owns| merchant
    classDef program fill:#dae8fc,stroke:#6c8ebf;
    classDef signer fill:#d5e8d4,stroke:#82b366;
    classDef state fill:#ffe6cc,stroke:#d79b00;

Accounts

AccountSignerWritableOwner
Subscription·Subscriptions
Plan··Subscriptions
SA(alice, USDC mint)··Subscriptions
alice/ATA(USDC mint)·token
merchant/ATA(USDC mint)·token
merchantsystem
USDC mint··token
token··BPFL…
eventAuthority··system
Subscriptions··BPFL…
Call tree and logs
A puller transfers against a subscription
merchant (5973cu)
└─ Subscriptions::transferSubscription ✓ 5973cu
   ├─ token::transferChecked ✓ 113cu
   └─ Subscriptions::emit:subscriptionTransferEvent ✓ 137cu
Program De1egAFMkMWZSN5rYXRj9CAdheBamobVNubTsi9avR44 invoke [1]
Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]
Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 113 of 195646 compute units
Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success
Program De1egAFMkMWZSN5rYXRj9CAdheBamobVNubTsi9avR44 invoke [2]
Program De1egAFMkMWZSN5rYXRj9CAdheBamobVNubTsi9avR44 consumed 137 of 194189 compute units
Program De1egAFMkMWZSN5rYXRj9CAdheBamobVNubTsi9avR44 success
Program De1egAFMkMWZSN5rYXRj9CAdheBamobVNubTsi9avR44 consumed 5973 of 200000 compute units
Program De1egAFMkMWZSN5rYXRj9CAdheBamobVNubTsi9avR44 success

Integration

What an integrator writes: the derivations and the instruction, byte-identical to the transaction this page witnessed. Addresses are this scenario’s; supply your own.

#![allow(unused)]
fn main() {
use solana_instruction::{AccountMeta, Instruction};
use solana_pubkey::{pubkey, Pubkey};

// --- transferSubscription ---
let program_id: Pubkey = pubkey!("De1egAFMkMWZSN5rYXRj9CAdheBamobVNubTsi9avR44");
let subscription_pda: Pubkey = pubkey!("6pQDdNgFksFRPMJQFvuiMheurG16kVi12So7UutPzWjH"); // Subscription: this scenario's value; supply your own
let plan_pda: Pubkey = pubkey!("2PQuEytmHE7C2a9ynpmsRfYK7UgXNtCqSR4qnNNgeZwp"); // Plan: this scenario's value; supply your own
let subscription_authority: Pubkey = pubkey!("4RDKDQRnKtZC5ti4wunSAayArwxdZida5St595CiKJ7i"); // SA(alice, USDC mint): this scenario's value; supply your own
let delegator_ata: Pubkey = pubkey!("3fVRopUs1BsF6RqteSmcyctBCx2YQ6Zkv7prsUz95VHY"); // supply your own
let receiver_ata: Pubkey = pubkey!("3FuYuMdLiEnmSsoGZEiXUDJSpiLgeKTmNTWUQkLAZyH1"); // supply your own
let caller: Pubkey = pubkey!("4jnW4cX63scWb3VGDVaacYunDWNFsGERr3YYUKPuNxJp"); // merchant: this scenario's value; supply your own
let token_mint: Pubkey = pubkey!("HhbhtPWNt5Rd2gvrjhGCekommdNQwVLY6ZMp3aBv4xtG"); // USDC mint: this scenario's value; supply your own
let token_program: Pubkey = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); // the token program
let event_authority = Pubkey::find_program_address(&[b"event_authority"], &program_id).0;
let ix = Instruction {
    program_id,
    accounts: vec![
        AccountMeta::new(subscription_pda, false), // subscriptionPda
        AccountMeta::new_readonly(plan_pda, false), // planPda
        AccountMeta::new_readonly(subscription_authority, false), // subscriptionAuthority
        AccountMeta::new(delegator_ata, false), // delegatorAta
        AccountMeta::new(receiver_ata, false), // receiverAta
        AccountMeta::new_readonly(caller, true), // caller
        AccountMeta::new_readonly(token_mint, false), // tokenMint
        AccountMeta::new_readonly(token_program, false), // tokenProgram
        AccountMeta::new_readonly(event_authority, false), // eventAuthority
        AccountMeta::new_readonly(program_id, false), // selfProgram
    ],
    // discriminator ++ borsh(transferData: {amount: 10000000, delegator: 2rj1…, mint: Hhbh…})
    data: vec![0x0a, 0x80, 0x96, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x98, 0xa3, 0xfc, 0x7a, 0x9f, 0x8b, 0x5c, 0x8b, 0x0e, 0xad, 0xe0, 0x77, 0x5a, 0x59, 0x3e, 0x44, 0x99, 0xa9, 0x76, 0x55, 0x97, 0xcd, 0x1b, 0xdc, 0xae, 0x63, 0xcd, 0x00, 0xf5, 0x19, 0xbc, 0xf8, 0x21, 0x76, 0xd4, 0x30, 0xe3, 0x1b, 0x7f, 0x8d, 0xe5, 0x16, 0xa8, 0xf6, 0xa3, 0x97, 0xc4, 0x57, 0x80, 0x12, 0x98, 0x73, 0x7a, 0xb3, 0x11, 0x69, 0xc0, 0x99, 0x8f, 0x1c, 0x02, 0x2b, 0xa1],
};
}

The same call through the Codama-generated TypeScript client (the async builder derives every PDA the resolver derived here, so only the free inputs appear):

import { address } from "@solana/kit";
import { getTransferSubscriptionInstructionAsync } from "your-generated-client";

const ix = await getTransferSubscriptionInstructionAsync({
  subscriptionPda: address("6pQDdNgFksFRPMJQFvuiMheurG16kVi12So7UutPzWjH"), // Subscription
  planPda: address("2PQuEytmHE7C2a9ynpmsRfYK7UgXNtCqSR4qnNNgeZwp"), // Plan
  subscriptionAuthority: address("4RDKDQRnKtZC5ti4wunSAayArwxdZida5St595CiKJ7i"), // SA(alice, USDC mint)
  delegatorAta: address("3fVRopUs1BsF6RqteSmcyctBCx2YQ6Zkv7prsUz95VHY"),
  receiverAta: address("3FuYuMdLiEnmSsoGZEiXUDJSpiLgeKTmNTWUQkLAZyH1"),
  caller: callerSigner, // merchant (a TransactionSigner)
  tokenMint: address("HhbhtPWNt5Rd2gvrjhGCekommdNQwVLY6ZMp3aBv4xtG"), // USDC mint
  // tokenProgram defaults to the token program
  transferData: {
    amount: 10000000n,
    delegator: address("2rj1EEgg32bupe12mMYnPFaKQCPPyYBhKYmDFZ8cXxpT") /* alice */,
    mint: address("HhbhtPWNt5Rd2gvrjhGCekommdNQwVLY6ZMp3aBv4xtG") /* USDC mint */
  },
});
How this page verifies itself: the test that ran it

The narrative above is this test; the page and the code cannot drift.

#![allow(unused)]
fn main() {
#[test]
fn transfer_subscription_success() {
    let mut story = Story::load(SO, IDL);
    let end_ts = story.now() + days(30) as i64;
    let s = setup_plan_and_subscription(&mut story, 50_000_000, 1, end_ts, vec![], vec![]);

    story.then_holds(
        "the merchant ATA starts empty",
        story.token_balance(&s.merchant_ata) == 0,
    );

    let outcome = pull(&mut story, &s, &s.merchant, 10_000_000, None).expect_success();

    story.then_holds(
        "the merchant received 10 tokens",
        story.token_balance(&s.merchant_ata) == 10_000_000,
    );
    story.then_holds(
        "the pulled-in-period is 10 tokens",
        amount_pulled(&story, &s.subscription_pda) == 10_000_000,
    );
    emit_scenario(
        &story,
        &outcome,
        "A puller transfers against a subscription",
    );
}
}