From 2e949a1c9fca419bdf671ceebddf166249cec38e Mon Sep 17 00:00:00 2001 From: ndkazu Date: Thu, 18 Apr 2024 16:24:05 +0900 Subject: [PATCH] Investor Menu --- .../src/components/pages/Dashboard.tsx | 4 +- fs-dapp_2.0/src/components/pages/Investor.tsx | 67 +++++++++++++++++-- fs-dapp_2.0/src/contexts/AppContext.tsx | 2 +- fs-dapp_2.0/src/contexts/types.ts | 6 +- pallets/housing_fund/src/lib.rs | 13 ++++ pallets/roles/src/types.rs | 3 +- 6 files changed, 82 insertions(+), 13 deletions(-) diff --git a/fs-dapp_2.0/src/components/pages/Dashboard.tsx b/fs-dapp_2.0/src/components/pages/Dashboard.tsx index e0ce7fba..7a0a64d7 100644 --- a/fs-dapp_2.0/src/components/pages/Dashboard.tsx +++ b/fs-dapp_2.0/src/components/pages/Dashboard.tsx @@ -3,7 +3,7 @@ import { useAppContext } from '../../contexts/AppContext'; import { useAccountContext } from '../../contexts/Account_Context'; import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js'; import { Pie } from 'react-chartjs-2'; -import BN from 'bn.js'; +import {BN,formatBalance} from '@polkadot/util'; import { toUnit } from '../shared/utils'; import { NavLink } from 'react-router-dom'; import { all } from 'axios'; @@ -133,7 +133,7 @@ export default function Dashboard() {

DASHBOARD

- House Fund: {!treasury_balance ? '0' : toUnit(treasury_balance, 3).toString()} FS + House Fund: {!treasury_balance ? '0' :formatBalance(treasury_balance,{ decimals:12, withUnit:'FS',withSi: true, withZero: false })}

Total Number of Users: {total_users_nbr}

diff --git a/fs-dapp_2.0/src/components/pages/Investor.tsx b/fs-dapp_2.0/src/components/pages/Investor.tsx index 169216e3..e8bfa23f 100644 --- a/fs-dapp_2.0/src/components/pages/Investor.tsx +++ b/fs-dapp_2.0/src/components/pages/Investor.tsx @@ -3,34 +3,87 @@ import React, { useEffect,useState } from 'react'; import { useAppContext } from '../../contexts/AppContext'; import { arrangeText } from './Council'; import { InvestorData } from '@/src/contexts/types'; +import {BN,formatBalance} from '@polkadot/util'; +import { Card, Button } from 'antd'; +const data0:InvestorData={name:"",address:"",balance:"",fund_share:"",available_funds:"", reserved_funds:"", invested_funds:""} +const inv_image = "../../../INVESTOR.png" export default function Investors(){ const { api, blocks, selectedAccount, web3Name, credentials, dispatch } = useAppContext(); const { role, balance,investor, dispatch0 } = useAccountContext(); //Get user infos -const[idata,setIdatas] = useState(); +const[idata,setIdatas] = useState(); useEffect(()=>{ if (!api||!selectedAccount) return; let address0=selectedAccount.address; - api.query.rolesModule.investorLog(address0,(data:any)=>{ - let data0= data.toHuman(); + api.query.system.account(address0, ({ data: free }: { data: { free: BN } }) => { + formatBalance.setDefaults({ decimals: 12, unit: 'FS' }); + const free0 = formatBalance(free.free,{ withSi: true, withZero: false }); + dispatch0({ type: 'SET_BALANCE', payload: free0 }); + + + }); + api.query.rolesModule.investorLog(address0,(datalog:any)=>{ + let data0= datalog.toHuman(); if(data0){ let txt = data0.infos - let infos = arrangeText(txt); + let infos = arrangeText(txt); + let datas0:InvestorData={...data0,name:infos[0],address:selectedAccount.address,balance:balance,fund_share:data0.share}; - // let datas:InvestorData={name:infos[0],address:selectedAccount.address,balance:balance,fund_share:data0.share} + api.query.housingFundModule.contributions(address0,(data:any)=>{ + let data0 = data.toHuman(); + if (data0){ + formatBalance.setDefaults({ decimals: 12, unit: 'FS' }); + + let available_funds=formatBalance(new BN(data0.availableBalance.toString().split(',').join('')),{ withSi: true, withZero: false }); + let reserved_funds=formatBalance(new BN(data0.reservedBalance.toString().split(',').join('')),{ withSi: true, withZero: false }); + let invested_funds=formatBalance(new BN(data0.contributedBalance.toString().split(',').join('')),{ withSi: true, withZero: false }); + + let datas:InvestorData={...datas0,available_funds,reserved_funds,invested_funds}; + setIdatas(datas) + dispatch0({ type: 'SET_INVESTOR', payload: datas }); + } + }) } }) -}) + +},[selectedAccount,blocks,api,dispatch0,balance]) + + +const style1= { width: 450, height:400, background:`white`}; return(
- Investor Page +
+

+ { + investor? + } + style={style1} + > +

Investor Name: {investor.name.split(':')[1]}

+

Wallet Balance: {investor.balance}

+

Available Balance in Fund: {investor.available_funds}

+

Share in Housing Fund: {investor.fund_share}

+

Reserved for Purchase: {investor.reserved_funds}

+

Amount Invested: {investor.invested_funds}

+ :"NO DATA"} +

