Skip to content

Commit 7857e28

Browse files
committed
simplify
1 parent afde8f3 commit 7857e28

File tree

2 files changed

+60
-84
lines changed

2 files changed

+60
-84
lines changed

examples/oft-solana/programs/oft/src/instructions/renounce_freeze.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,41 @@
11
use crate::*;
22
use anchor_lang::solana_program;
3-
use anchor_spl::token_2022::spl_token_2022;
4-
use anchor_spl::token_2022::spl_token_2022::instruction::AuthorityType;
5-
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};
3+
use anchor_spl::{
4+
token_2022::{
5+
spl_token_2022::instruction::AuthorityType,
6+
spl_token_2022::{self, solana_program::program_option::COption},
7+
},
8+
token_interface::{Mint, TokenInterface},
9+
};
610

711
#[derive(Accounts)]
812
#[instruction()]
913
pub struct RenounceFreezeAuthority<'info> {
1014
pub signer: Signer<'info>,
1115
#[account(
12-
mut,
1316
seeds = [OFT_SEED, oft_store.token_escrow.as_ref()],
1417
bump = oft_store.bump,
1518
constraint = is_valid_signer(signer.key(), &oft_store) @OFTError::Unauthorized
1619
)]
1720
pub oft_store: Account<'info, OFTStore>,
1821
#[account(
1922
mut,
20-
address = oft_store.token_escrow,
21-
token::authority = oft_store,
22-
token::mint = token_mint,
23-
token::token_program = token_program
24-
)]
25-
pub token_escrow: InterfaceAccount<'info, TokenAccount>,
26-
27-
#[account(
28-
mut,
23+
constraint = token_mint.freeze_authority.is_some() @CustomError::NoFreezeAuthority,
2924
address = oft_store.token_mint,
3025
mint::token_program = token_program
3126
)]
3227
pub token_mint: InterfaceAccount<'info, Mint>,
33-
#[account(mut)]
28+
#[account(
29+
constraint = token_mint.freeze_authority == COption::Some(current_authority.key()) @CustomError::InvalidFreezeAuthority
30+
)]
3431
pub current_authority: AccountInfo<'info>,
3532
pub token_program: Interface<'info, TokenInterface>,
3633
}
3734

3835
//
3936
impl RenounceFreezeAuthority<'_> {
4037
pub fn apply(ctx: Context<RenounceFreezeAuthority>) -> Result<()> {
41-
let mint = &ctx.accounts.token_mint;
42-
43-
// Ensure freeze authority is set
44-
require!(
45-
mint.freeze_authority.is_some(),
46-
CustomError::NoFreezeAuthority
47-
);
48-
49-
let oft_store_seed = ctx.accounts.token_escrow.key();
38+
let oft_store_seed = ctx.accounts.oft_store.token_escrow.key();
5039
let seeds: &[&[u8]] = &[
5140
OFT_SEED,
5241
oft_store_seed.as_ref(),
@@ -78,11 +67,12 @@ impl RenounceFreezeAuthority<'_> {
7867
}
7968
}
8069

81-
// Custom error for validation
8270
#[error_code]
8371
pub enum CustomError {
8472
#[msg("No freeze authority exists on this mint.")]
8573
NoFreezeAuthority,
74+
#[msg("The provided authority does not match the freeze authority.")]
75+
InvalidFreezeAuthority,
8676
}
8777

