]> git.codecow.com Git - libnemo.git/commitdiff
Add chaining functions for handling receivables.
authorChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 04:04:37 +0000 (21:04 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 04:04:37 +0000 (21:04 -0700)
src/lib/block.ts

index b70e1d793a9a4cf9fe0af3aa2e38f50114937956..e4e8a6ded97698aa0cb61daf93b7d0dc86989019 100644 (file)
@@ -96,6 +96,38 @@ abstract class Block {
                }
        }
 
+       /**
+       * Set a block hash as the source send of a receive block.
+       *
+       * @param {string} hash - Hash of send block to be received
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       from (hash: string): Block
+       /**
+       * Set a send block as the source of a receive block.
+       *
+       * @param {Block} block - Send block to be received
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       from (block: Block): Block
+       from (send: unknown): Block {
+               try {
+                       if (typeof send !== 'string' && !(send instanceof Block)) {
+                               throw new TypeError('Invalid send to be received')
+                       }
+                       if (this.subtype !== 'receive') {
+                               throw new TypeError('Invalid subtype')
+                       }
+                       this.link = (typeof send === 'string')
+                               ? send
+                               : send.hash
+                       this.representative = this.account.representative
+                       return this
+               } catch (err) {
+                       throw new Error('Failed to receive a corresponding send', { cause: err })
+               }
+       }
+
        /**
        * Calculates proof-of-work using a pool of Web Workers.
        *
@@ -147,6 +179,59 @@ abstract class Block {
                return res.hash
        }
 
+       /**
+       * Set the amount of nano that this block will receive from a paired send.
+       *
+       * @param {bigint} amount - Amount that was sent from sender in raw
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       receive (amount: bigint): Block
+       /**
+       * Set the amount of nano that this block will receive from a paired send.
+       *
+       * @param {number} amount - Amount that was sent from sender in nano (10³⁰ raw)
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       receive (amount: number): Block
+       /**
+       * Set the amount of nano that this block will receive from a paired send.
+       *
+       * @param {string} amount - Amount that was sent from sender in raw
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       receive (amount: string): Block
+       receive (amount: unknown): Block {
+               const currentBalance = this.balance
+               try {
+                       if (this.subtype != null) {
+                               throw new Error(`Block is already configured as ${this.subtype}`)
+                       }
+                       this.subtype = 'receive'
+                       switch (typeof amount) {
+                               case 'bigint': {
+                                       this.balance += amount
+                                       break
+                               }
+                               case 'number': {
+                                       this.balance += BigInt(amount) * (1n << 30n)
+                                       break
+                               }
+                               case 'string': {
+                                       this.balance += BigInt(amount)
+                                       break
+                               }
+                               default: {
+                                       throw new TypeError('Invalid amount')
+                               }
+                       }
+                       return this
+               } catch (err) {
+                       this.subtype = undefined
+                       this.balance = currentBalance
+                       throw new TypeError('Failed to configure receive block', { cause: err })
+               }
+       }
+
        /**
        * Set the amount of nano that this block will send to a recipient account.
        *