Skip to main content

create_numeric_event

Function create_numeric_event 

Source
pub fn create_numeric_event(
    secp: &Secp256k1<All>,
    key_pair: &Keypair,
    event_id: &str,
    base: u16,
    num_digits: u16,
    is_signed: bool,
    precision: i32,
    unit: &str,
    event_maturity_epoch: u32,
    nonces: &[XOnlyPublicKey],
) -> Result<OracleAnnouncement, Error>
Expand description

Creates a numeric event announcement for oracle events with numeric outcomes.

This function creates an OracleAnnouncement for events where the outcome is a numeric value that can be decomposed into digits. The value is represented in a specified base (currently only base 2 is supported) and can be signed or unsigned.

§Arguments

  • secp - Secp256k1 context for cryptographic operations
  • key_pair - Oracle’s key pair for signing the announcement
  • event_id - Unique identifier for this event
  • base - Numeric base for digit decomposition (must be 2)
  • num_digits - Number of digits in the numeric representation
  • is_signed - Whether the numeric value can be negative
  • precision - Decimal precision for the numeric value
  • unit - Unit of measurement for the numeric value
  • event_maturity_epoch - Unix timestamp when the event matures
  • nonces - Vector of public keys for nonces (length must match required nonces)

§Returns

  • Ok(OracleAnnouncement) - The signed announcement if successful
  • Err(Error::InvalidEventId) - If the event_id is empty
  • Err(Error::InvalidBase) - If base is not 2
  • Err(Error::InvalidNumberOfDigits) - If num_digits is 0 or more than 63
  • Err(Error::InvalidNonces) - If the number of nonces doesn’t match the required count
  • Err(Error::Internal) - If cryptographic operations fail

§Example

use kormir::*;
use bitcoin::secp256k1::{rand, Secp256k1, SecretKey};
use secp256k1_zkp::Keypair;

let secp = Secp256k1::new();
let key_pair = Keypair::new(&secp, &mut rand::thread_rng());
let nonce_keys: Vec<SecretKey> = (0..6)
    .map(|_| SecretKey::from_keypair(&Keypair::new(&secp, &mut rand::thread_rng())))
    .collect();
let nonces = nonce_keys.iter().map(|k| k.x_only_public_key(&secp).0).collect::<Vec<bitcoin::XOnlyPublicKey>>();

let announcement = create_numeric_event(
    &secp,
    &key_pair,
    &"temperature".to_string(),
    2, // base 2
    5, // 5 digits
    true, // signed
    1, // 1 decimal place
    &"°C".to_string(),
    1640995200, // 2022-01-01 00:00:00 UTC
    &nonces,
).unwrap();

§DLC Spec