+ +

+ +

+ +
+ +
) } \ No newline at end of file diff --git a/fs-dapp_2.0/src/contexts/AppContext.tsx b/fs-dapp_2.0/src/contexts/AppContext.tsx index c2bbda89..40b8069a 100644 --- a/fs-dapp_2.0/src/contexts/AppContext.tsx +++ b/fs-dapp_2.0/src/contexts/AppContext.tsx @@ -2,7 +2,7 @@ import { createContext, useContext, useReducer, ReactNode } from 'react'; import { ApiPromise } from '@polkadot/api'; import { InjectedAccountWithMeta } from '@polkadot/extension-inject/types'; import { AppState } from './types'; -import BN from 'bn.js'; +import {BN} from '@polkadot/util'; const initialState: AppState = { api: null, diff --git a/fs-dapp_2.0/src/contexts/types.ts b/fs-dapp_2.0/src/contexts/types.ts index 0d4ebb57..0a7e0ce0 100644 --- a/fs-dapp_2.0/src/contexts/types.ts +++ b/fs-dapp_2.0/src/contexts/types.ts @@ -63,11 +63,13 @@ export interface DataType { } export interface InvestorData{ - name:string|undefined; + name:string; address:string; - balance: string|undefined; + balance: string; fund_share: string; available_funds:string; + reserved_funds:string; + invested_funds:string; } diff --git a/pallets/housing_fund/src/lib.rs b/pallets/housing_fund/src/lib.rs index 9fb680c2..62b7b450 100644 --- a/pallets/housing_fund/src/lib.rs +++ b/pallets/housing_fund/src/lib.rs @@ -213,6 +213,19 @@ pub mod pallet { ExistenceRequirement::AllowDeath, )?; + //Contributors shares are updated in their respective profiles + let shares = Self::get_contribution_share(); + //let keys = ROLES::InvestorLog::::ke + for i in shares { + if ROLES::InvestorLog::::contains_key(i.account_id.clone()) { + let mut inv=ROLES::InvestorLog::::get(&i.account_id).unwrap(); + inv.share=i.share; + ROLES::InvestorLog::::mutate(i.account_id.clone(),|val|{ + *val=Some(inv); + }); + } + } + // Emit an event. Self::deposit_event(Event::ContributeSucceeded(who, amount, block_number)); diff --git a/pallets/roles/src/types.rs b/pallets/roles/src/types.rs index 17df1535..7552be8f 100644 --- a/pallets/roles/src/types.rs +++ b/pallets/roles/src/types.rs @@ -15,6 +15,7 @@ pub use frame_support::{ dispatch::GetDispatchInfo, PalletId, }; +use sp_runtime::Percent; pub use sp_std::vec::Vec; pub use frame_system::{ensure_signed, ensure_root, pallet_prelude::*, RawOrigin}; pub use scale_info::{prelude::vec, TypeInfo}; @@ -94,7 +95,7 @@ impl Proposal{ pub struct Investor { pub account_id: T::AccountId, pub age: BlockNumberOf, - pub share: BalanceOf, + pub share: Percent, pub selections: u32, pub infos: BoundedVecOf }