@@ -38,7 +38,7 @@ import {
3838} from '../stream/Chain.js'
3939import { createStreamReceipt } from '../stream/Receipt.js'
4040import type { ChannelState , Storage } from '../stream/Storage.js'
41- import { deductFromChannel , updateChannel } from '../stream/Storage.js'
41+ import { deductFromChannel } from '../stream/Storage.js'
4242import type { SignedVoucher , StreamCredentialPayload , StreamReceipt } from '../stream/Types.js'
4343import { parseVoucherFromPayload , verifyVoucher } from '../stream/Voucher.js'
4444
@@ -236,12 +236,9 @@ export async function settle(
236236 const settledAmount = channel . highestVoucher . cumulativeAmount
237237 const txHash = await settleOnChain ( client , escrowContract , channel . highestVoucher )
238238
239- await updateChannel ( storage , channelId , ( current ) => {
240- if ( ! current ) return null
241- const nextSettled =
242- settledAmount > current . settledOnChain ? settledAmount : current . settledOnChain
243- return { ...current , settledOnChain : nextSettled }
244- } )
239+ const nextSettled =
240+ settledAmount > channel . settledOnChain ? settledAmount : channel . settledOnChain
241+ await storage . set ( channelId , { ...channel , settledOnChain : nextSettled } )
245242
246243 return txHash
247244}
@@ -369,19 +366,16 @@ async function verifyAndAcceptVoucher(parameters: {
369366 throw new InvalidSignatureError ( { reason : 'invalid voucher signature' } )
370367 }
371368
372- const updated = await updateChannel ( storage , channelId , ( current ) => {
373- if ( ! current ) throw new ChannelNotFoundError ( { reason : 'channel not found' } )
374- if ( voucher . cumulativeAmount > current . highestVoucherAmount ) {
375- return {
376- ...current ,
377- deposit : onChain . deposit ,
378- highestVoucherAmount : voucher . cumulativeAmount ,
379- highestVoucher : voucher ,
380- }
381- }
382- return current
383- } )
384- if ( ! updated ) throw new ChannelNotFoundError ( { reason : 'channel not found' } )
369+ const updated =
370+ voucher . cumulativeAmount > channel . highestVoucherAmount
371+ ? {
372+ ...channel ,
373+ deposit : onChain . deposit ,
374+ highestVoucherAmount : voucher . cumulativeAmount ,
375+ highestVoucher : voucher ,
376+ }
377+ : { ...channel , deposit : onChain . deposit }
378+ await storage . set ( channelId , updated )
385379
386380 return createStreamReceipt ( {
387381 challengeId : challenge . id ,
@@ -451,30 +445,32 @@ async function handleOpen(
451445 throw new InvalidSignatureError ( { reason : 'invalid voucher signature' } )
452446 }
453447
454- const updated = await updateChannel ( storage , payload . channelId , ( existing ) => {
455- if ( existing ) {
456- if ( voucher . cumulativeAmount < existing . settledOnChain ) {
457- throw new VerificationFailedError ( {
458- reason : 'voucher amount is below settled on-chain amount' ,
459- } )
460- }
448+ const existing = await storage . get ( payload . channelId )
461449
462- if ( voucher . cumulativeAmount > existing . highestVoucherAmount ) {
463- return {
464- ...existing ,
465- deposit : onChain . deposit ,
466- highestVoucherAmount : voucher . cumulativeAmount ,
467- highestVoucher : voucher ,
468- authorizedSigner,
469- }
470- }
471- return {
472- ...existing ,
473- deposit : onChain . deposit ,
474- authorizedSigner,
475- }
450+ let updated : ChannelState
451+ if ( existing ) {
452+ if ( voucher . cumulativeAmount < existing . settledOnChain ) {
453+ throw new VerificationFailedError ( {
454+ reason : 'voucher amount is below settled on-chain amount' ,
455+ } )
476456 }
477- return {
457+
458+ updated =
459+ voucher . cumulativeAmount > existing . highestVoucherAmount
460+ ? {
461+ ...existing ,
462+ deposit : onChain . deposit ,
463+ highestVoucherAmount : voucher . cumulativeAmount ,
464+ highestVoucher : voucher ,
465+ authorizedSigner,
466+ }
467+ : {
468+ ...existing ,
469+ deposit : onChain . deposit ,
470+ authorizedSigner,
471+ }
472+ } else {
473+ updated = {
478474 channelId : payload . channelId ,
479475 payer : onChain . payer ,
480476 payee : onChain . payee ,
@@ -489,9 +485,8 @@ async function handleOpen(
489485 finalized : false ,
490486 createdAt : new Date ( ) ,
491487 }
492- } )
493-
494- if ( ! updated ) throw new VerificationFailedError ( { reason : 'failed to create channel' } )
488+ }
489+ await storage . set ( payload . channelId , updated )
495490
496491 return createStreamReceipt ( {
497492 challengeId : challenge . id ,
@@ -534,17 +529,15 @@ async function handleTopUp(
534529 feePayer,
535530 } )
536531
537- const updated = await updateChannel ( storage , payload . channelId , ( current ) => {
538- if ( ! current ) throw new ChannelNotFoundError ( { reason : 'channel not found' } )
539- return { ...current , deposit : onChainDeposit }
540- } )
532+ const updated = { ...channel , deposit : onChainDeposit }
533+ await storage . set ( payload . channelId , updated )
541534
542535 return createStreamReceipt ( {
543536 challengeId : challenge . id ,
544537 channelId : payload . channelId ,
545- acceptedCumulative : updated ?. highestVoucherAmount ?? channel . highestVoucherAmount ,
546- spent : updated ? .spent ?? 0n ,
547- units : updated ? .units ?? 0 ,
538+ acceptedCumulative : updated . highestVoucherAmount ,
539+ spent : updated . spent ,
540+ units : updated . units ,
548541 } )
549542}
550543
@@ -651,23 +644,21 @@ async function handleClose(
651644 txHash = await closeOnChain ( client , methodDetails . escrowContract , voucher )
652645 }
653646
654- const updated = await updateChannel ( storage , payload . channelId , ( current ) => {
655- if ( ! current ) return null
656- return {
657- ...current ,
658- deposit : onChain . deposit ,
659- highestVoucherAmount : voucher . cumulativeAmount ,
660- highestVoucher : voucher ,
661- finalized : true ,
662- }
663- } )
647+ const updated = {
648+ ...channel ,
649+ deposit : onChain . deposit ,
650+ highestVoucherAmount : voucher . cumulativeAmount ,
651+ highestVoucher : voucher ,
652+ finalized : true ,
653+ }
654+ await storage . set ( payload . channelId , updated )
664655
665656 return createStreamReceipt ( {
666657 challengeId : challenge . id ,
667658 channelId : payload . channelId ,
668659 acceptedCumulative : voucher . cumulativeAmount ,
669- spent : updated ?. spent ?? channel . spent ,
670- units : updated ?. units ?? channel . units ,
660+ spent : updated . spent ,
661+ units : updated . units ,
671662 ...( txHash !== undefined && { txHash } ) ,
672663 } )
673664}
0 commit comments