-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add current state of wormhole gateway
- Loading branch information
Showing
17 changed files
with
2,963 additions
and
1,184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "wormhole-gateway" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "wormhole_gateway" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
no-log-ix-name = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
|
||
[dependencies] | ||
anchor-lang = { version="0.28.0", features = ["init-if-needed"]} | ||
anchor-spl = "0.28.0" | ||
tbtc = { path = "../tbtc" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use anchor_lang::prelude::error_code; | ||
|
||
#[error_code] | ||
pub enum WormholeGatewayError { | ||
MintingLimitExceeded, | ||
IsNotAuthority, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
pub mod error; | ||
|
||
mod processor; | ||
pub(crate) use processor::*; | ||
|
||
mod state; | ||
pub use state::*; | ||
|
||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("8H9F5JGbEMyERycwaGuzLS5MQnV7dn2wm2h6egJ3Leiu"); | ||
|
||
#[program] | ||
pub mod wormhole_gateway { | ||
use std::os::unix::process; | ||
|
||
use super::*; | ||
|
||
pub fn initialize(ctx: Context<Initialize>, minting_limit: u64) -> Result<()> { | ||
processor::initialize(ctx, minting_limit) | ||
} | ||
|
||
pub fn update_gateway_address( | ||
ctx: Context<UpdateGatewayAddress>, | ||
chain_id: u16, | ||
gateway_address: [u8; 32] | ||
) -> Result<()> { | ||
processor::update_gateway_address(ctx, chain_id, gateway_address) | ||
} | ||
|
||
pub fn update_minting_limit( | ||
ctx: Context<UpdateMintingLimit>, | ||
new_limit: u64, | ||
) -> Result<()> { | ||
processor::update_minting_limit(ctx, new_limit) | ||
} | ||
|
||
pub fn receive_tbtc( | ||
ctx: Context<ReceiveTbtc>, | ||
) -> Result<()> { | ||
processor::receive_tbtc(ctx) | ||
} | ||
|
||
pub fn send_tbtc( | ||
ctx: Context<SendTbtc>, | ||
amount: u64, | ||
recipient_chain: u16, | ||
arbiter_fee: u64, | ||
nonce: u32, | ||
) -> Result<()> { | ||
processor::send_tbtc(ctx, amount, recipient_chain, arbiter_fee, nonce) | ||
} | ||
|
||
pub fn deposit_wormhole_tbtc(ctx: Context<DepositWormholeTbtc>, amount: u64) -> Result<()> { | ||
processor::deposit_wormhole_tbtc(ctx, amount) | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
cross-chain/solana/programs/wormhole-gateway/src/processor/deposit_wormhole_tbtc.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::{ | ||
error::WormholeGatewayError, | ||
state::WormholeGateway, | ||
}; | ||
use tbtc::{tbtc}; | ||
|
||
use anchor_lang::prelude::*; | ||
use anchor_spl::token; | ||
|
||
#[derive(Accounts)] | ||
#[instruction(amount: u64)] | ||
pub struct DepositWormholeTbtc<'info> { | ||
#[account(mut)] | ||
pub tbtc_mint: Account<'info, token::Mint>, | ||
pub tbtc: Account<'info, tbtc::Tbtc>, | ||
pub minter_info: Account<'info, tbtc::MinterInfo>, | ||
|
||
// Use the associated token account for the recipient. | ||
#[account( | ||
associated_token::mint = tbtc_mint, | ||
associated_token::authority = recipient, | ||
)] | ||
pub recipient_account: Account<'info, token::TokenAccount>, | ||
pub recipient: Signer<'info>, | ||
#[account( | ||
constraint = wormhole_gateway.minting_limit > wormhole_gateway.minted_amount + amount @ WormholeGatewayError::MintingLimitExceeded | ||
)] | ||
pub wormhole_gateway: Account<'info, WormholeGateway>, | ||
} | ||
|
||
pub fn deposit_wormhole_tbtc( | ||
ctx: Context<DepositWormholeTbtc>, | ||
amount: u64, | ||
) -> Result<()> { | ||
ctx.accounts.wormhole_gateway.minted_amount += amount; | ||
|
||
let transfer_cpi_ctx = CpiContext::new_with_signer( | ||
// | ||
); | ||
// wormhole::transfer | ||
|
||
let seed_prefix = WormholeGateway::SEED_PREFIX; | ||
let key_seed = ctx.accounts.wormhole_gateway.key(); | ||
let gateway_bump = ctx.accounts.wormhole_gateway.self_bump; | ||
|
||
let signer: &[&[&[u8]]] = &[&[seed_prefix, key_seed.as_ref(), &[gateway_bump]]]; | ||
|
||
let mint_cpi_ctx = CpiContext::new_with_signer( | ||
ctx.accounts.tbtc.to_account_info(), | ||
tbtc::Mint { | ||
tbtc_mint: ctx.accounts.tbtc_mint.to_account_info(), | ||
tbtc: ctx.accounts.tbtc.to_account_info(), | ||
minter_info: ctx.accounts.minter_info.to_account_info(), | ||
minter: ctx.accounts.wormhole_gateway.to_account_info(), | ||
recipient_account: ctx.accounts.recipient_account.to_account_info(), | ||
recipient: ctx.accounts.recipient.to_account_info(), | ||
payer: ctx.accounts.payer.to_account_info(), | ||
}, | ||
signer, | ||
); | ||
tbtc::mint(mint_cpi_ctx, amount); | ||
} |
46 changes: 46 additions & 0 deletions
46
cross-chain/solana/programs/wormhole-gateway/src/processor/initialize.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::{ | ||
state::WormholeGateway, | ||
}; | ||
|
||
use anchor_lang::prelude::*; | ||
use anchor_spl::token; | ||
|
||
#[derive(Accounts)] | ||
pub struct Initialize<'info> { | ||
#[account( | ||
seeds = [SEED_PREFIX_TBTC_MINT, tbtc.key().as_ref()], | ||
bump, | ||
mint::decimals = 9, | ||
mint::authority = tbtc_mint, | ||
)] | ||
pub tbtc_mint: Account<'info, token::Mint>, | ||
pub tbtc: Account<'info, tbtc::Tbtc>, | ||
|
||
#[account( | ||
init, payer = authority, space = WormholeGateway::MAXIMUM_SIZE | ||
)] | ||
pub wormhole_gateway: Account<'info, WormholeGateway>, | ||
|
||
pub wormhole_token_bridge: Account<'info, _>, | ||
pub wormhole_bridge_token_mint: Account<'info, token::Mint>, | ||
|
||
#[account(mut)] | ||
pub authority: Signer<'info>, | ||
|
||
pub token_program: Program<'info, token::Token>, | ||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
pub fn initialize(ctx: Context<Initialize>, minting_limit: u64) -> Result<()> { | ||
ctx.accounts.wormhole_gateway.set_inner(WormholeGateway { | ||
authority: ctx.accounts.authority.key(), | ||
wormhole_token_bridge: ctx.accounts.wormhole_token_bridge.key(), | ||
wormhole_bridge_token_mint: ctx.accounts.wormhole_bridge_token_mint.key(), | ||
tbtc: ctx.accounts.tbtc.key(), | ||
tbtc_mint: ctx.accounts.tbtc_mint.key(), | ||
minting_limit: minting_limit, | ||
minted_amount: 0, | ||
self_bump: ctx.bumps.get("wormhole_gateway").unwrap(), | ||
}); | ||
Ok(()) | ||
} |
17 changes: 17 additions & 0 deletions
17
cross-chain/solana/programs/wormhole-gateway/src/processor/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
mod deposit_wormhole_tbtc; | ||
pub use deposit_wormhole_tbtc::*; | ||
|
||
mod initialize; | ||
pub use initialize::*; | ||
|
||
mod receive_tbtc; | ||
pub use receive_tbtc::*; | ||
|
||
mod send_tbtc; | ||
pub use send_tbtc::*; | ||
|
||
mod update_gateway_address; | ||
pub use update_gateway_address::*; | ||
|
||
mod update_minting_limit; | ||
pub use update_minting_limit::*; |
70 changes: 70 additions & 0 deletions
70
cross-chain/solana/programs/wormhole-gateway/src/processor/receive_tbtc.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use crate::{ | ||
state::WormholeGateway, | ||
}; | ||
|
||
use tbtc::{tbtc}; | ||
|
||
use anchor_lang::prelude::*; | ||
use anchor_spl::token; | ||
|
||
#[derive(Accounts)] | ||
pub struct ReceiveTbtc<'info> { | ||
#[account(mut)] | ||
pub tbtc_mint: Account<'info, token::Mint>, | ||
pub tbtc: Account<'info, tbtc::Tbtc>, | ||
pub minter_info: Account<'info, tbtc::MinterInfo>, | ||
|
||
// Use the associated token account for the recipient. | ||
#[account( | ||
associated_token::mint = tbtc_mint, | ||
associated_token::authority = recipient, | ||
)] | ||
pub recipient_account: Account<'info, token::TokenAccount>, | ||
/// CHECK: the recipient doesn't need to sign the mint, | ||
/// and it doesn't conform to any specific rules. | ||
/// Validating the recipient is the minter's responsibility. | ||
pub recipient: UncheckedAccount<'info>, | ||
|
||
#[account(mut)] | ||
pub payer: Signer<'info>, | ||
|
||
pub wormhole_gateway: Account<'info, WormholeGateway>, | ||
} | ||
|
||
pub fn receive_tbtc( | ||
ctx: Context<ReceiveTbtc>, | ||
) -> Result <()> { | ||
// get balance delta | ||
|
||
let amount = _; | ||
|
||
let minted_amount = ctx.accounts.wormhole_gateway.minted_amount; | ||
let minting_limit = ctx.accounts.wormhole_gateway.minting_limit; | ||
|
||
if (minted_amount + amount > minting_limit) { | ||
// transfer bridge token | ||
} else { | ||
ctx.accounts.wormhole_gateway.minted_amount += amount; | ||
|
||
let seed_prefix = WormholeGateway::SEED_PREFIX; | ||
let key_seed = ctx.accounts.wormhole_gateway.key(); | ||
let gateway_bump = ctx.accounts.wormhole_gateway.self_bump; | ||
|
||
let signer: &[&[&[u8]]] = &[&[seed_prefix, key_seed.as_ref(), &[gateway_bump]]]; | ||
|
||
let mint_cpi_ctx = CpiContext::new_with_signer( | ||
ctx.accounts.tbtc.to_account_info(), | ||
tbtc::Mint { | ||
tbtc_mint: ctx.accounts.tbtc_mint.to_account_info(), | ||
tbtc: ctx.accounts.tbtc.to_account_info(), | ||
minter_info: ctx.accounts.minter_info.to_account_info(), | ||
minter: ctx.accounts.wormhole_gateway.to_account_info(), | ||
recipient_account: ctx.accounts.recipient_account.to_account_info(), | ||
recipient: ctx.accounts.recipient.to_account_info(), | ||
payer: ctx.accounts.payer.to_account_info(), | ||
}, | ||
signer, | ||
); | ||
tbtc::mint(mint_cpi_ctx, amount) | ||
} | ||
} |
Oops, something went wrong.