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

Updates for Oracles to transition to helium-lib #401

Open
wants to merge 15 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
360 changes: 117 additions & 243 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions helium-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ hex-literal = "0"
chrono = {version = "0", features = ["serde"]}
thiserror = "1"
async-trait = "0"
anchor-client = {version = "0.30.0", features = ["async"] }
anchor-spl = { version = "0.30.0", features = ["mint", "token"] }
anchor-client = {version = "0.29.0", features = ["async"] }
anchor-spl = { version = "0.29.0", features = ["mint", "token"] }
url = {version = "2", features = ["serde"]}
h3o = {version = "0", features = ["serde"]}
helium-crypto = {workspace = true}
Expand All @@ -29,7 +29,7 @@ bincode = "1.3.3"
reqwest = { version = "0", default-features = false, features = [
"rustls-tls",
] }
helium-anchor-gen = {git = "https://github.com/helium/helium-anchor-gen.git", branch = "main" }
helium-anchor-gen = {git = "https://github.com/helium/helium-anchor-gen.git" }
spl-associated-token-account = { version = "*", features = ["no-entrypoint"] }
spl-account-compression = { version = "0.3", features = ["no-entrypoint"] }
tonic = { version = "0", features = ["tls", "tls-roots"] }
Expand Down
66 changes: 66 additions & 0 deletions helium-lib/src/boosting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use chrono::{DateTime, Utc};
use helium_anchor_gen::hexboosting::accounts::StartBoostV0;

use crate::{
anchor_lang::{InstructionData, ToAccountMetas},
client::SolanaRpcClient,
error::Error,
hexboosting,
keypair::{Keypair, Pubkey},
solana_sdk::{instruction::Instruction, signature::Signer, transaction::Transaction},
};

pub trait StartBoostingHex {
fn start_authority(&self) -> Pubkey;
fn boost_config(&self) -> Pubkey;
fn boosted_hex(&self) -> Pubkey;
fn activation_ts(&self) -> DateTime<Utc>;
}

pub async fn start_boost<C: AsRef<SolanaRpcClient>>(
client: &C,
keypair: &Keypair,
updates: impl IntoIterator<Item = impl StartBoostingHex>,
) -> Result<Transaction, Error> {
fn mk_accounts(
start_authority: Pubkey,
boost_config: Pubkey,
boosted_hex: Pubkey,
) -> StartBoostV0 {
StartBoostV0 {
start_authority,
boost_config,
boosted_hex,
}
}

let mut ixs = vec![];
for update in updates {
let accounts = mk_accounts(
update.start_authority(),
update.boost_config(),
update.boosted_hex(),
);
let ix = Instruction {
program_id: hexboosting::id(),
accounts: accounts.to_account_metas(None),
data: hexboosting::instruction::StartBoostV0 {
_args: hexboosting::StartBoostArgsV0 {
start_ts: update.activation_ts().timestamp(),
},
}
.data(),
};
ixs.push(ix);
}

let recent_blockhash = client.as_ref().get_latest_blockhash().await?;
let tx = Transaction::new_signed_with_payer(
&ixs,
Some(&keypair.pubkey()),
&[keypair],
recent_blockhash,
);

Ok(tx)
}
33 changes: 27 additions & 6 deletions helium-lib/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
data_credits, entity_key::AsEntityKey, helium_entity_manager, helium_sub_daos, keypair::Pubkey,
lazy_distributor, programs::TOKEN_METADATA_PROGRAM_ID, token::Token,
};
use chrono::Timelike;
use sha2::{Digest, Sha256};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -97,6 +98,17 @@ impl Dao {
);
key
}

pub fn dc_account_payer() -> Pubkey {
let (key, _) = Pubkey::find_program_address(&[b"account_payer"], &data_credits::id());
key
}

