Skip to content

Commit cdf1a98

Browse files
authored
Merge pull request RGB-Tools#69 from bitwalt/feat/push-asset-amount
Add push_asset_amount to OpenChannelRequest and update validation logic
2 parents 648bd3b + d419b04 commit cdf1a98

12 files changed

Lines changed: 360 additions & 9 deletions

openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,9 @@ components:
22722272
- string
22732273
- 'null'
22742274
example: rgb:CJkb4YZw-jRiz2sk-~PARPio-wtVYI1c-XAEYCqO-wTfvRZ8
2275+
push_asset_amount:
2276+
type: integer
2277+
example: 100
22752278
public:
22762279
type: boolean
22772280
example: true

src/ldk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ async fn handle_ldk_events(
590590
&PathBuf::from(&static_state.ldk_data_dir),
591591
);
592592

593-
let channel_rgb_amount: u64 = rgb_info.local_rgb_amount;
593+
let channel_rgb_amount = rgb_info.local_rgb_amount + rgb_info.remote_rgb_amount;
594594
let asset_id = rgb_info.contract_id.to_string();
595595
let assignment = match rgb_info.schema {
596596
AssetSchema::Nia | AssetSchema::Cfa => Assignment::Fungible(channel_rgb_amount),

src/routes.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ pub(crate) struct OpenChannelRequest {
839839
pub(crate) push_msat: u64,
840840
pub(crate) asset_amount: Option<u64>,
841841
pub(crate) asset_id: Option<String>,
842+
pub(crate) push_asset_amount: Option<u64>,
842843
pub(crate) public: bool,
843844
pub(crate) with_anchors: bool,
844845
pub(crate) fee_base_msat: Option<u32>,
@@ -3044,6 +3045,21 @@ pub(crate) async fn open_channel(
30443045
)));
30453046
}
30463047

