Skip to content

Commit 3351dbd

Browse files
Make decimals dynamic
1 parent c8f47bb commit 3351dbd

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

tokens/transfer-tokens/seahorse/programs/seahorse/src/dot/program.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ pub fn mint_token_handler<'info>(
3636
to: recipient.clone().to_account_info(),
3737
},
3838
),
39-
(amount * <u64 as TryFrom<_>>::try_from(10).unwrap().pow(6)),
39+
(amount
40+
* <u64 as TryFrom<_>>::try_from(10)
41+
.unwrap()
42+
.pow(<u32 as TryFrom<_>>::try_from(mint.decimals.clone()).unwrap())),
4043
)
4144
.unwrap();
4245
}
@@ -46,7 +49,12 @@ pub fn transfer_handler<'info>(
4649
mut recipient: SeahorseAccount<'info, '_, TokenAccount>,
4750
mut signer: SeahorseSigner<'info, '_>,
4851
mut amount: u64,
52+
mut mint: SeahorseAccount<'info, '_, Mint>,
4953
) -> () {
54+
if !(signer_token_account.mint == mint.key()) {
55+
panic!("Mint is not the token account mint");
56+
}
57+
5058
token::transfer(
5159
CpiContext::new(
5260
signer_token_account.programs.get("token_program"),
@@ -56,7 +64,10 @@ pub fn transfer_handler<'info>(
5664
to: recipient.clone().to_account_info(),
5765
},
5866
),
59-
(amount * <u64 as TryFrom<_>>::try_from(10).unwrap().pow(6)),
67+
(amount
68+
* <u64 as TryFrom<_>>::try_from(10)
69+
.unwrap()
70+
.pow(<u32 as TryFrom<_>>::try_from(mint.decimals.clone()).unwrap())),
6071
)
6172
.unwrap();
6273
}

tokens/transfer-tokens/seahorse/programs/seahorse/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ mod seahorse {
323323
pub recipient: Box<Account<'info, TokenAccount>>,
324324
#[account(mut)]
325325
pub signer: Signer<'info>,
326+
#[account(mut)]
327+
pub mint: Box<Account<'info, Mint>>,
326328
pub token_program: Program<'info, Token>,
327329
}
328330

@@ -350,11 +352,17 @@ mod seahorse {
350352
programs: &programs_map,
351353
};
352354

355+
let mint = SeahorseAccount {
356+
account: &ctx.accounts.mint,
357+
programs: &programs_map,
358+
};
359+
353360
transfer_handler(
354361
signer_token_account.clone(),
355362
recipient.clone(),
356363
signer.clone(),
357364
amount,
365+
mint.clone(),
358366
);
359367

360368
return Ok(());

tokens/transfer-tokens/seahorse/programs_py/seahorse.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def mint_token(
2626
mint.mint(
2727
authority = signer,
2828
to = recipient,
29-
amount = amount * u64(10) ** 6
29+
amount = amount * u64(10) ** u32(mint.decimals)
3030
)
3131

3232

@@ -48,10 +48,12 @@ def transfer(
4848
signer_token_account: TokenAccount,
4949
recipient: TokenAccount,
5050
signer: Signer,
51-
amount: u64
51+
amount: u64,
52+
mint: TokenMint
5253
):
54+
assert signer_token_account.mint() == mint.key(), 'Mint is not the token account mint'
5355
signer_token_account.transfer(
5456
authority = signer,
5557
to = recipient,
56-
amount = amount * u64(10) ** 6
58+
amount = amount * u64(10) ** u32(mint.decimals)
5759
)

tokens/transfer-tokens/seahorse/tests/seahorse.py

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ async def main():
6666
"system_program": SYS_PROGRAM_ID,
6767
"rent": RENT,
6868
"token_program": TOKEN_PROGRAM_ID,
69+
"mint": mint.pubkey()
6970
}, signers=[program.provider.wallet.payer]))
7071

7172
print("Transfer signature: ", transfer)

0 commit comments

Comments
 (0)