-
Notifications
You must be signed in to change notification settings - Fork 251
feat(oft-solana): create new instruction for renouncing freeze authority #1144
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
Draft
nazreen
wants to merge
5
commits into
main
Choose a base branch
from
nazreen/oft-solana/renounce-freeze
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
examples/oft-solana/programs/oft/src/instructions/renounce_freeze.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| use crate::*; | ||
| use anchor_lang::solana_program; | ||
| use anchor_spl::{ | ||
| token_2022::{ | ||
| spl_token_2022::instruction::AuthorityType, | ||
| spl_token_2022::{self, solana_program::program_option::COption}, | ||
| }, | ||
| token_interface::{Mint, TokenInterface}, | ||
| }; | ||
|
|
||
| #[derive(Accounts)] | ||
| #[instruction()] | ||
| pub struct RenounceFreezeAuthority<'info> { | ||
| pub admin: Signer<'info>, | ||
| #[account( | ||
| seeds = [OFT_SEED, oft_store.token_escrow.as_ref()], | ||
| bump = oft_store.bump, | ||
| has_one = admin @OFTError::Unauthorized | ||
| )] | ||
| pub oft_store: Account<'info, OFTStore>, | ||
| #[account( | ||
| mut, | ||
| constraint = token_mint.freeze_authority.is_some() @CustomError::NoFreezeAuthority, | ||
| address = oft_store.token_mint, | ||
| mint::token_program = token_program | ||
| )] | ||
| pub token_mint: InterfaceAccount<'info, Mint>, | ||
| #[account( | ||
| constraint = token_mint.freeze_authority == COption::Some(current_authority.key()) @CustomError::InvalidFreezeAuthority | ||
| )] | ||
| pub current_authority: AccountInfo<'info>, | ||
| pub token_program: Interface<'info, TokenInterface>, | ||
| } | ||
|
|
||
| // | ||
| impl RenounceFreezeAuthority<'_> { | ||
| pub fn apply(ctx: Context<RenounceFreezeAuthority>) -> Result<()> { | ||
| let oft_store_seed = ctx.accounts.oft_store.token_escrow.key(); | ||
| let seeds: &[&[u8]] = &[ | ||
| OFT_SEED, | ||
| oft_store_seed.as_ref(), | ||
| &[ctx.accounts.oft_store.bump], | ||
| ]; | ||
|
|
||
| // Create the set_authority instruction directly | ||
| let ix = spl_token_2022::instruction::set_authority( | ||
| ctx.accounts.token_program.key, | ||
| &ctx.accounts.token_mint.key(), | ||
| None, // new authority | ||
| AuthorityType::FreezeAccount, | ||
| &ctx.accounts.current_authority.key(), | ||
| &[&ctx.accounts.oft_store.key()], | ||
| )?; | ||
|
|
||
| // Invoke with signing | ||
| solana_program::program::invoke_signed( | ||
| &ix, | ||
| &[ | ||
| ctx.accounts.token_mint.to_account_info(), | ||
| ctx.accounts.current_authority.to_account_info(), | ||
| ctx.accounts.oft_store.to_account_info(), | ||
| ], | ||
| &[&seeds], | ||
| )?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[error_code] | ||
| pub enum CustomError { | ||
| #[msg("No freeze authority exists on this mint.")] | ||
| NoFreezeAuthority, | ||
| #[msg("The provided authority does not match the freeze authority.")] | ||
| InvalidFreezeAuthority, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { AnchorProvider, Program } from '@coral-xyz/anchor' | ||
| import NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet' | ||
| import { TOKEN_PROGRAM_ID, getMint } from '@solana/spl-token' | ||
| import { task } from 'hardhat/config' | ||
|
|
||
| import { types } from '@layerzerolabs/devtools-evm-hardhat' | ||
| import { EndpointId } from '@layerzerolabs/lz-definitions' | ||
|
|
||
| import { deriveConnection, getExplorerTxLink } from './index' | ||
|
|
||
| interface Args { | ||
| eid: EndpointId | ||
| programId: string | ||
| oftStore: string | ||
| computeUnitPriceScaleFactor: number | ||
| } | ||
|
|
||
| // TODO: no need to pass in oft store? since we can derive | ||
| task('lz:oft:solana:renounce-freeze', 'Renounce freeze authority for an OFT token') | ||
| .addParam('eid', 'The endpoint ID', undefined, types.eid) | ||
| .addParam('programId', 'The OFT program ID', undefined, types.string) | ||
| .addParam('oftStore', 'The OFT Store public key', undefined, types.string) | ||
| .addParam('computeUnitPriceScaleFactor', 'The compute unit price scale factor', 4, types.float, true) | ||
| .setAction(async ({ eid, programId: programIdStr, oftStore: oftStoreStr, computeUnitPriceScaleFactor }: Args) => { | ||
| const { connection, umiWalletSigner, web3JsKeypair } = await deriveConnection(eid) | ||
|
|
||
| // TODO: clean up below block | ||
| const wallet = new NodeWallet(web3JsKeypair) | ||
| const provider = new AnchorProvider(connection, wallet, { | ||
| commitment: 'processed', | ||
| }) | ||
|
|
||
| const IDL = await import('../../target/idl/oft.json').then((module) => module.default) | ||
| const types = await import('../../target/types/oft').then((module) => module) | ||
|
|
||
| // @ts-expect-error we can ignore the IDL type error, which is a quick or Anchor | ||
| const program = new Program<typeof types.IDL>(IDL, programIdStr, provider) | ||
|
|
||
| const oftStoreAccount = await program.account.oftStore.fetch(oftStoreStr) | ||
| const tokenMint = oftStoreAccount.tokenMint | ||
|
|
||
| const mintAccount = await getMint(connection, tokenMint) // to support Token-2022, pass in program ID as third param here | ||
|
|
||
| // we assume that the mint authority is set to the multisig | ||
| const multisig = mintAccount.mintAuthority | ||
|
|
||
| if (!multisig) { | ||
| throw new Error('Mint authority is not set.') | ||
| } | ||
|
|
||
| const oftStoreAccountData = await program.account.oftStore.fetch(oftStoreStr) | ||
|
|
||
| const signerIsAdmin = umiWalletSigner.publicKey.toString() == oftStoreAccountData.admin.toString() | ||
| if (!signerIsAdmin) { | ||
| throw new Error('Your keypair is not the OFT Store admin.') | ||
| } | ||
|
|
||
| // Call the method | ||
| try { | ||
| const tx = await program.methods | ||
| .renounceFreeze() // Method name | ||
| .accounts({ | ||
| signer: umiWalletSigner.publicKey, | ||
| oftStore: oftStoreStr, | ||
| tokenMint: tokenMint, | ||
| currentAuthority: multisig, | ||
| tokenProgram: TOKEN_PROGRAM_ID.toBase58(), // currently only supports SPL Token standard | ||
| }) | ||
| .signers([web3JsKeypair]) | ||
| .rpc() | ||
|
|
||
| console.log('Transaction successful:', getExplorerTxLink(tx, eid == EndpointId.SOLANA_V2_TESTNET)) | ||
| } catch (err) { | ||
| console.error('Transaction failed:', err) | ||
| } | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.