Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registry Instruction #48

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Registry Instruction #48

wants to merge 9 commits into from

Conversation

vsiva308
Copy link
Member

@vsiva308 vsiva308 commented Apr 2, 2022

Working on creating a registry:

  • Registry account holds data containing initialized subscriptions
  • Each subscription takes up 48 bytes (32 payee key, 8 amount, 8 duration)

When a user calls the instruction:

  • Checks if subscription is already in the account
  • If not, adds subscription to account (append) by increasing account size

To Do:

  • Copy over data from old account to new account
  • Ensure that lamports calculation and rent transfer works properly

@vsiva308 vsiva308 linked an issue Apr 2, 2022 that may be closed by this pull request
Comment on lines 112 to 144
let existing_lamports = registry_ai.lamports();
**registry_ai.lamports.borrow_mut() = 0;

//increment registry count and change seeds
registry_count += 1;
let registry_seeds = &[
b"subscription_registry",
&registry_count.to_le_bytes(),
]

//create new registry account with more space for data
invoke_signed(
let new_length = registry_length + 48;
&system_instruction::create_account(
user_ai.key,
registry_ai.key,
rent::Rent::get()?.minimum_balance(new_length - registry_length),
new_length as u64,
program_id,
),
&[
user_ai.clone();
registry_ai.clone();
system_program_ai.clone();
],
&[registry_seeds],
);

//add the lamports from before adding this space
let user_added_lamports = registry_ai.lamports();
**registry_ai.lamports.borrow_mut() = user_added_lamports
.checked_add(existing_lamports)
.ok_or(TokenError::Overflow)?;
Copy link
Member

@alecchendev alecchendev Apr 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two main things:

  1. Another account - you will actually need to pass in another account (where the seeds are "subscription_registry" and registry_count + 1 as you've correctly defined) - your current code works for the very first initialization, but if you are going from an existing registry and adding a new entry, you cannot just recreate the account with more space which you're doing here, you'll have to initialize a new account with more space
  2. Transferring lamports - your logic right now is technically correct but it's kinda wonky lol - you alr know system_instruction::create_account provides a certain amount of SOL, so I would recommend transferring the lamports from the current to new registry account first, and then calling system_instruction::create_account to fill in the rest

@vsiva308 vsiva308 marked this pull request as ready for review April 6, 2022 06:13
Copy link
Member

@alecchendev alecchendev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really great job. All the logic makes sense to me - just need to make sure it compiles + write some client code to test. I'll definitely help you out with that at tonight's meeting 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📎 Subscription Registry
2 participants