cln: adds new RPC method to issue assets
This commit introduce a new RPC method for issuing new assets. Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
parent
6809025d3b
commit
2d71bebf67
|
@ -79,6 +79,7 @@ pub fn build_plugin() -> anyhow::Result<Plugin<State>> {
|
|||
methods: [
|
||||
rgb_balance,
|
||||
rgb_fundchannel,
|
||||
rgb_issue_asset,
|
||||
],
|
||||
hooks: [],
|
||||
};
|
||||
|
@ -99,6 +100,11 @@ fn rgb_fundchannel(plugin: &mut Plugin<State>, request: Value) -> Result<Value,
|
|||
walletrpc::fund_rgb_channel(plugin, request)
|
||||
}
|
||||
|
||||
#[rpc_method(rpc_name = "issueasset", description = "Issue a new RGB asset")]
|
||||
fn rgb_issue_asset(plugin: &mut Plugin<State>, request: Value) -> Result<Value, PluginError> {
|
||||
walletrpc::rgb_issue_new_assert(plugin, request)
|
||||
}
|
||||
|
||||
fn read_secret(file: fs::File, network: &str) -> anyhow::Result<ExtendedPrivKey> {
|
||||
let buffer = io::BufReader::new(file);
|
||||
let network = vlsbtc::Network::from_str(network)?;
|
||||
|
|
|
@ -164,3 +164,29 @@ pub fn fund_rgb_channel(plugin: &mut Plugin<State>, request: Value) -> Result<Va
|
|||
"rgb_info": info,
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct NewAssetRequest {
|
||||
amounts: Vec<u64>,
|
||||
ticker: String,
|
||||
name: String,
|
||||
precision: u8,
|
||||
}
|
||||
|
||||
pub fn rgb_issue_new_assert(
|
||||
plugin: &mut Plugin<State>,
|
||||
request: Value,
|
||||
) -> Result<Value, PluginError> {
|
||||
log::info!("calling rgb issue asset with request body: `{request}`");
|
||||
let request: NewAssetRequest = json::from_value(request)?;
|
||||
let rgb = plugin.state.manager();
|
||||
let assert = rgb
|
||||
.issue_asset_nia(
|
||||
request.ticker,
|
||||
request.name,
|
||||
request.precision,
|
||||
request.amounts,
|
||||
)
|
||||
.map_err(|err| error!("{err}"))?;
|
||||
Ok(json::to_value(assert)?)
|
||||
}
|
||||
|
|
|
@ -108,7 +108,7 @@ impl Wallet {
|
|||
};
|
||||
let assert = self.wallet.lock().unwrap().issue_asset_nia(
|
||||
online.clone(),
|
||||
ticker,
|
||||
ticker,
|
||||
name,
|
||||
precision,
|
||||
amounts,
|
||||
|
|
|
@ -4,6 +4,7 @@ use std::sync::Arc;
|
|||
|
||||
use bitcoin::bip32::ExtendedPrivKey;
|
||||
use bitcoin::Network;
|
||||
use rgb_lib::wallet::AssetNIA;
|
||||
use rgb_lib::wallet::Balance;
|
||||
use rgb_lib::wallet::Recipient;
|
||||
use rgb_lib::wallet::RecipientData;
|
||||
|
@ -61,6 +62,18 @@ impl RGBManager {
|
|||
self.consignment_proxy.clone()
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
pub fn issue_asset_nia(
|
||||
&self,
|
||||
ticker: String,
|
||||
name: String,
|
||||
precision: u8,
|
||||
amounts: Vec<u64>,
|
||||
) -> anyhow::Result<AssetNIA> {
|
||||
self.wallet
|
||||
.issue_asset_nia(ticker, name, precision, amounts)
|
||||
}
|
||||
|
||||
pub fn assert_balance(&self, asset_id: String) -> anyhow::Result<Balance> {
|
||||
let balance = self
|
||||
.wallet
|
||||
|
|
|
@ -93,6 +93,21 @@ macro_rules! wait_sync {
|
|||
}};
|
||||
}
|
||||
|
||||
pub fn make_new_asset_id(node: &cln::Node, ticker: String, name: String) -> anyhow::Result<String> {
|
||||
let asset: serde_json::Value = node.rpc().call(
|
||||
"issueasset",
|
||||
json!({
|
||||
"name": name,
|
||||
"ticker": ticker,
|
||||
"amounts": [10000],
|
||||
"precision": 0,
|
||||
}),
|
||||
)?;
|
||||
log::info!("new asset generated is `{asset}`");
|
||||
let asset_id = asset.get("asset_id").unwrap();
|
||||
Ok(asset_id.to_string())
|
||||
}
|
||||
|
||||
/// Open a channel from node_a -> node_b
|
||||
pub fn open_rgb_channel(
|
||||
node_a: &cln::Node,
|
||||
|
@ -105,23 +120,18 @@ pub fn open_rgb_channel(
|
|||
|
||||
wait_sync!(node_a);
|
||||
|
||||
if dual_open {
|
||||
let addr = node_b.rpc().newaddr(None)?.address.unwrap();
|
||||
fund_wallet(node_b.btc(), &addr, 6)?;
|
||||
}
|
||||
|
||||
let getinfo2 = node_b.rpc().getinfo()?;
|
||||
node_a
|
||||
.rpc()
|
||||
.connect(&getinfo2.id, Some(&format!("127.0.0.1:{}", node_b.port)))?;
|
||||
let listfunds = node_a.rpc().listfunds()?;
|
||||
log::debug!("list funds {:?}", listfunds);
|
||||
// TODO generate a new channel
|
||||
let asset_id = make_new_asset_id(node_a, "USTD".to_string(), "Tether".to_string())?;
|
||||
node_a.rpc().call(
|
||||
"fundrgbchannel",
|
||||
serde_json::json!({
|
||||
"peer_id": getinfo2.id,
|
||||
"peer_id": getinfo2.id,
|
||||
"amount_msat": "all",
|
||||
"asset_id": "Tether/USTD",
|
||||
"asset_id": asset_id,
|
||||
}),
|
||||
)?;
|
||||
wait!(
|
||||
|
|
Loading…
Reference in New Issue