Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
RomeroYang committed Mar 4, 2022
2 parents 4551bbc + 57e62d8 commit 05d2ab3
Show file tree
Hide file tree
Showing 29 changed files with 1,923 additions and 1,960 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## [0.4.3] - 20220304

* bump polkadot-js/api v7.10.1
* fix javascript polyfill issue

## [0.4.2] - 20220221

* bump polkadot-js/api v7.9.1
* bump typescript v4.5.5
* bump webview_flutter: ^3.0.0

## [0.4.1] - 20220209

* bump polkadot-js/api v7.2.1
Expand Down
10 changes: 5 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.4.0"
version: "0.4.2"
process:
dependency: transitive
description:
Expand Down Expand Up @@ -365,28 +365,28 @@ packages:
name: webview_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
version: "3.0.0"
webview_flutter_android:
dependency: transitive
description:
name: webview_flutter_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.15"
version: "2.8.3"
webview_flutter_platform_interface:
dependency: transitive
description:
name: webview_flutter_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.8.0"
webview_flutter_wkwebview:
dependency: transitive
description:
name: webview_flutter_wkwebview
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.14"
version: "2.7.1"
win32:
dependency: transitive
description:
Expand Down
4 changes: 0 additions & 4 deletions js_api/.babelrc

This file was deleted.

8 changes: 7 additions & 1 deletion js_api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Wrap `@polkadot-js/api` to provide APIs for polkawallet/sdk.

@polkadot-js/api: ^v1.29.1
@polkadot-js/api: ^7.9.1

## build & test

Expand All @@ -16,3 +16,9 @@ yarn run build
To test:
open `./test/index.html` in chrome.
open chrome dev console and run `runTests().then(console.log)`

### `polkadot-js/api` hack

1. remove `connectWithRetry()` in `node_modules/@polkadot/rpc-provider/ws/index.js` to avoid auto-connect.
2. replace value of `packageInfo.path` in `node_modules/@polkadot/api/packageInfo.js` to avoid `Invalid URL` issue in webview.
3. lock `bn.js: 4.12.0` to avoid [this issue](https://github.com/polkadot-js/api/issues/4024).
11 changes: 11 additions & 0 deletions js_api/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
assumptions: { setPublicClassFields: true, privateFieldsAsProperties: true },
presets: [["@babel/preset-env", { modules: false }], "@babel/preset-typescript"],
plugins: [
"@babel/plugin-proposal-private-methods",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-runtime",
"@babel/plugin-transform-classes",
"@babel/plugin-transform-modules-commonjs",
],
};
2 changes: 1 addition & 1 deletion js_api/dist/main.js

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions js_api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkawallet/js-api",
"version": "0.3.8",
"version": "0.4.2",
"main": "index.js",
"license": "Apache-2.0",
"keywords": [
Expand All @@ -24,18 +24,19 @@
"build-dev": "webpack --mode development"
},
"resolutions": {
"@polkadot/api": "^7.2.1"
"@polkadot/api": "^7.10.1",
"bn.js": "4.12.0"
},
"dependencies": {
"@polkadot/api": "^7.2.1",
"@polkadot/extension-dapp": "^0.40.3",
"@polkadot/ui-shared": "0.86.7",
"@polkadot/api": "^7.10.1",
"@polkadot/extension-dapp": "^0.42.7",
"@polkadot/ui-shared": "^1.1.1",
"@walletconnect/client": "2.0.0-alpha.26",
"bn.js": "^5.1.2",
"oo7-substrate": "^0.8.0",
"process": "^0.11.10",
"secp256k1": "^3.8.0",
"typescript": "^4.3.5"
"typescript": "^4.5.5"
},
"devDependencies": {
"@babel/core": "^7.14.8",
Expand All @@ -48,8 +49,9 @@
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-prettier": "^3.1.2",
"prettier": "^1.19.1",
"source-map-loader": "^3.0.1",
"stream-browserify": "^3.0.0",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1"
"webpack-cli": "^4.9.2"
}
}
6 changes: 3 additions & 3 deletions js_api/src/constants/networkMetadata.ts

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions js_api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ send("log", "main js loaded");
(<any>window).send = send;

