Skip to content

Commit

Permalink
solana: fix decimals == 8
Browse files Browse the repository at this point in the history
  • Loading branch information
a5-pickle committed Aug 3, 2023
1 parent d4683b7 commit f2ff015
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cross-chain/solana/programs/tbtc/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ pub struct GuardianAdded {
#[event]
pub struct GuardianRemoved {
pub guardian: Pubkey,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Initialize<'info> {
seeds = [SEED_PREFIX_TBTC_MINT],
bump,
payer = authority,
mint::decimals = 9,
mint::decimals = 8,
mint::authority = config,
)]
mint: Account<'info, token::Mint>,
Expand Down
3 changes: 0 additions & 3 deletions cross-chain/solana/programs/wormhole-gateway/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ pub enum WormholeGatewayError {
#[msg("Amount must not be 0.")]
ZeroAmount = 0x50,

#[msg("Amount too low to bridge.")]
TruncatedZeroAmount = 0x60,

#[msg("Token Bridge transfer already redeemed.")]
TransferAlreadyRedeemed = 0x70,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ pub fn send_tbtc_gateway(ctx: Context<SendTbtcGateway>, args: SendTbtcGatewayArg
let token_bridge_transfer_authority = &ctx.accounts.token_bridge_transfer_authority;
let token_program = &ctx.accounts.token_program;

let gateway = ctx.accounts.gateway_info.address;

// Prepare for wrapped tBTC transfer (this method also truncates the amount to prevent having to
// handle dust since tBTC has >8 decimals).
let amount = super::burn_and_prepare_transfer(
super::burn_and_prepare_transfer(
super::PrepareTransfer {
custodian: &mut ctx.accounts.custodian,
tbtc_mint: &ctx.accounts.tbtc_mint,
Expand All @@ -137,18 +139,12 @@ pub fn send_tbtc_gateway(ctx: Context<SendTbtcGateway>, args: SendTbtcGatewayArg
token_program,
},
amount,
)?;

let gateway = ctx.accounts.gateway_info.address;

emit!(crate::event::WormholeTbtcSent {
amount,
recipient_chain,
gateway,
Some(gateway),
recipient,
arbiter_fee: Default::default(),
nonce
});
None, // arbiter_fee
nonce,
)?;

let custodian = &ctx.accounts.custodian;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ pub struct PrepareTransfer<'ctx, 'info> {
token_program: &'ctx Program<'info, token::Token>,
}

pub fn burn_and_prepare_transfer(prepare_transfer: PrepareTransfer, amount: u64) -> Result<u64> {
pub fn burn_and_prepare_transfer(
prepare_transfer: PrepareTransfer,
amount: u64,
recipient_chain: u16,
gateway: Option<[u8; 32]>,
recipient: [u8; 32],
arbiter_fee: Option<u64>,
nonce: u32,
) -> Result<()> {
let PrepareTransfer {
custodian,
tbtc_mint,
Expand All @@ -48,13 +56,10 @@ pub fn burn_and_prepare_transfer(prepare_transfer: PrepareTransfer, amount: u64)
token_program,
} = prepare_transfer;

let truncated = 10 * (amount / 10);
require_gt!(truncated, 0, WormholeGatewayError::TruncatedZeroAmount);

// Account for burning tBTC.
custodian
.minted_amount
.checked_sub(truncated)
.checked_sub(amount)
.ok_or(WormholeGatewayError::MintedAmountUnderflow)?;

// Burn TBTC mint.
Expand All @@ -70,6 +75,15 @@ pub fn burn_and_prepare_transfer(prepare_transfer: PrepareTransfer, amount: u64)
amount,
)?;

emit!(crate::event::WormholeTbtcSent {
amount,
recipient_chain,
gateway: gateway.unwrap_or_default(),
recipient,
arbiter_fee: arbiter_fee.unwrap_or_default(),
nonce
});

// Delegate authority to Token Bridge's transfer authority.
token::approve(
CpiContext::new_with_signer(
Expand All @@ -81,8 +95,6 @@ pub fn burn_and_prepare_transfer(prepare_transfer: PrepareTransfer, amount: u64)
},
&[&[Custodian::SEED_PREFIX, &[custodian.bump]]],
),
truncated,
)?;

Ok(truncated)
amount,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn send_tbtc_wrapped(ctx: Context<SendTbtcWrapped>, args: SendTbtcWrappedArg
let token_program = &ctx.accounts.token_program;

// Prepare for wrapped tBTC transfer.
let amount = super::burn_and_prepare_transfer(
super::burn_and_prepare_transfer(
super::PrepareTransfer {
custodian: &mut ctx.accounts.custodian,
tbtc_mint: &ctx.accounts.tbtc_mint,
Expand All @@ -127,16 +127,12 @@ pub fn send_tbtc_wrapped(ctx: Context<SendTbtcWrapped>, args: SendTbtcWrappedArg
token_program,
},
amount,
)?;

emit!(crate::event::WormholeTbtcSent {
amount,
recipient_chain,
gateway: Default::default(),
None, // gateway
recipient,
arbiter_fee,
nonce
});
Some(arbiter_fee),
nonce,
)?;

let custodian = &ctx.accounts.custodian;

Expand Down

0 comments on commit f2ff015

Please sign in to comment.