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

Refactor #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 22 additions & 9 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
members = [
"programs/*"
"programs/*",
"adapter_common"
]
10 changes: 10 additions & 0 deletions adapter_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "adapter-common"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anchor-lang = "0.24.2"
anchor-spl = "0.24.2"
81 changes: 81 additions & 0 deletions adapter_common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::hash::hash;
use anchor_lang::solana_program::instruction::AccountMeta;
use anchor_spl::token::TokenAccount;

pub fn load_token_account_and_balance<'info>(
remaining_accounts: &[AccountInfo<'info>],
account_index: usize,
) -> TokenAccountAndBalance<'info> {
let token_account_info = &remaining_accounts[account_index];
let token_account = Account::<TokenAccount>::try_from(token_account_info).unwrap();
let balance_before = token_account.amount.clone();
return TokenAccountAndBalance {
token_accout: token_account,
balance_before: balance_before,
};
}

pub struct TokenAccountAndBalance<'info> {
token_accout: Account<'info, TokenAccount>,
balance_before: u64,
}

impl<'info> TokenAccountAndBalance<'info> {
pub fn get_balance_change(&mut self) -> u64 {
self.token_accout.reload().unwrap();
let balance_before = self.balance_before;
let balance_after = self.token_accout.amount;
if balance_after > balance_before {
balance_after.checked_sub(balance_before).unwrap()
} else if balance_after == balance_before {
0_u64
} else {
balance_before.checked_sub(balance_after).unwrap()
}
}
}

pub fn load_remaining_accounts<'info>(
remaining_accounts: &[AccountInfo<'info>],
index_array: Vec<usize>,
) -> Vec<AccountMeta> {
let mut accounts: Vec<AccountMeta> = vec![];
for index in index_array.iter() {
if remaining_accounts[*index].is_writable {
accounts.push(AccountMeta::new(
remaining_accounts[*index].key(),
remaining_accounts[*index].is_signer,
))
} else {
accounts.push(AccountMeta::new_readonly(
remaining_accounts[*index].key(),
remaining_accounts[*index].is_signer,
))
}
}
return accounts;
}

pub fn sighash(namespace: &str, name: &str) -> [u8; 8] {
let preimage = format!("{}:{}", namespace, name);
let mut sighash = [0u8; 8];

sighash.copy_from_slice(&hash(preimage.as_bytes()).to_bytes()[..8]);
sighash
}


#[error_code]
pub enum ErrorCode {
#[msg("Unsupported Action")]
UnsupportedAction,
#[msg("Unsupported PoolDirection")]
UnsupportedPoolDirection,
#[msg("Unsupported Action Version")]
UnsupportedVersion,
#[msg("Unsupported Vault Protocol")]
UnsupportedVaultProtocol,
#[msg("Index might out of bound, currently only support 30 addresses")]
IndexOutOfBound,
}
2 changes: 1 addition & 1 deletion programs/adapter-friktion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ default = []

[dependencies]
anchor-lang = "0.24.2"
anchor-spl = "0.24.2"
adapter-common = { path = "../../adapter_common",version= "0.1.0" }
Loading