async function connectAll(nodes: string[]) {
return Promise.race(nodes.map(node => connect([node])));
return Promise.race(nodes.map((node) => connect([node])));
}

/**
Expand All @@ -29,7 +29,7 @@ async function connectAll(nodes: string[]) {
*/
async function connect(nodes: string[]) {
(<any>window).api = undefined;

return new Promise(async (resolve, reject) => {
const wsProvider = new WsProvider(nodes);
try {
Expand All @@ -39,16 +39,16 @@ async function connect(nodes: string[]) {
[`${KUSAMA_GENESIS}-9122`]: localMetadata["kusama"],
[`${POLKADOT_GENESIS}-9122`]: localMetadata["polkadot"],
[`${STATEMINE_GENESIS}-504`]: localMetadata["statemine"],
},
} as any,
});
if (!(<any>window).api) {
(<any>window).api = res;
const url = nodes[(<any>res)._options.provider.__private_16_endpointIndex];
const url = nodes[(<any>res)._options.provider.__private_27_endpointIndex];
send("log", `${url} wss connected success`);
resolve(url);
} else {
res.disconnect();
const url = nodes[(<any>res)._options.provider.__private_16_endpointIndex];
const url = nodes[(<any>res)._options.provider.__private_27_endpointIndex];
send("log", `${url} wss success and disconnected`);
resolve(url);
}
Expand Down
41 changes: 34 additions & 7 deletions js_api/src/service/gov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DeriveCollectiveProposal, DeriveReferendumExt, DeriveCouncilVotes } fro
import { SubmittableExtrinsic } from "@polkadot/api/types";
import { getTypeDef, Option, Bytes } from "@polkadot/types";
import { OpenTip, AccountId } from "@polkadot/types/interfaces";
import { formatBalance, stringToU8a, BN_ZERO, hexToString } from "@polkadot/util";
import { stringToU8a, BN_ZERO, hexToString, BN_MILLION } from "@polkadot/util";
import BN from "bn.js";

import { approxChanges } from "../utils/referendumApproxChanges";
Expand Down Expand Up @@ -134,6 +134,17 @@ async function fetchProposals(api: ApiPromise) {
});
}

/**
* Query next external proposal.
*/
async function fetchExternal(api: ApiPromise) {
const external = await api.derive.democracy.nextExternal();
if (!external) return null;

external.image.proposal = _transfromProposalMeta(external.image.proposal) as any;
return external;
}

