Skip to content

Commit

Permalink
-power is dynamic
Browse files Browse the repository at this point in the history
-reserved in staking is dynamic
  • Loading branch information
isunaslabs committed Dec 17, 2022
1 parent ec529d2 commit 8eddec4
Show file tree
Hide file tree
Showing 34 changed files with 777 additions and 399 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,5 @@
"stylelint-config-standard": "^28.0.0",
"tailwindcss": "^3.1.8"
},
"dependencies": {
"@polkadot/api": "^9.4.1"
}
"dependencies": {}
}
9 changes: 7 additions & 2 deletions packages/app-config/src/chains/crab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import depositABI from "../abi/testNet/deposit.json";
export const crab: ChainConfig = {
name: "Crab",
displayName: "Crab",
explorerURL: ["https://pangolin.subscan.io/"],
rpcURL: ["https://pangolin-rpc.darwinia.network/"],
explorerURLs: ["https://crab.subscan.io/"],
httpsURLs: ["https://crab-rpc.darwinia.network"],
kton: {
address: "0x0000000000000000000000000000000000000402",
symbol: "CKTON",
Expand All @@ -26,4 +26,9 @@ export const crab: ChainConfig = {
deposit: depositABI,
},
chainId: 44,
substrate: {
graphQlURL: "https://subql.darwinia.network/subql-apps-crab/",
wssURL: "wss://crab-rpc.darwinia.network",
httpsURL: "https://crab-rpc.darwinia.network",
},
};
9 changes: 7 additions & 2 deletions packages/app-config/src/chains/darwinia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import depositABI from "../abi/testNet/deposit.json";
export const darwinia: ChainConfig = {
name: "Darwinia",
displayName: "Darwinia",
explorerURL: ["https://pangolin.subscan.io/"],
rpcURL: ["https://pangolin-rpc.darwinia.network/"],
explorerURLs: ["https://darwinia.subscan.io/"],
httpsURLs: ["https://rpc.darwinia.network"],
kton: {
address: "0x0000000000000000000000000000000000000402",
symbol: "KTON",
Expand All @@ -26,4 +26,9 @@ export const darwinia: ChainConfig = {
deposit: depositABI,
},
chainId: 46,
substrate: {
graphQlURL: "https://subql.darwinia.network/subql-apps-darwinia/",
wssURL: "wss://rpc.darwinia.network",
httpsURL: "https://rpc.darwinia.network",
},
};
11 changes: 8 additions & 3 deletions packages/app-config/src/chains/testNet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import depositABI from "../abi/testNet/deposit.json";
export const testNet: ChainConfig = {
name: "Pangolin",
displayName: "TestNet",
explorerURL: ["https://pangolin.subscan.io/"],
// rpcURL: ["ws://g1.dev.darwinia.network:20000"],
rpcURL: ["https://pangolin-rpc.darwinia.network/"],
explorerURLs: ["https://pangolin.subscan.io/"],
// httpsURLs: ["ws://g1.dev.darwinia.network:20000"],
httpsURLs: ["https://pangolin-rpc.darwinia.network/"],
kton: {
address: "0x0000000000000000000000000000000000000402",
symbol: "PKTON",
Expand All @@ -28,4 +28,9 @@ export const testNet: ChainConfig = {
deposit: depositABI,
},
chainId: 43,
substrate: {
graphQlURL: "https://subql.darwinia.network/subql-apps-pangolin/",
wssURL: "wss://pangolin-rpc.darwinia.network/",
httpsURL: "https://pangolin-rpc.darwinia.network",
},
};
2 changes: 1 addition & 1 deletion packages/app-config/src/supportedNetworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import { crab } from "./chains/crab";
import { darwinia } from "./chains/darwinia";
import { testNet } from "./chains/testNet";

export const supportedNetworks: ChainConfig[] = [testNet];
export const supportedNetworks: ChainConfig[] = [crab, darwinia, testNet];
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@darwinia/app-wallet",
"name": "@darwinia/app-providers",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
Expand All @@ -22,11 +22,18 @@
"typescript": "^4.8.3"
},
"dependencies": {
"@darwinia/api-derive": "^2.8.10",
"@darwinia/app-config": "^1.0.0",
"@darwinia/app-utils": "^1.0.0",
"@darwinia/types": "^2.8.10",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@ethersproject/transactions": "^5.7.0",
"@polkadot/api": "^9.10.2",
"@polkadot/types": "^9.10.3",
"axios": "^1.2.1",
"ethers": "^5.7.2",
"react": "^18.2.0"
"react": "^18.2.0",
"rxjs": "^7.8.0"
}
}
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/app-providers/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./walletProvider";
export * from "./storageProvider";
177 changes: 177 additions & 0 deletions packages/app-providers/src/storageProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { createContext, PropsWithChildren, useCallback, useContext, useEffect, useState } from "react";
import { StorageCtx, Asset } from "@darwinia/app-types";
import BigNumber from "bignumber.js";
import { useWallet } from "./walletProvider";
import { WsProvider, ApiPromise } from "@polkadot/api";
import { FrameSystemAccountInfo } from "@darwinia/api-derive/accounts/types";
import { Option } from "@polkadot/types";
import { DarwiniaStakingStructsStakingLedger } from "@darwinia/api-derive/darwiniaStaking/types";
import { convertAssetToPower, formatToEther } from "@darwinia/app-utils";

const initialState: StorageCtx = {
lockedKTON: undefined,
lockedRING: undefined,
power: undefined,
asset: undefined,
refresh: () => {
//ignore
},
};

type UnSubscription = () => void;
interface Pool {
ring: BigNumber;
kton: BigNumber;
}

interface ActiveAsset {
ring: BigNumber;
kton: BigNumber;
}

const StorageContext = createContext(initialState);

export const StorageProvider = ({ children }: PropsWithChildren) => {
const [lockedRING, setLockedRING] = useState<BigNumber>(BigNumber(0));
const [lockedKTON, setLockedKTON] = useState<BigNumber>(BigNumber(0));
const [power, setPower] = useState<BigNumber>(BigNumber(0));
const { selectedNetwork, selectedAccount } = useWallet();
const [apiPromise, setApiPromise] = useState<ApiPromise>();
const [pool, setPool] = useState<Pool>({ ring: BigNumber(0), kton: BigNumber(0) });
const [activeAsset, setActiveAsset] = useState<ActiveAsset>({ ring: BigNumber(0), kton: BigNumber(0) });
const [asset, setAsset] = useState<Asset>();
const account = "5EXZAE7Q4GH2QzSpzEBZ6ogigrYWYPnmr4PZf787HcGEFyMK";

const initNetworkStorage = async (rpcURL: string) => {
try {
const provider = new WsProvider(rpcURL);
const api = new ApiPromise({
provider,
});

api.on("connected", async () => {
const readyAPI = await api.isReady;
setApiPromise(readyAPI);
});
api.on("disconnected", () => {
// console.log("disconnected");
});
api.on("error", () => {
// console.log("error");
});
} catch (e) {
//ignore
}
};

/*calculate power*/
useEffect(() => {
const power = convertAssetToPower(activeAsset.ring, activeAsset.kton, pool.ring, pool.kton);
setPower(power);
}, [pool, activeAsset]);

/*Get staking ledger*/
useEffect(() => {
const getStakingLedger = async () => {
const ledger = await apiPromise?.query.staking.ledger(account);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const darwiniaLedger = ledger as unknown as Option<DarwiniaStakingStructsStakingLedger>;
if (darwiniaLedger && darwiniaLedger.isSome) {
const unwrappedLedger = darwiniaLedger.unwrap();
unwrappedLedger;
const activeRing = unwrappedLedger.active;
const activeKton = unwrappedLedger.activeKton;
const lockedRing = unwrappedLedger.activeDepositRing;
const bondedRing = activeRing.toBn().sub(lockedRing.toBn());
const bondedKton = activeKton;
setAsset({
ring: {
locked: BigNumber(formatToEther(unwrappedLedger.activeDepositRing.toString())),
bonded: BigNumber(formatToEther(bondedRing.toString())),
},
kton: {
bonded: BigNumber(formatToEther(bondedKton.toString())),
},
});

setActiveAsset({
ring: BigNumber(activeRing.toString()),
kton: BigNumber(activeKton.toString()),
});
}
};
getStakingLedger().catch((e) => {
console.log(e);
//ignore
});
}, [apiPromise]);

// fetch data from kton and ring pool
useEffect(() => {
const getPool = async () => {
const kton = (await apiPromise?.query.staking.ktonPool()) ?? 0;
const ring = (await apiPromise?.query.staking.ringPool()) ?? 0;
setPool({
kton: BigNumber(kton.toString()),
ring: BigNumber(ring.toString()),
});
};

getPool().catch(() => {
//ignore
});
}, [apiPromise]);

/*Monitor account balance*/
useEffect(() => {
let unsubscription: UnSubscription | undefined;
const getBalance = async () => {
try {
const res = await apiPromise?.query.system.account(account, (accountInfo: FrameSystemAccountInfo) => {
console.log("accountInfo", accountInfo.toJSON());
});
unsubscription = res as unknown as UnSubscription;
} catch (e) {
// ignore
}
};

getBalance().catch(() => {
//do nothing
});

return () => {
if (unsubscription) {
unsubscription();
}
};
}, [apiPromise]);

useEffect(() => {
if (!selectedNetwork) {
return;
}
initNetworkStorage(selectedNetwork.substrate.wssURL);
}, [selectedNetwork]);

const refresh = useCallback(() => {
// console.log("refresh storage====");
}, []);

return (
<StorageContext.Provider
value={{
lockedKTON,
lockedRING,
power,
refresh,
asset,
}}
>
{children}
</StorageContext.Provider>
);
};

export const useStorage = () => useContext(StorageContext);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createContext, PropsWithChildren, useCallback, useContext, useEffect, useState } from "react";
import { createContext, PropsWithChildren, useCallback, useContext, useEffect, useRef, useState } from "react";
import { dAppSupportedWallets } from "@darwinia/app-config";
import { ChainConfig, WalletCtx, WalletError, SupportedWallet, WalletConfig } from "@darwinia/app-types";
import { Contract, ethers } from "ethers";
Expand Down Expand Up @@ -58,6 +58,7 @@ export const WalletProvider = ({ children }: PropsWithChildren) => {
/* Listen to metamask account changes */
useEffect(() => {
if (!isWalletInstalled() || !isWalletConnected) {
setSelectedAccount(undefined);
return;
}

Expand Down Expand Up @@ -180,8 +181,8 @@ export const WalletProvider = ({ children }: PropsWithChildren) => {
nativeCurrency: {
...selectedNetwork.ring,
},
rpcUrls: [...selectedNetwork.rpcURL],
blockExplorerUrls: [...selectedNetwork.explorerURL],
rpcUrls: [...selectedNetwork.httpsURLs],
blockExplorerUrls: [...selectedNetwork.explorerURLs],
},
],
});
Expand Down
11 changes: 11 additions & 0 deletions packages/app-providers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.rollup.json",
"compilerOptions": {
"baseUrl": ".",
"rootDir": "src",
"paths": {
"my-darwinia-storage": ["../../node_modules/darwinia-storage/dist"]
}
},
"include": ["src"]
}
1 change: 1 addition & 0 deletions packages/app-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./others";
export * from "./wallet";
export * from "./staking";
export * from "./storage";
19 changes: 19 additions & 0 deletions packages/app-types/src/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import BigNumber from "bignumber.js";