3048+
if let Some(push_asset_amount) = payload.push_asset_amount {
3049+
if colored_info.is_none() {
3050+
return Err(APIError::InvalidAmount(s!(
3051+
"push_asset_amount can only be used with RGB channels (asset_id must be specified)"
3052+
)));
3053+
}
3054+
if let Some((_, asset_amount)) = &colored_info {
3055+
if push_asset_amount > *asset_amount {
3056+
return Err(APIError::InvalidAmount(s!(
3057+
"push_asset_amount cannot be higher than asset_amount"
3058+
)));
3059+
}
3060+
}
3061+
}
3062+
30473063
if colored_info.is_some() && !payload.with_anchors {
30483064
return Err(APIError::AnchorsRequired);
30493065
}
@@ -3175,6 +3191,7 @@ pub(crate) async fn open_channel(
31753191
temporary_channel_id,
31763192
Some(config),
31773193
consignment_endpoint,
3194+
payload.push_asset_amount,
31783195
)
31793196
.map_err(|e| {
31803197
*unlocked_state.rgb_send_lock.lock().unwrap() = false;
@@ -3201,11 +3218,12 @@ pub(crate) async fn open_channel(
32013218
tracing::info!("EVENT: initiated channel with peer {}", peer_pubkey);
32023219

32033220
if let Some((contract_id, asset_amount)) = &colored_info {
3221+
let push_amount = payload.push_asset_amount.unwrap_or(0);
32043222
let rgb_info = RgbInfo {
32053223
contract_id: *contract_id,
32063224
schema: schema.unwrap(),
3207-
local_rgb_amount: *asset_amount,
3208-
remote_rgb_amount: 0,
3225+
local_rgb_amount: *asset_amount - push_amount,
3226+
remote_rgb_amount: push_amount,
32093227
};
32103228
write_rgb_channel_info(
32113229
&get_rgb_channel_info_path(

src/test/close_coop_vanilla.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ async fn without_anchors() {
138138
None,
139139
None,
140140
None,
141+
None,
141142
false,
142143
)
143144
.await;

src/test/concurrent_openchannel.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ async fn concurrent_openchannel() {
5959
Some(PUSH_MSAT),
6060
Some(ASSET_AMOUNT),
6161
Some(&asset_id),
62+
None,
6263
20,
6364
),
6465
open_channel_with_retry(
@@ -69,6 +70,7 @@ async fn concurrent_openchannel() {
6970
Some(PUSH_MSAT),
7071
Some(ASSET_AMOUNT),
7172
Some(&asset_id),
73+
None,
7274
20,
7375
),
7476
open_channel_with_retry(
@@ -79,6 +81,7 @@ async fn concurrent_openchannel() {
7981
Some(PUSH_MSAT),
8082
Some(ASSET_AMOUNT),
8183
Some(&asset_id),
84+
None,
8285
20,
8386
),
8487
open_channel_with_retry(
@@ -89,6 +92,7 @@ async fn concurrent_openchannel() {
8992
Some(PUSH_MSAT),
9093
Some(ASSET_AMOUNT),
9194
Some(&asset_id),
95+
None,
9296
20,
9397
),
9498
open_channel_with_retry(
@@ -99,6 +103,7 @@ async fn concurrent_openchannel() {
99103
Some(PUSH_MSAT),
100104
Some(ASSET_AMOUNT),
101105
Some(&asset_id),
106+
None,
102107
20,
103108
)
104109
);

src/test/getchannelid.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ async fn getchannelid_success() {
3737
Some(&asset_id),
3838
None,
3939
None,
40+
None,
4041
Some(&temporary_channel_id),
4142
true,
4243
)

src/test/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ async fn open_channel(
10841084
None,
10851085
None,
10861086
None,
1087+
None,
10871088
true,
10881089
)
10891090
.await
@@ -1098,6 +1099,7 @@ async fn open_channel_with_retry(
10981099
push_msat: Option<u64>,
10991100
asset_amount: Option<u64>,
11001101
asset_id: Option<&str>,
1102+
push_asset_amount: Option<u64>,
11011103
max_retries: u32,
11021104
) -> Channel {
11031105
let mut attempt = 0;
@@ -1111,6 +1113,7 @@ async fn open_channel_with_retry(
11111113
push_msat,
11121114
asset_amount,
11131115
asset_id,
1116+
push_asset_amount,
11141117
None,
11151118
None,
11161119
None,
@@ -1146,6 +1149,7 @@ async fn open_channel_raw(
11461149
push_msat: Option<u64>,
11471150
asset_amount: Option<u64>,
11481151
asset_id: Option<&str>,
1152+
push_asset_amount: Option<u64>,
11491153
fee_base_msat: Option<u32>,
11501154
fee_proportional_millionths: Option<u32>,
11511155
temporary_channel_id: Option<&str>,
@@ -1180,6 +1184,7 @@ async fn open_channel_raw(
11801184
push_msat: push_msat.unwrap_or(0),
11811185
asset_amount,
11821186
asset_id: asset_id.map(|a| a.to_string()),
1187+
push_asset_amount,
11831188
public: true,
11841189
with_anchors,
11851190
fee_base_msat,
@@ -1207,10 +1212,18 @@ async fn open_channel_raw(
12071212
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
12081213
let channels = list_channels(node_address).await;
12091214
if let Some(channel) = channels.iter().find(|c| {
1215+
let asset_amounts_match = if asset_id.is_some() {
1216+
let local_amount = asset_amount.unwrap_or(0) - push_asset_amount.unwrap_or(0);
1217+
let remote_amount = push_asset_amount.unwrap_or(0);
1218+
c.asset_local_amount == Some(local_amount)
1219+
&& c.asset_remote_amount == Some(remote_amount)
1220+
} else {
1221+
c.asset_local_amount.is_none() && c.asset_remote_amount.is_none()
1222+
};
12101223
!c.ready
12111224
&& c.peer_pubkey == dest_peer_pubkey
12121225
&& c.asset_id == asset_id.map(|id| id.to_string())
1213-
&& c.asset_local_amount == asset_amount
1226+
&& asset_amounts_match
12141227
}) {
12151228
if let Some(txid) = &channel.funding_txid {
12161229
let txout = _get_txout(txid);
@@ -1254,6 +1267,7 @@ async fn open_channel_with_custom_data(
12541267
push_msat: Option<u64>,
12551268
asset_amount: Option<u64>,
12561269
asset_id: Option<&str>,
1270+
push_asset_amount: Option<u64>,
12571271
fee_base_msat: Option<u32>,
12581272
fee_proportional_millionths: Option<u32>,
12591273
temporary_channel_id: Option<&str>,
@@ -1267,6 +1281,7 @@ async fn open_channel_with_custom_data(
12671281
push_msat,
12681282
asset_amount,
12691283
asset_id,
1284+
push_asset_amount,
12701285
fee_base_msat,
12711286
fee_proportional_millionths,
12721287
temporary_channel_id,
@@ -1870,6 +1885,7 @@ mod multi_open_close;
18701885
mod open_after_double_send;
18711886
mod openchannel_fail;
18721887
mod openchannel_optional_addr;
1888+
mod openchannel_push_asset_amount;
18731889
mod payment;
18741890
mod refuse_high_fees;
18751891
mod restart;

0 commit comments

Comments
 (0)