-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from instytutfi/feature/accounts-loader
- Loading branch information
Showing
12 changed files
with
192 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@onhive.io/astro-loader", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"author": "mietek.dev <[email protected]>", | ||
"repository": { | ||
"type": "git", | ||
|
@@ -22,21 +22,21 @@ | |
"prepare": "bunx husky" | ||
}, | ||
"dependencies": { | ||
"@hiveio/dhive": "^1.3.1-beta" | ||
"@hiveio/dhive": "^1.3.2" | ||
}, | ||
"devDependencies": { | ||
"@eslint/js": "^9.13.0", | ||
"@faker-js/faker": "^9.1.0", | ||
"@eslint/js": "^9.15.0", | ||
"@faker-js/faker": "^9.2.0", | ||
"@types/bun": "latest", | ||
"eslint": "^9.13.0", | ||
"eslint": "^9.15.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"globals": "^15.11.0", | ||
"husky": "^9.1.6", | ||
"globals": "^15.12.0", | ||
"husky": "^9.1.7", | ||
"lint-staged": "^15.2.10", | ||
"prettier": "^3.3.3", | ||
"tsup": "^8.3.0", | ||
"typescript": "^5.6.3", | ||
"typescript-eslint": "^8.11.0" | ||
"tsup": "^8.3.5", | ||
"typescript": "^5.7.2", | ||
"typescript-eslint": "^8.15.0" | ||
}, | ||
"peerDependencies": { | ||
"astro": "^4.16.7" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { DynamicGlobalProperties, ExtendedAccount } from "@hiveio/dhive"; | ||
import { type Account } from "~/schema/accounts.ts"; | ||
import { balanceToMoney, vestsToHive } from "~/schema/utils.ts"; | ||
|
||
const adaptAccount = ( | ||
account: ExtendedAccount, | ||
params: DynamicGlobalProperties | ||
): Account => { | ||
const jsonMetadata = JSON.parse(account.posting_json_metadata); | ||
|
||
return { | ||
id: account.id.toString(), | ||
name: account.name, | ||
created: new Date(account.created), | ||
wallet: { | ||
hive: { | ||
liquid: balanceToMoney(account.balance as string, 3)!, | ||
frozen: vestsToHive(account.vesting_shares as string, params) | ||
}, | ||
hbd: { | ||
liquid: balanceToMoney(account.hbd_balance as string, 3)!, | ||
frozen: balanceToMoney(account.savings_hbd_balance as string, 3)! | ||
} | ||
}, | ||
postCount: account.post_count, | ||
profile: { | ||
name: jsonMetadata.profile.name, | ||
about: jsonMetadata.profile.about, | ||
coverImage: jsonMetadata.profile.cover_image, | ||
profileImage: jsonMetadata.profile.profile_image, | ||
website: jsonMetadata.profile.website, | ||
location: jsonMetadata.profile.location | ||
} | ||
}; | ||
}; | ||
|
||
export { adaptAccount }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { hive } from "~/api/common"; | ||
import type { Account } from "~/schema/accounts.ts"; | ||
import { adaptAccount } from "~/adapters/accounts.ts"; | ||
|
||
export async function getAccounts(usernames: string[]): Promise<Account[]> { | ||
const params = await hive.database.getDynamicGlobalProperties(); | ||
const accounts = await hive.database.getAccounts(usernames); | ||
return accounts.map((acc) => adaptAccount(acc, params)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { hiveBlogLoader } from "~/loaders/blogLoader"; | ||
|
||
export type { Post } from "~/schema/posts.ts"; | ||
export type { Community } from "~/schema/community.ts"; | ||
export type { Communities } from "~/schema/communities.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Loader } from "astro/loaders"; | ||
import { typeToZ } from "~/schema/utils.ts"; | ||
import type { Account } from "~/schema/accounts.ts"; | ||
import { getAccounts } from "~/api/accounts.ts"; | ||
|
||
export function hiveAccountsLoader(username: string | string[]): Loader { | ||
return { | ||
name: "hive-accounts-loader", | ||
load: async function (this: Loader, { store, logger }) { | ||
logger.debug(`Fetching accounts [usernames: ${username}]`); | ||
const data = await getAccounts( | ||
Array.isArray(username) ? username : [username] | ||
); | ||
store.clear(); | ||
data.forEach((account) => { | ||
store.set({ | ||
id: account.id.toString(), | ||
data: { ...account } | ||
}); | ||
}); | ||
}, | ||
schema: typeToZ<Account[]> | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export type Account = { | ||
id: string; | ||
name: string; | ||
created: Date; | ||
wallet: Wallet; | ||
postCount: number; | ||
profile: Profile; | ||
}; | ||
|
||
export type Profile = { | ||
name: string; | ||
about: string; | ||
coverImage: string; | ||
profileImage: string; | ||
website: string; | ||
location: string; | ||
}; | ||
|
||
export type Wallet = { | ||
hive: { | ||
liquid: Money; | ||
frozen: Money; | ||
}; | ||
hbd: { | ||
liquid: Money; | ||
frozen: Money; | ||
}; | ||
}; | ||
|
||
export interface MoneyInterface { | ||
currency: string; | ||
amount: string; | ||
precision: number; | ||
|
||
asFloat(): number; | ||
} | ||
|
||
export class Money implements MoneyInterface { | ||
currency: string; | ||
amount: string; | ||
precision: number; | ||
|
||
constructor(currency: string, amount: string, precision: number) { | ||
this.currency = currency; | ||
this.amount = parseFloat(amount).toFixed(precision).toString(); | ||
this.precision = precision; | ||
} | ||
|
||
asFloat(): number { | ||
return parseFloat(this.amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export type Community = { | ||
export type Communities = { | ||
id: string; | ||
name: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,30 @@ | ||
import { z } from "astro/zod"; | ||
import { Money } from "~/schema/accounts.ts"; | ||
import type { DynamicGlobalProperties } from "@hiveio/dhive"; | ||
|
||
export function typeToZ<T>() { | ||
return z.custom<T>(() => true) as z.ZodType<T>; | ||
} | ||
|
||
export function balanceToMoney( | ||
balance: string, | ||
precision: number | ||
): Money | null { | ||
const [amount, currency] = balance.split(" "); | ||
if (!amount || !currency) { | ||
throw new Error(`Invalid balance: ${balance}`); | ||
} | ||
return new Money(currency, amount, precision); | ||
} | ||
|
||
export const vestsToHive = ( | ||
vests: string, | ||
params: DynamicGlobalProperties | ||
): Money => { | ||
const [v] = vests.split(" "); | ||
const [totalFund] = (params.total_vesting_fund_hive as string).split(" "); | ||
const [totalShares] = (params.total_vesting_shares as string).split(" "); | ||
const hive = | ||
(parseFloat(v!) * parseFloat(totalFund!)) / parseFloat(totalShares!); | ||
return new Money("HIVE", hive.toString(), 3); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, test, expect } from "bun:test"; | ||
import { getAccounts } from "~/api/accounts.ts"; | ||
|
||
describe("getAccounts", () => { | ||
test("returns accounts", async () => { | ||
const accounts = await getAccounts(["hive.coding", "mciszczon"]); | ||
expect(accounts).toBeArray(); | ||
expect(accounts.length).toEqual(2); | ||
const acc1 = accounts[0]!; | ||
expect(acc1.name).toEqual("hive.coding"); | ||
const acc2 = accounts[1]!; | ||
expect(acc2.name).toEqual("mciszczon"); | ||
expect(acc2.wallet.hive.liquid.asFloat()).toBeNumber(); | ||
expect(acc2.wallet.hive.frozen.asFloat()).toBeNumber(); | ||
}); | ||
}); |