Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add balance and transfer #24

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/extension-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.13.10",
"@cennznet/api": "^1.4.2",
"@cennznet/extension-inject": "^0.38.3",
"@cennznet/util": "^1.5.0-alpha.1",
"@polkadot/api": "^4.4.1",
"@polkadot/keyring": "^6.0.5",
"@polkadot/phishing": "^0.6.72",
Expand Down
53 changes: 53 additions & 0 deletions packages/extension-base/src/api/balances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2019-2021 @polkadot/extension-ui authors & contributors
// Copyright 2021 @cennznet/extension-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { formatUnits, isNull } from '@cennznet/util';

import { DEFAULT_DECIMALS, defaultBalances } from './constants';
import { getConnection } from './connections';
import { Balances } from '../types';
import { Connection } from './types';

const cache = new Map<string, Balances>();

export function addBalances (addressAndHash: string, balances: Balances) {
cache.set(addressAndHash, balances);
}

export function getStoredBalances (address: string, genesisHash: string): Balances {
const key = `${address}_${genesisHash}`;

if (cache.has(key)) {
return cache.get(key) as Balances;
}

return defaultBalances;
}

export function getBalances (address: string, genesisHash: string): Promise<Balances | null> {
return new Promise<Balances | null>(async (resolve, reject) => {
const onConnect = async (connection: Connection) => {
const { api, assets } = connection as Connection;
const cennzWei = (api && assets) ? (await api.query.genericAsset.freeBalance(assets.cennz.id, address)).toString() : '0';
const cpayWei = (api && assets) ? (await api.query.genericAsset.freeBalance(assets.cpay.id, address)).toString() : '0';
const balances = {
cennz: parseFloat(formatUnits(cennzWei, assets ? assets.cennz.decimals : DEFAULT_DECIMALS)),
cpay: parseFloat(formatUnits(cpayWei, assets ? assets.cpay.decimals : DEFAULT_DECIMALS)),
};

addBalances(`${address}_${genesisHash}`, balances);

resolve(balances);
};

const connection = getConnection(genesisHash, onConnect);
if (isNull(connection)) {
resolve(null);
}

if (!connection?.isConnecting) {
await onConnect(connection as Connection);
}
});
}
65 changes: 65 additions & 0 deletions packages/extension-base/src/api/connections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2019-2021 @polkadot/extension-ui authors & contributors
// Copyright 2021 @cennznet/extension-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { Api } from '@cennznet/api';

import { defaultAsset, endpoints } from './constants';
import { Assets, Connection, Connections } from './types';

const connections: Connections = {};

export function getConnection (genesisHash: string, onConnect?: (connection: Connection) => void): Connection | null {
if (connections.hasOwnProperty(genesisHash)) {
return connections[genesisHash];
}

if (endpoints.hasOwnProperty(genesisHash)) {
connections[genesisHash] = {
isConnecting: true,
}

Api.create({ provider: endpoints[genesisHash] }).then(async (api: Api) => {
const cennzId = parseInt((await api.query.genericAsset.stakingAssetId()).toString());
const cpayId = parseInt((await api.query.genericAsset.spendingAssetId()).toString());
const assets: Assets = { cennz: defaultAsset, cpay: defaultAsset };

try {
const registeredAssets = (await (api.rpc as any).genericAsset.registeredAssets()).toJSON();
registeredAssets.forEach((asset: [number, { symbol: string, decimalPlaces: number }]) => {
if (asset[0] === cennzId) {
assets.cennz = {
id: cennzId,
decimals: asset[1].decimalPlaces,
};
} else if (asset[0] === cpayId) {
assets.cpay = {
id: cpayId,
decimals: asset[1].decimalPlaces,
};
}
});
} catch (e) {
assets.cennz = {
id: cennzId,
decimals: 4,
};
assets.cpay = {
id: cpayId,
decimals: 4,
};
}

connections[genesisHash].api = api;
connections[genesisHash].assets = assets;
connections[genesisHash].isConnecting = false;

onConnect && onConnect(connections[genesisHash]);
});

return connections[genesisHash];
}

return null;
}

34 changes: 34 additions & 0 deletions packages/extension-base/src/api/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2019-2021 @polkadot/extension-ui authors & contributors
// Copyright 2021 @cennznet/extension-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { Asset, Endpoints, GenesisHashes } from './types';
import { Balances } from '../types';

export const genesisHashes: GenesisHashes = {
AZALEA: '0x0d0971c150a9741b8719b3c6c9c2e96ec5b2e3fb83641af868e6650f3e263ef0',
NIKAU: '0xc65170707265757d8a1fb8e039062286b8f0092f2984f5938588bd8e0f21ca2e',
RATA: '0x6f2ca0f5c770a9212d30b49604b2e45e2d3c949a3a940bfe908d68dbc4a0415e',
DEVELOPMENT: '0xba29ccef64182e17dee0f9d8bbaddc69e439acdc9409149e5c409d696c14232e',
};

export const endpoints: Endpoints = {
[genesisHashes.AZALEA]: 'wss://cennznet.unfrastructure.io/public/ws',
[genesisHashes.NIKAU]: 'wss://nikau.centrality.me/public/ws',
[genesisHashes.RATA]: 'wss://kong2.centrality.me/public/rata/ws',
[genesisHashes.DEVELOPMENT]: 'ws://localhost:9944',
};

export const defaultBalances: Balances = {
cennz: 0,
cpay: 0,
};

export const defaultAsset: Asset = {
id: 0,
decimals: 0,
};

export const DEFAULT_DECIMALS = 4;

export const DEFAULT_GENESIS_HASH = genesisHashes.AZALEA;
43 changes: 43 additions & 0 deletions packages/extension-base/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2019-2021 @polkadot/extension-ui authors & contributors
// Copyright 2021 @cennznet/extension-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { getConnection } from '@cennznet/extension-base/api/connections';
import { Connection } from '@cennznet/extension-base/api/types';
import { Keyring } from '@polkadot/ui-keyring';
import { genesisHashes } from './constants';

export * from './balances';

export function isChainSupported (genesisHash?: string | null, includeAnyChain: boolean = true): boolean {
return (!genesisHash && includeAnyChain) || Object.values(genesisHashes).includes(genesisHash as string);
}

export function transfer(genesisHash: string, fromAddress: string, toAddress: string, assetType: string, amount: string, password: string, keyring: Keyring): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
const onConnect = async (connection: Connection) => {
const { api, assets } = connection as Connection;
const assetId = assets ? (assetType === 'cennz' ? assets.cennz.id : assets.cpay.id) : 0;
const pair = keyring.getPair(fromAddress);

try {
pair.unlock(password);

if (api && assets) {
const tx = await api.tx.genericAsset.transfer(assetId, toAddress, amount);

const txid = (await tx.signAndSend(pair)).toString();

resolve(txid);
}
} catch (e) {
reject(e);
}
};
const connection = getConnection(genesisHash, onConnect);

if (!connection?.isConnecting) {
await onConnect(connection as Connection);
}
});
}
33 changes: 33 additions & 0 deletions packages/extension-base/src/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2019-2021 @polkadot/extension-ui authors & contributors
// Copyright 2021 @cennznet/extension-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { Api } from '@cennznet/api';

export type GenesisHashes = {
[chain: string]: string,
};

export type Endpoints = {
[genesisHash: string]: string
};

export interface Connection {
api?: Api,
assets?: Assets,
isConnecting: boolean,
}

export type Connections = {
[genesisHash: string]: Connection
};

export interface Asset {
id: number,
decimals: number,
}

export interface Assets {
cennz: Asset,
cpay: Asset,
}
Loading