Skip to content

Commit

Permalink
Adding scaling fees based on native SVM rent (#139)
Browse files Browse the repository at this point in the history
* Adding scaling fees based on native SVM rent.

* Fix upload.
  • Loading branch information
blockiosaurus authored Sep 8, 2024
1 parent 8110778 commit 65c3a71
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-programs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ jobs:
name: program-builds
# First wildcard ensures exported paths are consistently under the programs folder.
path: ./program*/.bin/*.so
include-hidden-files: true
if-no-files-found: error
11 changes: 10 additions & 1 deletion programs/token-metadata/program/src/state/fee.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
use super::*;
use solana_program::{rent::Rent, sysvar::Sysvar};

pub(crate) const FEE_AUTHORITY: Pubkey = pubkey!("Levytx9LLPzAtDJJD7q813Zsm8zg9e1pb53mGxTKpD7");

const CREATE_FEE_SCALAR: usize = 1308;
const CREATE_FEE_OFFSET: u64 = 5440;
// create_metadata_accounts_v3, create, print edition commands
pub const CREATE_FEE: u64 = 10_000_000;
pub fn get_create_fee() -> Result<u64, ProgramError> {
let rent = Rent::get()?.minimum_balance(CREATE_FEE_SCALAR);

Ok(rent
.checked_add(CREATE_FEE_OFFSET)
.ok_or(MetadataError::NumericalOverflowError)?)
}

pub const FEE_FLAG_SET: u8 = 1;
pub const FEE_FLAG_CLEARED: u8 = 0;
4 changes: 2 additions & 2 deletions programs/token-metadata/program/src/utils/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use solana_program::{
sysvar::Sysvar,
};

use crate::state::{fee::CREATE_FEE, Metadata, TokenMetadataAccount, METADATA_FEE_FLAG_INDEX};
use crate::state::{get_create_fee, Metadata, TokenMetadataAccount, METADATA_FEE_FLAG_INDEX};

#[repr(C)]
#[derive(Debug, Clone, Copy)]
Expand All @@ -16,7 +16,7 @@ pub(crate) fn levy(args: LevyArgs) -> ProgramResult {
// Fund metadata account with rent + Metaplex fee.
let rent = Rent::get()?;

let fee = CREATE_FEE + rent.minimum_balance(Metadata::size());
let fee = get_create_fee()? + rent.minimum_balance(Metadata::size());

invoke(
&solana_program::system_instruction::transfer(
Expand Down
6 changes: 3 additions & 3 deletions programs/token-metadata/program/tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod fees {
};
use token_metadata::{
instruction::{collect_fees, BurnArgs, UpdateArgs},
state::{CREATE_FEE, FEE_FLAG_CLEARED, METADATA_FEE_FLAG_INDEX},
state::{FEE_FLAG_CLEARED, METADATA_FEE_FLAG_INDEX},
};

use super::*;
Expand Down Expand Up @@ -132,7 +132,7 @@ mod fees {
println!("Transaction size: {:?}", tx.message().serialize().len());
context.banks_client.process_transaction(tx).await.unwrap();

let expected_balance = num_accounts * CREATE_FEE;
let expected_balance = num_accounts * SOLANA_CREATE_FEE;

let recipient_balance = get_account(&mut context, &recipient.pubkey())
.await
Expand Down Expand Up @@ -205,7 +205,7 @@ mod fees {
);
context.banks_client.process_transaction(tx).await.unwrap();

let expected_balance = CREATE_FEE;
let expected_balance = SOLANA_CREATE_FEE;

let recipient_balance = get_account(&mut context, &recipient.pubkey())
.await
Expand Down
7 changes: 4 additions & 3 deletions programs/token-metadata/program/tests/utils/digital_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ use token_metadata::{
state::{
AssetData, Collection, CollectionDetails, Creator, MasterEditionV2, Metadata, PrintSupply,
ProgrammableConfig, TokenDelegateRole, TokenMetadataAccount, TokenRecord, TokenStandard,
CREATE_FEE, EDITION, EDITION_MARKER_BIT_SIZE, FEE_FLAG_SET, METADATA_FEE_FLAG_INDEX,
PREFIX,
EDITION, EDITION_MARKER_BIT_SIZE, FEE_FLAG_SET, METADATA_FEE_FLAG_INDEX, PREFIX,
},
utils::unpack,
ID,
};

use crate::SOLANA_CREATE_FEE;

use super::{airdrop, create_mint, create_token_account, get_account, mint_tokens};

pub const DEFAULT_NAME: &str = "Digital Asset";
Expand Down Expand Up @@ -1473,7 +1474,7 @@ impl DigitalAsset {
let rent = context.banks_client.get_rent().await.unwrap();
let rent_exempt = rent.minimum_balance(account.data.len());

let expected_lamports = rent_exempt + CREATE_FEE;
let expected_lamports = rent_exempt + SOLANA_CREATE_FEE;

assert_eq!(account.lamports, expected_lamports);
assert_eq!(account.data[METADATA_FEE_FLAG_INDEX], FEE_FLAG_SET);
Expand Down
5 changes: 2 additions & 3 deletions programs/token-metadata/program/tests/utils/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use token_metadata::{
instruction,
state::{
Collection, CollectionDetails, Creator, DataV2, Metadata as TmMetadata,
TokenMetadataAccount, TokenStandard, Uses, CREATE_FEE, FEE_FLAG_SET,
METADATA_FEE_FLAG_INDEX, PREFIX,
TokenMetadataAccount, TokenStandard, Uses, FEE_FLAG_SET, METADATA_FEE_FLAG_INDEX, PREFIX,
},
ID,
};
Expand Down Expand Up @@ -679,7 +678,7 @@ impl Metadata {
let rent = context.banks_client.get_rent().await.unwrap();
let rent_exempt = rent.minimum_balance(account.data.len());

let expected_lamports = rent_exempt + CREATE_FEE;
let expected_lamports = rent_exempt + SOLANA_CREATE_FEE;

assert_eq!(account.lamports, expected_lamports);
assert_eq!(account.data[METADATA_FEE_FLAG_INDEX], FEE_FLAG_SET);
Expand Down
2 changes: 2 additions & 0 deletions programs/token-metadata/program/tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub const DEFAULT_COLLECTION_DETAILS: Option<CollectionDetails> = {
Some(CollectionDetails::V1 { size: 0 })
};

pub const SOLANA_CREATE_FEE: u64 = 10_000_000;

pub fn program_test() -> ProgramTest {
let mut program_test = ProgramTest::new("token_metadata", token_metadata::ID, None);
program_test.add_program("spl_token_2022", spl_token_2022::ID, None);
Expand Down

0 comments on commit 65c3a71

Please sign in to comment.