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

Commit 56dd582

Browse files
authored
Remove supply from Token (#82)
1 parent a831edd commit 56dd582

File tree

9 files changed

+121
-341
lines changed

9 files changed

+121
-341
lines changed

token-swap/inc/token-swap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ typedef enum TokenSwap_SwapInstruction_Tag {
7373
* 0. `[]` Token-swap
7474
* 1. `[]` $authority
7575
* 2. `[writable]` SOURCE Pool account, amount is transferable by $authority.
76-
* 4. `[writable]` Pool MINT account, $authority is the owner.
7776
* 5. `[writable]` token_a Account to withdraw FROM.
7877
* 6. `[writable]` token_b Account to withdraw FROM.
7978
* 7. `[writable]` token_a user Account.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ export async function withdraw(): Promise<void> {
244244
await tokenSwap.withdraw(
245245
authority,
246246
tokenAccountPool,
247-
tokenPool.publicKey,
248247
tokenAccountA,
249248
tokenAccountB,
250249
userAccountA,

token-swap/js/client/token-swap.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ export class TokenSwap {
453453
async withdraw(
454454
authority: PublicKey,
455455
sourcePoolAccount: PublicKey,
456-
poolToken: PublicKey,
457456
fromA: PublicKey,
458457
fromB: PublicKey,
459458
userAccountA: PublicKey,
@@ -468,7 +467,6 @@ export class TokenSwap {
468467
this.withdrawInstruction(
469468
authority,
470469
sourcePoolAccount,
471-
poolToken,
472470
fromA,
473471
fromB,
474472
userAccountA,
@@ -483,7 +481,6 @@ export class TokenSwap {
483481
withdrawInstruction(
484482
authority: PublicKey,
485483
sourcePoolAccount: PublicKey,
486-
poolToken: PublicKey,
487484
fromA: PublicKey,
488485
fromB: PublicKey,
489486
userAccountA: PublicKey,
@@ -509,7 +506,6 @@ export class TokenSwap {
509506
{pubkey: this.tokenSwap, isSigner: false, isWritable: false},
510507
{pubkey: authority, isSigner: false, isWritable: false},
511508
{pubkey: sourcePoolAccount, isSigner: false, isWritable: true},
512-
{pubkey: poolToken, isSigner: false, isWritable: true},
513509
{pubkey: fromA, isSigner: false, isWritable: true},
514510
{pubkey: fromB, isSigner: false, isWritable: true},
515511
{pubkey: userAccountA, isSigner: false, isWritable: true},

token-swap/src/lib.rs

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ pub enum SwapInstruction {
8080
/// 0. `[]` Token-swap
8181
/// 1. `[]` $authority
8282
/// 2. `[writable]` SOURCE Pool account, amount is transferable by $authority.
83-
/// 4. `[writable]` Pool MINT account, $authority is the owner.
8483
/// 5. `[writable]` token_a Account to withdraw FROM.
8584
/// 6. `[writable]` token_b Account to withdraw FROM.
8685
/// 7. `[writable]` token_a user Account.
@@ -289,7 +288,6 @@ pub enum State {
289288
struct Invariant {
290289
token_a: u64,
291290
token_b: u64,
292-
pool: Option<u64>,
293291
fee: Fee,
294292
}
295293
impl Invariant {
@@ -310,17 +308,6 @@ impl Invariant {
310308
fn exchange_rate(&self, token_a: u64) -> Option<u64> {
311309
token_a.checked_mul(self.token_b)?.checked_div(self.token_a)
312310
}
313-
fn redeem(&self, user_pool: u64) -> Option<(u64, u64)> {
314-
let token_a = self
315-
.token_a
316-
.checked_mul(user_pool)?
317-
.checked_div(self.pool?)?;
318-
let token_b = self
319-
.token_b
320-
.checked_mul(user_pool)?
321-
.checked_div(self.pool?)?;
322-
Some((token_a, token_b))
323-
}
324311
}
325312

326313
impl State {
@@ -403,20 +390,13 @@ impl State {
403390
token_program_id: &Pubkey,
404391
swap: &Pubkey,
405392
burn_account: &Pubkey,
406-
mint: &Pubkey,
407393
authority: &Pubkey,
408394
amount: u64,
409395
) -> Result<(), ProgramError> {
410396
let swap_string = swap.to_string();
411397
let signers = &[&[&swap_string[..32]][..]];
412-
let ix = spl_token::instruction::burn(
413-
token_program_id,
414-
burn_account,
415-
mint,
416-
authority,
417-
&[],
418-
amount,
419-
)?;
398+
let ix =
399+
spl_token::instruction::burn(token_program_id, burn_account, authority, &[], amount)?;
420400
invoke_signed(&ix, accounts, signers)
421401
}
422402

@@ -500,9 +480,6 @@ impl State {
500480
if spl_token::option::COption::Some(*authority_info.key) != pool_mint.owner {
501481
return Err(Error::InvalidOwner.into());
502482
}
503-
if 0 != pool_mint.info.supply {
504-
return Err(Error::InvalidSupply.into());
505-
}
506483
if token_b.amount == 0 {
507484
return Err(Error::InvalidSupply.into());
508485
}
@@ -573,7 +550,6 @@ impl State {
573550
token_a: into_token.amount,
574551
token_b: from_token.amount,
575552
fee: token_swap.fee,
576-
pool: None,
577553
};
578554
let output = invariant
579555
.swap(amount)
@@ -635,7 +611,6 @@ impl State {
635611
token_a: token_a.amount,
636612
token_b: token_b.amount,
637613
fee: token_swap.fee,
638-
pool: None,
639614
};
640615
let b_amount = invariant
641616
.exchange_rate(a_amount)
@@ -686,7 +661,6 @@ impl State {
686661
let swap_info = next_account_info(account_info_iter)?;
687662
let authority_info = next_account_info(account_info_iter)?;
688663
let source_info = next_account_info(account_info_iter)?;
689-
let pool_info = next_account_info(account_info_iter)?;
690664
let token_a_info = next_account_info(account_info_iter)?;
691665
let token_b_info = next_account_info(account_info_iter)?;
692666
let dest_token_a_info = next_account_info(account_info_iter)?;
@@ -703,23 +677,21 @@ impl State {
703677
if *token_b_info.key != token_swap.token_b {
704678
return Err(Error::InvalidInput.into());
705679
}
706-
if *pool_info.key != token_swap.pool_mint {
707-
return Err(Error::InvalidInput.into());
708-
}
680+
709681
let token_a = Self::token_account_deserialize(token_a_info)?;
710682
let token_b = Self::token_account_deserialize(token_b_info)?;
711-
let pool_token = Self::token_deserialize(pool_info)?;
712683

713684
let invariant = Invariant {
714685
token_a: token_a.amount,
715686
token_b: token_b.amount,
716687
fee: token_swap.fee,
717-
pool: Some(pool_token.info.supply),
718688
};
719689

720-
let (a_amount, b_amount) = invariant
721-
.redeem(amount)
690+
let a_amount = amount;
691+
let b_amount = invariant
692+
.exchange_rate(a_amount)
722693
.ok_or_else(|| Error::CalculationFailure)?;
694+
723695
Self::token_transfer(
724696
accounts,
725697
token_program_info.key,
@@ -743,7 +715,6 @@ impl State {
743715
token_program_info.key,
744716
swap_info.key,
745717
source_info.key,
746-
pool_info.key,
747718
authority_info.key,
748719
amount,
749720
)?;
@@ -827,7 +798,7 @@ mod tests {
827798
account::Account, account_info::create_is_signer_account_infos, instruction::Instruction,
828799
};
829800
use spl_token::{
830-
instruction::{initialize_account, initialize_mint, TokenInfo},
801+
instruction::{initialize_account, initialize_mint},
831802
state::State as SplState,
832803
};
833804

@@ -863,7 +834,7 @@ mod tests {
863834
fn mint_token(
864835
program_id: &Pubkey,
865836
authority_key: &Pubkey,
866-
supply: u64,
837+
amount: u64,
867838
) -> ((Pubkey, Account), (Pubkey, Account)) {
868839
let token_key = pubkey_rand();
869840
let mut token_account = Account::new(0, size_of::<SplState>(), &program_id);
@@ -887,13 +858,11 @@ mod tests {
887858
&token_key,
888859
Some(&account_key),
889860
Some(&authority_key),
890-
TokenInfo {
891-
supply,
892-
decimals: 2,
893-
},
861+
amount,
862+
2,
894863
)
895864
.unwrap(),
896-
if supply == 0 {
865+
if amount == 0 {
897866
vec![&mut token_account, &mut authority_account]
898867
} else {
899868
vec![

token/inc/token.h

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,6 @@
1717
*/
1818
#define Token_MIN_SIGNERS 1
1919

20-
/**
21-
* Specifies the financial specifics of a token.
22-
*/
23-
typedef struct Token_TokenInfo {
24-
/**
25-
* Total supply of tokens.
26-
*/
27-
uint64_t supply;
28-
/**
29-
* Number of base 10 digits to the right of the decimal place in the total supply.
30-
*/
31-
uint64_t decimals;
32-
} Token_TokenInfo;
33-
3420
/**
3521
* Instructions supported by the token program.
3622
*/
@@ -186,9 +172,13 @@ typedef enum Token_TokenInstruction_Tag {
186172

187173
typedef struct Token_InitializeMint_Body {
188174
/**
189-
* The financial specifics of the token.
175+
* Initial amount of tokens to mint.
190176
*/
191-
Token_TokenInfo info;
177+
uint64_t amount;
178+
/**
179+
* Number of base 10 digits to the right of the decimal place.
180+
*/
181+
uint8_t decimals;
192182
} Token_InitializeMint_Body;
193183

194184
typedef struct Token_InitializeMultisig_Body {
@@ -271,16 +261,16 @@ typedef struct Token_COption_Pubkey {
271261
* matching types my inter-opt.
272262
*/
273263
typedef struct Token_Token {
274-
/**
275-
* Token details.
276-
*/
277-
Token_TokenInfo info;
278264
/**
279265
* Optional owner, used to mint new tokens. The owner may only
280266
* be provided during mint creation. If no owner is present then the mint
281267
* has a fixed supply and no further tokens may be minted.
282268
*/
283269
Token_COption_Pubkey owner;
270+
/**
271+
* Number of base 10 digits to the right of the decimal place.
272+
*/
273+
uint8_t decimals;
284274
} Token_Token;
285275

286276
/**

token/js/cli/token-test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ export async function createMint(): Promise<void> {
120120
);
121121

122122
const mintInfo = await testToken.getMintInfo();
123-
assert(mintInfo.supply.toNumber() == 10000);
124123
assert(mintInfo.decimals == 2);
125124
assert(mintInfo.owner == null);
126125

@@ -264,7 +263,6 @@ export async function mintTo(): Promise<void> {
264263

265264
{
266265
const mintInfo = await testMintableToken.getMintInfo();
267-
assert(mintInfo.supply.toNumber() == 10000);
268266
assert(mintInfo.decimals == 2);
269267
if (mintInfo.owner === null) {
270268
throw new Error('owner should not be null');
@@ -285,7 +283,6 @@ export async function mintTo(): Promise<void> {
285283

286284
{
287285
const mintInfo = await testMintableToken.getMintInfo();
288-
assert(mintInfo.supply.toNumber() == 10042);
289286
assert(mintInfo.decimals == 2);
290287
if (mintInfo.owner === null) {
291288
throw new Error('owner should not be null');
@@ -303,16 +300,12 @@ export async function mintTo(): Promise<void> {
303300
}
304301

305302
export async function burn(): Promise<void> {
306-
let mintInfo = await testToken.getMintInfo();
307-
const supply = mintInfo.supply.toNumber();
308303
let accountInfo = await testToken.getAccountInfo(testAccount);
309304
const amount = accountInfo.amount.toNumber();
310305

311306
await testToken.burn(testAccount, testAccountOwner, [], 1);
312307
await sleep(500);
313308

314-
mintInfo = await testToken.getMintInfo();
315-
assert(mintInfo.supply.toNumber() == supply - 1);
316309
accountInfo = await testToken.getAccountInfo(testAccount);
317310
assert(accountInfo.amount.toNumber() == amount - 1);
318311
}

token/js/client/token.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ export class TokenAmount extends BN {
5757
* Information about the mint
5858
*/
5959
type MintInfo = {|
60-
/**
61-
* Total supply of tokens
62-
*/
63-
supply: TokenAmount,
64-
6560
/**
6661
* Number of base 10 digits to the right of the decimal place
6762
*/
@@ -74,11 +69,11 @@ type MintInfo = {|
7469

7570
const MintLayout = BufferLayout.struct([
7671
BufferLayout.u8('state'),
77-
Layout.uint64('supply'),
78-
BufferLayout.nu64('decimals'),
7972
BufferLayout.u32('option'),
8073
Layout.publicKey('owner'),
81-
BufferLayout.nu64('padding'),
74+
BufferLayout.u8('decimals'),
75+
BufferLayout.u16('padding1'),
76+
BufferLayout.nu64('padding2'),
8277
]);
8378

8479
/**
@@ -258,10 +253,11 @@ export class Token {
258253
*
259254
* @param connection The connection to use
260255
* @param owner User account that will own the returned account
261-
* @param supply Total supply of the new mint
256+
* @param supply Initial supply to mint
262257
* @param decimals Location of the decimal place
263258
* @param programId Optional token programId, uses the system programId by default
264-
* @return Token object for the newly minted token, Public key of the account holding the total supply of new tokens
259+
* @return Token object for the newly minted token, Public key of the account
260+
* holding the total amount of new tokens
265261
*/
266262
static async createMint(
267263
connection: Connection,
@@ -303,7 +299,7 @@ export class Token {
303299
const commandDataLayout = BufferLayout.struct([
304300
BufferLayout.u8('instruction'),
305301
Layout.uint64('supply'),
306-
BufferLayout.nu64('decimals'),
302+
BufferLayout.u8('decimals'),
307303
]);
308304
let data = Buffer.alloc(1024);
309305
{
@@ -482,7 +478,6 @@ export class Token {
482478
if (mintInfo.state !== 1) {
483479
throw new Error(`Invalid account data`);
484480
}
485-
mintInfo.supply = TokenAmount.fromBuffer(mintInfo.supply);
486481
if (mintInfo.option === 0) {
487482
mintInfo.owner = null;
488483
} else {
@@ -1029,7 +1024,6 @@ export class Token {
10291024

10301025
let keys = [
10311026
{pubkey: account, isSigner: false, isWritable: true},
1032-
{pubkey: this.publicKey, isSigner: false, isWritable: true},
10331027
];
10341028
if (authority instanceof Account) {
10351029
keys.push({pubkey: authority.publicKey, isSigner: true, isWritable: false});

0 commit comments

Comments
 (0)