Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 8674173

Browse files
authored
token-swap: Use find_program_address on-chain during init (#2363)
1 parent 40ab297 commit 8674173

File tree

7 files changed

+81
-137
lines changed

7 files changed

+81
-137
lines changed

token-swap/js/cli/token-swap-test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import {Numberu64} from '../dist';
1919
let tokenSwap: TokenSwap;
2020
// authority of the token and accounts
2121
let authority: PublicKey;
22-
// nonce used to generate the authority public key
23-
let nonce: number;
22+
// bump seed used to generate the authority public key
23+
let bumpSeed: number;
2424
// owner of the user accounts
2525
let owner: Account;
2626
// Token pool
@@ -95,7 +95,7 @@ export async function createTokenSwap(
9595
owner = await newAccountWithLamports(connection, 1000000000);
9696
const tokenSwapAccount = new Account();
9797

98-
[authority, nonce] = await PublicKey.findProgramAddress(
98+
[authority, bumpSeed] = await PublicKey.findProgramAddress(
9999
[tokenSwapAccount.publicKey.toBuffer()],
100100
TOKEN_SWAP_PROGRAM_ID,
101101
);
@@ -161,7 +161,6 @@ export async function createTokenSwap(
161161
tokenAccountPool,
162162
TOKEN_SWAP_PROGRAM_ID,
163163
TOKEN_PROGRAM_ID,
164-
nonce,
165164
TRADING_FEE_NUMERATOR,
166165
TRADING_FEE_DENOMINATOR,
167166
OWNER_TRADING_FEE_NUMERATOR,

token-swap/js/src/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Numberu64 extends BN {
5757
export const TokenSwapLayout = BufferLayout.struct([
5858
BufferLayout.u8('version'),
5959
BufferLayout.u8('isInitialized'),
60-
BufferLayout.u8('nonce'),
60+
BufferLayout.u8('bumpSeed'),
6161
Layout.publicKey('tokenProgramId'),
6262
Layout.publicKey('tokenAccountA'),
6363
Layout.publicKey('tokenAccountB'),
@@ -180,7 +180,6 @@ export class TokenSwap {
180180
tokenAccountPool: PublicKey,
181181
tokenProgramId: PublicKey,
182182
swapProgramId: PublicKey,
183-
nonce: number,
184183
tradeFeeNumerator: number,
185184
tradeFeeDenominator: number,
186185
ownerTradeFeeNumerator: number,
@@ -204,7 +203,6 @@ export class TokenSwap {
204203
];
205204
const commandDataLayout = BufferLayout.struct([
206205
BufferLayout.u8('instruction'),
207-
BufferLayout.u8('nonce'),
208206
BufferLayout.nu64('tradeFeeNumerator'),
209207
BufferLayout.nu64('tradeFeeDenominator'),
210208
BufferLayout.nu64('ownerTradeFeeNumerator'),
@@ -228,7 +226,6 @@ export class TokenSwap {
228226
const encodeLength = commandDataLayout.encode(
229227
{
230228
instruction: 0, // InitializeSwap instruction
231-
nonce,
232229
tradeFeeNumerator,
233230
tradeFeeDenominator,
234231
ownerTradeFeeNumerator,
@@ -334,7 +331,6 @@ export class TokenSwap {
334331
* @param payer Pays for the transaction
335332
* @param tokenSwapAccount The token swap account
336333
* @param authority The authority over the swap and accounts
337-
* @param nonce The nonce used to generate the authority
338334
* @param tokenAccountA: The token swap's Token A account
339335
* @param tokenAccountB: The token swap's Token B account
340336
* @param poolToken The pool token
@@ -359,7 +355,6 @@ export class TokenSwap {
359355
tokenAccountPool: PublicKey,
360356
swapProgramId: PublicKey,
361357
tokenProgramId: PublicKey,
362-
nonce: number,
363358
tradeFeeNumerator: number,
364359
tradeFeeDenominator: number,
365360
ownerTradeFeeNumerator: number,
@@ -421,7 +416,6 @@ export class TokenSwap {
421416
tokenAccountPool,
422417
tokenProgramId,
423418
swapProgramId,
424-
nonce,
425419
tradeFeeNumerator,
426420
tradeFeeDenominator,
427421
ownerTradeFeeNumerator,

token-swap/program/fuzz/src/native_token_swap.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use solana_program::{bpf_loader, entrypoint::ProgramResult, pubkey::Pubkey, syst
1919

2020
pub struct NativeTokenSwap {
2121
pub user_account: NativeAccountData,
22-
pub nonce: u8,
22+
pub bump_seed: u8,
2323
pub authority_account: NativeAccountData,
2424
pub fees: Fees,
2525
pub swap_curve: SwapCurve,
@@ -51,7 +51,7 @@ impl NativeTokenSwap {
5151
user_account.is_signer = true;
5252
let mut swap_account =
5353
NativeAccountData::new(SwapVersion::LATEST_LEN, spl_token_swap::id());
54-
let (authority_key, nonce) = Pubkey::find_program_address(
54+
let (authority_key, bump_seed) = Pubkey::find_program_address(
5555
&[&swap_account.key.to_bytes()[..]],
5656
&spl_token_swap::id(),
5757
);
@@ -86,7 +86,6 @@ impl NativeTokenSwap {
8686
&pool_mint_account.key,
8787
&pool_fee_account.key,
8888
&pool_token_account.key,
89-
nonce,
9089
fees.clone(),
9190
swap_curve.clone(),
9291
)
@@ -109,7 +108,7 @@ impl NativeTokenSwap {
109108

110109
Self {
111110
user_account,
112-
nonce,
111+
bump_seed,
113112
authority_account,
114113
fees,
115114
swap_curve,

token-swap/program/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub enum SwapError {
1212
#[error("Swap account already in use")]
1313
AlreadyInUse,
1414
/// The program address provided doesn't match the value generated by the program.
15-
#[error("Invalid program address generated from nonce and key")]
15+
#[error("Invalid program address generated from bump seed and key")]
1616
InvalidProgramAddress,
1717
/// The owner of the input isn't set to the program address generated by the program.
1818
#[error("Input account owner is not the program address")]

token-swap/program/src/instruction.rs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ use arbitrary::Arbitrary;
2020
#[repr(C)]
2121
#[derive(Debug, PartialEq)]
2222
pub struct Initialize {
23-
/// nonce used to create valid program address
24-
pub nonce: u8,
2523
/// all swap fees
2624
pub fees: Fees,
2725
/// swap curve info for pool, including CurveType and anything
@@ -195,16 +193,11 @@ impl SwapInstruction {
195193
let (&tag, rest) = input.split_first().ok_or(SwapError::InvalidInstruction)?;
196194
Ok(match tag {
197195
0 => {
198-
let (&nonce, rest) = rest.split_first().ok_or(SwapError::InvalidInstruction)?;
199196
if rest.len() >= Fees::LEN {
200197
let (fees, rest) = rest.split_at(Fees::LEN);
201198
let fees = Fees::unpack_unchecked(fees)?;
202199
let swap_curve = SwapCurve::unpack_unchecked(rest)?;
203-
Self::Initialize(Initialize {
204-
nonce,
205-
fees,
206-
swap_curve,
207-
})
200+
Self::Initialize(Initialize { fees, swap_curve })
208201
} else {
209202
return Err(SwapError::InvalidInstruction.into());
210203
}
@@ -275,13 +268,8 @@ impl SwapInstruction {
275268
pub fn pack(&self) -> Vec<u8> {
276269
let mut buf = Vec::with_capacity(size_of::<Self>());
277270
match &*self {
278-
Self::Initialize(Initialize {
279-
nonce,
280-
fees,
281-
swap_curve,
282-
}) => {
271+
Self::Initialize(Initialize { fees, swap_curve }) => {
283272
buf.push(0);
284-
buf.push(*nonce);
285273
let mut fees_slice = [0u8; Fees::LEN];
286274
Pack::pack_into_slice(fees, &mut fees_slice[..]);
287275
buf.extend_from_slice(&fees_slice);
@@ -351,15 +339,10 @@ pub fn initialize(
351339
pool_pubkey: &Pubkey,
352340
fee_pubkey: &Pubkey,
353341
destination_pubkey: &Pubkey,
354-
nonce: u8,
355342
fees: Fees,
356343
swap_curve: SwapCurve,
357344
) -> Result<Instruction, ProgramError> {
358-
let init_data = SwapInstruction::Initialize(Initialize {
359-
nonce,
360-
fees,
361-
swap_curve,
362-
});
345+
let init_data = SwapInstruction::Initialize(Initialize { fees, swap_curve });
363346
let data = init_data.pack();
364347

365348
let accounts = vec![
@@ -606,21 +589,16 @@ mod tests {
606589
host_fee_numerator,
607590
host_fee_denominator,
608591
};
609-
let nonce: u8 = 255;
610592
let amp: u64 = 1;
611593
let curve_type = CurveType::Stable;
612594
let calculator = Box::new(StableCurve { amp });
613595
let swap_curve = SwapCurve {
614596
curve_type,
615597
calculator,
616598
};
617-
let check = SwapInstruction::Initialize(Initialize {
618-
nonce,
619-
fees,
620-
swap_curve,
621-
});
599+
let check = SwapInstruction::Initialize(Initialize { fees, swap_curve });
622600
let packed = check.pack();
623-
let mut expect = vec![0u8, nonce];
601+
let mut expect = vec![0u8];
624602
expect.extend_from_slice(&trade_fee_numerator.to_le_bytes());
625603
expect.extend_from_slice(&trade_fee_denominator.to_le_bytes());
626604
expect.extend_from_slice(&owner_trade_fee_numerator.to_le_bytes());

0 commit comments

Comments
 (0)