Skip to content

Commit

Permalink
Add current state of wormhole gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
eth-r committed Jul 31, 2023
1 parent c4e9bc8 commit 1892686
Show file tree
Hide file tree
Showing 17 changed files with 2,963 additions and 1,184 deletions.
3 changes: 2 additions & 1 deletion cross-chain/solana/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ seeds = false
skip-lint = false
[programs.localnet]
tbtc = "HksEtDgsXJV1BqcuhzbLRTmXp5gHgHJktieJCtQd3pG"
wormhole-gateway = "8H9F5JGbEMyERycwaGuzLS5MQnV7dn2wm2h6egJ3Leiu"

[registry]
url = "https://api.apr.dev"

[provider]
cluster = "Localnet"
wallet = "~/.config/solana/id.json"
wallet = "/home/eth/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
9 changes: 9 additions & 0 deletions cross-chain/solana/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions cross-chain/solana/programs/wormhole-gateway/Cargo.toml
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" }
2 changes: 2 additions & 0 deletions cross-chain/solana/programs/wormhole-gateway/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
7 changes: 7 additions & 0 deletions cross-chain/solana/programs/wormhole-gateway/src/error.rs
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,
}
57 changes: 57 additions & 0 deletions cross-chain/solana/programs/wormhole-gateway/src/lib.rs
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)
}
}
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);
}
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 cross-chain/solana/programs/wormhole-gateway/src/processor/mod.rs
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::*;
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)
}
}
Loading

0 comments on commit 1892686

Please sign in to comment.