From 909311241e4984a28450e4328b2643a91fdd3dc6 Mon Sep 17 00:00:00 2001 From: Alex Freska Date: Thu, 18 Apr 2024 17:06:49 -0400 Subject: [PATCH] feat: walletd-js --- .changeset/famous-llamas-peel.md | 5 + .changeset/silent-pillows-whisper.md | 6 + .vscode/spellright.dict | 4 + README.md | 7 +- apps/walletd/config/routes.ts | 4 +- libs/walletd-js/.babelrc | 12 ++ libs/walletd-js/.eslintrc.json | 21 +++ libs/walletd-js/README.md | 30 ++++ libs/walletd-js/jest.config.ts | 17 ++ libs/walletd-js/package.json | 11 ++ libs/walletd-js/project.json | 42 +++++ libs/walletd-js/rollup.config.js | 18 ++ libs/walletd-js/src/example.ts | 18 ++ libs/walletd-js/src/index.ts | 249 +++++++++++++++++++++++++++ libs/walletd-js/tsconfig.json | 25 +++ libs/walletd-js/tsconfig.lib.json | 22 +++ libs/walletd-js/tsconfig.spec.json | 19 ++ libs/walletd-react/src/api.ts | 101 ++++++----- libs/walletd-types/src/api.ts | 39 +++++ tsconfig.base.json | 1 + 20 files changed, 601 insertions(+), 50 deletions(-) create mode 100644 .changeset/famous-llamas-peel.md create mode 100644 .changeset/silent-pillows-whisper.md create mode 100644 libs/walletd-js/.babelrc create mode 100644 libs/walletd-js/.eslintrc.json create mode 100644 libs/walletd-js/README.md create mode 100644 libs/walletd-js/jest.config.ts create mode 100644 libs/walletd-js/package.json create mode 100644 libs/walletd-js/project.json create mode 100644 libs/walletd-js/rollup.config.js create mode 100644 libs/walletd-js/src/example.ts create mode 100644 libs/walletd-js/src/index.ts create mode 100644 libs/walletd-js/tsconfig.json create mode 100644 libs/walletd-js/tsconfig.lib.json create mode 100644 libs/walletd-js/tsconfig.spec.json diff --git a/.changeset/famous-llamas-peel.md b/.changeset/famous-llamas-peel.md new file mode 100644 index 000000000..8c9bfc079 --- /dev/null +++ b/.changeset/famous-llamas-peel.md @@ -0,0 +1,5 @@ +--- +'@siafoundation/walletd-js': minor +--- + +Introduced a new walletd-js library. diff --git a/.changeset/silent-pillows-whisper.md b/.changeset/silent-pillows-whisper.md new file mode 100644 index 000000000..4bf792e1d --- /dev/null +++ b/.changeset/silent-pillows-whisper.md @@ -0,0 +1,6 @@ +--- +'@siafoundation/walletd-js': patch +'@siafoundation/walletd-types': patch +--- + +Organized walletd types and constants. diff --git a/.vscode/spellright.dict b/.vscode/spellright.dict index 894aa4479..c42dcac67 100644 --- a/.vscode/spellright.dict +++ b/.vscode/spellright.dict @@ -1,2 +1,6 @@ siad sia +renterd +hostd +walletd +js diff --git a/README.md b/README.md index 5c44340c5..21efc34a0 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,14 @@ User interfaces for the Sia software. The latest Sia software takes a modular ap The Sia web libraries provide developers with convenient TypeScript SDKs for using Sia core types, blockchain utilities, data fetching, daemon-specific React hooks, and components for common functionality such as Siacoin/fiat input fields, transaction lists, files, and more. - [@siafoundation/sdk](libs/sdk) - SDK for interacting directly with the Sia network from browsers and web clients. -- [@siafoundation/renterd-types](libs/renterd-types) - Types for `renterd`. - [@siafoundation/renterd-js](libs/renterd-js) - SDK for interacting with `renterd`. - [@siafoundation/renterd-react](libs/renterd-react) - React hooks for interacting with `renterd`. -- [@siafoundation/hostd-types](libs/hostd-types) - Types for `hostd`. +- [@siafoundation/renterd-types](libs/renterd-types) - Types for `renterd`. - [@siafoundation/hostd-react](libs/hostd-react) - React hooks for interacting with `hostd`. -- [@siafoundation/walletd-types](libs/walletd-types) - Types for `walletd`. +- [@siafoundation/hostd-types](libs/hostd-types) - Types for `hostd`. +- [@siafoundation/walletd-js](libs/walletd-js) - SDK for interacting with `walletd`. - [@siafoundation/walletd-react](libs/walletd-react) - React hooks for interacting with `walletd`. +- [@siafoundation/walletd-types](libs/walletd-types) - Types for `walletd`. - [@siafoundation/design-system](libs/design-system) - React-based design system used across Sia apps and websites. - [@siafoundation/sia-central-types](libs/sia-central-types) - Types for the Sia Central API. - [@siafoundation/sia-central-js](libs/sia-central-js) - SDK for interacting with the Sia Central API. diff --git a/apps/walletd/config/routes.ts b/apps/walletd/config/routes.ts index 1ba0cf289..fa3a94c25 100644 --- a/apps/walletd/config/routes.ts +++ b/apps/walletd/config/routes.ts @@ -1,4 +1,4 @@ -import { syncerPeersKey } from '@siafoundation/walletd-react' +import { syncerPeersRoute } from '@siafoundation/walletd-types' export const routes = { home: '/', @@ -15,4 +15,4 @@ export const routes = { login: '/login', } -export const connectivityRoute = syncerPeersKey +export const connectivityRoute = syncerPeersRoute diff --git a/libs/walletd-js/.babelrc b/libs/walletd-js/.babelrc new file mode 100644 index 000000000..1ea870ead --- /dev/null +++ b/libs/walletd-js/.babelrc @@ -0,0 +1,12 @@ +{ + "presets": [ + [ + "@nx/react/babel", + { + "runtime": "automatic", + "useBuiltIns": "usage" + } + ] + ], + "plugins": [] +} diff --git a/libs/walletd-js/.eslintrc.json b/libs/walletd-js/.eslintrc.json new file mode 100644 index 000000000..04a282406 --- /dev/null +++ b/libs/walletd-js/.eslintrc.json @@ -0,0 +1,21 @@ +{ + "extends": ["plugin:@nx/react", "../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "rules": { + "@nx/dependency-checks": [ + "error", + { + "ignoredFiles": ["libs/walletd-js/rollup.config.js"] + } + ] + }, + "overrides": [ + { + "files": ["*.json"], + "parser": "jsonc-eslint-parser", + "rules": { + "@nx/dependency-checks": "error" + } + } + ] +} diff --git a/libs/walletd-js/README.md b/libs/walletd-js/README.md new file mode 100644 index 000000000..9dace1494 --- /dev/null +++ b/libs/walletd-js/README.md @@ -0,0 +1,30 @@ +# walletd-js + +SDK for interacting with `walletd`. + +## Installation + +```sh +npm install @siafoundation/walletd-js +``` + +## Usage + +```js +import { Walletd } from '@siafoundation/walletd-js' + +const walletd = Walletd({ + api: 'http://localhost:9980/api', + password: 'password1337', +}) + +const events = await walletd.walletEvents({ + params: { + id: 'wallet-1', + limit: 10, + offset: 0, + }, +}) +const peers = await walletd.syncerPeers() +console.log(events.data, peers.data) +``` diff --git a/libs/walletd-js/jest.config.ts b/libs/walletd-js/jest.config.ts new file mode 100644 index 000000000..f16a6b1f1 --- /dev/null +++ b/libs/walletd-js/jest.config.ts @@ -0,0 +1,17 @@ +/* eslint-disable */ +export default { + displayName: 'walletd-js', + preset: '../../jest.preset.js', + transform: { + '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest', + '^.+\\.[tj]sx?$': [ + 'babel-jest', + { + presets: ['@nx/next/babel'], + plugins: ['@babel/plugin-transform-private-methods'], + }, + ], + }, + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../coverage/libs/walletd-js', +} diff --git a/libs/walletd-js/package.json b/libs/walletd-js/package.json new file mode 100644 index 000000000..72eedd5bf --- /dev/null +++ b/libs/walletd-js/package.json @@ -0,0 +1,11 @@ +{ + "name": "@siafoundation/walletd-js", + "description": "SDK for interacting with `walletd`.", + "version": "0.0.0", + "license": "MIT", + "dependencies": { + "@siafoundation/request": "0.2.0", + "@siafoundation/walletd-types": "0.1.0" + }, + "types": "./src/index.d.ts" +} diff --git a/libs/walletd-js/project.json b/libs/walletd-js/project.json new file mode 100644 index 000000000..2063c1147 --- /dev/null +++ b/libs/walletd-js/project.json @@ -0,0 +1,42 @@ +{ + "name": "walletd-js", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/walletd-js/src", + "projectType": "library", + "tags": [], + "targets": { + "build": { + "executor": "@nx/rollup:rollup", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/libs/walletd-js", + "tsConfig": "libs/walletd-js/tsconfig.lib.json", + "project": "libs/walletd-js/package.json", + "entryFile": "libs/walletd-js/src/index.ts", + "external": ["react/jsx-runtime"], + "compiler": "tsc", + "outputFileName": "index.js", + "rollupConfig": "libs/walletd-js/rollup.config.js", + "assets": [ + { + "glob": "libs/walletd-js/*.md", + "input": ".", + "output": "." + } + ] + }, + "configurations": {} + }, + "lint": { + "executor": "@nx/eslint:lint", + "outputs": ["{options.outputFile}"] + }, + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/libs/walletd-js"], + "options": { + "jestConfig": "libs/walletd-js/jest.config.ts" + } + } + } +} diff --git a/libs/walletd-js/rollup.config.js b/libs/walletd-js/rollup.config.js new file mode 100644 index 000000000..3f5e4e997 --- /dev/null +++ b/libs/walletd-js/rollup.config.js @@ -0,0 +1,18 @@ +// eslint-disable-next-line @typescript-eslint/no-var-requires +const preserveDirectives = require('rollup-plugin-preserve-directives') + +// https://github.com/rollup/rollup/issues/4699#issuecomment-1465302665 +function getRollupOptions(options) { + return { + ...options, + output: { + ...options.output, + preserveModules: true, + format: 'esm', + sourcemap: true, + }, + plugins: options.plugins.concat(preserveDirectives.default()), + } +} + +module.exports = getRollupOptions diff --git a/libs/walletd-js/src/example.ts b/libs/walletd-js/src/example.ts new file mode 100644 index 000000000..776207ec3 --- /dev/null +++ b/libs/walletd-js/src/example.ts @@ -0,0 +1,18 @@ +import { Walletd } from '.' + +export async function example() { + const walletd = Walletd({ + api: 'http://localhost:9980/api', + password: 'password1337', + }) + + const events = await walletd.walletEvents({ + params: { + id: 'wallet-1', + limit: 10, + offset: 0, + }, + }) + const peers = await walletd.syncerPeers() + console.log(events.data, peers.data) +} diff --git a/libs/walletd-js/src/index.ts b/libs/walletd-js/src/index.ts new file mode 100644 index 000000000..1345211eb --- /dev/null +++ b/libs/walletd-js/src/index.ts @@ -0,0 +1,249 @@ +import { + ConsensusNetworkParams, + ConsensusNetworkPayload, + ConsensusNetworkResponse, + ConsensusTipParams, + ConsensusTipPayload, + ConsensusTipResponse, + ConsensusTipStateParams, + ConsensusTipStatePayload, + ConsensusTipStateResponse, + RescanParams, + RescanPayload, + RescanResponse, + RescanStartParams, + RescanStartPayload, + RescanStartResponse, + StateParams, + StatePayload, + StateResponse, + SyncerConnectParams, + SyncerConnectPayload, + SyncerConnectResponse, + SyncerPeersParams, + SyncerPeersPayload, + SyncerPeersResponse, + TxPoolBroadcastParams, + TxPoolBroadcastPayload, + TxPoolBroadcastResponse, + TxPoolFeeParams, + TxPoolFeePayload, + TxPoolFeeResponse, + TxPoolTransactionsParams, + TxPoolTransactionsPayload, + TxPoolTransactionsResponse, + WalletAddParams, + WalletAddPayload, + WalletAddResponse, + WalletAddressAddParams, + WalletAddressAddPayload, + WalletAddressAddResponse, + WalletAddressDeleteParams, + WalletAddressDeletePayload, + WalletAddressDeleteResponse, + WalletAddressesParams, + WalletAddressesPayload, + WalletAddressesResponse, + WalletBalanceParams, + WalletBalancePayload, + WalletBalanceResponse, + WalletDeleteParams, + WalletDeletePayload, + WalletDeleteResponse, + WalletEventsParams, + WalletEventsPayload, + WalletEventsResponse, + WalletFundSiacoinParams, + WalletFundSiacoinPayload, + WalletFundSiacoinResponse, + WalletFundSiafundParams, + WalletFundSiafundPayload, + WalletFundSiafundResponse, + WalletOutputsSiacoinParams, + WalletOutputsSiacoinPayload, + WalletOutputsSiacoinResponse, + WalletOutputsSiafundParams, + WalletOutputsSiafundPayload, + WalletOutputsSiafundResponse, + WalletReleaseParams, + WalletReleasePayload, + WalletReleaseResponse, + WalletReserveParams, + WalletReservePayload, + WalletReserveResponse, + WalletTxPoolParams, + WalletTxPoolPayload, + WalletTxPoolResponse, + WalletUpdateParams, + WalletUpdatePayload, + WalletUpdateResponse, + WalletsParams, + WalletsPayload, + WalletsResponse, + consensusNetworkRoute, + consensusTipRoute, + consensusTipStateRoute, + rescanRoute, + stateRoute, + syncerConnectRoute, + syncerPeersRoute, + txPoolBroadcastRoute, + txPoolFeeRoute, + txPoolTransactionsRoute, + walletsIdAddressesAddrRoute, + walletsIdAddressesRoute, + walletsIdBalanceRoute, + walletsIdEventsRoute, + walletsIdFundRoute, + walletsIdFundSfRoute, + walletsIdOutputsSiacoinRoute, + walletsIdOutputsSiafundRoute, + walletsIdReleaseRoute, + walletsIdReserveRoute, + walletsIdRoute, + walletsIdTxPoolRoute, + walletsRoute, +} from '@siafoundation/walletd-types' +import { buildRequestHandler, initAxios } from '@siafoundation/request' + +export function Walletd({ api, password }: { api: string; password?: string }) { + const axios = initAxios(api, password) + return { + axios, + state: buildRequestHandler( + axios, + 'get', + stateRoute + ), + consensusTip: buildRequestHandler< + ConsensusTipParams, + ConsensusTipPayload, + ConsensusTipResponse + >(axios, 'get', consensusTipRoute), + consensusTipState: buildRequestHandler< + ConsensusTipStateParams, + ConsensusTipStatePayload, + ConsensusTipStateResponse + >(axios, 'get', consensusTipStateRoute), + consensusNetwork: buildRequestHandler< + ConsensusNetworkParams, + ConsensusNetworkPayload, + ConsensusNetworkResponse + >(axios, 'get', consensusNetworkRoute), + syncerPeers: buildRequestHandler< + SyncerPeersParams, + SyncerPeersPayload, + SyncerPeersResponse + >(axios, 'get', syncerPeersRoute), + syncerConnect: buildRequestHandler< + SyncerConnectParams, + SyncerConnectPayload, + SyncerConnectResponse + >(axios, 'post', syncerConnectRoute), + txPoolTransactions: buildRequestHandler< + TxPoolTransactionsParams, + TxPoolTransactionsPayload, + TxPoolTransactionsResponse + >(axios, 'get', txPoolTransactionsRoute), + txPoolFee: buildRequestHandler< + TxPoolFeeParams, + TxPoolFeePayload, + TxPoolFeeResponse + >(axios, 'get', txPoolFeeRoute), + txPoolBroadcast: buildRequestHandler< + TxPoolBroadcastParams, + TxPoolBroadcastPayload, + TxPoolBroadcastResponse + >(axios, 'post', txPoolBroadcastRoute), + rescanStart: buildRequestHandler< + RescanStartParams, + RescanStartPayload, + RescanStartResponse + >(axios, 'post', rescanRoute), + rescanStatus: buildRequestHandler< + RescanParams, + RescanPayload, + RescanResponse + >(axios, 'get', rescanRoute), + wallets: buildRequestHandler< + WalletsParams, + WalletsPayload, + WalletsResponse + >(axios, 'get', walletsRoute), + walletAdd: buildRequestHandler< + WalletAddParams, + WalletAddPayload, + WalletAddResponse + >(axios, 'post', walletsRoute), + walletUpdate: buildRequestHandler< + WalletUpdateParams, + WalletUpdatePayload, + WalletUpdateResponse + >(axios, 'post', walletsIdRoute), + walletDelete: buildRequestHandler< + WalletDeleteParams, + WalletDeletePayload, + WalletDeleteResponse + >(axios, 'delete', walletsIdRoute), + walletAddresses: buildRequestHandler< + WalletAddressesParams, + WalletAddressesPayload, + WalletAddressesResponse + >(axios, 'get', walletsIdAddressesRoute), + walletAddressAdd: buildRequestHandler< + WalletAddressAddParams, + WalletAddressAddPayload, + WalletAddressAddResponse + >(axios, 'put', walletsIdAddressesRoute), + walletAddressDelete: buildRequestHandler< + WalletAddressDeleteParams, + WalletAddressDeletePayload, + WalletAddressDeleteResponse + >(axios, 'delete', walletsIdAddressesAddrRoute), + walletBalance: buildRequestHandler< + WalletBalanceParams, + WalletBalancePayload, + WalletBalanceResponse + >(axios, 'get', walletsIdBalanceRoute), + walletEvents: buildRequestHandler< + WalletEventsParams, + WalletEventsPayload, + WalletEventsResponse + >(axios, 'get', walletsIdEventsRoute), + walletTxPool: buildRequestHandler< + WalletTxPoolParams, + WalletTxPoolPayload, + WalletTxPoolResponse + >(axios, 'get', walletsIdTxPoolRoute), + walletOutputsSiacoin: buildRequestHandler< + WalletOutputsSiacoinParams, + WalletOutputsSiacoinPayload, + WalletOutputsSiacoinResponse + >(axios, 'get', walletsIdOutputsSiacoinRoute), + walletOutputsSiafund: buildRequestHandler< + WalletOutputsSiafundParams, + WalletOutputsSiafundPayload, + WalletOutputsSiafundResponse + >(axios, 'get', walletsIdOutputsSiafundRoute), + walletFundSiacoin: buildRequestHandler< + WalletFundSiacoinParams, + WalletFundSiacoinPayload, + WalletFundSiacoinResponse + >(axios, 'post', walletsIdFundRoute), + walletFundSiafund: buildRequestHandler< + WalletFundSiafundParams, + WalletFundSiafundPayload, + WalletFundSiafundResponse + >(axios, 'post', walletsIdFundSfRoute), + walletReserve: buildRequestHandler< + WalletReserveParams, + WalletReservePayload, + WalletReserveResponse + >(axios, 'post', walletsIdReserveRoute), + walletRelease: buildRequestHandler< + WalletReleaseParams, + WalletReleasePayload, + WalletReleaseResponse + >(axios, 'post', walletsIdReleaseRoute), + } +} diff --git a/libs/walletd-js/tsconfig.json b/libs/walletd-js/tsconfig.json new file mode 100644 index 000000000..4c089585e --- /dev/null +++ b/libs/walletd-js/tsconfig.json @@ -0,0 +1,25 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "jsx": "react-jsx", + "allowJs": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/walletd-js/tsconfig.lib.json b/libs/walletd-js/tsconfig.lib.json new file mode 100644 index 000000000..d73537814 --- /dev/null +++ b/libs/walletd-js/tsconfig.lib.json @@ -0,0 +1,22 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [ + "node", + "@nx/react/typings/cssmodule.d.ts", + "@nx/react/typings/image.d.ts" + ] + }, + "exclude": [ + "**/*.spec.ts", + "**/*.test.ts", + "**/*.spec.tsx", + "**/*.test.tsx", + "**/*.spec.js", + "**/*.test.js", + "**/*.spec.jsx", + "**/*.test.jsx" + ], + "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"] +} diff --git a/libs/walletd-js/tsconfig.spec.json b/libs/walletd-js/tsconfig.spec.json new file mode 100644 index 000000000..503e9a83d --- /dev/null +++ b/libs/walletd-js/tsconfig.spec.json @@ -0,0 +1,19 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.test.tsx", + "src/**/*.spec.tsx", + "src/**/*.test.js", + "src/**/*.spec.js", + "src/**/*.test.jsx", + "src/**/*.spec.jsx", + "src/**/*.d.ts" + ] +} diff --git a/libs/walletd-react/src/api.ts b/libs/walletd-react/src/api.ts index 682b925d2..a62fe7379 100644 --- a/libs/walletd-react/src/api.ts +++ b/libs/walletd-react/src/api.ts @@ -77,6 +77,29 @@ import { WalletUpdateResponse, WalletsParams, WalletsResponse, + consensusNetworkRoute, + consensusTipRoute, + consensusTipStateRoute, + rescanRoute, + stateRoute, + syncerConnectRoute, + syncerPeersRoute, + txPoolBroadcastRoute, + txPoolFeeRoute, + txPoolTransactionsRoute, + walletsIdAddressesAddrRoute, + walletsIdAddressesRoute, + walletsIdBalanceRoute, + walletsIdEventsRoute, + walletsIdFundRoute, + walletsIdFundSfRoute, + walletsIdOutputsSiacoinRoute, + walletsIdOutputsSiafundRoute, + walletsIdReleaseRoute, + walletsIdReserveRoute, + walletsIdRoute, + walletsIdTxPoolRoute, + walletsRoute, } from '@siafoundation/walletd-types' // state @@ -84,7 +107,7 @@ import { export function useNodeState(args?: HookArgsSwr) { return useGetSwr({ ...args, - route: '/state', + route: stateRoute, }) } @@ -95,7 +118,7 @@ export function useConsensusTip( ) { return useGetSwr({ ...args, - route: '/consensus/tip', + route: consensusTipRoute, }) } @@ -104,7 +127,7 @@ export function useConsensusTipState( ) { return useGetSwr({ ...args, - route: '/consensus/tipstate', + route: consensusTipStateRoute, }) } @@ -113,7 +136,7 @@ export function useConsensusNetwork( ) { return useGetSwr({ ...args, - route: '/consensus/network', + route: consensusNetworkRoute, }) } @@ -143,14 +166,12 @@ export function useEstimatedNetworkBlockHeight(): number { // syncer -export const syncerPeersKey = '/syncer/peers' - export function useSyncerPeers( args?: HookArgsSwr ) { return useGetSwr({ ...args, - route: syncerPeersKey, + route: syncerPeersRoute, }) } @@ -164,17 +185,16 @@ export function useSyncerConnect( return usePostFunc( { ...args, - route: '/syncer/connect', + route: syncerConnectRoute, }, async (mutate) => { - mutate((key) => key === syncerPeersKey) + mutate((key) => key === syncerPeersRoute) } ) } // txpool -const txPoolTransactionsRoute = '/txpool/transactions' export function useTxPoolTransactions( args?: HookArgsSwr ) { @@ -184,7 +204,7 @@ export function useTxPoolTransactions( export function useTxPoolFee( args?: HookArgsSwr ) { - return useGetSwr({ ...args, route: '/txpool/fee' }) + return useGetSwr({ ...args, route: txPoolFeeRoute }) } export function useTxPoolBroadcast( @@ -197,15 +217,15 @@ export function useTxPoolBroadcast( return usePostFunc( { ...args, - route: '/txpool/broadcast', + route: txPoolBroadcastRoute, }, async (mutate) => { await delay(2_000) mutate((key) => { return ( key.startsWith(txPoolTransactionsRoute) || - // Most importantly to trigger /wallets/:id/txpool - key.startsWith('/wallets') + // Most importantly to trigger /wallets/:id/txpool. + key.startsWith(walletsRoute) ) }) } @@ -224,13 +244,13 @@ export function useRescanStart( return usePostFunc( { ...args, - route: '/rescan', + route: rescanRoute, }, async (mutate) => { - // Do not block the hook method from returning and allowing consumer to toast success etc + // Do not block the hook method from returning and allowing consumer to toast success etc. const func = async () => { await delay(1_000) - await mutate((key) => key.startsWith('/rescan')) + await mutate((key) => key.startsWith(rescanRoute)) } func() } @@ -242,14 +262,12 @@ export function useRescanStatus( ) { return useGetSwr({ ...args, - route: '/rescan', + route: rescanRoute, }) } // wallet -const walletsRoute = '/wallets' - export function useWallets(args?: HookArgsSwr) { return useGetSwr({ ...args, @@ -263,7 +281,7 @@ export function useWalletAdd( return usePostFunc( { ...args, - route: '/wallets', + route: walletsRoute, }, async (mutate) => { mutate((key) => key.startsWith(walletsRoute)) @@ -281,7 +299,7 @@ export function useWalletUpdate( return usePostFunc( { ...args, - route: '/wallets/:id', + route: walletsIdRoute, }, async (mutate) => { mutate((key) => key.startsWith(walletsRoute)) @@ -297,24 +315,21 @@ export function useWalletDelete( > ) { return useDeleteFunc( - { ...args, route: '/wallets/:id' }, + { ...args, route: walletsIdRoute }, async (mutate, data) => { - mutate((key) => - key.startsWith(walletsRoute.replace(':id', data.params.id)) - ) + mutate((key) => key.startsWith(walletsRoute)) } ) } // addresses -export const walletAddressesRoute = '/wallets/:id/addresses' export function useWalletAddresses( args: HookArgsSwr ) { return useGetSwr({ ...args, - route: walletAddressesRoute, + route: walletsIdAddressesRoute, }) } @@ -328,11 +343,11 @@ export function useWalletAddressAdd( return usePutFunc( { ...args, - route: '/wallets/:id/addresses', + route: walletsIdAddressesRoute, }, async (mutate, data) => { mutate((key) => - key.startsWith('/wallets/:id'.replace(':id', data.params.id)) + key.startsWith(walletsIdRoute.replace(':id', data.params.id)) ) } ) @@ -346,43 +361,39 @@ export function useWalletAddressDelete( > ) { return useDeleteFunc( - { ...args, route: '/wallets/:id/addresses/:addr' }, + { ...args, route: walletsIdAddressesAddrRoute }, async (mutate, data) => { mutate((key) => - key.startsWith(walletAddressesRoute.replace(':id', data.params.id)) + key.startsWith(walletsIdAddressesRoute.replace(':id', data.params.id)) ) } ) } -const walletBalanceRoute = '/wallets/:id/balance' export function useWalletBalance( args: HookArgsSwr ) { return useGetSwr({ ...args, - route: walletBalanceRoute, + route: walletsIdBalanceRoute, }) } -const walletEventsRoute = '/wallets/:id/events' export function useWalletEvents( args: HookArgsSwr ) { return useGetSwr({ ...args, - route: walletEventsRoute, + route: walletsIdEventsRoute, }) } -const walletTxPoolRoute = '/wallets/:id/txpool' - export function useWalletTxPool( args: HookArgsSwr ) { return useGetSwr({ ...args, - route: walletTxPoolRoute, + route: walletsIdTxPoolRoute, }) } @@ -391,7 +402,7 @@ export function useWalletOutputsSiacoin( ) { return useGetSwr({ ...args, - route: '/wallets/:id/outputs/siacoin', + route: walletsIdOutputsSiacoinRoute, }) } @@ -400,7 +411,7 @@ export function useWalletOutputsSiafund( ) { return useGetSwr({ ...args, - route: '/wallets/:id/outputs/siafund', + route: walletsIdOutputsSiafundRoute, }) } @@ -411,7 +422,7 @@ export function useWalletFundSiacoin( WalletFundSiacoinResponse > ) { - return usePostFunc({ ...args, route: '/wallets/:id/fund' }) + return usePostFunc({ ...args, route: walletsIdFundRoute }) } export function useWalletFundSiafund( @@ -421,7 +432,7 @@ export function useWalletFundSiafund( WalletFundSiafundResponse > ) { - return usePostFunc({ ...args, route: '/wallets/:id/fundsf' }) + return usePostFunc({ ...args, route: walletsIdFundSfRoute }) } export function useWalletReserve( @@ -431,7 +442,7 @@ export function useWalletReserve( WalletReserveResponse > ) { - return usePostFunc({ ...args, route: '/wallets/:id/reserve' }) + return usePostFunc({ ...args, route: walletsIdReserveRoute }) } export function useWalletRelease( @@ -441,5 +452,5 @@ export function useWalletRelease( WalletReleaseResponse > ) { - return usePostFunc({ ...args, route: '/wallets/:id/release' }) + return usePostFunc({ ...args, route: walletsIdReleaseRoute }) } diff --git a/libs/walletd-types/src/api.ts b/libs/walletd-types/src/api.ts index 16c97d942..f065bb2ee 100644 --- a/libs/walletd-types/src/api.ts +++ b/libs/walletd-types/src/api.ts @@ -20,9 +20,34 @@ import { WalletMetadata, } from './types' +export const stateRoute = '/state' +export const consensusTipRoute = '/consensus/tip' +export const consensusTipStateRoute = '/consensus/tipstate' +export const consensusNetworkRoute = '/consensus/network' +export const syncerPeersRoute = '/syncer/peers' +export const syncerConnectRoute = '/syncer/connect' +export const txPoolTransactionsRoute = '/txpool/transactions' +export const txPoolFeeRoute = '/txpool/fee' +export const txPoolBroadcastRoute = '/txpool/broadcast' +export const rescanRoute = '/rescan' +export const walletsRoute = '/wallets' +export const walletsIdRoute = '/wallets/:id' +export const walletsIdAddressesRoute = '/wallets/:id/addresses' +export const walletsIdAddressesAddrRoute = '/wallets/:id/addresses/:addr' +export const walletsIdBalanceRoute = '/wallets/:id/balance' +export const walletsIdEventsRoute = '/wallets/:id/events' +export const walletsIdTxPoolRoute = '/wallets/:id/txpool' +export const walletsIdOutputsSiacoinRoute = '/wallets/:id/outputs/siacoin' +export const walletsIdOutputsSiafundRoute = '/wallets/:id/outputs/siafund' +export const walletsIdFundRoute = '/wallets/:id/fund' +export const walletsIdFundSfRoute = '/wallets/:id/fundsf' +export const walletsIdReserveRoute = '/wallets/:id/reserve' +export const walletsIdReleaseRoute = '/wallets/:id/release' + // state export type StateParams = void +export type StatePayload = void export type StateResponse = { version: string commit: string @@ -34,17 +59,21 @@ export type StateResponse = { // consensus export type ConsensusTipParams = void +export type ConsensusTipPayload = void export type ConsensusTipResponse = ChainIndex export type ConsensusTipStateParams = void +export type ConsensusTipStatePayload = void export type ConsensusTipStateResponse = ConsensusState export type ConsensusNetworkParams = void +export type ConsensusNetworkPayload = void export type ConsensusNetworkResponse = ConsensusNetwork // syncer export type SyncerPeersParams = void +export type SyncerPeersPayload = void export type SyncerPeersResponse = GatewayPeer[] export type SyncerConnectParams = void @@ -54,12 +83,14 @@ export type SyncerConnectResponse = never // txpool export type TxPoolTransactionsParams = void +export type TxPoolTransactionsPayload = void export type TxPoolTransactionsResponse = { transactions: Transaction[] v2transactions: V2Transaction[] } export type TxPoolFeeParams = void +export type TxPoolFeePayload = void export type TxPoolFeeResponse = Currency export type TxPoolBroadcastParams = void @@ -76,6 +107,7 @@ export type RescanStartPayload = BlockHeight export type RescanStartResponse = void export type RescanParams = void +export type RescanPayload = void export type RescanResponse = { startIndex: ChainIndex index: ChainIndex @@ -86,6 +118,7 @@ export type RescanResponse = { // wallet export type WalletsParams = void +export type WalletsPayload = void export type WalletsResponse = Wallet[] export type WalletAddParams = void @@ -111,6 +144,7 @@ export type WalletDeleteResponse = never // addresses export type WalletAddressesParams = { id: string } +export type WalletAddressesPayload = void export type WalletAddressesResponse = WalletAddress[] export type WalletAddressAddParams = { id: string } @@ -122,6 +156,7 @@ export type WalletAddressDeletePayload = void export type WalletAddressDeleteResponse = never export type WalletBalanceParams = { id: string } +export type WalletBalancePayload = void export type WalletBalanceResponse = { siacoins: Currency immatureSiacoins: Currency @@ -129,15 +164,19 @@ export type WalletBalanceResponse = { } export type WalletEventsParams = { id: string; offset: number; limit: number } +export type WalletEventsPayload = void export type WalletEventsResponse = WalletEvent[] export type WalletTxPoolParams = { id: string } +export type WalletTxPoolPayload = void export type WalletTxPoolResponse = PoolTransaction[] export type WalletOutputsSiacoinParams = { id: string } +export type WalletOutputsSiacoinPayload = void export type WalletOutputsSiacoinResponse = SiacoinElement[] export type WalletOutputsSiafundParams = { id: string } +export type WalletOutputsSiafundPayload = void export type WalletOutputsSiafundResponse = SiafundElement[] export type WalletFundSiacoinParams = { diff --git a/tsconfig.base.json b/tsconfig.base.json index 98ce748ce..50c28aaed 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -29,6 +29,7 @@ "@siafoundation/renterd-js": ["libs/renterd-js/src/index.ts"], "@siafoundation/walletd-types": ["libs/walletd-types/src/index.ts"], "@siafoundation/walletd-react": ["libs/walletd-react/src/index.ts"], + "@siafoundation/wallet-js": ["libs/wallet-js/src/index.ts"], // "buildLibsFromSource": false does not work with the next executor // this is a very annoying workaround "@siafoundation/sdk": ["dist/libs/sdk/index.esm.js"],