]> zoso.dev Git - libnemo.git/commitdiff
Return bigint directly from account properties to let consumers do math on the raw...
authorChris Duncan <chris@zoso.dev>
Tue, 8 Oct 2024 09:02:56 +0000 (02:02 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 8 Oct 2024 09:02:56 +0000 (02:02 -0700)
src/lib/account.ts
src/lib/tools.ts
test/refresh-accounts.test.mjs

index 80c358c14b9a28b1975068a9ebdd8c432ed6d9d3..356d1abc73dd7185b024b0bc527284f359f3caa8 100644 (file)
@@ -31,10 +31,10 @@ export class Account {
        get privateKey () { return this.#prv }\r
        get index () { return this.#i }\r
        get frontier () { return this.#f }\r
-       get balance () { return this.#b?.toString() }\r
-       get receivable () { return this.#r?.toString() }\r
+       get balance () { return this.#b }\r
+       get receivable () { return this.#r }\r
        get representative () { return this.#rep }\r
-       get weight () { return this.#w?.toString() }\r
+       get weight () { return this.#w }\r
 \r
        set frontier (v) { this.#f = v }\r
        set balance (v) { this.#b = v ? BigInt(v) : undefined }\r
index 13b79b7611ccd51024e999a4386bfb943e168dd2..fb5c13d3c7e82d491daa16b7fe7acb0fa1ce5a4c 100644 (file)
@@ -14,12 +14,15 @@ import { SendBlock } from './block.js'
 /**
 * Converts a decimal amount from one unit divider to another.
 *
-* @param {string} amount - Decimal amount to convert
+* @param {bigint|string} amount - Decimal amount to convert
 * @param {string} inputUnit - Current denomination
 * @param {string} outputUnit - Desired denomination
 */
-export async function convert (amount: string, inputUnit: string, outputUnit: string): Promise<string> {
-       if (!amount || !/^[0-9]*\.?[0-9]*$/.test(amount)) {
+export async function convert (amount: bigint | string, inputUnit: string, outputUnit: string): Promise<string> {
+       if (typeof amount === 'bigint') {
+               amount = amount.toString()
+       }
+       if (typeof amount !== 'string' || amount === '' || !/^[0-9]*\.?[0-9]*$/.test(amount)) {
                throw new Error('Invalid amount')
        }
        let [i = '', f = ''] = amount.toString().split('.')
@@ -131,9 +134,9 @@ export async function sweep (node: Node | string | URL, wallet: Blake2bWallet |
                if (account.representative?.address && account.frontier) {
                        const block = new SendBlock(
                                account,
-                               account.balance ?? '0',
+                               account.balance?.toString() ?? '0',
                                recipientAccount.address,
-                               account.balance ?? '0',
+                               account.balance?.toString() ?? '0',
                                account.representative.address,
                                account.frontier
                        )
index 7767b4c49ed2f115a97e07a431befc9942114bd0..c1427d3c6a0077d09ec6e732a18135f42dbb5c78 100644 (file)
@@ -18,110 +18,110 @@ const node = new Node(process.env.NODE_URL, process.env.API_KEY_NAME, process.en
 const skip = true
 
 describe('refreshing account info', { skip }, async () => {
-  it('should fetch balance, frontier, and representative', async () => {
-    const accounts = await wallet.accounts()
-    const account = accounts[0]
-    await account.refresh(node)
-
-    assert.equal(typeof account.balance, 'string')
-    assert.notEqual(account.balance, undefined)
-    assert.notEqual(account.balance, null)
-    assert.notEqual(account.balance, '')
-    assert.equal(isNaN(parseInt(account.balance)), false)
-    assert.equal(parseInt(account.balance) < 0, false)
-
-    assert.equal(typeof account.frontier, 'string')
-    assert.notEqual(account.frontier, undefined)
-    assert.notEqual(account.frontier, null)
-    assert.notEqual(account.frontier, '')
-    assert.match(account.frontier, /^[0-9A-F]{64}$/i)
-
-    assert.equal(account.representative.constructor, Account)
-    assert.notEqual(account.representative, undefined)
-    assert.notEqual(account.representative, null)
-    assert.notEqual(account.representative, '')
-    assert.notEqual(account.representative.address, undefined)
-    assert.notEqual(account.representative.address, null)
-    assert.notEqual(account.representative.address, '')
-  })
-
-  it('should throw when refreshing unopened account', async () => {
-    const accounts = await wallet.accounts(0x7fffffff)
-    const account = accounts[0]
-    await assert.rejects(account.refresh(node),
-      { message: 'Account not found' })
-  })
-
-  it('should throw when referencing invalid account index', async () => {
-    await assert.rejects(wallet.accounts(0x80000000),
-      { message: 'Invalid child key index 0x80000000' })
-  })
-
-  it('should throw with invalid node', async () => {
-    const invalidNode = new Node('http://invalid.com')
-    const accounts = await wallet.accounts()
-    const account = accounts[0]
-    await assert.rejects(account.refresh(invalidNode),
-      { message: 'Account not found' })
-  })
+       it('should fetch balance, frontier, and representative', async () => {
+               const accounts = await wallet.accounts()
+               const account = accounts[0]
+               await account.refresh(node)
+
+               assert.equal(typeof account.balance, 'string')
+               assert.notEqual(account.balance, undefined)
+               assert.notEqual(account.balance, null)
+               assert.notEqual(account.balance, '')
+               assert.equal(isNaN(parseInt(account.balance)), false)
+               assert.equal(parseInt(account.balance) < 0, false)
+
+               assert.equal(typeof account.frontier, 'string')
+               assert.notEqual(account.frontier, undefined)
+               assert.notEqual(account.frontier, null)
+               assert.notEqual(account.frontier, '')
+               assert.match(account.frontier, /^[0-9A-F]{64}$/i)
+
+               assert.equal(account.representative.constructor, Account)
+               assert.notEqual(account.representative, undefined)
+               assert.notEqual(account.representative, null)
+               assert.notEqual(account.representative, '')
+               assert.notEqual(account.representative.address, undefined)
+               assert.notEqual(account.representative.address, null)
+               assert.notEqual(account.representative.address, '')
+       })
+
+       it('should throw when refreshing unopened account', async () => {
+               const accounts = await wallet.accounts(0x7fffffff)
+               const account = accounts[0]
+               await assert.rejects(account.refresh(node),
+                       { message: 'Account not found' })
+       })
+
+       it('should throw when referencing invalid account index', async () => {
+               await assert.rejects(wallet.accounts(0x80000000),
+                       { message: 'Invalid child key index 0x80000000' })
+       })
+
+       it('should throw with invalid node', async () => {
+               const invalidNode = new Node('http://invalid.com')
+               const accounts = await wallet.accounts()
+               const account = accounts[0]
+               await assert.rejects(account.refresh(invalidNode),
+                       { message: 'Account not found' })
+       })
 })
 
 describe('finding next unopened account', { skip }, async () => {
-  it('should return correct account from test vector', async () => {
-    const account = await wallet.getNextNewAccount(node)
-    assert.ok(account)
-    assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_1)
-    assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1)
-  })
-
-  it('should return successfully for small batch size', async () => {
-    const account = await wallet.getNextNewAccount(node, 1)
-    assert.ok(account)
-    assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_1)
-    assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1)
-  })
-
-  it('should return successfully for large batch size', async () => {
-    const account = await wallet.getNextNewAccount(node, 100)
-    assert.ok(account)
-    assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_1)
-    assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1)
-  })
-
-  it('should throw on invalid node URL', async () => {
-    await assert.rejects(wallet.getNextNewAccount())
-    await assert.rejects(wallet.getNextNewAccount(null))
-    await assert.rejects(wallet.getNextNewAccount(1))
-    await assert.rejects(wallet.getNextNewAccount(''))
-    await assert.rejects(wallet.getNextNewAccount('foo'))
-  })
-
-  it('should throw on invalid batch size', async () => {
-    await assert.rejects(wallet.getNextNewAccount(node, null))
-    await assert.rejects(wallet.getNextNewAccount(node, -1))
-    await assert.rejects(wallet.getNextNewAccount(node, ''))
-    await assert.rejects(wallet.getNextNewAccount(node, 'foo'))
-    await assert.rejects(wallet.getNextNewAccount(node, { 'foo': 'bar' }))
-  })
+       it('should return correct account from test vector', async () => {
+               const account = await wallet.getNextNewAccount(node)
+               assert.ok(account)
+               assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_1)
+               assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1)
+       })
+
+       it('should return successfully for small batch size', async () => {
+               const account = await wallet.getNextNewAccount(node, 1)
+               assert.ok(account)
+               assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_1)
+               assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1)
+       })
+
+       it('should return successfully for large batch size', async () => {
+               const account = await wallet.getNextNewAccount(node, 100)
+               assert.ok(account)
+               assert.equal(account.address, NANO_TEST_VECTORS.ADDRESS_1)
+               assert.equal(account.publicKey, NANO_TEST_VECTORS.PUBLIC_1)
+       })
+
+       it('should throw on invalid node URL', async () => {
+               await assert.rejects(wallet.getNextNewAccount())
+               await assert.rejects(wallet.getNextNewAccount(null))
+               await assert.rejects(wallet.getNextNewAccount(1))
+               await assert.rejects(wallet.getNextNewAccount(''))
+               await assert.rejects(wallet.getNextNewAccount('foo'))
+       })
+
+       it('should throw on invalid batch size', async () => {
+               await assert.rejects(wallet.getNextNewAccount(node, null))
+               await assert.rejects(wallet.getNextNewAccount(node, -1))
+               await assert.rejects(wallet.getNextNewAccount(node, ''))
+               await assert.rejects(wallet.getNextNewAccount(node, 'foo'))
+               await assert.rejects(wallet.getNextNewAccount(node, { 'foo': 'bar' }))
+       })
 })
 
 describe('refreshing wallet accounts', { skip }, async () => {
-  it('should get balance, frontier, and representative for one account', async () => {
-    const accounts = await wallet.refresh(node)
-    assert.ok(accounts[0] instanceof Account)
-    assert.equal(typeof accounts[0].balance, 'string')
-    assert.notEqual(accounts[0].frontier, undefined)
-    assert.notEqual(accounts[0].frontier, null)
-    assert.equal(typeof accounts[0].frontier, 'string')
-  })
-
-  it('should get balance, frontier, and representative for multiple accounts', async () => {
-    const accounts = await wallet.refresh(node, 0, 2)
-    assert.equal(accounts.length, 1)
-    assert.ok(accounts[0] instanceof Account)
-  })
-
-  it('should handle failure gracefully', async () => {
-    await assert.doesNotReject(wallet.refresh(node, 0, 20))
-  })
+       it('should get balance, frontier, and representative for one account', async () => {
+               const accounts = await wallet.refresh(node)
+               assert.ok(accounts[0] instanceof Account)
+               assert.equal(typeof accounts[0].balance, 'bigint')
+               assert.notEqual(accounts[0].frontier, undefined)
+               assert.notEqual(accounts[0].frontier, null)
+               assert.equal(typeof accounts[0].frontier, 'string')
+       })
+
+       it('should get balance, frontier, and representative for multiple accounts', async () => {
+               const accounts = await wallet.refresh(node, 0, 2)
+               assert.equal(accounts.length, 1)
+               assert.ok(accounts[0] instanceof Account)
+       })
+
+       it('should handle failure gracefully', async () => {
+               await assert.doesNotReject(wallet.refresh(node, 0, 20))
+       })
 })