Skip to content

Commit

Permalink
cch: pay wrapped btc to hub
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Jun 17, 2024
1 parent 444fb6e commit 8b2c95f
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/cch/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ impl CchActor {
.amount_milli_satoshis()
.ok_or(CchError::BTCInvoiceMissingAmount)? as u128;

let fee = amount_msat * (self.config.fee_rate_per_million_sats as u128)
/ 1_000_000_000_000u128
let fee_sats = amount_msat * (self.config.fee_rate_per_million_sats as u128)
/ 1_000_000_000u128
+ (self.config.base_fee_sats as u128);

let wrapped_btc_type_script: ckb_jsonrpc_types::Script = get_script_by_contract(
Expand All @@ -309,7 +309,7 @@ impl CchActor {
ckb_pay_req: Default::default(),
payment_hash: invoice.payment_hash().encode_hex(),
payment_preimage: None,
amount_sats: amount_msat / 1_000_000u128 + fee,
amount_sats: amount_msat / 1_000u128 + fee_sats,
status: CchOrderStatus::Pending,
};
order.generate_ckb_invoice()?;
Expand Down Expand Up @@ -374,12 +374,12 @@ impl CchActor {
let duration_since_epoch = SystemTime::now().duration_since(UNIX_EPOCH)?;
let hash_bin = hex::decode(&receive_btc.payment_hash)?;

let order_value = receive_btc.amount_msat as u128;
let fee = order_value * (self.config.fee_rate_per_million_sats as u128)
/ 1_000_000_000_000u128
let amount_msat = receive_btc.amount_msat as u128;
let fee_sats = amount_msat * (self.config.fee_rate_per_million_sats as u128)
/ 1_000_000_000u128
+ (self.config.base_fee_sats as u128);
let amount_sats = (order_value / 1_000_000u128)
.checked_sub(fee)
let amount_sats = (amount_msat / 1_000u128)
.checked_sub(fee_sats)
.ok_or(CchError::ReceiveBTCOrderAmountTooSmall)?;

let mut client = state.lnd_connection.create_invoices_client().await?;
Expand Down
4 changes: 2 additions & 2 deletions tests/bruno/e2e/cross-chain-hub/01-add-btc-invoice.bru
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ body:json {
}

script:post-response {
bru.setEnvVar("BTC_PAY_REQ", res.body.payment_request)
bru.setEnvVar("PAYMENT_HASH", res.body.r_hash)
bru.setVar("BTC_PAY_REQ", res.body.payment_request)
bru.setVar("PAYMENT_HASH", res.body.r_hash)
}

docs {
Expand Down
37 changes: 37 additions & 0 deletions tests/bruno/e2e/cross-chain-hub/03-node1-connect-node3.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
meta {
name: 03-node1-connect-node3
type: http
seq: 3
}

post {
url: {{NODE1_RPC_URL}}
body: json
auth: none
}

headers {
Content-Type: application/json
Accept: application/json
}
body:json {
{
"id": "42",
"jsonrpc": "2.0",
"method": "connect_peer",
"params": [
{"address": "{{NODE3_ADDR}}"}
]
}
}

assert {
res.body.error: isUndefined
res.body.result: isNull
}

script:post-response {
// Dialing a peer is async in tentacle. Sleep for some time to make sure
// we're connected to the peer.
await new Promise(r => setTimeout(r, 1000));
}
48 changes: 48 additions & 0 deletions tests/bruno/e2e/cross-chain-hub/04-node1-open-channel-to-node3.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
meta {
name: 04-node1-open-channel-to-node3
type: http
seq: 4
}

post {
url: {{NODE1_RPC_URL}}
body: json
auth: none
}

headers {
Content-Type: application/json
Accept: application/json
}


body:json {
{
"id": "42",
"jsonrpc": "2.0",
"method": "open_channel",
"params": [
{
"peer_id": "{{NODE3_PEERID}}",
"funding_amount": "0x2710",
"funding_udt_type_script": {
"code_hash": "0xe1e354d6d643ad42724d40967e334984534e0367405c5ae42a9d7d63d77df419",
"hash_type": "data1",
"args": "0x32e555f3ff8e135cece1351a6a2971518392c1e30375c1e006ad0ce8eac07947"
}
}
]
}
}

assert {
res.body.error: isUndefined
res.body.result.temporary_channel_id: isDefined
}

script:post-response {
await new Promise(r => setTimeout(r, 1000));
console.log("N1N3 response: ", res.body);
console.log("N1N3 response: ", res.body.result.temporary_channel_id);
bru.setVar("N1N3_TEMP_CHANNEL_ID", res.body.result.temporary_channel_id);
}
41 changes: 41 additions & 0 deletions tests/bruno/e2e/cross-chain-hub/05-node3-accept-channel.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
meta {
name: 05-node3-accept-channel
type: http
seq: 5
}

post {
url: {{NODE3_RPC_URL}}
body: json
auth: none
}

headers {
Content-Type: application/json
Accept: application/json
}

body:json {
{
"id": "42",
"jsonrpc": "2.0",
"method": "list_channels",
"params": [
{
"peer_id": "{{NODE1_PEERID}}"
}
]
}
}

assert {
res.body.error: isUndefined
res.body.result.channels: isDefined
}

script:post-response {
// Sleep for sometime to make sure current operation finishes before next request starts.
await new Promise(r => setTimeout(r, 2000));
console.log("accept channel result: ", res.body);
bru.setVar("N1N3_CHANNEL_ID", res.body.result.channels[0].channel_id);
}
55 changes: 55 additions & 0 deletions tests/bruno/e2e/cross-chain-hub/06-ckb-generate-blocks.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
meta {
name: 06-ckb-generate-blocks
type: http
seq: 6
}

post {
url: {{CKB_RPC_URL}}
body: json
auth: none
}

headers {
Content-Type: application/json
Accept: application/json
}

body:json {
{
"id": {{iteration}},
"jsonrpc": "2.0",
"method": "generate_block",
"params": []
}
}

vars:post-response {
max_iterations: 10
}

assert {
res.status: eq 200
}

script:pre-request {
// Script taken from https://github.com/usebruno/bruno/discussions/385#discussioncomment-8015350
// This does not seem to work.
if(bru.getVar("iteration") === undefined){
console.log("Started generating blocks for channel 2...");
bru.setVar("iteration", 0);
}
}

script:post-response {
if(bru.getVar("iteration") < bru.getVar("max_iterations") -1){
bru.setVar("iteration", bru.getVar("iteration") + 1);
// This is the name of this bruno file, set this to continue generating blocks.
bru.setNextRequest("06-ckb-generate-blocks");
} else {
// Don't know why it takes so long for funding transaction to be confirmed.
await new Promise(r => setTimeout(r, 5000));
}
await new Promise(r => setTimeout(r, 10));
console.log("Generated the " + bru.getVar("iteration") + "th block");
}
49 changes: 49 additions & 0 deletions tests/bruno/e2e/cross-chain-hub/07-node1-add-tlc.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
meta {
name: 07-node1-add-tlc
type: http
seq: 7
}

post {
url: {{NODE1_RPC_URL}}
body: json
auth: none
}

headers {
Content-Type: application/json
Accept: application/json
}

body:json {
{
"id": "42",
"jsonrpc": "2.0",
"method": "add_tlc",
"params": [
{
"channel_id": "{{N1N3_CHANNEL_ID}}",
"amount": "0x1388",
"payment_hash": "{{PAYMENT_HASH}}",
"expiry": 40
}
]
}
}

assert {
res.body.error: isUndefined
res.body.result.tlc_id: isDefined
}

script:pre-request {
console.log("sending node1 AddTlc:", bru.getVar("payment_hash"));
console.log("amount: ", bru.getVar("payment_amount"));
}

script:post-response {
// Sleep for sometime to make sure current operation finishes before next request starts.
await new Promise(r => setTimeout(r, 100));
console.log("response from node1 AddTlc:", res.body);
bru.setVar("N1N3_TLC_ID1", res.body.result.tlc_id);
}

0 comments on commit 8b2c95f

Please sign in to comment.