forked from wormhole-foundation/native-token-transfers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounts.rs
99 lines (83 loc) · 2.87 KB
/
accounts.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use anchor_lang::prelude::*;
use wormhole_anchor_sdk::wormhole;
use wormhole_io::TypePrefixedPayload;
cfg_if::cfg_if! {
if #[cfg(feature = "tilt-devnet2")] {
const FINALITY: wormhole::Finality = wormhole::Finality::Confirmed;
} else if #[cfg(feature = "tilt-devnet")] {
const FINALITY: wormhole::Finality = wormhole::Finality::Confirmed;
} else {
const FINALITY: wormhole::Finality = wormhole::Finality::Finalized;
}
}
// TODO: should we add emitter in here too?
#[derive(Accounts)]
pub struct WormholeAccounts<'info> {
// wormhole stuff
#[account(mut)]
/// CHECK: address will be checked by the wormhole core bridge
pub bridge: Account<'info, wormhole::BridgeData>,
#[account(mut)]
/// CHECK: account will be checked by the wormhole core bridge
pub fee_collector: UncheckedAccount<'info>,
#[account(mut)]
/// CHECK: account will be checked and maybe initialized by the wormhole core bridge
pub sequence: UncheckedAccount<'info>,
pub program: Program<'info, wormhole::program::Wormhole>,
pub system_program: Program<'info, System>,
// legacy
pub clock: Sysvar<'info, Clock>,
pub rent: Sysvar<'info, Rent>,
}
pub fn post_message<'info, A: TypePrefixedPayload>(
wormhole: &WormholeAccounts<'info>,
payer: AccountInfo<'info>,
message: AccountInfo<'info>,
emitter: AccountInfo<'info>,
emitter_bump: u8,
payload: &A,
additional_seeds: &[&[&[u8]]],
) -> Result<()> {
let batch_id = 0;
pay_wormhole_fee(wormhole, &payer)?;
let ix = wormhole::PostMessage {
config: wormhole.bridge.to_account_info(),
message,
emitter,
sequence: wormhole.sequence.to_account_info(),
payer: payer.to_account_info(),
fee_collector: wormhole.fee_collector.to_account_info(),
clock: wormhole.clock.to_account_info(),
rent: wormhole.rent.to_account_info(),
system_program: wormhole.system_program.to_account_info(),
};
let seeds: &[&[&[&[u8]]]] = &[
&[&[b"emitter".as_slice(), &[emitter_bump]]],
additional_seeds,
];
wormhole::post_message(
CpiContext::new_with_signer(wormhole.program.to_account_info(), ix, &seeds.concat()),
batch_id,
TypePrefixedPayload::to_vec_payload(payload),
FINALITY,
)?;
Ok(())
}
fn pay_wormhole_fee<'info>(
wormhole: &WormholeAccounts<'info>,
payer: &AccountInfo<'info>,
) -> Result<()> {
if wormhole.bridge.fee() > 0 {
anchor_lang::system_program::transfer(
CpiContext::new(
wormhole.system_program.to_account_info(),
anchor_lang::system_program::Transfer {
from: payer.to_account_info(),
to: wormhole.fee_collector.to_account_info(),
},
),
wormhole.bridge.fee(),
)?;
}
Ok(())
}