pub fn dc_key() -> Pubkey {
let (key, _) =
Pubkey::find_program_address(&[b"dc", Token::Dc.mint().as_ref()], &data_credits::id());
key
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -151,12 +163,6 @@ impl SubDao {
key
}

pub fn dc_key() -> Pubkey {
let (key, _) =
Pubkey::find_program_address(&[b"dc", Token::Dc.mint().as_ref()], &data_credits::id());
key
}

pub fn delegated_dc_key(&self, router_key: &str) -> Pubkey {
let hash = Sha256::digest(router_key);
let (key, _) = Pubkey::find_program_address(
Expand Down Expand Up @@ -231,4 +237,19 @@ impl SubDao {
);
key
}

pub fn epoch_info_key(&self) -> Pubkey {
const EPOCH_LENGTH: u32 = 60 * 60 * 24;
let epoch = chrono::Utc::now().second() / EPOCH_LENGTH;

let (key, _) = Pubkey::find_program_address(
&[
"sub_dao_epoch_info".as_bytes(),
self.key().as_ref(),
&epoch.to_le_bytes(),
],
&helium_sub_daos::ID,
);
key
}
}
88 changes: 84 additions & 4 deletions helium-lib/src/dc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use anchor_client::anchor_lang::AccountDeserialize;
use helium_anchor_gen::{
data_credits::accounts::BurnDelegatedDataCreditsV0,
helium_sub_daos::{self, DaoV0, SubDaoV0},
};

use crate::{
anchor_lang::{InstructionData, ToAccountMetas},
anchor_spl, circuit_breaker,
Expand All @@ -6,6 +12,7 @@ use crate::{
data_credits,
error::{DecodeError, Error},
keypair::{Keypair, Pubkey},
priority_fee,
solana_sdk::{instruction::Instruction, signer::Signer, transaction::Transaction},
token::{Token, TokenAmount},
};
Expand Down Expand Up @@ -37,7 +44,7 @@ pub async fn mint<C: AsRef<SolanaRpcClient>>(
hnt_price_oracle: Pubkey,
) -> impl ToAccountMetas {
data_credits::accounts::MintDataCreditsV0 {
data_credits: SubDao::dc_key(),
data_credits: Dao::dc_key(),
owner,
hnt_mint: *Token::Hnt.mint(),
dc_mint: *Token::Dc.mint(),
Expand All @@ -55,7 +62,7 @@ pub async fn mint<C: AsRef<SolanaRpcClient>>(

let hnt_price_oracle = client
.as_ref()
.anchor_account::<data_credits::DataCreditsV0>(&SubDao::dc_key())
.anchor_account::<data_credits::DataCreditsV0>(&Dao::dc_key())
.await?
.hnt_price_oracle;

Expand Down Expand Up @@ -89,7 +96,7 @@ pub async fn delegate<C: AsRef<SolanaRpcClient>>(
fn mk_accounts(delegated_dc_key: Pubkey, subdao: SubDao, owner: Pubkey) -> impl ToAccountMetas {
data_credits::accounts::DelegateDataCreditsV0 {
delegated_data_credits: delegated_dc_key,
data_credits: SubDao::dc_key(),
data_credits: Dao::dc_key(),
dc_mint: *Token::Dc.mint(),
dao: Dao::Hnt.key(),
sub_dao: subdao.key(),
Expand Down Expand Up @@ -136,7 +143,7 @@ pub async fn burn<C: AsRef<SolanaRpcClient>>(
data_credits::accounts::BurnWithoutTrackingV0BurnAccounts {
burner: Token::Dc.associated_token_adress(&owner),
dc_mint: *Token::Dc.mint(),
data_credits: SubDao::dc_key(),
data_credits: Dao::dc_key(),
token_program: anchor_spl::token::ID,
system_program: solana_sdk::system_program::ID,
associated_token_program: anchor_spl::associated_token::ID,
Expand All @@ -162,3 +169,76 @@ pub async fn burn<C: AsRef<SolanaRpcClient>>(
);
Ok(tx)
}

pub async fn burn_delegated<C: AsRef<SolanaRpcClient>>(
client: &C,
sub_dao: SubDao,
keypair: &Keypair,
amount: u64,
router_key: Pubkey,
) -> Result<solana_sdk::transaction::Transaction, Error> {
fn mk_accounts(
sub_dao: SubDao,
router_key: Pubkey,
dc_burn_authority: Pubkey,
registrar: Pubkey,
) -> BurnDelegatedDataCreditsV0 {
let delegated_data_credits = SubDao::Iot.delegated_dc_key(&router_key.to_string());
let escrow_account = SubDao::Iot.escrow_key(&delegated_data_credits);

BurnDelegatedDataCreditsV0 {
sub_dao_epoch_info: sub_dao.epoch_info_key(),
delegated_data_credits,
escrow_account,

dao: Dao::Hnt.key(),
sub_dao: sub_dao.key(),

account_payer: Dao::dc_account_payer(),
data_credits: Dao::dc_key(),
dc_burn_authority,
dc_mint: *Token::Dc.mint(),
registrar,

token_program: anchor_spl::token::ID,
helium_sub_daos_program: helium_sub_daos::id(),
system_program: solana_sdk::system_program::ID,
}
}

let (dc_burn_authority, registrar) = {
let account_data = client.as_ref().get_account_data(&SubDao::Iot.key()).await?;
let sub_dao = SubDaoV0::try_deserialize(&mut account_data.as_ref())?;

let account_data = client.as_ref().get_account_data(&Dao::Hnt.key()).await?;
let dao = DaoV0::try_deserialize(&mut account_data.as_ref())?;

(sub_dao.dc_burn_authority, dao.registrar)
};

let accounts = mk_accounts(sub_dao, router_key, dc_burn_authority, registrar);
let burn_ix = solana_sdk::instruction::Instruction {
program_id: data_credits::id(),
accounts: accounts.to_account_metas(None),
data: data_credits::instruction::BurnDelegatedDataCreditsV0 {
_args: data_credits::BurnDelegatedDataCreditsArgsV0 { amount },
}
.data(),
};

let ixs = [
priority_fee::compute_price_instruction(300_000),
priority_fee::compute_price_instruction_for_accounts(client, &burn_ix.accounts).await?,
burn_ix,
];

let recent_blockhash = client.as_ref().get_latest_blockhash().await?;

let tx = Transaction::new_signed_with_payer(
&ixs,
Some(&keypair.pubkey()),
&[keypair],
recent_blockhash,
);
Ok(tx)
}
2 changes: 2 additions & 0 deletions helium-lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ impl Error {

#[derive(Debug, Error)]
pub enum EncodeError {
#[error("io: {0}")]
Io(#[from] std::io::Error),
#[error("proto: {0}")]
Proto(#[from] helium_proto::EncodeError),
#[error("bincode: {0}")]
Expand Down
4 changes: 2 additions & 2 deletions helium-lib/src/hotspot/dataonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod iot {
key_to_asset: dao.entity_key_to_kta_key(&entity_key),
sub_dao: SubDao::Iot.key(),
dc_mint: *Token::Dc.mint(),
dc: SubDao::dc_key(),
dc: Dao::dc_key(),
compression_program: SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
data_credits_program: data_credits::id(),
helium_sub_daos_program: helium_sub_daos::id(),
Expand Down Expand Up @@ -137,7 +137,7 @@ mod mobile {
key_to_asset: dao.entity_key_to_kta_key(&entity_key),
sub_dao: SubDao::Mobile.key(),
dc_mint: *Token::Dc.mint(),
dc: SubDao::dc_key(),
dc: Dao::dc_key(),
dnt_mint: *Token::Mobile.mint(),
dnt_price: *Token::Mobile.price_key().unwrap(), // safe to unwrap
dnt_burner: Token::Mobile.associated_token_adress(&owner),
Expand Down
2 changes: 1 addition & 1 deletion helium-lib/src/hotspot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub async fn direct_update<C: AsRef<SolanaRpcClient> + AsRef<DasClient>>(
dao: Dao::Hnt.key(),
sub_dao: subdao.key(),
dc_mint: *Token::Dc.mint(),
dc: SubDao::dc_key(),
dc: Dao::dc_key(),
compression_program: SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
data_credits_program: data_credits::id(),
token_program: anchor_spl::token::ID,
Expand Down
3 changes: 2 additions & 1 deletion helium-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod asset;
pub mod b64;
pub mod client;

pub mod boosting;
pub mod dao;
pub mod dc;
pub mod entity_key;
Expand All @@ -20,7 +21,7 @@ pub use anchor_client::solana_client;
pub use anchor_spl;
pub use helium_anchor_gen::{
anchor_lang, circuit_breaker, data_credits, helium_entity_manager, helium_sub_daos,
lazy_distributor,
hexboosting, lazy_distributor,
};
pub use solana_sdk;
pub use solana_sdk::bs58;
Expand Down
12 changes: 6 additions & 6 deletions helium-wallet/src/read_write.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::result::{bail, Result};
use anyhow::{bail, Result};
use helium_crypto::{ecc_compact, ed25519, multisig, KeyType};
use io::{Read, Write};
use std::io;
Expand All @@ -7,11 +7,11 @@ pub trait ReadWrite {
fn read(reader: &mut dyn Read) -> Result<Self>
where
Self: std::marker::Sized;
fn write(&self, writer: &mut dyn Write) -> Result;
fn write(&self, writer: &mut dyn Write) -> Result<()>;
}

impl ReadWrite for helium_crypto::PublicKey {
fn write(&self, writer: &mut dyn io::Write) -> Result {
fn write(&self, writer: &mut dyn io::Write) -> Result<()> {
Ok(writer.write_all(&self.to_vec())?)
}

Expand All @@ -22,7 +22,7 @@ impl ReadWrite for helium_crypto::PublicKey {
KeyType::Ed25519 => ed25519::PUBLIC_KEY_LENGTH,
KeyType::EccCompact => ecc_compact::PUBLIC_KEY_LENGTH,
KeyType::MultiSig => multisig::PUBLIC_KEY_LENGTH,
KeyType::Secp256k1 => bail!("Secp256k1 key type unsupported for read."),
KeyType::Secp256k1 => bail!("Secp256k1 key type unsupported for read.",),
KeyType::Rsa => bail!("RSA key type unsupported for read."),
};
data.resize(key_size, 0);
Expand All @@ -32,7 +32,7 @@ impl ReadWrite for helium_crypto::PublicKey {
}

impl ReadWrite for helium_lib::keypair::Pubkey {
fn write(&self, writer: &mut dyn io::Write) -> Result {
fn write(&self, writer: &mut dyn io::Write) -> Result<()> {
writer.write_all(&self.to_bytes())?;
Ok(())
}
Expand All @@ -45,7 +45,7 @@ impl ReadWrite for helium_lib::keypair::Pubkey {
}

impl ReadWrite for helium_lib::keypair::Keypair {
fn write(&self, writer: &mut dyn io::Write) -> Result {
fn write(&self, writer: &mut dyn io::Write) -> Result<()> {
writer.write_all(&self.to_bytes())?;
Ok(())
}
Expand Down
Loading