export interface StakingAsset {
locked?: BigNumber;
bonded: BigNumber;
}

export interface Asset {
ring: StakingAsset;
kton: StakingAsset;
}

export interface StorageCtx {
power: BigNumber | undefined;
lockedRING: BigNumber | undefined;
lockedKTON: BigNumber | undefined;
asset: Asset | undefined;
refresh: () => void;
}
12 changes: 10 additions & 2 deletions packages/app-types/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,24 @@ export interface ContractABI {
deposit: ContractInterface;
}

export interface Substrate {
wssURL: string;
httpsURL: string;
metadata?: string;
graphQlURL: string;
}

export interface ChainConfig {
name: ChainName; // this name is used to set the chain name in MetaMask, the user will later see this name on Metamask
displayName: string; // This name is used on the dApp just for the user to see
chainId: number;
ring: Token;
kton: Token;
explorerURL: [string];
rpcURL: [string];
httpsURLs: string[];
explorerURLs: string[];
contractInterface: ContractABI;
contractAddresses: ContractAddress;
substrate: Substrate;
}

export interface WalletExtension {
Expand Down
2 changes: 2 additions & 0 deletions packages/app-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"dependencies": {
"@darwinia/app-config": "^1.0.0",
"@darwinia/app-types": "^1.0.0",
"bignumber.js": "^9.1.1",
"ethers": "^5.7.2",
"moment": "^2.29.4"
}
}
Loading

0 comments on commit 8eddec4

Please sign in to comment.