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

Commit a831edd

Browse files
authored
Use variant fields (#90)
1 parent db1da9b commit a831edd

File tree

5 files changed

+100
-68
lines changed

5 files changed

+100
-68
lines changed

token-swap/js/cli/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ async function main() {
3030
main()
3131
.catch(err => {
3232
console.error(err);
33-
process.exit(-1)
33+
process.exit(-1);
3434
})
3535
.then(() => process.exit());

token/inc/token.h

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ typedef enum Token_TokenInstruction_Tag {
3838
/**
3939
* Initializes a new mint and optionally deposits all the newly minted tokens in an account.
4040
*
41-
* The `InitializeWrappedAccount` instruction requires no signers and MUST be included within
42-
* the Transaction that creates the uninitialized account with the system program. Otherwise
43-
* another party can acquire ownership of the uninitialized token account.
41+
* The `InitializeMint` instruction requires no signers and MUST be included within
42+
* the same Transaction as the system program's `CreateInstruction` that creates the account
43+
* being initialized. Otherwise another party can acquire ownership of the uninitialized account.
4444
*
4545
* Accounts expected by this instruction:
4646
*
@@ -56,9 +56,9 @@ typedef enum Token_TokenInstruction_Tag {
5656
/**
5757
* Initializes a new account to hold tokens.
5858
*
59-
* The `InitializeWrappedAccount` instruction requires no signers and MUST be included within
60-
* the Transaction that creates the uninitialized account with the system program. Otherwise
61-
* another party can acquire ownership of the uninitialized token account.
59+
* The `InitializeAccount` instruction requires no signers and MUST be included within
60+
* the same Transaction as the system program's `CreateInstruction` that creates the account
61+
* being initialized. Otherwise another party can acquire ownership of the uninitialized account.
6262
*
6363
* Accounts expected by this instruction:
6464
*
@@ -74,9 +74,9 @@ typedef enum Token_TokenInstruction_Tag {
7474
* token instruction that require an owner/delegate to be present. The variant field represents the
7575
* number of signers (M) required to validate this multisignature account.
7676
*
77-
* The `InitializeWrappedAccount` instruction requires no signers and MUST be included within
78-
* the Transaction that creates the uninitialized account with the system program. Otherwise
79-
* another party can acquire ownership of the uninitialized token account.
77+
* The `InitializeMultisig` instruction requires no signers and MUST be included within
78+
* the same Transaction as the system program's `CreateInstruction` that creates the account
79+
* being initialized. Otherwise another party can acquire ownership of the uninitialized account.
8080
*
8181
* Accounts expected by this instruction:
8282
*
@@ -168,46 +168,62 @@ typedef enum Token_TokenInstruction_Tag {
168168
*/
169169
MintTo,
170170
/**
171-
* Burns tokens by removing them from an account and the mint's total supply.
171+
* Burns tokens by removing them from an account and thus circulation.
172172
*
173173
* Accounts expected by this instruction:
174174
*
175175
* * Single owner/delegate
176176
* 0. `[writable]` The account to burn from.
177-
* 1. `[writable]` The mint being burned.
178177
* 2. `[signer]` The account's owner/delegate.
179178
*
180179
* * Multisignature owner/delegate
181180
* 0. `[writable]` The account to burn from.
182-
* 1. `[writable]` The mint being burned.
183181
* 2. `[]` The account's multisignature owner/delegate
184182
* 3. ..3+M '[signer]' M signer accounts.
185183
*/
186184
Burn,
187185
} Token_TokenInstruction_Tag;
188186

189187
typedef struct Token_InitializeMint_Body {
190-
Token_TokenInfo _0;
188+
/**
189+
* The financial specifics of the token.
190+
*/
191+
Token_TokenInfo info;
191192
} Token_InitializeMint_Body;
192193

193194
typedef struct Token_InitializeMultisig_Body {
194-
uint8_t _0;
195+
/**
196+
* The number of signers (M) required to validate this multisignature account.
197+
*/
198+
uint8_t m;
195199
} Token_InitializeMultisig_Body;
196200

197201
typedef struct Token_Transfer_Body {
198-
uint64_t _0;
202+
/**
203+
* The amount of tokens to transfer.
204+
*/
205+
uint64_t amount;
199206
} Token_Transfer_Body;
200207

201208
typedef struct Token_Approve_Body {
202-
uint64_t _0;
209+
/**
210+
* The amount of tokens the delegate is approved for.
211+
*/
212+
uint64_t amount;
203213
} Token_Approve_Body;
204214

205215
typedef struct Token_MintTo_Body {
206-
uint64_t _0;
216+
/**
217+
* The amount of new tokens to mint.
218+
*/
219+
uint64_t amount;
207220
} Token_MintTo_Body;
208221

209222
typedef struct Token_Burn_Body {
210-
uint64_t _0;
223+
/**
224+
* The amount of tokens to burn.
225+
*/
226+
uint64_t amount;
211227
} Token_Burn_Body;
212228

213229
typedef struct Token_TokenInstruction {

token/js/cli/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ async function main() {
4747
main()
4848
.catch(err => {
4949
console.error(err);
50-
process.exit(-1)
50+
process.exit(-1);
5151
})
5252
.then(() => process.exit());

token/src/instruction.rs

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ pub enum TokenInstruction {
4242
/// 2. `[]` (optional) The owner/multisignature of the mint if supply is non-zero, if
4343
/// present then further minting is supported.
4444
///
45-
InitializeMint(TokenInfo),
45+
InitializeMint {
46+
/// The financial specifics of the token.
47+
info: TokenInfo,
48+
},
4649
/// Initializes a new account to hold tokens.
4750
///
4851
/// The `InitializeAccount` instruction requires no signers and MUST be included within
@@ -69,7 +72,10 @@ pub enum TokenInstruction {
6972
///
7073
/// 0. `[writable]` The multisignature account to initialize.
7174
/// 1. ..1+N. `[]` The signer accounts, must equal to N where 1 <= N <= 11.
72-
InitializeMultisig(u8),
75+
InitializeMultisig {
76+
/// The number of signers (M) required to validate this multisignature account.
77+
m: u8,
78+
},
7379
/// Transfers tokens from one account to another either directly or via a delegate.
7480
///
7581
/// Accounts expected by this instruction:
@@ -84,7 +90,10 @@ pub enum TokenInstruction {
8490
/// 1. `[writable]` The destination account.
8591
/// 2. '[]' The source account's multisignature owner/delegate.
8692
/// 3. ..3+M '[signer]' M signer accounts.
87-
Transfer(u64),
93+
Transfer {
94+
/// The amount of tokens to transfer.
95+
amount: u64,
96+
},
8897
/// Approves a delegate. A delegate is given the authority over
8998
/// tokens on behalf of the source account's owner.
9099
@@ -100,7 +109,10 @@ pub enum TokenInstruction {
100109
/// 1. `[]` The delegate.
101110
/// 2. '[]' The source account's multisignature owner.
102111
/// 3. ..3+M '[signer]' M signer accounts
103-
Approve(u64),
112+
Approve {
113+
/// The amount of tokens the delegate is approved for.
114+
amount: u64,
115+
},
104116
/// Revokes the delegate's authority.
105117
///
106118
/// Accounts expected by this instruction:
@@ -143,22 +155,26 @@ pub enum TokenInstruction {
143155
/// 1. `[writable]` The account to mint tokens to.
144156
/// 2. `[]` The mint's multisignature owner.
145157
/// 3. ..3+M '[signer]' M signer accounts.
146-
MintTo(u64),
147-
/// Burns tokens by removing them from an account and the mint's total supply.
158+
MintTo {
159+
/// The amount of new tokens to mint.
160+
amount: u64,
161+
},
162+
/// Burns tokens by removing them from an account and thus circulation.
148163
///
149164
/// Accounts expected by this instruction:
150165
///
151166
/// * Single owner/delegate
152167
/// 0. `[writable]` The account to burn from.
153-
/// 1. `[writable]` The mint being burned.
154168
/// 2. `[signer]` The account's owner/delegate.
155169
///
156170
/// * Multisignature owner/delegate
157171
/// 0. `[writable]` The account to burn from.
158-
/// 1. `[writable]` The mint being burned.
159172
/// 2. `[]` The account's multisignature owner/delegate
160173
/// 3. ..3+M '[signer]' M signer accounts.
161-
Burn(u64),
174+
Burn {
175+
/// The amount of tokens to burn.
176+
amount: u64,
177+
},
162178
}
163179
impl TokenInstruction {
164180
/// Deserializes a byte buffer into an [TokenInstruction](enum.TokenInstruction.html).
@@ -168,37 +184,37 @@ impl TokenInstruction {
168184
}
169185
Ok(match input[0] {
170186
0 => {
171-
if input.len() < size_of::<u8>() + size_of::<TokenInfo>() {
187+
if input.len() < size_of::<u8>() + size_of::<u64>() + size_of::<u8>() {
172188
return Err(ProgramError::InvalidAccountData);
173189
}
174190
#[allow(clippy::cast_ptr_alignment)]
175-
let info: &TokenInfo = unsafe { &*(&input[1] as *const u8 as *const TokenInfo) };
176-
Self::InitializeMint(*info)
191+
let info = unsafe { *(&input[1] as *const u8 as *const TokenInfo) };
192+
Self::InitializeMint { info }
177193
}
178194
1 => Self::InitializeAccount,
179195
2 => {
180196
if input.len() < size_of::<u8>() + size_of::<u8>() {
181197
return Err(ProgramError::InvalidAccountData);
182198
}
183199
#[allow(clippy::cast_ptr_alignment)]
184-
let m: &u8 = unsafe { &*(&input[1] as *const u8 as *const u8) };
185-
Self::InitializeMultisig(*m)
200+
let m = unsafe { *(&input[1] as *const u8) };
201+
Self::InitializeMultisig { m }
186202
}
187203
3 => {
188204
if input.len() < size_of::<u8>() + size_of::<u64>() {
189205
return Err(ProgramError::InvalidAccountData);
190206
}
191207
#[allow(clippy::cast_ptr_alignment)]
192-
let amount: &u64 = unsafe { &*(&input[1] as *const u8 as *const u64) };
193-
Self::Transfer(*amount)
208+
let amount = unsafe { *(&input[size_of::<u8>()] as *const u8 as *const u64) };
209+
Self::Transfer { amount }
194210
}
195211
4 => {
196212
if input.len() < size_of::<u8>() + size_of::<u64>() {
197213
return Err(ProgramError::InvalidAccountData);
198214
}
199215
#[allow(clippy::cast_ptr_alignment)]
200-
let amount: &u64 = unsafe { &*(&input[1] as *const u8 as *const u64) };
201-
Self::Approve(*amount)
216+
let amount = unsafe { *(&input[size_of::<u8>()] as *const u8 as *const u64) };
217+
Self::Approve { amount }
202218
}
203219
5 => Self::Revoke,
204220
6 => Self::SetOwner,
@@ -207,16 +223,16 @@ impl TokenInstruction {
207223
return Err(ProgramError::InvalidAccountData);
208224
}
209225
#[allow(clippy::cast_ptr_alignment)]
210-
let amount: &u64 = unsafe { &*(&input[1] as *const u8 as *const u64) };
211-
Self::MintTo(*amount)
226+
let amount = unsafe { *(&input[size_of::<u8>()] as *const u8 as *const u64) };
227+
Self::MintTo { amount }
212228
}
213229
8 => {
214230
if input.len() < size_of::<u8>() + size_of::<u64>() {
215231
return Err(ProgramError::InvalidAccountData);
216232
}
217233
#[allow(clippy::cast_ptr_alignment)]
218-
let amount: &u64 = unsafe { &*(&input[1] as *const u8 as *const u64) };
219-
Self::Burn(*amount)
234+
let amount = unsafe { *(&input[size_of::<u8>()] as *const u8 as *const u64) };
235+
Self::Burn { amount }
220236
}
221237
_ => return Err(ProgramError::InvalidAccountData),
222238
})
@@ -226,43 +242,43 @@ impl TokenInstruction {
226242
pub fn serialize(self: &Self) -> Result<Vec<u8>, ProgramError> {
227243
let mut output = vec![0u8; size_of::<TokenInstruction>()];
228244
match self {
229-
Self::InitializeMint(info) => {
245+
Self::InitializeMint { info } => {
230246
output[0] = 0;
231247
#[allow(clippy::cast_ptr_alignment)]
232248
let value = unsafe { &mut *(&mut output[1] as *mut u8 as *mut TokenInfo) };
233249
*value = *info;
234250
}
235251
Self::InitializeAccount => output[0] = 1,
236-
Self::InitializeMultisig(m) => {
252+
Self::InitializeMultisig { m } => {
237253
output[0] = 2;
238254
#[allow(clippy::cast_ptr_alignment)]
239-
let value = unsafe { &mut *(&mut output[1] as *mut u8 as *mut u8) };
255+
let value = unsafe { &mut *(&mut output[size_of::<u8>()] as *mut u8 as *mut u8) };
240256
*value = *m;
241257
}
242-
Self::Transfer(amount) => {
258+
Self::Transfer { amount } => {
243259
output[0] = 3;
244260
#[allow(clippy::cast_ptr_alignment)]
245-
let value = unsafe { &mut *(&mut output[1] as *mut u8 as *mut u64) };
261+
let value = unsafe { &mut *(&mut output[size_of::<u8>()] as *mut u8 as *mut u64) };
246262
*value = *amount;
247263
}
248-
Self::Approve(amount) => {
264+
Self::Approve { amount } => {
249265
output[0] = 4;
250266
#[allow(clippy::cast_ptr_alignment)]
251-
let value = unsafe { &mut *(&mut output[1] as *mut u8 as *mut u64) };
267+
let value = unsafe { &mut *(&mut output[size_of::<u8>()] as *mut u8 as *mut u64) };
252268
*value = *amount;
253269
}
254270
Self::Revoke => output[0] = 5,
255271
Self::SetOwner => output[0] = 6,
256-
Self::MintTo(amount) => {
272+
Self::MintTo { amount } => {
257273
output[0] = 7;
258274
#[allow(clippy::cast_ptr_alignment)]
259-
let value = unsafe { &mut *(&mut output[1] as *mut u8 as *mut u64) };
275+
let value = unsafe { &mut *(&mut output[size_of::<u8>()] as *mut u8 as *mut u64) };
260276
*value = *amount;
261277
}
262-
Self::Burn(amount) => {
278+
Self::Burn { amount } => {
263279
output[0] = 8;
264280
#[allow(clippy::cast_ptr_alignment)]
265-
let value = unsafe { &mut *(&mut output[1] as *mut u8 as *mut u64) };
281+
let value = unsafe { &mut *(&mut output[size_of::<u8>()] as *mut u8 as *mut u64) };
266282
*value = *amount;
267283
}
268284
}
@@ -276,12 +292,12 @@ pub fn initialize_mint(
276292
mint_pubkey: &Pubkey,
277293
account_pubkey: Option<&Pubkey>,
278294
owner_pubkey: Option<&Pubkey>,
279-
token_info: TokenInfo,
295+
info: TokenInfo,
280296
) -> Result<Instruction, ProgramError> {
281-
let data = TokenInstruction::InitializeMint(token_info).serialize()?;
297+
let data = TokenInstruction::InitializeMint { info }.serialize()?;
282298

283299
let mut accounts = vec![AccountMeta::new(*mint_pubkey, false)];
284-
if token_info.supply != 0 {
300+
if info.supply != 0 {
285301
match account_pubkey {
286302
Some(pubkey) => accounts.push(AccountMeta::new(*pubkey, false)),
287303
None => {
@@ -292,7 +308,7 @@ pub fn initialize_mint(
292308
match owner_pubkey {
293309
Some(pubkey) => accounts.push(AccountMeta::new_readonly(*pubkey, false)),
294310
None => {
295-
if token_info.supply == 0 {
311+
if info.supply == 0 {
296312
return Err(TokenError::OwnerRequiredIfNoInitialSupply.into());
297313
}
298314
}
@@ -340,7 +356,7 @@ pub fn initialize_multisig(
340356
{
341357
return Err(ProgramError::MissingRequiredSignature);
342358
}
343-
let data = TokenInstruction::InitializeMultisig(m).serialize()?;
359+
let data = TokenInstruction::InitializeMultisig { m }.serialize()?;
344360

345361
let mut accounts = Vec::with_capacity(1 + signer_pubkeys.len());
346362
accounts.push(AccountMeta::new(*multisig_pubkey, false));
@@ -364,7 +380,7 @@ pub fn transfer(
364380
signer_pubkeys: &[&Pubkey],
365381
amount: u64,
366382
) -> Result<Instruction, ProgramError> {
367-
let data = TokenInstruction::Transfer(amount).serialize()?;
383+
let data = TokenInstruction::Transfer { amount }.serialize()?;
368384

369385
let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
370386
accounts.push(AccountMeta::new(*source_pubkey, false));
@@ -393,7 +409,7 @@ pub fn approve(
393409
signer_pubkeys: &[&Pubkey],
394410
amount: u64,
395411
) -> Result<Instruction, ProgramError> {
396-
let data = TokenInstruction::Approve(amount).serialize()?;
412+
let data = TokenInstruction::Approve { amount }.serialize()?;
397413

398414
let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
399415
accounts.push(AccountMeta::new_readonly(*source_pubkey, false));
@@ -476,7 +492,7 @@ pub fn mint_to(
476492
signer_pubkeys: &[&Pubkey],
477493
amount: u64,
478494
) -> Result<Instruction, ProgramError> {
479-
let data = TokenInstruction::MintTo(amount).serialize()?;
495+
let data = TokenInstruction::MintTo { amount }.serialize()?;
480496

481497
let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
482498
accounts.push(AccountMeta::new(*mint_pubkey, false));
@@ -505,7 +521,7 @@ pub fn burn(
505521
signer_pubkeys: &[&Pubkey],
506522
amount: u64,
507523
) -> Result<Instruction, ProgramError> {
508-
let data = TokenInstruction::Burn(amount).serialize()?;
524+
let data = TokenInstruction::Burn { amount }.serialize()?;
509525

510526
let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
511527
accounts.push(AccountMeta::new(*account_pubkey, false));

0 commit comments

Comments
 (0)