Skip to content

Commit

Permalink
token-2022: Add scaled amount extension (#7511)
Browse files Browse the repository at this point in the history
* token-2022: Add scaled amount extension

#### Problem

The interest-bearing extension is useful for tokens that accrue in value
constantly, but many "rebasing" tokens on other blockchains employ a
different method of updating the number of tokens in accounts.

Rather than setting a rate and allowing the number to change
automatically over time, they set a scaling factor for the tokens by
hand.

#### Summary of changes

Add a new `ScaledUiAmount` extension to token-2022 for doing just that.
This is essentially a simplified version of the interest-bearing
extension, where someone just sets a scaling value into the mint
directly. The scale has no impact on the operation of the token, just on
the output of `amount_to_ui_amount` and `ui_amount_to_amount`.

* Add timestamp, rename to "multiplier"

* Update token/program-2022/src/extension/scaled_ui_amount/mod.rs

Co-authored-by: samkim-crypto <[email protected]>

* Address feedback

---------

Co-authored-by: samkim-crypto <[email protected]>
  • Loading branch information
joncinque and samkim-crypto authored Nov 25, 2024
1 parent 74e2559 commit 4349310
Show file tree
Hide file tree
Showing 10 changed files with 1,097 additions and 4 deletions.
44 changes: 42 additions & 2 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ use {
ConfidentialTransferFeeConfig,
},
cpi_guard, default_account_state, group_member_pointer, group_pointer,
interest_bearing_mint, memo_transfer, metadata_pointer, transfer_fee, transfer_hook,
BaseStateWithExtensions, Extension, ExtensionType, StateWithExtensionsOwned,
interest_bearing_mint, memo_transfer, metadata_pointer, scaled_ui_amount, transfer_fee,
transfer_hook, BaseStateWithExtensions, Extension, ExtensionType,
StateWithExtensionsOwned,
},
instruction, offchain,
solana_zk_sdk::{
Expand Down Expand Up @@ -188,6 +189,10 @@ pub enum ExtensionInitializationParams {
authority: Option<Pubkey>,
member_address: Option<Pubkey>,
},
ScaledUiAmountConfig {
authority: Option<Pubkey>,
multiplier: f64,
},
}
impl ExtensionInitializationParams {
/// Get the extension type associated with the init params
Expand All @@ -207,6 +212,7 @@ impl ExtensionInitializationParams {
}
Self::GroupPointer { .. } => ExtensionType::GroupPointer,
Self::GroupMemberPointer { .. } => ExtensionType::GroupMemberPointer,
Self::ScaledUiAmountConfig { .. } => ExtensionType::ScaledUiAmount,
}
}
/// Generate an appropriate initialization instruction for the given mint
Expand Down Expand Up @@ -316,6 +322,15 @@ impl ExtensionInitializationParams {
authority,
member_address,
),
Self::ScaledUiAmountConfig {
authority,
multiplier,
} => scaled_ui_amount::instruction::initialize(
token_program_id,
mint,
authority,
multiplier,
),
}
}
}
Expand Down Expand Up @@ -1805,6 +1820,31 @@ where
.await
}

/// Update multiplier
pub async fn update_multiplier<S: Signers>(
&self,
authority: &Pubkey,
new_multiplier: f64,
new_multiplier_effective_timestamp: i64,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let signing_pubkeys = signing_keypairs.pubkeys();
let multisig_signers = self.get_multisig_signers(authority, &signing_pubkeys);

self.process_ixs(
&[scaled_ui_amount::instruction::update_multiplier(
&self.program_id,
self.get_address(),
authority,
&multisig_signers,
new_multiplier,
new_multiplier_effective_timestamp,
)?],
signing_keypairs,
)
.await
}

/// Update transfer hook program id
pub async fn update_transfer_hook_program_id<S: Signers>(
&self,
Expand Down
Loading

0 comments on commit 4349310

Please sign in to comment.