Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solana: fix finalize transfer #60

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ use omni_types::{near_events::Nep141LockerEvent, OmniAddress};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::{
instruction::{AccountMeta, Instruction},
program_option::COption,
program_pack::Pack,
pubkey::Pubkey,
signature::{Keypair, Signature},
signer::Signer,
system_program, sysvar,
transaction::Transaction,
};
use spl_token::state::Mint;

mod error;
mod instructions;
Expand Down Expand Up @@ -39,7 +42,6 @@ pub struct TransferId {
pub struct DepositPayload {
pub destination_nonce: u64,
pub transfer_id: TransferId,
pub token: String,
pub amount: u128,
pub recipient: Pubkey,
pub fee_recipient: Option<String>,
Expand Down Expand Up @@ -273,12 +275,16 @@ impl SolanaBridgeClient {
],
&spl_associated_token_account::ID,
);
let (vault, _) = Pubkey::find_program_address(&[b"vault", token.as_ref()], program_id);

let (wormhole_bridge, wormhole_fee_collector, wormhole_sequence) =
self.get_wormhole_accounts().await?;
let wormhole_message = Keypair::new();

let is_solana_native = match self.get_token_owner(token).await? {
COption::Some(owner) => owner == authority,
COption::None => false,
};

let instruction_data = InitTransfer {
amount,
recipient,
Expand All @@ -293,7 +299,13 @@ impl SolanaBridgeClient {
AccountMeta::new_readonly(authority, false),
AccountMeta::new(token, false),
AccountMeta::new(from_token_account, false),
AccountMeta::new(vault, false), // Optional
if is_solana_native {
let (vault, _) =
Pubkey::find_program_address(&[b"vault", token.as_ref()], program_id);
AccountMeta::new(vault, false)
} else {
AccountMeta::new(*program_id, false) // Vault is not present for non-native tokens
},
AccountMeta::new(sol_vault, false),
AccountMeta::new(keypair.pubkey(), true),
AccountMeta::new_readonly(config, false),
Expand Down Expand Up @@ -531,7 +543,6 @@ impl SolanaBridgeClient {
origin_chain: 1,
origin_nonce: message_payload.transfer_id.origin_nonce,
},
token: "wrap.testnet".to_string(),
amount: message_payload.amount.into(),
recipient: match message_payload.recipient {
OmniAddress::Sol(addr) => Pubkey::new_from_array(addr.0),
Expand Down Expand Up @@ -585,6 +596,17 @@ impl SolanaBridgeClient {
Ok(signature)
}

async fn get_token_owner(&self, token: Pubkey) -> Result<COption<Pubkey>, SolanaClientError> {
let client = self.client()?;

let mint_account = client.get_account(&token).await?;

let mint_data = Mint::unpack(&mint_account.data)
.map_err(|e| SolanaClientError::InvalidAccountData(e.to_string()))?;

Ok(mint_data.mint_authority)
}

pub fn client(&self) -> Result<&RpcClient, SolanaClientError> {
self.client.as_ref().ok_or(SolanaClientError::ConfigError(
"Client not initialized".to_string(),
Expand Down
Loading