Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
boufni95 committed Apr 24, 2024
1 parent c573b00 commit d9433ee
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
24 changes: 17 additions & 7 deletions src/services/lnd/lnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,23 @@ export default class {
const abortController = new AbortController()
const req = OpenChannelReq(destination, closeAddress, fundingAmount, pushSats)
const stream = this.lightning.openChannel(req, { abort: abortController.signal })
stream.responses.onMessage(message => {
console.log("message", message)
})
stream.responses.onError(error => {
console.log("error", error)
return new Promise((res, rej) => {
stream.responses.onMessage(message => {
console.log("message", message)
switch (message.update.oneofKind) {
case 'chanPending':
abortController.abort()
res(Buffer.from(message.pendingChanId).toString('base64'))
break
default:
abortController.abort()
rej("unexpected state response: " + message.update.oneofKind)
}
})
stream.responses.onError(error => {
console.log("error", error)
rej(error)
})
})
}
}


28 changes: 21 additions & 7 deletions src/tests/networkSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,39 @@ export const setupNetwork = async () => {
}

const sendBalancingPayment = async (instances: LndInstances) => {
const invoice = await instances.dave.NewInvoice(20_000_000, "balancing_payment", 3600)
const payment = await instances.bob.PayInvoice(invoice.payRequest, 0, 500_000)
const invoice = await instances.dave.NewInvoice(5_000_000, "balancing_payment", 3600)
const payment = await instances.bob.PayInvoice(invoice.payRequest, 0, 100_000)
console.log({ payment })
}

const openChannels = async (core: BitcoinCoreWrapper, instances: LndInstances, info: InstancesInfo, addresses: Addresses) => {
await instances.bob.OpenChannel(info.carol.pubkey, addresses.bob, 50_000_000, 0)
await instances.carol.OpenChannel(info.alice.pubkey, addresses.carol, 50_000_000, 0)
await instances.alice.OpenChannel(info.dave.pubkey, addresses.alice, 50_000_000, 0)
await slowMine(core, 10)
await openChannel(instances.bob, info.carol.pubkey, addresses.bob)
await openChannel(instances.carol, info.alice.pubkey, addresses.carol)
await openChannel(instances.alice, info.dave.pubkey, addresses.alice)
await slowMine(core, 6)

}

const openChannel = async (instance: LND, to: string, closeAddr: string) => {
for (let i = 0; i < 10; i++) {
try {
await instance.OpenChannel(to, closeAddr, 10_000_000, 0)
console.log("success opening channel")
break
} catch (e) {
await new Promise((resolve) => setTimeout(resolve, 1000))
console.log("error opening channel", e)
}
}
throw new Error("could not open channel after 10 tries")
}

const sendCoinsToAddresses = async (core: BitcoinCoreWrapper, addresses: Addresses) => {
await core.SendToAddress(addresses.alice, 10)
await core.SendToAddress(addresses.bob, 10)
await core.SendToAddress(addresses.carol, 10)
await core.SendToAddress(addresses.dave, 10)
await slowMine(core, 10)
await core.Mine(6)
}

const slowMine = async (core: BitcoinCoreWrapper, blocks: number) => {
Expand Down

0 comments on commit d9433ee

Please sign in to comment.