1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
use anchor_lang::prelude::*;
pub mod errors;
pub mod instructions;
pub mod state;
pub mod utils;
declare_id!("CrncyaGmZfWvpxRcpHEkSrqeeyQsdn4MAedo9KuARAc4");
hpl_macros::platform_gate!();
use {
errors::ErrorCode,
instructions::*,
state::HolderStatus,
utils::{post_actions, pre_actions},
};
/// The entry point for the HPL currency manager program.
#[program]
pub mod hpl_currency_manager {
use super::*;
/// Create a new currency in the HPL Hive Control program.
///
/// This function serves as a platform gate to manage assets for a project. It calls the
/// `platform_gate_fn` from the `hpl_hive_control` instructions module to check permissions
/// and then calls `create_currency` from the `hpl_hive_control::instructions` module to
/// perform the actual currency creation.
///
/// # Parameters
///
/// - `ctx`: The program context with accounts and instructions provided by the runtime.
/// - `args`: The arguments required to create a new currency.
///
/// # Errors
///
/// This function can return an error if the platform gate fails or if the currency creation
/// encounters any issues.
pub fn create_currency(ctx: Context<CreateCurrency>, args: CreateCurrencyArgs) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::ManageCurrencies,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.authority.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&ctx.accounts.delegate_authority,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
instructions::create_currency(ctx, args)
}
/// Update an existing currency in the HPL Hive Control program.
///
/// This function serves as a platform gate to manage assets for a project. It calls the
/// `platform_gate_fn` from the `hpl_hive_control` instructions module to check permissions
/// and then calls `update_currency` from the `hpl_hive_control::instructions` module to
/// perform the actual currency update.
///
/// # Parameters
///
/// - `ctx`: The program context with accounts and instructions provided by the runtime.
/// - `args`: The arguments required to update an existing currency.
///
/// # Errors
///
/// This function can return an error if the platform gate fails or if the currency update
/// encounters any issues.
pub fn update_currency(ctx: Context<UpdateCurrency>, args: UpdateCurrencyArgs) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::ManageCurrencies,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.authority.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&ctx.accounts.delegate_authority,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
instructions::update_currency(ctx, args)
}
/// Wrap a currency in the HPL Hive Control program.
///
/// This function serves as a platform gate to manage assets for a project. It checks
/// permissions using the `platform_gate_fn` from the `hpl_hive_control` instructions
/// module. If the platform gate is successful, it proceeds to wrap the currency.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
///
/// # Errors
///
/// This function returns an error if the platform gate fails or if the currency wrapping
/// encounters any issues.
pub fn wrap_currency(ctx: Context<WrapCurrency>) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::ManageCurrencies,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.authority.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&ctx.accounts.delegate_authority,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
instructions::wrap_currency(ctx)
}
/// Create a holder account in the HPL Hive Control program.
///
/// This function serves as a platform gate to manage public low-level actions for a project.
/// It checks permissions using the `platform_gate_fn` from the `hpl_hive_control` instructions
/// module. If the platform gate is successful, it proceeds to create the holder account.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
///
/// # Errors
///
/// This function returns an error if the platform gate fails or if any issues occur during the
/// holder account creation process.
pub fn create_holder_account(ctx: Context<CreateHolderAccount>) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::PublicLow,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&None,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
let currency = &ctx.accounts.currency.clone();
let mint = &ctx.accounts.mint.clone();
let token_account = &ctx.accounts.token_account.clone();
let token_program = &ctx.accounts.token_program.clone();
instructions::create_holder_account(ctx)?;
post_actions(currency, mint, token_account, token_program)
}
/// Wrap a holder account in the HPL Hive Control program.
///
/// This function serves as a platform gate to manage public low-level actions for a project.
/// It checks permissions using the `platform_gate_fn` from the `hpl_hive_control` instructions
/// module. If the platform gate is successful, it proceeds to wrap the holder account.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
///
/// # Errors
///
/// This function returns an error if the platform gate fails or if any issues occur during the
/// holder account creation process.
pub fn wrap_holder_account(ctx: Context<WrapHolderAccount>) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::PublicLow,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&None,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
let currency = &ctx.accounts.currency.clone();
let token_program = &ctx.accounts.token_program.clone();
let token_account = &ctx.accounts.token_account.clone();
let mint = &ctx.accounts.mint.clone();
// Create the holder account
instructions::wrap_holder_account(ctx)?;
post_actions(currency, mint, token_account, token_program)
}
/// Fix a holder account in the HPL Hive Control program.
///
/// This function serves as a platform gate to manage public low-level actions for a project.
/// It checks permissions using the `platform_gate_fn` from the `hpl_hive_control` instructions
/// module. If the platform gate is successful, it proceeds to fix the holder account.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
///
/// # Errors
///
/// This function returns an error if the platform gate fails or if any issues occur during the
/// holder account creation process.
pub fn fix_holder_account(ctx: Context<FixHolderAccount>) -> Result<()> {
let currency = &ctx.accounts.currency.clone();
let token_program = &ctx.accounts.token_program.clone();
let token_account = &ctx.accounts.token_account.clone();
let mint = &ctx.accounts.mint.clone();
// Create the holder account
instructions::fix_holder_account(ctx)?;
post_actions(currency, mint, token_account, token_program)
}
/// Mint new currency in the HPL Hive Control program.
///
/// This function serves as a platform gate to manage assets for a project. It checks
/// permissions using the `platform_gate_fn` from the `hpl_hive_control` instructions
/// module. If the platform gate is successful and the holder account status is active,
/// it proceeds to mint the new currency. Otherwise, it returns an error.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
/// - `amount`: The amount of currency to be minted.
///
/// # Errors
///
/// This function returns an error if the platform gate fails, the holder account is inactive,
/// or if any issues occur during the currency minting process.
pub fn mint_currency(ctx: Context<MintCurrency>, amount: u64) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::PublicLow,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.authority.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&None,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
// Check if the holder account status is active, if not, return an error
if ctx.accounts.holder_account.status == HolderStatus::Inactive {
return Err(ErrorCode::InactiveHolder.into());
}
// Perform pre-actions before instructionn
pre_actions(
&ctx.accounts.currency,
&ctx.accounts.mint,
&ctx.accounts.token_account,
&ctx.accounts.token_program,
)?;
let currency = &ctx.accounts.currency.clone();
let token_program = &ctx.accounts.token_program.clone();
let token_account = &ctx.accounts.token_account.clone();
let mint = &ctx.accounts.mint.clone();
// Mint the specified amount of currency
instructions::mint_currency(ctx, amount)?;
post_actions(currency, mint, token_account, token_program)
}
/// Fund an account in the HPL Hive Control program.
///
/// This function performs a platform gate to determine the action based on the caller's
/// program ID. If the caller's program ID matches the HPL Hive Control program ID, it
/// checks permissions using the `platform_gate_fn` for driver actions. Otherwise, it
/// checks if the caller's program ID is allowed based on the list of allowed programs.
/// If the caller's program ID is neither the HPL Hive Control program ID nor in the list
/// of allowed programs, it returns an unauthorized error.
///
/// If the holder account status is inactive, it returns an error. Otherwise, it performs
/// pre-actions before funding the account, calls the `fund_account` instruction to perform
/// the funding, and then performs post-actions.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
/// - `amount`: The amount to fund the account with.
///
/// # Errors
///
/// This function returns an error if the platform gate fails, the holder account is inactive,
/// the caller's program ID is not authorized, or if any issues occur during the funding process.
pub fn fund_account(ctx: Context<FundAccount>, amount: u64) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::FeeExempt,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.owner.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&None,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
// Perform pre-actions before instructionn
pre_actions(
&ctx.accounts.currency,
&ctx.accounts.mint,
&ctx.accounts.token_account,
&ctx.accounts.token_program,
)?;
let currency = &ctx.accounts.currency.clone();
let token_program = &ctx.accounts.token_program.clone();
let token_account = &ctx.accounts.token_account.clone();
let mint = &ctx.accounts.mint.clone();
// Fund the account with the specified amount
instructions::fund_account(ctx, amount)?;
post_actions(currency, mint, token_account, token_program)
}
/// Burn currency in the HPL Hive Control program.
///
/// This function performs a platform gate to determine the action based on the caller's
/// program ID. If the caller's program ID matches the HPL Hive Control program ID, it
/// checks permissions using the `platform_gate_fn` for driver actions. Otherwise, it
/// checks if the caller's program ID is allowed based on the list of allowed programs.
/// If the caller's program ID is neither the HPL Hive Control program ID nor in the list
/// of allowed programs, it returns an unauthorized error.
///
/// If the holder account status is inactive, it returns an error. Otherwise, it performs
/// pre-actions before burning the currency, calls the `burn_currency` instruction to perform
/// the burning, and then performs post-actions.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
/// - `amount`: The amount of currency to be burned.
///
/// # Errors
///
/// This function returns an error if the platform gate fails, the holder account is inactive,
/// the caller's program ID is not authorized, or if any issues occur during the burning process.
pub fn burn_currency(ctx: Context<BurnCurrency>, amount: u64) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::FeeExempt,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.authority.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&None,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
// Perform pre-actions before instructionn
pre_actions(
&ctx.accounts.currency,
&ctx.accounts.mint,
&ctx.accounts.token_account,
&ctx.accounts.token_program,
)?;
let currency = &ctx.accounts.currency.clone();
let token_program = &ctx.accounts.token_program.clone();
let token_account = &ctx.accounts.token_account.clone();
let mint = &ctx.accounts.mint.clone();
// Burn the specified amount of currency
instructions::burn_currency(ctx, amount)?;
post_actions(currency, mint, token_account, token_program)
}
/// Transfer currency in the HPL Hive Control program from one holder account to another.
///
/// This function performs a platform gate to determine the action based on the caller's
/// program ID. If the caller's program ID matches the HPL Hive Control program ID, it
/// checks permissions using the `platform_gate_fn` for driver actions. Otherwise, it
/// checks if the caller's program ID is allowed based on the list of allowed programs.
/// If the caller's program ID is neither the HPL Hive Control program ID nor in the list
/// of allowed programs, it returns an unauthorized error.
///
/// If either the sender's or receiver's holder account status is inactive, it returns an error.
/// Otherwise, it performs pre-actions before transferring the currency, calls the `transfer_currency`
/// instruction to perform the transfer, and then performs post-actions for both the sender's and
/// receiver's token accounts.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
/// - `amount`: The amount of currency to be transferred.
///
/// # Errors
///
/// This function returns an error if the platform gate fails, either the sender's or receiver's
/// holder account is inactive, the caller's program ID is not authorized, or if any issues occur
/// during the transfer process.
pub fn transfer_currency(ctx: Context<TransferCurrency>, amount: u64) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::FeeExempt,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.owner.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&None,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
// Perform pre-actions before instructionn
pre_actions(
&ctx.accounts.currency,
&ctx.accounts.mint,
&ctx.accounts.sender_token_account,
&ctx.accounts.token_program,
)?;
pre_actions(
&ctx.accounts.currency,
&ctx.accounts.mint,
&ctx.accounts.receiver_token_account,
&ctx.accounts.token_program,
)?;
let currency = &ctx.accounts.currency.clone();
let token_program = &ctx.accounts.token_program.clone();
let sender_token_account = &ctx.accounts.sender_token_account.clone();
let receiver_token_account = &ctx.accounts.receiver_token_account.clone();
let mint = &ctx.accounts.mint.clone();
// Transfer the specified amount of currency
instructions::transfer_currency(ctx, amount)?;
post_actions(currency, mint, sender_token_account, token_program)?;
post_actions(currency, mint, receiver_token_account, token_program)
}
///
/// This function performs a platform gate to determine the action based on the caller's
/// program ID. If the caller's program ID matches the HPL Hive Control program ID, it
/// checks permissions using the `platform_gate_fn` for driver actions. Otherwise, it
/// checks if the caller's program ID is allowed based on the list of allowed programs.
/// If the caller's program ID is neither the HPL Hive Control program ID nor in the list
/// of allowed programs, it returns an unauthorized error.
///
/// If the holder account status is inactive, it returns an error. Otherwise, it performs
/// pre-actions before approving the delegate, calls the `approve_delegate` instruction to
/// approve the delegate, and then performs post-actions.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
/// - `amount`: The amount of currency to be approved for delegation.
///
/// # Errors
///
/// This function returns an error if the platform gate fails, the holder account is inactive,
/// the caller's program ID is not authorized, or if any issues occur during the approval process.
pub fn approve_delegate(ctx: Context<ApproveDelegate>, amount: u64) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::FeeExempt,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.owner.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&None,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
// Perform pre-actions before instructionn
pre_actions(
&ctx.accounts.currency,
&ctx.accounts.mint,
&ctx.accounts.token_account,
&ctx.accounts.token_program,
)?;
let currency = &ctx.accounts.currency.clone();
let token_program = &ctx.accounts.token_program.clone();
let token_account = &ctx.accounts.token_account.clone();
let mint = &ctx.accounts.mint.clone();
// Approve the delegate to manage the specified amount of currency
instructions::approve_delegate(ctx, amount)?;
post_actions(currency, mint, token_account, token_program)
}
/// Revoke a delegate's permission to manage currency in the HPL Hive Control program.
///
/// This function performs a platform gate to determine the action based on the caller's
/// program ID. If the caller's program ID matches the HPL Hive Control program ID, it
/// checks permissions using the `platform_gate_fn` for driver actions. Otherwise, it
/// checks if the caller's program ID is allowed based on the list of allowed programs.
/// If the caller's program ID is neither the HPL Hive Control program ID nor in the list
/// of allowed programs, it returns an unauthorized error.
///
/// If the holder account status is inactive, it returns an error. Otherwise, it performs
/// pre-actions before revoking the delegate, calls the `revoke_delegate` instruction to
/// revoke the delegate, and then performs post-actions.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
///
/// # Errors
///
/// This function returns an error if the platform gate fails, the holder account is inactive,
/// the caller's program ID is not authorized, or if any issues occur during the revoking process.
pub fn revoke_delegate(ctx: Context<RevokeDelegate>) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::PublicHigh,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.authority.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&None,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
// Perform pre-actions before instructionn
pre_actions(
&ctx.accounts.currency,
&ctx.accounts.mint,
&ctx.accounts.token_account,
&ctx.accounts.token_program,
)?;
let currency = &ctx.accounts.currency.clone();
let token_program = &ctx.accounts.token_program.clone();
let token_account = &ctx.accounts.token_account.clone();
let mint = &ctx.accounts.mint.clone();
// Revoke the delegate's permission to manage currency
instructions::revoke_delegate(ctx)?;
post_actions(currency, mint, token_account, token_program)
}
/// Set the status of a holder account in the HPL Hive Control program.
///
/// This function performs a platform gate to determine the action based on the caller's
/// program ID. If the caller's program ID matches the HPL Hive Control program ID, it
/// checks permissions using the `platform_gate_fn` for driver actions. Otherwise, it
/// checks if the caller's program ID is allowed based on the list of allowed programs.
/// If the caller's program ID is neither the HPL Hive Control program ID nor in the list
/// of allowed programs, it returns an unauthorized error.
///
/// The function then calls the `set_holder_status` instruction to set the holder account's status.
///
/// # Parameters
///
/// - `ctx`: The program context containing accounts and instructions provided by the runtime.
/// - `status`: The status to be set for the holder account.
///
/// # Errors
///
/// This function returns an error if the platform gate fails, the caller's program ID is not authorized,
/// or if any issues occur during the process of setting the holder account's status.
pub fn set_holder_status(ctx: Context<SetHolderStatus>, status: HolderStatus) -> Result<()> {
platform_gate_cpi(
hpl_hive_control::state::SerializableActions::ManageCurrencyStatus,
None,
ctx.accounts.project.to_account_info(),
ctx.accounts.authority.to_account_info(),
ctx.accounts.payer.to_account_info(),
ctx.accounts.vault.to_account_info(),
&None,
ctx.accounts.system_program.to_account_info(),
ctx.accounts.hive_control.to_account_info(),
ctx.accounts.instructions_sysvar.to_account_info(),
)?;
// Call the `set_holder_status` instruction to set the holder account's status
instructions::set_holder_status(ctx, status)
}
}