Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: a61f117870814e5bdd4acb044f4e2b8c1b052eb4
  • Loading branch information
Lightspark Eng authored and zhenlu committed Apr 11, 2023
1 parent c21cf0e commit e1cfbe2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 28 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2023 Lightspark Group, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
17 changes: 14 additions & 3 deletions copy.bara.sky
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ core.workflow(
# Switch to ITERATIVE mode to import each commit separately.
mode = "SQUASH",

origin_files = glob(["js-sdk/**", "copy.bara.sky"]),
origin_files = glob(
["js-sdk/**", "copy.bara.sky"],
exclude = [
"js-sdk/examples/internal_example.js",
]
),

authoring = authoring.pass_thru("Lightspark Eng <[email protected]>"),
transformations = [
Expand Down Expand Up @@ -108,7 +113,10 @@ core.workflow(
# Switch to ITERATIVE mode to import each commit separately.
mode = "SQUASH",

origin_files = glob(["rust-sdk/lightspark/**", "copy.bara.sky"]),
origin_files = glob(
["rust-sdk/lightspark/**", "copy.bara.sky"],
exclude = ["rust-sdk/lightspark/examples/internal_example.rs"],
),

authoring = authoring.pass_thru("Lightspark Eng <[email protected]>"),
transformations = [
Expand All @@ -134,7 +142,10 @@ core.workflow(
# Switch to ITERATIVE mode to import each commit separately.
mode = "SQUASH",

origin_files = glob(["python-sdk/**", "copy.bara.sky"]),
origin_files = glob(
["python-sdk/**", "copy.bara.sky"],
exclude = ["python-sdk/examples/internal_example.py"],
),

authoring = authoring.pass_thru("Lightspark Eng <[email protected]>"),
transformations = [
Expand Down
42 changes: 28 additions & 14 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use lightspark::objects::currency_amount::CurrencyAmount;
use lightspark::objects::deposit::Deposit;
use lightspark::objects::lightspark_node::LightsparkNode;
use lightspark::objects::outgoing_payment::OutgoingPayment;
use lightspark::objects::permission::Permission;
use lightspark::objects::transaction::Transaction;
use lightspark::objects::withdrawal::Withdrawal;
use lightspark::objects::withdrawal_mode::WithdrawalMode;
Expand All @@ -28,11 +27,13 @@ fn print_fees(fees: Option<CurrencyAmount>) {

#[tokio::main]
async fn main() {
// Setting up the account.
let api_id = std::env::var("LIGHTSPARK_API_TOKEN_CLIENT_ID").unwrap();
let api_token = std::env::var("LIGHTSPARK_API_TOKEN_CLIENT_SECRET").unwrap();

let node_password = std::env::var("LIGHTSPARK_TEST_NODE_PASSWORD").unwrap();

// Create LightsparkClient
let auth_provider = AccountAuthProvider::new(api_id, api_token);
let client = match LightsparkClient::new(auth_provider) {
Ok(value) => value,
Expand All @@ -42,6 +43,7 @@ async fn main() {
}
};

// Bitcoin Fee estimate
if let Ok(fee_estimate) = client
.get_bitcoin_fee_estimates(BitcoinNetwork::Regtest)
.await
Expand All @@ -60,6 +62,7 @@ async fn main() {
}
println!();

// Get current account
let account = match client.get_current_account().await {
Ok(v) => v,
Err(err) => {
Expand All @@ -72,6 +75,7 @@ async fn main() {
println!("You account name is {}", name);
}

// Get current account's API tokens
let connection = match account.get_api_tokens(&client.requester, None).await {
Ok(v) => v,
Err(err) => {
Expand All @@ -85,10 +89,8 @@ async fn main() {
);
println!();

if let Ok(new_api_token) = client
.create_api_token("Test token", Some(vec![Permission::All]))
.await
{
// Create a new API Token
if let Ok(new_api_token) = client.create_api_token("Test token", true, true).await {
println!("Created API token {}.", new_api_token.0.id);

let connection = match account.get_api_tokens(&client.requester, None).await {
Expand All @@ -101,6 +103,7 @@ async fn main() {
println!("You now have {} active API token(s).", connection.count);
println!();

// Delete the created API token
match client.delete_api_token(new_api_token.0.id.as_str()).await {
Ok(()) => println!("Deleted API token {}", new_api_token.0.id),
Err(err) => {
Expand All @@ -123,6 +126,7 @@ async fn main() {
println!();
}

// Get account balance
let local_balance = account
.get_local_balance(&client.requester, Some(vec![BitcoinNetwork::Regtest]), None)
.await;
Expand All @@ -145,6 +149,7 @@ async fn main() {

println!();

// Get nodes in the account.
let node_connections = match account
.get_nodes(
&client.requester,
Expand Down Expand Up @@ -173,6 +178,7 @@ async fn main() {
let node_id = node_id.unwrap();
let node_name = node_name.unwrap();

// Fund node in test mode.
match client.fund_node(node_id.as_str(), 10000).await {
Ok(amount) => {
println!(
Expand All @@ -185,6 +191,7 @@ async fn main() {
}
}

// Get transactions in the account
let transactions_connection = match account
.get_transactions(
&client.requester,
Expand Down Expand Up @@ -243,6 +250,7 @@ async fn main() {

println!();

// Pagination
let page_size = 10;
let mut iterations = 0;
let mut has_next = true;
Expand Down Expand Up @@ -288,6 +296,7 @@ async fn main() {
}
println!();

// Get transactions in the past 24 hours
let time = Utc::now() - Duration::hours(24);
let transactions_connection = match account
.get_transactions(
Expand Down Expand Up @@ -315,6 +324,7 @@ async fn main() {
transactions_connection.count
);

// Get a transaction detail.
if let Some(deposit_transaction_id) = deposit_transaction_id {
let deposit: Deposit = match client
.get_entity::<Deposit>(deposit_transaction_id.as_str())
Expand All @@ -337,6 +347,7 @@ async fn main() {
println!();
}

// Create a lightning invoice
let invoice = match client
.create_invoice(&node_id, 42000, Some("Pizza"), None)
.await
Expand All @@ -354,6 +365,7 @@ async fn main() {
);
println!();

// Decode a payment request
let decoded_request = match client
.get_decoded_payment_request(invoice.data.encoded_payment_request.as_str())
.await
Expand All @@ -379,6 +391,7 @@ async fn main() {
}
println!();

// Unlock the node
match client
.recover_node_signing_key(node_id.as_str(), node_password.as_str())
.await
Expand Down Expand Up @@ -437,8 +450,8 @@ async fn main() {
//
// match client
// .get_lightning_fee_estimate_for_node(
// node_id.as_str(),
// /* Node Public Key */,
// node_id.as_str(),
// /* Node Public Key */,
// 500000)
// .await
// {
Expand All @@ -452,7 +465,7 @@ async fn main() {
// println!("{}", err);
// }
// };

// Key Send
//
// let payment = match client
Expand Down Expand Up @@ -503,19 +516,19 @@ async fn main() {
println!();
}

// Fetch the channels for Node 1
let node = match client
.get_entity::<LightsparkNode>(node_id.as_str())
.await
{
// Fetch the channels for the node
let node = match client.get_entity::<LightsparkNode>(node_id.as_str()).await {
Ok(v) => v,
Err(err) => {
println!("{}", err);
return;
}
};
if let Ok(channels_connection) = node.get_channels(&client.requester, Some(10), None).await {
println!("{} has {} channel(s):", node_name, channels_connection.count);
println!(
"{} has {} channel(s):",
node_name, channels_connection.count
);
for channel in channels_connection.entities {
if let Some(node_entity) = channel.remote_node {
if let Ok(remote_node) = client
Expand All @@ -540,6 +553,7 @@ async fn main() {
}
println!();

// Execute a custom graphql operation
let mut variables: HashMap<&str, Value> = HashMap::new();
variables.insert("networks", BitcoinNetwork::Regtest.into());

Expand Down
22 changes: 13 additions & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ impl LightsparkClient {
pub async fn create_api_token(
&self,
name: &str,
permission: Option<Vec<Permission>>,
transact: bool,
test_mode: bool,
) -> Result<(ApiToken, String), Error> {
let operation = format!(
"
Expand All @@ -209,11 +210,13 @@ impl LightsparkClient {

let mut variables: HashMap<&str, Value> = HashMap::new();
variables.insert("name", name.into());
if permission.is_none() {
variables.insert("permissions", vec![Permission::All].into());
} else {
variables.insert("permissions", permission.into());
}
let permission = match (test_mode, transact) {
(true, true) => vec![Permission::RegtestView, Permission::RegtestTransact],
(true, false) => vec![Permission::RegtestView],
(false, true) => vec![Permission::MainnetView, Permission::MainnetTransact],
(false, false) => vec![Permission::MainnetView],
};
variables.insert("permissions", permission.into());

let value = serde_json::to_value(variables).map_err(Error::ConversionError)?;
let json = self
Expand Down Expand Up @@ -492,7 +495,9 @@ impl LightsparkClient {
let mut variables: HashMap<&str, Value> = HashMap::new();
variables.insert("node_id", node_id.clone().into());
variables.insert("encoded_invoice", encoded_invoice.clone().into());
variables.insert("amount_msats", amount_msats.into());
if let Some(amount_msats) = amount_msats {
variables.insert("amount_msats", amount_msats.into());
}
variables.insert("timeout_secs", timeout_secs.into());
variables.insert("maximum_fees_msats", maximum_fees_msats.into());

Expand Down Expand Up @@ -625,7 +630,7 @@ impl LightsparkClient {
"
mutation RequestWithdrawal(
$node_id: ID!
$amount_sats: Int!
$amount_sats: Long!
$bitcoin_address: String!
$withdrawal_mode: WithdrawalMode!
) {{
Expand Down Expand Up @@ -659,7 +664,6 @@ impl LightsparkClient {
.await
.map_err(Error::ClientError)?;

println!("{}", json);
let result = serde_json::from_value(json["request_withdrawal"]["request"].clone())
.map_err(Error::JsonError)?;
Ok(result)
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
//! let api_id = <your api token id>;
//! let api_token = <your api token secret>;
//! let auth_provider = AccountAuthProvider::new(api_id, api_token);
//! let client = LightsparkClient::new(auth_provider);
//! let client = match LightsparkClient::new(auth_provider) {
//! Ok(value) => value,
//! Err(err) => {
//! println!("{}", err);
//! return;
//! }
//! };
//! ```
//!
//! You are now ready to use the Lightspark SDK!
Expand Down

0 comments on commit e1cfbe2

Please sign in to comment.