Skip to content

Commit

Permalink
solana: add events
Browse files Browse the repository at this point in the history
  • Loading branch information
a5-pickle committed Aug 2, 2023
1 parent ca5b8b3 commit d4683b7
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cross-chain/solana/programs/tbtc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub use constants::*;

pub mod error;

pub(crate) mod event;

mod processor;
pub(crate) use processor::*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ pub fn add_guardian(ctx: Context<AddGuardian>) -> Result<()> {
// Update config.
ctx.accounts.config.num_guardians += 1;

emit!(crate::event::GuardianAdded { guardian });

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ pub fn add_minter(ctx: Context<AddMinter>) -> Result<()> {

// Update config.
ctx.accounts.config.num_minters += 1;

emit!(crate::event::MinterAdded { minter });

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ pub struct RemoveGuardian<'info> {

pub fn remove_guardian(ctx: Context<RemoveGuardian>) -> Result<()> {
let guardians: &mut Vec<_> = &mut ctx.accounts.guardians;
match guardians
.iter()
.position(|&guardian| guardian == ctx.accounts.guardian.key())
{
let removed = ctx.accounts.guardian.key();
match guardians.iter().position(|&guardian| guardian == removed) {
Some(index) => {
// Remove pubkey to guardians account.
guardians.swap_remove(index);

// Update config.
ctx.accounts.config.num_guardians -= 1;

emit!(crate::event::GuardianRemoved { guardian: removed });

Ok(())
}
None => err!(TbtcError::GuardianNonexistent),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ pub struct RemoveMinter<'info> {

pub fn remove_minter(ctx: Context<RemoveMinter>) -> Result<()> {
let minters: &mut Vec<_> = &mut ctx.accounts.minters;
match minters
.iter()
.position(|&minter| minter == ctx.accounts.minter.key())
{
let removed = ctx.accounts.minter.key();
match minters.iter().position(|&minter| minter == removed) {
Some(index) => {
// Remove pubkey to minters account.
minters.swap_remove(index);

// Update config.
ctx.accounts.config.num_minters -= 1;

emit!(crate::event::MinterRemoved { minter: removed });

Ok(())
}
None => err!(TbtcError::GuardianNonexistent),
Expand Down
34 changes: 34 additions & 0 deletions cross-chain/solana/programs/wormhole-gateway/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use anchor_lang::prelude::*;

#[event]
pub struct WormholeTbtcReceived {
pub receiver: Pubkey,
pub amount: u64,
}

#[event]
pub struct WormholeTbtcSent {
pub amount: u64,
pub recipient_chain: u16,
pub gateway: [u8; 32],
pub recipient: [u8; 32],
pub arbiter_fee: u64,
pub nonce: u32,
}

#[event]
pub struct WormholeTbtcDeposited {
pub depositor: Pubkey,
pub amount: u64,
}

#[event]
pub struct GatewayAddressUpdated {
pub chain: u16,
pub gateway: [u8; 32],
}

#[event]
pub struct MintingLimitUpdated {
pub minting_limit: u64,
}
2 changes: 2 additions & 0 deletions cross-chain/solana/programs/wormhole-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub mod constants;

pub mod error;

pub(crate) mod event;

mod processor;
pub(crate) use processor::*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ pub fn receive_tbtc(ctx: Context<ReceiveTbtc>, _message_hash: [u8; 32]) -> Resul
// Because we are working with wrapped token amounts, we can take the amount as-is and determine
// whether to mint or transfer based on the minting limit.
let amount = ctx.accounts.posted_vaa.data().amount();
let recipient = &ctx.accounts.recipient;

emit!(crate::event::WormholeTbtcReceived {
receiver: recipient.key(),
amount
});

let updated_minted_amount = ctx.accounts.custodian.minted_amount.saturating_add(amount);
let custodian_seeds = &[Custodian::SEED_PREFIX, &[ctx.accounts.custodian.bump]];
Expand All @@ -198,7 +204,7 @@ pub fn receive_tbtc(ctx: Context<ReceiveTbtc>, _message_hash: [u8; 32]) -> Resul
associated_token::Create {
payer: ctx.accounts.payer.to_account_info(),
associated_token: ata.to_account_info(),
authority: ctx.accounts.recipient.to_account_info(),
authority: recipient.to_account_info(),
mint: wrapped_tbtc_mint.to_account_info(),
token_program: ctx.accounts.token_program.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ pub fn send_tbtc_gateway(ctx: Context<SendTbtcGateway>, args: SendTbtcGatewayArg
amount,
)?;

let gateway = ctx.accounts.gateway_info.address;

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

let custodian = &ctx.accounts.custodian;

// Finally transfer wrapped tBTC with the recipient encoded as this transfer's message.
Expand Down Expand Up @@ -180,7 +191,7 @@ pub fn send_tbtc_gateway(ctx: Context<SendTbtcGateway>, args: SendTbtcGatewayArg
),
nonce,
amount,
ctx.accounts.gateway_info.address,
gateway,
recipient_chain,
recipient.to_vec(),
&crate::ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ pub fn send_tbtc_wrapped(ctx: Context<SendTbtcWrapped>, args: SendTbtcWrappedArg
amount,
)?;

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

let custodian = &ctx.accounts.custodian;

// Because the wormhole-anchor-sdk does not support relayable transfers (i.e. payload ID == 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ pub fn update_gateway_address(
ctx: Context<UpdateGatewayAddress>,
args: UpdateGatewayAddressArgs,
) -> Result<()> {
let UpdateGatewayAddressArgs { address, .. } = args;
let UpdateGatewayAddressArgs { chain, address } = args;

ctx.accounts.gateway_info.set_inner(GatewayInfo {
bump: ctx.bumps["gateway_info"],
address,
});

emit!(crate::event::GatewayAddressUpdated {
chain,
gateway: address
});

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ pub struct UpdateMintingLimit<'info> {

pub fn update_minting_limit(ctx: Context<UpdateMintingLimit>, new_limit: u64) -> Result<()> {
ctx.accounts.custodian.minting_limit = new_limit;

emit!(crate::event::MintingLimitUpdated {
minting_limit: new_limit
});

Ok(())
}

0 comments on commit d4683b7

Please sign in to comment.