Skip to content

Commit

Permalink
feat(solana): adds shared folder
Browse files Browse the repository at this point in the history
  • Loading branch information
allemanfredi committed Nov 29, 2024
1 parent 34593ca commit bfc7770
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/solana/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"programs/*"
"programs/*",
"shared/*"
]
resolver = "2"

Expand Down
3 changes: 2 additions & 1 deletion packages/solana/programs/debridge-reporter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = "0.30.1"
debridge-solana-sdk = { git = "ssh://git@github.com/debridge-finance/debridge-solana-sdk.git", version = "1.0.2" }
debridge-solana-sdk = "1.0.1"
slots = { path = "../../shared/slots" }
13 changes: 0 additions & 13 deletions packages/solana/programs/debridge-reporter/src/error.rs

This file was deleted.

11 changes: 8 additions & 3 deletions packages/solana/programs/debridge-reporter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use debridge_solana_sdk::sending;
use slots::get_slot;

declare_id!("2TvQ6gqQGAifdV2cQ1f8zHGYV2t6wPUNTKzpcALt8rX7");

pub mod contexts;
pub mod error;
pub mod utils;

pub use contexts::*;
pub use utils::{get_slot, u64_to_u8_32};

#[derive(BorshSerialize)]
pub struct Message {
Expand Down Expand Up @@ -46,3 +44,10 @@ pub mod debridge_reporter {
Ok(())
}
}

pub fn u64_to_u8_32(number: u64) -> [u8; 32] {
let mut bytes = [0u8; 32];
let number_bytes = number.to_be_bytes();
bytes[24..].copy_from_slice(&number_bytes);
bytes
}
16 changes: 16 additions & 0 deletions packages/solana/shared/slots/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "slots"
version = "0.1.0"
description = "Utilities functions to access Solana slots"
edition = "2021"

[features]
default = []
cpi = ["no-entrypoint"]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = "0.30.1"
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::sysvar;

use crate::error::ErrorCode;

pub fn get_slot(slot_hashes: &AccountInfo, slot_number: u64) -> Result<(u64, [u8; 32])> {
if *slot_hashes.key != sysvar::slot_hashes::ID {
return Err(error!(ErrorCode::InvalidSlotHashesSysVar));
Expand Down Expand Up @@ -35,12 +33,14 @@ pub fn get_slot(slot_hashes: &AccountInfo, slot_number: u64) -> Result<(u64, [u8
Err(error!(ErrorCode::SlotNotFound))
}

pub fn u64_to_u8_32(number: u64) -> [u8; 32] {
let mut bytes = [0u8; 32];
let number_bytes = number.to_be_bytes(); // Convert u64 to a big-endian 8-byte array

// Copy the 8 bytes of the u64 into the last 8 bytes of the 32-byte array
bytes[24..].copy_from_slice(&number_bytes);

bytes
#[error_code]
pub enum ErrorCode {
#[msg("Invalid latest hash hash.")]
InvalidLatestHashLength,
#[msg("Invalid slot hashes sysvar.")]
InvalidSlotHashesSysVar,
#[msg("Slot hashes not available.")]
SlotHashesNotAvailable,
#[msg("Slot not found")]
SlotNotFound,
}

0 comments on commit bfc7770

Please sign in to comment.