Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
22388o committed Jan 14, 2025
1 parent 0fc7e3a commit 679a597
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 57 deletions.
83 changes: 59 additions & 24 deletions lib/lightning/channelconfig.dart
Original file line number Diff line number Diff line change
@@ -1,36 +1,71 @@
'package:ldk_node_package/ChannelConfig';

ChannelConfig({
required this.forwardingFeeProportionalMillionths,
required this.forwardingFeeBaseMsat,
required this.cltvExpiryDelta,
required this.maxDustHtlcExposure,
required this.forceCloseAvoidanceMaxFeeSatoshis,
required this.acceptUnderpayingHtlcs,
});
class ChannelConfig {
final int forwardingFeeProportionalMillionths;
final int forwardingFeeBaseMsat;
final int cltvExpiryDelta;
final int maxDustHtlcExposure;
final int forceCloseAvoidanceMaxFeeSatoshis;
final bool acceptUnderpayingHtlcs;

class ChannelConfigBuilder {
late int forwardingFeeProportionalMillionths;
late int forwardingFeeBaseMsat;
late int cltvExpiryDelta;
late int maxDustHtlcExposure;
late int forceCloseAvoidanceMaxFeeSatoshis;
late bool acceptUnderpayingHtlcs;
}

ChannelConfigBuilder({
ChannelConfig({
required this.forwardingFeeProportionalMillionths,
required this.forwardingFeeBaseMsat,
required this.cltvExpiryDelta,
required this.maxDustHtlcExposure,
required this.forceCloseAvoidanceMaxFeeSatoshis,
required this.acceptUnderpayingHtlcs,
});
}

class ChannelConfigBuilder {
late int forwardingFeeProportionalMillionths;
late int forwardingFeeBaseMsat;
late int cltvExpiryDelta;
late int maxDustHtlcExposure;
late int forceCloseAvoidanceMaxFeeSatoshis;
late bool acceptUnderpayingHtlcs;
int forwardingFeeProportionalMillionths = 0;
int forwardingFeeBaseMsat = 0;
int cltvExpiryDelta = 0;
int maxDustHtlcExposure = 0;
int forceCloseAvoidanceMaxFeeSatoshis = 0;
bool acceptUnderpayingHtlcs = false;

ChannelConfigBuilder();

ChannelConfigBuilder setForwardingFeeProportionalMillionths(int value) {
forwardingFeeProportionalMillionths = value;
return this;
}

ChannelConfigBuilder setForwardingFeeBaseMsat(int value) {
forwardingFeeBaseMsat = value;
return this;
}

ChannelConfigBuilder setCltvExpiryDelta(int value) {
cltvExpiryDelta = value;
return this;
}

ChannelConfigBuilder setMaxDustHtlcExposure(int value) {
maxDustHtlcExposure = value;
return this;
}

ChannelConfigBuilder setForceCloseAvoidanceMaxFeeSatoshis(int value) {
forceCloseAvoidanceMaxFeeSatoshis = value;
return this;
}

ChannelConfigBuilder setAcceptUnderpayingHtlcs(bool value) {
acceptUnderpayingHtlcs = value;
return this;
}

ChannelConfig build() {
return ChannelConfig(
forwardingFeeProportionalMillionths: forwardingFeeProportionalMillionths,
forwardingFeeBaseMsat: forwardingFeeBaseMsat,
cltvExpiryDelta: cltvExpiryDelta,
maxDustHtlcExposure: maxDustHtlcExposure,
forceCloseAvoidanceMaxFeeSatoshis: forceCloseAvoidanceMaxFeeSatoshis,
acceptUnderpayingHtlcs: acceptUnderpayingHtlcs,
);
}
}
97 changes: 64 additions & 33 deletions lib/lightning/lightning.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,91 @@ import 'package:ldk_node/ldk_node.dart';
import '../dlc/contract.dart';
import '../dlc/oracle.dart';
import 'taproot_channel.dart';

class DLC {
final OracleClient oracle;
final OracleClient oracleClient;
final Channel channel;
final Contract contract;

DLC(this.oracle, this.channel);

/// Funds the DLC channel and waits for confirmation
Future<void> fund(int fundingAmount) async {
final fundingTx = await channel.fund(fundingAmount);
// Wait for the funding transaction to be confirmed
await channel.waitForConfirmation(fundingTx);
// Wait for the channel to be fully funded
await channel.waitForFunded();
// Initiate the DLC contract
await oracle.initiateContract(/* Contract parameters */);
// Monitor the Oracle for updates
await monitorOracle();
try {
final fundingTx = await channel.fund(fundingAmount);
print('Funding transaction broadcasted: $fundingTx');

// Wait for the funding transaction to be confirmed
await channel.waitForConfirmation(fundingTx);
print('Funding transaction confirmed.');

// Wait for the channel to be fully funded
await channel.waitForFunded();
print('Channel fully funded.');

// Initiate the DLC contract
await oracle.initiateContract(/* Pass appropriate contract parameters */);
print('DLC contract initiated.');

// Monitor the Oracle for updates
await monitorOracle();
} catch (e) {
print('Error while funding DLC: $e');
rethrow; // Optionally rethrow if the caller needs to handle this
}
}

/// Monitors the Oracle for updates and adjusts the contract state
Future<void> monitorOracle() async {
// Continuously monitor the Oracle for updates
// Verify Oracle data signatures
// Update the contract state based on Oracle data
await oracle.updateContractState(/* Oracle data */);
try {
// Continuously monitor the Oracle for updates
// Verify Oracle data signatures and update the contract state
await oracle.updateContractState(/* Oracle data */);
print('Oracle state updated.');
} catch (e) {
print('Error while monitoring Oracle: $e');
}
}

/// Executes the contract logic
Future<void> executeContract() async {
// Implement contract execution logic
// Determine contract outcomes
// Create and broadcast settlement transactions
// Distribute funds based on contract terms
try {
// Implement contract execution logic here
// Example:
// - Determine contract outcomes
// - Create and broadcast settlement transactions
// - Distribute funds based on contract terms
print('Executing contract...');
// Add execution logic
} catch (e) {
print('Error while executing contract: $e');
}
}
}

void main() async {
// Initialize LDK and set up the Lightning Network channel
final config = ChannelConfig.defaultConfig();
final chan = createChannel(config, Network.bitcoin);
try {
// Initialize LDK and set up the Lightning Network channel
final config = ChannelConfig.defaultConfig(); // Assuming a default configuration is provided
final chan = createChannel(config, Network.bitcoin);

// Initialize the Oracle client
final oracle = OracleClient(/* Oracle configuration */);
// Initialize the Oracle client
final oracle = OracleClient(/* Oracle configuration */);

// Create a DLC instance
final dlc = DLC(oracle, chan);
// Create a DLC instance
final dlc = DLC(oracle, chan);

// Fund the DLC
await dlc.fund(100000);
// Fund the DLC
print('Funding the DLC...');
await dlc.fund(100000);

// Start monitoring the Oracle
await dlc.monitorOracle();
// Start monitoring the Oracle
print('Monitoring Oracle...');
await dlc.monitorOracle();

// Execute the contract when conditions are met
await dlc.executeContract();
// Execute the contract when conditions are met
print('Executing DLC contract...');
await dlc.executeContract();
} catch (e) {
print('An error occurred in main: $e');
}
}

0 comments on commit 679a597

Please sign in to comment.