瀏覽代碼

Make decimals dynamic

Valentin Madrid 2 年之前
父節點
當前提交
3351dbdc0b

+ 13 - 2
tokens/transfer-tokens/seahorse/programs/seahorse/src/dot/program.rs

@@ -36,7 +36,10 @@ pub fn mint_token_handler<'info>(
                 to: recipient.clone().to_account_info(),
             },
         ),
-        (amount * <u64 as TryFrom<_>>::try_from(10).unwrap().pow(6)),
+        (amount
+            * <u64 as TryFrom<_>>::try_from(10)
+                .unwrap()
+                .pow(<u32 as TryFrom<_>>::try_from(mint.decimals.clone()).unwrap())),
     )
     .unwrap();
 }
@@ -46,7 +49,12 @@ pub fn transfer_handler<'info>(
     mut recipient: SeahorseAccount<'info, '_, TokenAccount>,
     mut signer: SeahorseSigner<'info, '_>,
     mut amount: u64,
+    mut mint: SeahorseAccount<'info, '_, Mint>,
 ) -> () {
+    if !(signer_token_account.mint == mint.key()) {
+        panic!("Mint is not the token account mint");
+    }
+
     token::transfer(
         CpiContext::new(
             signer_token_account.programs.get("token_program"),
@@ -56,7 +64,10 @@ pub fn transfer_handler<'info>(
                 to: recipient.clone().to_account_info(),
             },
         ),
-        (amount * <u64 as TryFrom<_>>::try_from(10).unwrap().pow(6)),
+        (amount
+            * <u64 as TryFrom<_>>::try_from(10)
+                .unwrap()
+                .pow(<u32 as TryFrom<_>>::try_from(mint.decimals.clone()).unwrap())),
     )
     .unwrap();
 }

+ 8 - 0
tokens/transfer-tokens/seahorse/programs/seahorse/src/lib.rs

@@ -323,6 +323,8 @@ mod seahorse {
         pub recipient: Box<Account<'info, TokenAccount>>,
         #[account(mut)]
         pub signer: Signer<'info>,
+        #[account(mut)]
+        pub mint: Box<Account<'info, Mint>>,
         pub token_program: Program<'info, Token>,
     }
 
@@ -350,11 +352,17 @@ mod seahorse {
             programs: &programs_map,
         };
 
+        let mint = SeahorseAccount {
+            account: &ctx.accounts.mint,
+            programs: &programs_map,
+        };
+
         transfer_handler(
             signer_token_account.clone(),
             recipient.clone(),
             signer.clone(),
             amount,
+            mint.clone(),
         );
 
         return Ok(());

+ 5 - 3
tokens/transfer-tokens/seahorse/programs_py/seahorse.py

@@ -26,7 +26,7 @@ def mint_token(
   mint.mint(
     authority = signer,
     to = recipient,
-    amount = amount * u64(10) ** 6
+    amount = amount * u64(10) ** u32(mint.decimals)
   )
 
 
@@ -48,10 +48,12 @@ def transfer(
   signer_token_account: TokenAccount,
   recipient: TokenAccount,
   signer: Signer,
-  amount: u64
+  amount: u64,
+  mint: TokenMint
 ):
+  assert signer_token_account.mint() == mint.key(), 'Mint is not the token account mint'
   signer_token_account.transfer(
     authority = signer,
     to = recipient,
-    amount = amount * u64(10) ** 6
+    amount = amount * u64(10) ** u32(mint.decimals)
   )

+ 1 - 0
tokens/transfer-tokens/seahorse/tests/seahorse.py

@@ -66,6 +66,7 @@ async def main():
         "system_program": SYS_PROGRAM_ID,
         "rent": RENT,
         "token_program": TOKEN_PROGRAM_ID,
+        "mint": mint.pubkey()
     }, signers=[program.provider.wallet.payer]))
 
     print("Transfer signature: ", transfer)