syntax = "proto3";

package boltzrpc;

service Boltz {
  /* Gets general information about this Boltz instance and the nodes it is connected to */
  rpc GetInfo (GetInfoRequest) returns (GetInfoResponse);

  /* Gets the balance for either all wallets or just a single one if specified */
  rpc GetBalance (GetBalanceRequest) returns (GetBalanceResponse);


  /* Gets a hex encoded transaction from a transaction hash on the specified network */
  rpc GetTransaction (GetTransactionRequest) returns (GetTransactionResponse);

  /* Broadcasts a hex encoded transaction on the specified network */
  rpc BroadcastTransaction (BroadcastTransactionRequest) returns (BroadcastTransactionResponse);


  /* Gets a fee estimation in satoshis per vbyte for either all currencies or just a single one if specified */ 
  rpc GetFeeEstimation (GetFeeEstimationRequest) returns (GetFeeEstimationResponse);


  /* Gets a new address of a specified wallet. The "type" parameter is optional and defaults to "OutputType.LEGACY" */
  rpc NewAddress (NewAddressRequest) returns (NewAddressResponse);

  /* Sends onchain coins to a specified address */
  rpc SendCoins (SendCoinsRequest) returns (SendCoinsResponse);


  /* Creates a new Swap from the chain to Lightning */
  rpc CreateSwap (CreateSwapRequest) returns (CreateSwapResponse);

  /* Creates a new Swap from Lightning to the chain */
  rpc CreateReverseSwap (CreateReverseSwapRequest) returns (CreateReverseSwapResponse);


  /* Adds an entry to the list of addresses SubscribeTransactions listens to */
  rpc ListenOnAddress (ListenOnAddressRequest) returns (ListenOnAddressResponse);


  /* Subscribes to a stream of confirmed transactions to addresses that were specified with "ListenOnAddress" */
  rpc SubscribeTransactions (SubscribeTransactionsRequest) returns (stream SubscribeTransactionsResponse);

  /* Subscribes to a stream of invoice events */
  rpc SubscribeInvoices (SubscribeInvoicesRequest) returns (stream SubscribeInvoicesResponse);

  /* Subscribes to a stream of swap outputs that Boltz claims */
  rpc SubscribeClaims (SubscribeClaimsRequest) returns (stream SubscribeClaimsResponse);

  /* Subscribes to a stream of lockup transactions that Boltz refunds */
  rpc SubscribeRefunds (SubscribeRefundsRequest) returns (stream SubscribeRefundsResponse);

  /* Subscribes to a stream of channel backups */
  rpc SubscribeChannelBackups (SubscribeChannelBackupsRequest) returns (stream ChannelBackup);
}

enum OutputType {
  BECH32 = 0;
  COMPATIBILITY = 1;
  LEGACY = 2;
}

enum OrderSide {
  BUY = 0;
  SELL = 1;
}

enum InvoiceEvent {
  PAID = 0;
  FAILED_TO_PAY = 1;
  SETTLED = 2;
}

message GetInfoRequest {}
message GetInfoResponse {
  string version = 1;
  map<string, CurrencyInfo> chains = 2;
}

message CurrencyInfo {
  ChainInfo chain = 1;
  LndInfo lnd = 2;
}

message ChainInfo {
  uint32 version = 1;
  uint64 protocolversion = 2;
  uint64 blocks = 3;
  uint64 connections = 4;
  string error = 5;
}

message LndInfo {
  string version = 1;
  LndChannels lnd_channels = 2;
  uint64 block_height = 3;
  string error = 4;
}
message LndChannels {
  uint32 active = 1;
  uint32 inactive = 2;
  uint32 pending = 3;
}

message GetBalanceRequest {
  string currency = 1;
}
message GetBalanceResponse {
  map<string, Balance> balances = 1;
}
message Balance {
  WalletBalance wallet_balance = 1;
  LightningBalance lightning_balance = 2;
}
message LightningBalance {
  WalletBalance wallet_balance = 1; 
  ChannelBalance channel_balance = 2; 
}
message WalletBalance {
  uint64 total_balance = 1;
  uint64 confirmed_balance = 2;
  uint64 unconfirmed_balance = 3;
}
message ChannelBalance {
  uint64 local_balance = 1;
  uint64 remote_balance = 2;
}

message NewAddressRequest {
  string currency = 1;
  OutputType type = 2;
}
message NewAddressResponse {
  string address = 1;
}

message GetTransactionRequest {
  string currency = 1;
  string transaction_hash = 2;
}
message GetTransactionResponse {
  string transaction_hex = 1;
}

message GetFeeEstimationRequest {
  string currency = 1;
  uint64 blocks = 2; 
}
message GetFeeEstimationResponse {
  map<string, uint32> fees = 1;
}

message BroadcastTransactionRequest {
  string currency = 1;
  string transaction_hex = 2;
}
message BroadcastTransactionResponse {
  string transaction_hash = 1;
}

message ListenOnAddressRequest {
  string currency = 1;
  string address = 2;
}
message ListenOnAddressResponse {}

message SubscribeTransactionsRequest {}
message SubscribeTransactionsResponse {
  string output_address = 1;
  string transaction_hash = 2;
  uint64 amount_received = 3;
}

message SubscribeInvoicesRequest {}
message SubscribeInvoicesResponse {
  InvoiceEvent event = 1;
  string invoice = 2;

  oneof event_details {
    string preimage = 3;

    uint32 routing_fee = 4;
  }
}

message SubscribeClaimsRequest {}
message SubscribeClaimsResponse {
  string lockup_transaction_hash = 1;
  uint32 lockup_vout = 2;

  uint32 miner_fee = 3;
}

message SubscribeRefundsRequest {}
message SubscribeRefundsResponse {
  string lockup_transaction_hash = 1;
  uint32 lockup_vout = 2;

  uint32 miner_fee = 3;
}

message CreateSwapRequest {
  string base_currency = 1;
  string quote_currency = 2;
  OrderSide order_side = 3;
  float rate = 4;
  uint64 fee = 5;
  string invoice = 6;
  string refund_public_key = 7;
  OutputType output_type = 8;
  uint64 timeout_block_number = 9;
}
message CreateSwapResponse {
  string redeem_script = 1;
  uint64 timeout_block_height = 2;
  string address = 3;
  uint64 expected_amount = 4;
}

message CreateReverseSwapRequest {
  string base_currency = 1;
  string quote_currency = 2;
  OrderSide order_side = 3;
  float rate = 4;
  uint64 fee = 5;
  string claim_public_key = 6;
  // Amount of the invoice that will be returned
  uint64 amount = 7;
  uint64 timeout_block_number = 8;
}
message CreateReverseSwapResponse {
  string invoice = 1;
  string redeem_script = 2;
  string lockup_address = 3;
  string lockup_transaction = 4;
  string lockup_transaction_hash = 5;
  uint64 amount_sent = 6;
  uint32 miner_fee = 7;
}

message SendCoinsRequest {
  string currency = 1;
  string address = 2;
  uint64 amount = 3;
  uint32 sat_per_vbyte = 4;
  bool send_all = 5;
}
message SendCoinsResponse {
  string transaction_hash = 1;
  uint32 vout = 2;
}

message SubscribeChannelBackupsRequest {}

message ChannelBackup {
  string currency = 1;

  // A multi channel backup contains all open channels
  string multi_channel_backup = 2;
}