8878
fn is_valid_signer(signer: Pubkey, oft_store: &OFTStore) -> bool {
Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { AnchorProvider, Program } from '@coral-xyz/anchor'
22
import NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet'
3-
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
4-
import { PublicKey } from '@solana/web3.js'
3+
import { TOKEN_PROGRAM_ID, getMint } from '@solana/spl-token'
54
import { task } from 'hardhat/config'
65

6+
import { types } from '@layerzerolabs/devtools-evm-hardhat'
77
import { EndpointId } from '@layerzerolabs/lz-definitions'
88

99
import { deriveConnection, getExplorerTxLink } from './index'
@@ -12,79 +12,65 @@ interface Args {
1212
eid: EndpointId
1313
programId: string
1414
oftStore: string
15-
tokenEscrow: string
16-
tokenMint: string
17-
tokenProgram: string
18-
multisig: string
1915
computeUnitPriceScaleFactor: number
2016
}
2117

2218
// TODO: no need to pass in oft store? since we can derive
2319
task('lz:oft:solana:renounce-freeze', 'Renounce freeze authority for an OFT token')
2420
.addParam('eid', 'The endpoint ID', undefined, types.eid)
2521
.addParam('programId', 'The OFT program ID', undefined, types.string)
26-
.addParam('tokenEscrow', 'The OFT token escrow public key', undefined, types.string)
2722
.addParam('oftStore', 'The OFT Store public key', undefined, types.string)
28-
.addParam('tokenMint', 'The OFT token mint public key', undefined, types.string)
29-
.addParam('multisig', 'The multisig public key', undefined, types.string)
3023
.addParam('computeUnitPriceScaleFactor', 'The compute unit price scale factor', 4, types.float, true)
31-
.setAction(
32-
async ({
33-
eid,
34-
programId: programIdStr,
35-
oftStore: oftStoreStr,
36-
tokenEscrow: tokenEscrowStr,
37-
tokenMint: tokenMintStr,
38-
multisig: multiSigStr,
39-
computeUnitPriceScaleFactor,
40-
}: Args) => {
41-
const { connection, umi, umiWalletSigner, web3JsKeypair } = await deriveConnection(eid)
24+
.setAction(async ({ eid, programId: programIdStr, oftStore: oftStoreStr, computeUnitPriceScaleFactor }: Args) => {
25+
const { connection, umiWalletSigner, web3JsKeypair } = await deriveConnection(eid)
4226

43-
// TODO: clean up below block
44-
const wallet = new NodeWallet(web3JsKeypair)
45-
const provider = new AnchorProvider(connection, wallet, {
46-
commitment: 'processed',
47-
})
27+
// TODO: clean up below block
28+
const wallet = new NodeWallet(web3JsKeypair)
29+
const provider = new AnchorProvider(connection, wallet, {
30+
commitment: 'processed',
31+
})
4832

49-
const IDL = await import('../../target/idl/oft.json').then((module) => module.default)
50-
const anchorTypes = await import('../../target/types/oft').then((module) => module)
33+
const IDL = await import('../../target/idl/oft.json').then((module) => module.default)
34+
const types = await import('../../target/types/oft').then((module) => module)
5135

52-
// @ts-ignore we can ignore the IDL type error, which is a quirk of Anchor
53-
const program = new Program<typeof anchorTypes.IDL>(IDL, programIdStr, provider)
36+
// @ts-expect-error we can ignore the IDL type error, which is a quick or Anchor
37+
const program = new Program<typeof types.IDL>(IDL, programIdStr, provider)
5438

55-
const [oftStorePda, oftStoreBump] = PublicKey.findProgramAddressSync(
56-
[Buffer.from('OFT'), new PublicKey(tokenEscrowStr).toBuffer()],
57-
program.programId
58-
)
59-
if (oftStorePda.toString() != oftStoreStr) {
60-
throw new Error('Mismatch between Token Escrow address and derived OFT Store PDA')
61-
}
39+
const oftStoreAccount = await program.account.oftStore.fetch(oftStoreStr)
40+
const tokenMint = oftStoreAccount.tokenMint
6241

63-
const oftStoreAccountData = await program.account.oftStore.fetch(oftStoreStr)
42+
const mintAccount = await getMint(connection, tokenMint) // to support Token-2022, pass in program ID as third param here
6443

65-
const signerIsAdmin = umiWalletSigner.publicKey.toString() == oftStoreAccountData.admin.toString()
66-
if (!signerIsAdmin) {
67-
throw new Error('Your keypair is not the OFT Store admin.')
68-
}
44+
// we assume that the mint authority is set to the multisig
45+
const multisig = mintAccount.mintAuthority
6946

70-
// Call the method
71-
try {
72-
const tx = await program.methods
73-
.renounceFreeze() // Method name
74-
.accounts({
75-
signer: umiWalletSigner.publicKey,
76-
oftStore: oftStorePda,
77-
tokenEscrow: tokenEscrowStr,
78-
tokenMint: tokenMintStr,
79-
currentAuthority: multiSigStr,
80-
tokenProgram: TOKEN_PROGRAM_ID.toBase58(), // currently only supports SPL Token standard
81-
})
82-
.signers([web3JsKeypair])
83-
.rpc()
47+
if (!multisig) {
48+
throw new Error('Mint authority is not set.')
49+
}
50+
51+
const oftStoreAccountData = await program.account.oftStore.fetch(oftStoreStr)
52+
53+
const signerIsAdmin = umiWalletSigner.publicKey.toString() == oftStoreAccountData.admin.toString()
54+
if (!signerIsAdmin) {
55+
throw new Error('Your keypair is not the OFT Store admin.')
56+
}
57+
58+
// Call the method
59+
try {
60+
const tx = await program.methods
61+
.renounceFreeze() // Method name
62+
.accounts({
63+
signer: umiWalletSigner.publicKey,
64+
oftStore: oftStoreStr,
65+
tokenMint: tokenMint,
66+
currentAuthority: multisig,
67+
tokenProgram: TOKEN_PROGRAM_ID.toBase58(), // currently only supports SPL Token standard
68+
})
69+
.signers([web3JsKeypair])
70+
.rpc()
8471

85-
console.log('Transaction successful:', getExplorerTxLink(tx, eid == EndpointId.SOLANA_V2_TESTNET))
86-
} catch (err) {
87-
console.error('Transaction failed:', err)
88-
}
72+
console.log('Transaction successful:', getExplorerTxLink(tx, eid == EndpointId.SOLANA_V2_TESTNET))
73+
} catch (err) {
74+
console.error('Transaction failed:', err)
8975
}
90-
)
76+
})

0 commit comments

Comments
 (0)