/**
* Query votes of council members and candidates.
*/
Expand All @@ -157,15 +168,30 @@ const TREASURY_ACCOUNT = stringToU8a("modlpy/trsry".padEnd(32, "\0"));
* Query overview of treasury and spend proposals.
*/
async function getTreasuryOverview(api: ApiPromise) {
const proposals = await api.derive.treasury.proposals();
const balance = await api.derive.balances.account(TREASURY_ACCOUNT as AccountId);
const [bounties, proposals, balance] = await Promise.all([
api.derive.bounties?.bounties(),
api.derive.treasury.proposals(),
api.derive.balances.account(TREASURY_ACCOUNT as AccountId),
]);
const pendingBounties = bounties.reduce(
(total, { bounty: { status, value } }) => total.iadd(status.isApproved ? value : BN_ZERO),
new BN(0)
);
const pendingProposals = proposals.approvals.reduce((total, { proposal: { value } }) => total.iadd(value), new BN(0));
const burn =
balance.freeBalance.gt(BN_ZERO) && !(api.consts.treasury.burn as any).isZero()
? (api.consts.treasury.burn as any).mul(balance.freeBalance).div(BN_MILLION)
: BN_ZERO;
const res: any = {
...proposals,
};
res["balance"] = formatBalance(balance.freeBalance, {
forceUnit: "-",
withSi: false,
}).split(".")[0];
res["balance"] = balance.freeBalance.toString();
res["burn"] = burn.toString();
res["approved"] = pendingProposals.toString();
res["spendable"] = balance.freeBalance
.sub(pendingBounties)
.sub(pendingProposals)
.toString();
res.proposals.forEach((e: any) => {
if (e.council.length) {
e.council = e.council.map((i: any) => ({
Expand Down Expand Up @@ -240,6 +266,7 @@ export default {
fetchReferendums,
getReferendumVoteConvictions,
fetchProposals,
fetchExternal,
fetchCouncilVotes,
getCouncilMotions,
getTreasuryOverview,
Expand Down
5 changes: 4 additions & 1 deletion js_api/src/service/setting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ApiPromise } from "@polkadot/api";
import { SubstrateNetworkKeys } from "../constants/networkSpect";

const MAX_NOMINATIONS = 16;

/**
* subscribe messages of network state.
*
Expand Down Expand Up @@ -35,7 +37,8 @@ export async function getNetworkConst(api: ApiPromise) {
existentialDeposit: api.consts.balances?.existentialDeposit,
},
staking: {
maxNominations: api.consts.staking?.maxNominations,
maxNominations: api.consts.staking?.maxNominations || MAX_NOMINATIONS,
maxNominatorRewardedPerValidator: api.consts.staking?.maxNominatorRewardedPerValidator,
},
timestamp: {
minimumPeriod: api.consts.timestamp?.minimumPeriod,
Expand Down
12 changes: 7 additions & 5 deletions js_api/src/service/staking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,14 @@ async function querySortedTargets(api: ApiPromise) {
api.derive.staking.waitingInfo({withPrefs: true}),
api.derive.session.info(),
api.query.staking.minNominatorBond(),
api.query.staking.counterForNominators(),
api.derive.session.indexes().then(({ activeEra }) => activeEra.gt(BN_ZERO) ? activeEra.sub(BN_ONE) : undefined).then(lastEra => api.query.staking.erasValidatorReward([lastEra])),
]);

const partial = data[1] && data[2] && data[3] && data[4]
? _extractTargetsInfo(api, data[2], data[3], data[1], _transfromEra(data[4]), data[0])
? _extractTargetsInfo(api, data[2], data[3], data[1] as any, _transfromEra(data[4]), data[0] as any)
: {};
return { inflation: { inflation: 0, stakedReturn: 0 }, medianComm: 0, ...partial, minNominatorBond: data[5] };
return { inflation: { inflation: 0, stakedReturn: 0 }, medianComm: 0, ...partial, minNominatorBond: data[5], counterForNominators: data[6], lastReward: data[7] };
}

async function _getOwnStash(api: ApiPromise, accountId: string): Promise<[string, boolean]> {
Expand All @@ -578,11 +580,11 @@ async function _getOwnStash(api: ApiPromise, accountId: string): Promise<[string
api.query.staking.bonded(accountId),
api.query.staking.ledger(accountId),
]);
if (ownStash[0].isSome) {
if ((ownStash[0] as any).isSome) {
isOwnStash = true;
}
if (ownStash[1].isSome) {
stashId = ownStash[1].unwrap().stash.toString();
if ((ownStash[1] as any).isSome) {
stashId = (ownStash[1] as any).unwrap().stash.toString();
if (accountId != stashId) {
isOwnStash = false;
}
Expand Down
4 changes: 1 addition & 3 deletions js_api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
/* Basic Options */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [
"DOM"
] /* Specify library files to be included in the compilation. */,
"lib": ["DOM"] /* Specify library files to be included in the compilation. */,
"allowJs": true /* Allow javascript files to be compiled. */,
"checkJs": false /* Report errors in .js files. */,
"sourceMap": true /* Generates corresponding '.map' file. */,
Expand Down
12 changes: 11 additions & 1 deletion js_api/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const config = {
filename: "main.js",
},
resolve: {
extensions: [".ts", ".js", ".mjs", ".json"],
extensions: [".ts", ".js", ".mjs", ".cjs", ".json"],
fallback: { crypto: require.resolve("crypto-browserify"), stream: require.resolve("stream-browserify") },
},
plugins: [
Expand All @@ -29,6 +29,16 @@ const config = {
include: /node_modules/,
type: "javascript/auto",
},
{
test: /\.cjs$/,
include: path.resolve(__dirname, "node_modules/@polkadot/"),
use: "babel-loader",
},
{
test: /\.js$/,
include: path.resolve(__dirname, "node_modules/@polkadot/"),
use: "babel-loader",
},
],
},
};
Expand Down
Loading

0 comments on commit 05d2ab3

Please sign in to comment.