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

fix: use SPL ZRC20 withdraw fee to cover the ATA account creation rent fee #67

Merged
merged 7 commits into from
Dec 3, 2024

Conversation

brewmaster012
Copy link
Contributor

@brewmaster012 brewmaster012 commented Dec 3, 2024

closes #63
closes #4

conflicts with PR #65 : should only consider merging one.

important: all ZRC20 of SPL tokens must charge at least 2039280 lamports (0.002039280 SOL) as withdraw fee, otherwise the program PDA account might run into deficit.

assuming the withdrawal already paid at least this much in ZRC20 withdraw fee.
@codecov-commenter
Copy link

codecov-commenter commented Dec 3, 2024

Codecov Report

Attention: Patch coverage is 0% with 16 lines in your changes missing coverage. Please review.

Project coverage is 5.85%. Comparing base (7fb0a66) to head (505b74b).

Files with missing lines Patch % Lines
programs/protocol-contracts-solana/src/lib.rs 0.00% 16 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##            main     #67      +/-   ##
========================================
- Coverage   6.32%   5.85%   -0.48%     
========================================
  Files          1       1              
  Lines        411     410       -1     
========================================
- Hits          26      24       -2     
- Misses       385     386       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@lumtis lumtis left a comment

Choose a reason for hiding this comment

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

@skosito please have a look

@@ -386,6 +386,9 @@ pub mod gateway {
Errors::SPLAtaAndMintAddressMismatch,
);

let cost_gas = 5000; // default gas cost in lamports
let cost_ata_create = &mut 2039280; // default SPL ATA account creation rent fee in lamports
Copy link
Member

Choose a reason for hiding this comment

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

Is it an universal constant? Any link we can provide how the value is determined? I would use a ceiling value to take some more security like 2040000 or 2100000

Copy link
Contributor

Choose a reason for hiding this comment

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

i think it could be something that is calculated based on rent and space that ATA takes, but lets wait for @brewmaster012

this is same value i came up with by some research

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this number is only for ZRC20 gas_limit configuration reference. The cost_ata_create gets overwritten right after by *cost_ata_create = bal_before - bal_after; without being read.

I'm not against using rounded number because I myself also used it for Solana rent exampt here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it should be a constant, but let me see if using a computed is viable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we'll use whatever the CPI call actually debits from the signer. No longer need this constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in a92a84f

@@ -386,6 +386,9 @@ pub mod gateway {
Errors::SPLAtaAndMintAddressMismatch,
);

let cost_gas = 5000; // default gas cost in lamports
let cost_ata_create = &mut 2039280; // default SPL ATA account creation rent fee in lamports
Copy link
Contributor

Choose a reason for hiding this comment

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

i think it could be something that is calculated based on rent and space that ATA takes, but lets wait for @brewmaster012

this is same value i came up with by some research


let rent_payer_info = ctx.accounts.rent_payer_pda.to_account_info();
let cost = bal_before - bal_after;
rent_payer_info.sub_lamports(cost)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

can we remove rent_payer from program completely? it would be one less account in withdraw_spl (would be small change on zetaclient side too) and also removing function to init rent payer

anyways this will need update in e2e test covering this which we have on node atm, but it will be small change, i can take care of it when we merge this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, but i thought about it, we might have a need to store withdraw fee excess in a segregated account; this rent_payer_pda might be it.

this helps distributing fee excess to say zeta delegators easier in the future, so i say we keep it but not initialize it yet.

Copy link
Contributor Author

@brewmaster012 brewmaster012 Dec 3, 2024

Choose a reason for hiding this comment

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

actually this might not make much sense, let me remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed rent payer PDA in 4617aa4

programs/protocol-contracts-solana/src/lib.rs Show resolved Hide resolved
@@ -452,6 +447,12 @@ pub mod gateway {

transfer_checked(xfer_ctx, amount, decimals)?;
msg!("withdraw spl token successfully");
// Note: this pda.sub_lamports() must be done here due to this issue https://github.com/solana-labs/solana/issues/9711
Copy link
Contributor

Choose a reason for hiding this comment

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

in case ata already exists, this looks redundant, maybe we can have flag in if condition that checks for ata existance and based on that we do refunding here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in a92a84f

tests/protocol-contracts-solana.ts Outdated Show resolved Hide resolved
tests/protocol-contracts-solana.ts Outdated Show resolved Hide resolved

const to = await spl.getAssociatedTokenAddress(mint.publicKey, wallet2.publicKey);

let to_ata_bal = await conn.getBalance(to);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: there is no way to check that ata doesnt exist? it still might exist with 0 balance so test might be false positive here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

don't think a created account can have 0 lamports--it needs minimum rent

tests/protocol-contracts-solana.ts Outdated Show resolved Hide resolved
@@ -452,6 +447,12 @@ pub mod gateway {

transfer_checked(xfer_ctx, amount, decimals)?;
msg!("withdraw spl token successfully");
// Note: this pda.sub_lamports() must be done here due to this issue https://github.com/solana-labs/solana/issues/9711
// otherwise the previous CPI calls might fail with error:
Copy link
Contributor

Choose a reason for hiding this comment

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

if we can add more details on how we guarantee that there always will be funds in pda and no way to drain it that would be great, since it's not obvious from the program only

Copy link
Member

@lumtis lumtis left a comment

Choose a reason for hiding this comment

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

LGTM

@brewmaster012 brewmaster012 merged commit bdfc976 into main Dec 3, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants