Skip to content

Commit

Permalink
Remove supply from Token
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay committed Jul 7, 2020
1 parent 5080b8a commit ae4e26a
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 386 deletions.
1 change: 1 addition & 0 deletions ci/memo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ set -e

./do.sh update
./do.sh build memo
./do.sh clippy memo
./do.sh doc memo
./do.sh test memo
1 change: 0 additions & 1 deletion token-swap/inc/token-swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ typedef enum TokenSwap_SwapInstruction_Tag {
* 0. `[]` Token-swap
* 1. `[]` $authority
* 2. `[writable]` SOURCE Pool account, amount is transferable by $authority.
* 4. `[writable]` Pool MINT account, $authority is the owner.
* 5. `[writable]` token_a Account to withdraw FROM.
* 6. `[writable]` token_b Account to withdraw FROM.
* 7. `[writable]` token_a user Account.
Expand Down
1 change: 0 additions & 1 deletion token-swap/js/cli/token-swap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ export async function withdraw(): Promise<void> {
await tokenSwap.withdraw(
authority,
tokenAccountPool,
tokenPool.publicKey,
tokenAccountA,
tokenAccountB,
userAccountA,
Expand Down
4 changes: 0 additions & 4 deletions token-swap/js/client/token-swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ export class TokenSwap {
async withdraw(
authority: PublicKey,
sourcePoolAccount: PublicKey,
poolToken: PublicKey,
fromA: PublicKey,
fromB: PublicKey,
userAccountA: PublicKey,
Expand All @@ -468,7 +467,6 @@ export class TokenSwap {
this.withdrawInstruction(
authority,
sourcePoolAccount,
poolToken,
fromA,
fromB,
userAccountA,
Expand All @@ -483,7 +481,6 @@ export class TokenSwap {
withdrawInstruction(
authority: PublicKey,
sourcePoolAccount: PublicKey,
poolToken: PublicKey,
fromA: PublicKey,
fromB: PublicKey,
userAccountA: PublicKey,
Expand All @@ -509,7 +506,6 @@ export class TokenSwap {
{pubkey: this.tokenSwap, isSigner: false, isWritable: false},
{pubkey: authority, isSigner: false, isWritable: false},
{pubkey: sourcePoolAccount, isSigner: false, isWritable: true},
{pubkey: poolToken, isSigner: false, isWritable: true},
{pubkey: fromA, isSigner: false, isWritable: true},
{pubkey: fromB, isSigner: false, isWritable: true},
{pubkey: userAccountA, isSigner: false, isWritable: true},
Expand Down
55 changes: 12 additions & 43 deletions token-swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ pub enum SwapInstruction {
/// 0. `[]` Token-swap
/// 1. `[]` $authority
/// 2. `[writable]` SOURCE Pool account, amount is transferable by $authority.
/// 4. `[writable]` Pool MINT account, $authority is the owner.
/// 5. `[writable]` token_a Account to withdraw FROM.
/// 6. `[writable]` token_b Account to withdraw FROM.
/// 7. `[writable]` token_a user Account.
Expand Down Expand Up @@ -289,7 +288,6 @@ pub enum State {
struct Invariant {
token_a: u64,
token_b: u64,
pool: Option<u64>,
fee: Fee,
}
impl Invariant {
Expand All @@ -310,17 +308,6 @@ impl Invariant {
fn exchange_rate(&self, token_a: u64) -> Option<u64> {
token_a.checked_mul(self.token_b)?.checked_div(self.token_a)
}
fn redeem(&self, user_pool: u64) -> Option<(u64, u64)> {
let token_a = self
.token_a
.checked_mul(user_pool)?
.checked_div(self.pool?)?;
let token_b = self
.token_b
.checked_mul(user_pool)?
.checked_div(self.pool?)?;
Some((token_a, token_b))
}
}

impl State {
Expand Down Expand Up @@ -403,20 +390,13 @@ impl State {
token_program_id: &Pubkey,
swap: &Pubkey,
burn_account: &Pubkey,
mint: &Pubkey,
authority: &Pubkey,
amount: u64,
) -> Result<(), ProgramError> {
let swap_string = swap.to_string();
let signers = &[&[&swap_string[..32]][..]];
let ix = spl_token::instruction::burn(
token_program_id,
burn_account,
mint,
authority,
&[],
amount,
)?;
let ix =
spl_token::instruction::burn(token_program_id, burn_account, authority, &[], amount)?;
invoke_signed(&ix, accounts, signers)
}

Expand Down Expand Up @@ -500,9 +480,6 @@ impl State {
if spl_token::option::COption::Some(*authority_info.key) != pool_mint.owner {
return Err(Error::InvalidOwner.into());
}
if 0 != pool_mint.info.supply {
return Err(Error::InvalidSupply.into());
}
if token_b.amount == 0 {
return Err(Error::InvalidSupply.into());
}
Expand Down Expand Up @@ -573,7 +550,6 @@ impl State {
token_a: into_token.amount,
token_b: from_token.amount,
fee: token_swap.fee,
pool: None,
};
let output = invariant
.swap(amount)
Expand Down Expand Up @@ -635,7 +611,6 @@ impl State {
token_a: token_a.amount,
token_b: token_b.amount,
fee: token_swap.fee,
pool: None,
};
let b_amount = invariant
.exchange_rate(a_amount)
Expand Down Expand Up @@ -686,7 +661,6 @@ impl State {
let swap_info = next_account_info(account_info_iter)?;
let authority_info = next_account_info(account_info_iter)?;
let source_info = next_account_info(account_info_iter)?;
let pool_info = next_account_info(account_info_iter)?;
let token_a_info = next_account_info(account_info_iter)?;
let token_b_info = next_account_info(account_info_iter)?;
let dest_token_a_info = next_account_info(account_info_iter)?;
Expand All @@ -703,23 +677,21 @@ impl State {
if *token_b_info.key != token_swap.token_b {
return Err(Error::InvalidInput.into());
}
if *pool_info.key != token_swap.pool_mint {
return Err(Error::InvalidInput.into());
}

let token_a = Self::token_account_deserialize(token_a_info)?;
let token_b = Self::token_account_deserialize(token_b_info)?;
let pool_token = Self::token_deserialize(pool_info)?;

let invariant = Invariant {
token_a: token_a.amount,
token_b: token_b.amount,
fee: token_swap.fee,
pool: Some(pool_token.info.supply),
};

let (a_amount, b_amount) = invariant
.redeem(amount)
let a_amount = amount;
let b_amount = invariant
.exchange_rate(a_amount)
.ok_or_else(|| Error::CalculationFailure)?;

Self::token_transfer(
accounts,
token_program_info.key,
Expand All @@ -743,7 +715,6 @@ impl State {
token_program_info.key,
swap_info.key,
source_info.key,
pool_info.key,
authority_info.key,
amount,
)?;
Expand Down Expand Up @@ -827,7 +798,7 @@ mod tests {
account::Account, account_info::create_is_signer_account_infos, instruction::Instruction,
};
use spl_token::{
instruction::{initialize_account, initialize_mint, TokenInfo},
instruction::{initialize_account, initialize_mint},
state::State as SplState,
};

Expand Down Expand Up @@ -863,7 +834,7 @@ mod tests {
fn mint_token(
program_id: &Pubkey,
authority_key: &Pubkey,
supply: u64,
amount: u64,
) -> ((Pubkey, Account), (Pubkey, Account)) {
let token_key = pubkey_rand();
let mut token_account = Account::new(0, size_of::<SplState>(), &program_id);
Expand All @@ -887,13 +858,11 @@ mod tests {
&token_key,
Some(&account_key),
Some(&authority_key),
TokenInfo {
supply,
decimals: 2,
},
amount,
2,
)
.unwrap(),
if supply == 0 {
if amount == 0 {
vec![&mut token_account, &mut authority_account]
} else {
vec![
Expand Down
62 changes: 34 additions & 28 deletions token/inc/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@
*/
#define Token_MIN_SIGNERS 1

/**
* Specifies the financial specifics of a token.
*/
typedef struct Token_TokenInfo {
/**
* Total supply of tokens.
*/
uint64_t supply;
/**
* Number of base 10 digits to the right of the decimal place in the total supply.
*/
uint64_t decimals;
} Token_TokenInfo;

/**
* Instructions supported by the token program.
*/
Expand All @@ -48,7 +34,7 @@ typedef enum Token_TokenInstruction_Tag {
* 1.
* * If supply is non-zero: `[writable]` The account to hold all the newly minted tokens.
* * If supply is zero: `[]` The owner/multisignature of the mint.
* 2. `[]` (optional) The owner/multisignature of the mint if supply is non-zero, if
* 2. `[]` The owner/multisignature of the mint if supply is non-zero, if
* present then further minting is supported.
*
*/
Expand Down Expand Up @@ -155,46 +141,66 @@ typedef enum Token_TokenInstruction_Tag {
*/
MintTo,
/**
* Burns tokens by removing them from an account and the mint's total supply.
* Burns tokens by removing them from an account and thus circulation.
*
* Accounts expected by this instruction:
*
* * Single owner/delegate
* 0. `[writable]` The account to burn from.
* 1. `[writable]` The mint being burned.
* 2. `[signer]` The account's owner/delegate.
*
* * Multisignature owner/delegate
* 0. `[writable]` The account to burn from.
* 1. `[writable]` The mint being burned.
* 2. `[]` The account's multisignature owner/delegate
* 3. ..3+M '[signer]' M signer accounts.
*/
Burn,
} Token_TokenInstruction_Tag;

typedef struct Token_InitializeMint_Body {
Token_TokenInfo _0;
/**
* Initial amount of tokens to mint.
*/
uint64_t amount;
/**
* Number of base 10 digits to the right of the decimal place.
*/
uint8_t decimals;
} Token_InitializeMint_Body;

typedef struct Token_InitializeMultisig_Body {
uint8_t _0;
/**
* The number of signers (M) required to validate this multisignature account.
*/
uint8_t m;
} Token_InitializeMultisig_Body;

typedef struct Token_Transfer_Body {
uint64_t _0;
/**
* The amount of tokens to transfer.
*/
uint64_t amount;
} Token_Transfer_Body;

typedef struct Token_Approve_Body {
uint64_t _0;
/**
* The amount of tokens the delegate is approved for.
*/
uint64_t amount;
} Token_Approve_Body;

typedef struct Token_MintTo_Body {
uint64_t _0;
/**
* The amount of new tokens to mint.
*/
uint64_t amount;
} Token_MintTo_Body;

typedef struct Token_Burn_Body {
uint64_t _0;
/**
* The amount of tokens to burn.
*/
uint64_t amount;
} Token_Burn_Body;

typedef struct Token_TokenInstruction {
Expand Down Expand Up @@ -242,16 +248,16 @@ typedef struct Token_COption_Pubkey {
* matching types my inter-opt.
*/
typedef struct Token_Token {
/**
* Token details.
*/
Token_TokenInfo info;
/**
* Optional owner, used to mint new tokens. The owner may only
* be provided during mint creation. If no owner is present then the mint
* has a fixed supply and no further tokens may be minted.
*/
Token_COption_Pubkey owner;
/**
* Number of base 10 digits to the right of the decimal place.
*/
uint8_t decimals;
} Token_Token;

/**
Expand Down
7 changes: 0 additions & 7 deletions token/js/cli/token-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ export async function createMint(): Promise<void> {
);

const mintInfo = await testToken.getMintInfo();
assert(mintInfo.supply.toNumber() == 10000);
assert(mintInfo.decimals == 2);
assert(mintInfo.owner == null);

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

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

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

export async function burn(): Promise<void> {
let mintInfo = await testToken.getMintInfo();
const supply = mintInfo.supply.toNumber();
let accountInfo = await testToken.getAccountInfo(testAccount);
const amount = accountInfo.amount.toNumber();

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

mintInfo = await testToken.getMintInfo();
assert(mintInfo.supply.toNumber() == supply - 1);
accountInfo = await testToken.getAccountInfo(testAccount);
assert(accountInfo.amount.toNumber() == amount - 1);
}
Expand Down
Loading

0 comments on commit ae4e26a

Please sign in to comment.