From 858793b385c735893a29a8f4a36b5a096be39275 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 5 Aug 2025 21:04:37 -0700 Subject: [PATCH] Add chaining functions for handling receivables. --- src/lib/block.ts | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/lib/block.ts b/src/lib/block.ts index b70e1d7..e4e8a6d 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -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. * -- 2.47.3