storePaymentChannel method

  1. @override
Future<void> storePaymentChannel(
  1. PaymentChannel channel
)
override

Store a payment channel (insert, or replace every mutable column of an existing row with the same channelId).

Implementation

@override
Future<void> storePaymentChannel(PaymentChannel channel) async {
  _ensureInitialized();

  // Every column except the key and created_at is replaced on conflict:
  // the projection re-stores the whole channel after each event.
  await _pool!.execute(
    Sql.named('''
      INSERT INTO payment_channels (
        channel_id, wallet_id, role, client_peer_id, server_peer_id,
        funding_tx_id, funding_tx_hex, funding_output_index, funding_amount_sats,
        client_pub_key_hex, server_pub_key_hex, client_address_b58, server_address_b58,
        lock_time_unix, state, client_balance_sats, server_balance_sats,
        latest_sequence_number, latest_payment_tx_hex, refund_tx_hex,
        refund_client_sig_hex, refund_server_sig_hex, funding_ancestor_txids,
        context, created_at, closed_at, has_funding_merkle_proof,
        latest_payment_tx_id, settlement_tx_id, error_message
      ) VALUES (
        @channelId, @walletId, @role, @clientPeerId, @serverPeerId,
        @fundingTxId, @fundingTxHex, @fundingOutputIndex, @fundingAmountSats,
        @clientPubKeyHex, @serverPubKeyHex, @clientAddressB58, @serverAddressB58,
        @lockTimeUnix, @state, @clientBalanceSats, @serverBalanceSats,
        @latestSequenceNumber, @latestPaymentTxHex, @refundTxHex,
        @refundClientSigHex, @refundServerSigHex,
        CAST(@fundingAncestorTxids AS JSONB),
        @context, @createdAt, @closedAt, @hasFundingMerkleProof,
        @latestPaymentTxId, @settlementTxId, @errorMessage
      )
      ON CONFLICT (channel_id) DO UPDATE SET
        wallet_id = EXCLUDED.wallet_id,
        role = EXCLUDED.role,
        client_peer_id = EXCLUDED.client_peer_id,
        server_peer_id = EXCLUDED.server_peer_id,
        funding_tx_id = EXCLUDED.funding_tx_id,
        funding_tx_hex = EXCLUDED.funding_tx_hex,
        funding_output_index = EXCLUDED.funding_output_index,
        funding_amount_sats = EXCLUDED.funding_amount_sats,
        client_pub_key_hex = EXCLUDED.client_pub_key_hex,
        server_pub_key_hex = EXCLUDED.server_pub_key_hex,
        client_address_b58 = EXCLUDED.client_address_b58,
        server_address_b58 = EXCLUDED.server_address_b58,
        lock_time_unix = EXCLUDED.lock_time_unix,
        state = EXCLUDED.state,
        client_balance_sats = EXCLUDED.client_balance_sats,
        server_balance_sats = EXCLUDED.server_balance_sats,
        latest_sequence_number = EXCLUDED.latest_sequence_number,
        latest_payment_tx_hex = EXCLUDED.latest_payment_tx_hex,
        refund_tx_hex = EXCLUDED.refund_tx_hex,
        refund_client_sig_hex = EXCLUDED.refund_client_sig_hex,
        refund_server_sig_hex = EXCLUDED.refund_server_sig_hex,
        funding_ancestor_txids = EXCLUDED.funding_ancestor_txids,
        context = EXCLUDED.context,
        closed_at = EXCLUDED.closed_at,
        has_funding_merkle_proof = EXCLUDED.has_funding_merkle_proof,
        latest_payment_tx_id = EXCLUDED.latest_payment_tx_id,
        settlement_tx_id = EXCLUDED.settlement_tx_id,
        error_message = EXCLUDED.error_message
    '''),
    parameters: {
      'channelId': channel.channelId,
      'walletId': channel.walletId,
      'role': channel.role.name,
      'clientPeerId': channel.clientPeerId,
      'serverPeerId': channel.serverPeerId,
      'fundingTxId': channel.fundingTxId,
      'fundingTxHex': channel.fundingTxHex,
      'fundingOutputIndex': channel.fundingOutputIndex,
      'fundingAmountSats': channel.fundingAmountSats.toInt(),
      'clientPubKeyHex': channel.clientPubKeyHex,
      'serverPubKeyHex': channel.serverPubKeyHex,
      'clientAddressB58': channel.clientAddressB58,
      'serverAddressB58': channel.serverAddressB58,
      'lockTimeUnix': channel.lockTimeUnix,
      'state': channel.state.name,
      'clientBalanceSats': channel.clientBalanceSats.toInt(),
      'serverBalanceSats': channel.serverBalanceSats.toInt(),
      'latestSequenceNumber': channel.latestSequenceNumber,
      'latestPaymentTxHex': channel.latestPaymentTxHex,
      'refundTxHex': channel.refundTxHex,
      'refundClientSigHex': channel.refundClientSigHex,
      'refundServerSigHex': channel.refundServerSigHex,
      'fundingAncestorTxids': jsonEncode(channel.fundingAncestorTxids),
      'context': channel.context,
      'createdAt': channel.createdAt,
      'closedAt': channel.closedAt,
      'hasFundingMerkleProof': channel.hasFundingMerkleProof,
      'latestPaymentTxId': channel.latestPaymentTxId,
      'settlementTxId': channel.settlementTxId,
      'errorMessage': channel.errorMessage,
    },
  );
}