From 571f23d3e1f1745d81c784adf25a45b84f47c112 Mon Sep 17 00:00:00 2001 From: katty barroso Date: Mon, 28 Oct 2024 11:46:37 +0100 Subject: [PATCH] Retrieve account balance --- sdk-consumer/.gitignore | 24 ++++++++++++++++ sdk-consumer/README.md | 50 +++++++++++++++++++++++++++++++++ sdk-consumer/eslint.config.js | 28 ++++++++++++++++++ sdk-consumer/index.html | 13 +++++++++ sdk-consumer/package.json | 30 ++++++++++++++++++++ sdk-consumer/public/vite.svg | 1 + sdk-consumer/src/App.tsx | 47 +++++++++++++++++++++++++++++++ sdk-consumer/src/main.tsx | 9 ++++++ sdk-consumer/src/vite-env.d.ts | 1 + sdk-consumer/tsconfig.app.json | 25 +++++++++++++++++ sdk-consumer/tsconfig.json | 7 +++++ sdk-consumer/tsconfig.node.json | 23 +++++++++++++++ sdk-consumer/vite.config.ts | 7 +++++ 13 files changed, 265 insertions(+) create mode 100644 sdk-consumer/.gitignore create mode 100644 sdk-consumer/README.md create mode 100644 sdk-consumer/eslint.config.js create mode 100644 sdk-consumer/index.html create mode 100644 sdk-consumer/package.json create mode 100644 sdk-consumer/public/vite.svg create mode 100644 sdk-consumer/src/App.tsx create mode 100644 sdk-consumer/src/main.tsx create mode 100644 sdk-consumer/src/vite-env.d.ts create mode 100644 sdk-consumer/tsconfig.app.json create mode 100644 sdk-consumer/tsconfig.json create mode 100644 sdk-consumer/tsconfig.node.json create mode 100644 sdk-consumer/vite.config.ts diff --git a/sdk-consumer/.gitignore b/sdk-consumer/.gitignore new file mode 100644 index 0000000000..a547bf36d8 --- /dev/null +++ b/sdk-consumer/.gitignore @@ -0,0 +1,24 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/sdk-consumer/README.md b/sdk-consumer/README.md new file mode 100644 index 0000000000..74872fd4af --- /dev/null +++ b/sdk-consumer/README.md @@ -0,0 +1,50 @@ +# React + TypeScript + Vite + +This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. + +Currently, two official plugins are available: + +- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh +- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh + +## Expanding the ESLint configuration + +If you are developing a production application, we recommend updating the configuration to enable type aware lint rules: + +- Configure the top-level `parserOptions` property like this: + +```js +export default tseslint.config({ + languageOptions: { + // other options... + parserOptions: { + project: ['./tsconfig.node.json', './tsconfig.app.json'], + tsconfigRootDir: import.meta.dirname, + }, + }, +}) +``` + +- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked` +- Optionally add `...tseslint.configs.stylisticTypeChecked` +- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config: + +```js +// eslint.config.js +import react from 'eslint-plugin-react' + +export default tseslint.config({ + // Set the react version + settings: { react: { version: '18.3' } }, + plugins: { + // Add the react plugin + react, + }, + rules: { + // other rules... + // Enable its recommended rules + ...react.configs.recommended.rules, + ...react.configs['jsx-runtime'].rules, + }, +}) +``` diff --git a/sdk-consumer/eslint.config.js b/sdk-consumer/eslint.config.js new file mode 100644 index 0000000000..092408a9f0 --- /dev/null +++ b/sdk-consumer/eslint.config.js @@ -0,0 +1,28 @@ +import js from '@eslint/js' +import globals from 'globals' +import reactHooks from 'eslint-plugin-react-hooks' +import reactRefresh from 'eslint-plugin-react-refresh' +import tseslint from 'typescript-eslint' + +export default tseslint.config( + { ignores: ['dist'] }, + { + extends: [js.configs.recommended, ...tseslint.configs.recommended], + files: ['**/*.{ts,tsx}'], + languageOptions: { + ecmaVersion: 2020, + globals: globals.browser, + }, + plugins: { + 'react-hooks': reactHooks, + 'react-refresh': reactRefresh, + }, + rules: { + ...reactHooks.configs.recommended.rules, + 'react-refresh/only-export-components': [ + 'warn', + { allowConstantExport: true }, + ], + }, + }, +) diff --git a/sdk-consumer/index.html b/sdk-consumer/index.html new file mode 100644 index 0000000000..2ff7e95431 --- /dev/null +++ b/sdk-consumer/index.html @@ -0,0 +1,13 @@ + + + + + + + SDK demo + + +
+ + + diff --git a/sdk-consumer/package.json b/sdk-consumer/package.json new file mode 100644 index 0000000000..46d0287937 --- /dev/null +++ b/sdk-consumer/package.json @@ -0,0 +1,30 @@ +{ + "name": "sdk-consumer", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "tsc -b && vite build", + "lint": "eslint .", + "preview": "vite preview" + }, + "dependencies": { + "@centrifuge/centrifuge-sdk": "portal:/Users/kattybarroso/desktop/centrifuge/centrifuge-sdk", + "react": "^18.3.1", + "react-dom": "^18.3.1" + }, + "devDependencies": { + "@eslint/js": "^9.13.0", + "@types/react": "^18.3.11", + "@types/react-dom": "^18.3.1", + "@vitejs/plugin-react": "^4.3.3", + "eslint": "^9.13.0", + "eslint-plugin-react-hooks": "^5.0.0", + "eslint-plugin-react-refresh": "^0.4.13", + "globals": "^15.11.0", + "typescript": "~5.6.2", + "typescript-eslint": "^8.10.0", + "vite": "^5.4.9" + } +} diff --git a/sdk-consumer/public/vite.svg b/sdk-consumer/public/vite.svg new file mode 100644 index 0000000000..e7b8dfb1b2 --- /dev/null +++ b/sdk-consumer/public/vite.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/sdk-consumer/src/App.tsx b/sdk-consumer/src/App.tsx new file mode 100644 index 0000000000..c1be2d9839 --- /dev/null +++ b/sdk-consumer/src/App.tsx @@ -0,0 +1,47 @@ +import Centrifuge from '@centrifuge/centrifuge-sdk' +import { useEffect, useState } from 'react' + +function App() { + const [balance, setBalance] = useState(null) + + useEffect(() => { + const fetchBalance = async () => { + const centrifuge = new Centrifuge({ + environment: 'demo', + }) + + const address = '0x423420Ae467df6e90291fd0252c0A8a637C1e03f' + const chainId = 11155111 + + try { + const accountQuery = centrifuge.account(address, chainId) + + accountQuery.subscribe({ + next: (account) => { + account.balances().subscribe({ + next: (balanceValue) => { + console.log('Account Balance:', balanceValue) + setBalance(balanceValue.toString()) + }, + error: (error) => console.error('Error fetching balance:', error), + }) + }, + error: (error) => console.error('Error retrieving account:', error), + }) + } catch (error) { + console.error('General Error:', error) + } + } + + fetchBalance() + }, []) + + return ( +
+

Account Balance

+ {balance ?

{balance} TUSD

:

Loading balance...

} +
+ ) +} + +export default App diff --git a/sdk-consumer/src/main.tsx b/sdk-consumer/src/main.tsx new file mode 100644 index 0000000000..a436f98255 --- /dev/null +++ b/sdk-consumer/src/main.tsx @@ -0,0 +1,9 @@ +import { StrictMode } from 'react' +import { createRoot } from 'react-dom/client' +import App from './App.tsx' + +createRoot(document.getElementById('root')!).render( + + + +) diff --git a/sdk-consumer/src/vite-env.d.ts b/sdk-consumer/src/vite-env.d.ts new file mode 100644 index 0000000000..11f02fe2a0 --- /dev/null +++ b/sdk-consumer/src/vite-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/sdk-consumer/tsconfig.app.json b/sdk-consumer/tsconfig.app.json new file mode 100644 index 0000000000..5a2def4b7a --- /dev/null +++ b/sdk-consumer/tsconfig.app.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "target": "ES2020", + "useDefineForClassFields": true, + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "module": "ESNext", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "Bundler", + "allowImportingTsExtensions": true, + "isolatedModules": true, + "moduleDetection": "force", + "noEmit": true, + "jsx": "react-jsx", + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedSideEffectImports": true + }, + "include": ["src"] +} diff --git a/sdk-consumer/tsconfig.json b/sdk-consumer/tsconfig.json new file mode 100644 index 0000000000..1ffef600d9 --- /dev/null +++ b/sdk-consumer/tsconfig.json @@ -0,0 +1,7 @@ +{ + "files": [], + "references": [ + { "path": "./tsconfig.app.json" }, + { "path": "./tsconfig.node.json" } + ] +} diff --git a/sdk-consumer/tsconfig.node.json b/sdk-consumer/tsconfig.node.json new file mode 100644 index 0000000000..9dad70185e --- /dev/null +++ b/sdk-consumer/tsconfig.node.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "target": "ES2022", + "lib": ["ES2023"], + "module": "ESNext", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "Bundler", + "allowImportingTsExtensions": true, + "isolatedModules": true, + "moduleDetection": "force", + "noEmit": true, + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedSideEffectImports": true + }, + "include": ["vite.config.ts"] +} diff --git a/sdk-consumer/vite.config.ts b/sdk-consumer/vite.config.ts new file mode 100644 index 0000000000..8b0f57b91a --- /dev/null +++ b/sdk-consumer/vite.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite' +import react from '@vitejs/plugin-react' + +// https://vite.dev/config/ +export default defineConfig({ + plugins: [react()], +})