Compare commits
114 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb791d3963 | ||
|
|
bc49e75e61 | ||
|
|
67f8dcafdb | ||
|
|
e95117839b | ||
|
|
9f60a8cd5f | ||
|
|
e23fae86d5 | ||
|
|
94a4594d32 | ||
|
|
5be8ef8f0d | ||
|
|
e855d5a9d4 | ||
|
|
2a0e481710 | ||
|
|
161c121cb2 | ||
|
|
de38735498 | ||
|
|
ce1e6615e3 | ||
|
|
3ebe9f5243 | ||
|
|
f686cfcce7 | ||
|
|
c27e49c6f5 | ||
|
|
5c69f78676 | ||
|
|
8f00351405 | ||
|
|
1f6c8c2eb8 | ||
|
|
9bb1343255 | ||
|
|
94ebb321f1 | ||
|
|
63fd5baa8f | ||
|
|
ea3ee428d8 | ||
|
|
5cb1c2d33a | ||
|
|
600666de1c | ||
|
|
524885e8de | ||
|
|
1476e5e5e9 | ||
|
|
3f48b3c29f | ||
|
|
9debb854f9 | ||
|
|
db3fe7d103 | ||
|
|
a93063c6fb | ||
|
|
8e4bbd2b23 | ||
|
|
cf1c2ea58a | ||
|
|
ca2909e4ba | ||
|
|
cc9ea53e1a | ||
|
|
d876e19fa1 | ||
|
|
bb7b94dc35 | ||
|
|
be1deb1a75 | ||
|
|
14026fef42 | ||
|
|
b2ea2da6aa | ||
|
|
f1d21847db | ||
|
|
b1fcfd11f4 | ||
|
|
7d1f0e2900 | ||
|
|
fb377fb0d4 | ||
|
|
26de59ed79 | ||
|
|
964bd33bb1 | ||
|
|
f068ef76a5 | ||
|
|
e6e60ec6d0 | ||
|
|
9f9680ba92 | ||
|
|
bb63de1653 | ||
|
|
a2c2b24198 | ||
|
|
1944ffe017 | ||
|
|
d4aa36981d | ||
|
|
2c735fbc53 | ||
|
|
7038045bc8 | ||
|
|
0be5c969aa | ||
|
|
9ead2bdc41 | ||
|
|
1ae37ed2d3 | ||
|
|
7ca78c5679 | ||
|
|
cd94d01b06 | ||
|
|
dafc92809d | ||
|
|
298884d885 | ||
|
|
2a64c7c29a | ||
|
|
d2b7dc550a | ||
|
|
25f18d5455 | ||
|
|
c9c872c736 | ||
|
|
8394b7fb5a | ||
|
|
fb82db382e | ||
|
|
9f83f5cac3 | ||
|
|
45a895b64f | ||
|
|
93fbd6e13f | ||
|
|
b53490bd26 | ||
|
|
594376367a | ||
|
|
180fe57531 | ||
|
|
bddb4465fb | ||
|
|
3cdc647506 | ||
|
|
82ee7cc4f1 | ||
|
|
9f9658d6a0 | ||
|
|
1abc519297 | ||
|
|
b939bcd57d | ||
|
|
d487a27676 | ||
|
|
0525feb6f2 | ||
|
|
4ea343e369 | ||
|
|
6f2173778e | ||
|
|
bb6166e65f | ||
|
|
dec82380c3 | ||
|
|
a980d4ae11 | ||
|
|
5c194b6f5f | ||
|
|
c4662e6fd8 | ||
|
|
995de2012e | ||
|
|
b1a8c011f4 | ||
|
|
86cd550ad3 | ||
|
|
57529998f9 | ||
|
|
0e0d733e65 | ||
|
|
773618acfb | ||
|
|
c5fcb0d06b | ||
|
|
e25272c986 | ||
|
|
d389c6737e | ||
|
|
9e1313f140 | ||
|
|
d2a2887d32 | ||
|
|
b3d9549c0b | ||
|
|
240cadd045 | ||
|
|
d47faa4687 | ||
|
|
393c677a16 | ||
|
|
074ddd8632 | ||
|
|
a8a10aec0c | ||
|
|
fed93e2459 | ||
|
|
374df23011 | ||
|
|
9c098c3fbf | ||
|
|
4e799858ad | ||
|
|
ee6924324f | ||
|
|
75f86bb43e | ||
|
|
bfd3113a55 | ||
|
|
8d8d5bd7d7 |
@@ -1,4 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/api authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
module.exports = require('./babel.config.js');
|
||||
@@ -1,5 +0,0 @@
|
||||
.eslintrc.js
|
||||
**/build/*
|
||||
**/coverage/*
|
||||
**/node_modules/*
|
||||
docs/examples/promise/90_typegen/*
|
||||
@@ -5,6 +5,15 @@ const base = require('@polkadot/dev/config/eslint');
|
||||
|
||||
module.exports = {
|
||||
...base,
|
||||
ignorePatterns: [
|
||||
'.eslintrc.js',
|
||||
'.github/**',
|
||||
'.vscode/**',
|
||||
'.yarn/**',
|
||||
'**/build/*',
|
||||
'**/coverage/*',
|
||||
'**/node_modules/*'
|
||||
],
|
||||
parserOptions: {
|
||||
...base.parserOptions,
|
||||
project: [
|
||||
|
||||
2
.github/workflows/pr-any.yml
vendored
2
.github/workflows/pr-any.yml
vendored
@@ -9,7 +9,7 @@ jobs:
|
||||
name: ${{ matrix.step }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- uses: actions/checkout@v2
|
||||
- name: ${{ matrix.step }}
|
||||
run: |
|
||||
yarn install --immutable | grep -v 'YN0013'
|
||||
|
||||
2
.github/workflows/push-master.yml
vendored
2
.github/workflows/push-master.yml
vendored
@@ -13,7 +13,7 @@ jobs:
|
||||
if: "! startsWith(github.event.head_commit.message, '[CI Skip]') && github.repository == 'polkadot-js/api'"
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
token: ${{ secrets.GH_PAT }}
|
||||
- name: ${{ matrix.step }}
|
||||
|
||||
55
.yarn/releases/yarn-2.2.2.cjs
vendored
55
.yarn/releases/yarn-2.2.2.cjs
vendored
File diff suppressed because one or more lines are too long
55
.yarn/releases/yarn-2.3.3.cjs
vendored
Executable file
55
.yarn/releases/yarn-2.3.3.cjs
vendored
Executable file
File diff suppressed because one or more lines are too long
@@ -12,4 +12,4 @@ plugins:
|
||||
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
|
||||
spec: "@yarnpkg/plugin-workspace-tools"
|
||||
|
||||
yarnPath: .yarn/releases/yarn-2.2.2.cjs
|
||||
yarnPath: .yarn/releases/yarn-2.3.3.cjs
|
||||
|
||||
73
CHANGELOG.md
73
CHANGELOG.md
@@ -1,6 +1,72 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 2.2.1 Oct 13, 2020
|
||||
## 2.5.1 Nov 2, 2020
|
||||
|
||||
Upgrade priority: Low. Recommended for `api-contract` developers.
|
||||
|
||||
- **Breaking change** Substrate changed the `ContractExecResult` from `api.rpc.contracts.call`. The API does have pass-through support for both old and new variants via the `Contract` interface, however it means the structure has changed to accommodate the new Substrate structure. Refer to the [docs for the latest interface](https://polkadot.js.org/docs/api-contract/start/contract.read) (including `isSuccess/isError` -> `isOk/isErr`, and the availability of the `gasConsumed`)
|
||||
|
||||
Contributed:
|
||||
|
||||
- Dedup similar types on generation (Thanks to https://github.com/monitz87)
|
||||
|
||||
Changes:
|
||||
|
||||
- `Contract` execution will now return contract events decoded in the result
|
||||
- Add `.dryRun` on extrinsics (alongside `.paymentInfo` usage)
|
||||
- Add `system_syncState` and `syncstate_genSyncSpec` RPCs
|
||||
- Enhance error reporting, `Call` indicates explicit call as found
|
||||
- Adjust internal metadata decoration and use as exposed from `@types/metadata`
|
||||
- Expand Rococo known types to cater for v1 (specVersion 10)
|
||||
- `registry.hash` now returns an `H256` object (as opposed to raw Uint8Array)
|
||||
- Use utility logger in all relevant error messages (consistency in logging)
|
||||
- Add `derive.council.proposal(<hash>)` for retrieval of a single (expanded) proposal
|
||||
- Correctly and transparently format type generation types (allowing for nested variants)
|
||||
|
||||
|
||||
## 2.4.1 Oct 26, 2020
|
||||
|
||||
Upgrade priority: Low. No breaking changes, however it does move `api-contract` forward with additional features.
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add support for both `{i,u}256` types on contracts (Thanks to https://github.com/seanyoung)
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `.keysAt` & `.entriesAt` to query maps at a specific blockHash
|
||||
- Adjust ordering of type injection, any `types: { ... }` definitions not have precedence
|
||||
- Correctly format Tuples in both RPC and const definitions as part of type generation
|
||||
- Add node-template to known types (tracking inside API), remove warnings for the spec
|
||||
- Add initial implementation for new Substrate `MultiAddress` type
|
||||
- Add `api.derive.chain.subscribeNewBlocks` for full block retrievals
|
||||
- Contracts identifiers to `.{exec, read}('name', ...)` now matches on both snake_case and camelCase (consistency)
|
||||
- Cleanup contracts serialization for messages to allow both to and from
|
||||
- Add deserialization of contract events against the ABI
|
||||
- Adjust contracts metadata parsing to be future-proof with type retrievals
|
||||
- Add tests for Solang into contracts, re-organize layout per type
|
||||
|
||||
|
||||
## 2.3.1 Oct 19, 2020
|
||||
|
||||
Upgrade priority: Low. Recommended for `api-contract` developers and those using large messages via Node.js WS.
|
||||
|
||||
Contributed:
|
||||
|
||||
- Adjust frame size for Node.js WebSockets (Thanks to https://github.com/mnaamani)
|
||||
- Update warnings to reflect new docs URLs (Thanks to https://github.com/aphelionz)
|
||||
|
||||
Changes:
|
||||
|
||||
- Expose `dispatch{Error, Info}` on all `.tx` results (less boilerplate on info checks)
|
||||
- Add missing primitive types inside contracts type parsing
|
||||
- Adjust api-contract to expose `Contract.{query, tx}.<message>` and `Blueprint.tx.<constructor>`
|
||||
- Rework contracts registry to be generic to all scale-info definitions
|
||||
- Allow for auto-max weight on contract read/exec (useful in estimation)
|
||||
- Fix memoization where `BigInt` params are passed
|
||||
|
||||
|
||||
## 2.2.1 Oct 12, 2020
|
||||
|
||||
Upgrade priority: Low.
|
||||
|
||||
@@ -14,10 +80,11 @@ Contributed:
|
||||
Changes:
|
||||
|
||||
- Introduce `OriginCaller` type (& apply to scheduler types)
|
||||
- Adjust `ContractInfo` struct to cater for latest Substrate
|
||||
- Adjust `ContractInfo` type for latest Substrate (contracts)
|
||||
- Adjust `Schedule` type for latest Substrate (contracts)
|
||||
- Allow snake_case to be handled transparently in structs
|
||||
- Full new api-contract developer interfaces (Code, Blueprint, Contract)
|
||||
- Rework Abi & Abi Registry use & parse (on-top-of-contributed) for efficiency & non-global clobber
|
||||
- Rework Abi & Abi Registry use & parse (based on contributed) for efficiency & non-global clobber
|
||||
- Allow `null` input on `Text` values in addition to existing `undefined`
|
||||
- Remove all documentation after move to https://github.com/polkadot-js/docs repo
|
||||
- Adjust type encoding from TypeDef, s/encodeType/encodeTypeDef/
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Copyright 2017-2020 @polkadot/api authors & contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
function copy_folder () {
|
||||
SRC="packages/$1/build"
|
||||
DST="../apps/node_modules/@polkadot/$1"
|
||||
|
||||
echo "** Copying $SRC to $DST"
|
||||
|
||||
rm -rf $DST
|
||||
cp -r $SRC $DST
|
||||
}
|
||||
|
||||
yarn polkadot-dev-build-ts
|
||||
|
||||
copy_folder "api"
|
||||
copy_folder "api-contract"
|
||||
copy_folder "api-derive"
|
||||
copy_folder "metadata"
|
||||
copy_folder "rpc-core"
|
||||
copy_folder "rpc-provider"
|
||||
copy_folder "types"
|
||||
copy_folder "types-known"
|
||||
copy_folder "typegen"
|
||||
@@ -1,25 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Copyright 2017-2020 @polkadot/api authors & contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
function copy_folder () {
|
||||
SRC="packages/$1/build"
|
||||
DST="../test/js-test/node_modules/@polkadot/$1"
|
||||
|
||||
echo "** Copying $SRC to $DST"
|
||||
|
||||
rm -rf $DST
|
||||
cp -r $SRC $DST
|
||||
}
|
||||
|
||||
yarn polkadot-dev-build-ts
|
||||
|
||||
copy_folder "api"
|
||||
copy_folder "api-contract"
|
||||
copy_folder "api-derive"
|
||||
copy_folder "metadata"
|
||||
copy_folder "rpc-core"
|
||||
copy_folder "rpc-provider"
|
||||
copy_folder "types"
|
||||
copy_folder "types-known"
|
||||
copy_folder "typegen"
|
||||
0
docs/.nojekyll
Normal file
0
docs/.nojekyll
Normal file
12
docs/404.html
Normal file
12
docs/404.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="initial-scale=0.5, maximum-scale=1">
|
||||
<meta http-equiv="refresh" content="0;URL='https://polkadot.js.org/docs/api/'" />
|
||||
<title>Redirecting to https://polkadot.js.org/docs/api/</title>
|
||||
</head>
|
||||
<body>
|
||||
Redirecting to <a href="https://polkadot.js.org/docs/api/">https://polkadot.js.org/docs/api/</a>
|
||||
</body>
|
||||
</html>
|
||||
12
docs/index.html
Normal file
12
docs/index.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="initial-scale=0.5, maximum-scale=1">
|
||||
<meta http-equiv="refresh" content="0;URL='https://polkadot.js.org/docs/api/'" />
|
||||
<title>Redirecting to https://polkadot.js.org/docs/api/</title>
|
||||
</head>
|
||||
<body>
|
||||
Redirecting to <a href="https://polkadot.js.org/docs/api/">https://polkadot.js.org/docs/api/</a>
|
||||
</body>
|
||||
</html>
|
||||
@@ -25,5 +25,5 @@ module.exports = Object.assign({}, config, {
|
||||
'<rootDir>/packages/types/build',
|
||||
'<rootDir>/packages/types-known/build'
|
||||
],
|
||||
resolver: './jest.resolver.js'
|
||||
resolver: '@polkadot/dev/config/jest-resolver'
|
||||
});
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/api authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
const path = require('path');
|
||||
|
||||
module.exports = function resolver (file, config) {
|
||||
if (file.includes('package.json')) {
|
||||
return path.join(config.basedir.replace('/src', '/'), file);
|
||||
}
|
||||
|
||||
return config.defaultResolver(file, config);
|
||||
};
|
||||
22
package.json
22
package.json
@@ -8,7 +8,7 @@
|
||||
],
|
||||
"resolutions": {
|
||||
"babel-core": "^7.0.0-bridge.0",
|
||||
"typescript": "^4.0.3"
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "yarn build:interfaces && polkadot-dev-build-ts && (cd packages/typegen && copyfiles scripts/* build)",
|
||||
@@ -24,18 +24,14 @@
|
||||
"test:watch": "polkadot-dev-run-test --watch"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.11.6",
|
||||
"@babel/register": "^7.11.5",
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/dev": "^0.58.1",
|
||||
"@polkadot/ts": "^0.3.49",
|
||||
"@babel/core": "^7.12.3",
|
||||
"@babel/register": "^7.12.1",
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/dev": "^0.59.14",
|
||||
"@polkadot/ts": "^0.3.53",
|
||||
"@polkadot/typegen": "workspace:packages/typegen",
|
||||
"@types/jest": "^26.0.14",
|
||||
"@vuepress/plugin-search": "^1.6.0",
|
||||
"copyfiles": "^2.4.0",
|
||||
"typedoc": "^0.19.2",
|
||||
"typedoc-plugin-markdown": "^3.0.7",
|
||||
"typedoc-plugin-no-inherit": "^1.2.0"
|
||||
"@types/jest": "^26.0.15",
|
||||
"copyfiles": "^2.4.0"
|
||||
},
|
||||
"version": "2.2.1"
|
||||
"version": "2.5.1"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/api-contract",
|
||||
"version": "2.2.1",
|
||||
"version": "2.5.1",
|
||||
"description": "Interfaces for interacting with contracts and contract ABIs",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -26,11 +26,11 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-contract#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/api": "2.2.1",
|
||||
"@polkadot/rpc-core": "2.2.1",
|
||||
"@polkadot/types": "2.2.1",
|
||||
"@polkadot/util": "^3.5.1",
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/api": "^2.5.1",
|
||||
"@polkadot/rpc-core": "^2.5.1",
|
||||
"@polkadot/types": "^2.5.1",
|
||||
"@polkadot/util": "^3.7.1",
|
||||
"bn.js": "^5.1.3",
|
||||
"rxjs": "^6.6.3"
|
||||
}
|
||||
|
||||
@@ -7,88 +7,27 @@ import * as testContracts from '../test/contracts';
|
||||
|
||||
const abis: Record<string, any> = { ...testContracts };
|
||||
|
||||
function compare (name: string, messageIds: string[]): void {
|
||||
try {
|
||||
const inkAbi = new Abi(abis[name]);
|
||||
|
||||
expect(inkAbi.messages.map(({ identifier }) => identifier)).toEqual(messageIds);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
throw error;
|
||||
interface JSONAbi {
|
||||
spec: {
|
||||
messages: {
|
||||
name: string[] | string
|
||||
}[]
|
||||
}
|
||||
}
|
||||
|
||||
describe('Abi', (): void => {
|
||||
describe('construction', (): void => {
|
||||
it('initializes from a contract ABI (flipper)', (): void => {
|
||||
compare('flipper', ['flip', 'get']);
|
||||
});
|
||||
Object.entries(abis).forEach(([abiName, abi]) => {
|
||||
it(`initializes from a contract ABI (${abiName})`, (): void => {
|
||||
try {
|
||||
const messageIds = (abi as JSONAbi).spec.messages.map(({ name }) => Array.isArray(name) ? name[0] : name);
|
||||
const inkAbi = new Abi(abis[abiName]);
|
||||
|
||||
it('initializes from a contract ABI (incrementer)', (): void => {
|
||||
compare('incrementer', ['inc', 'get']);
|
||||
});
|
||||
expect(inkAbi.messages.map(({ identifier }) => identifier)).toEqual(messageIds);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
it('initializes from a contract ABI (erc20)', (): void => {
|
||||
compare('erc20', [
|
||||
'total_supply',
|
||||
'balance_of',
|
||||
'allowance',
|
||||
'transfer',
|
||||
'approve',
|
||||
'transfer_from'
|
||||
]);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (delegator)', (): void => {
|
||||
compare('delegator', [
|
||||
'get',
|
||||
'change',
|
||||
'switch'
|
||||
]);
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (dns)', (): void => {
|
||||
compare('dns', [
|
||||
'register',
|
||||
'set_address',
|
||||
'transfer',
|
||||
'get_address'
|
||||
]);
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (erc721)', (): void => {
|
||||
compare('erc721', [
|
||||
'balance_of',
|
||||
'owner_of',
|
||||
'get_approved',
|
||||
'is_approved_for_all',
|
||||
'set_approval_for_all',
|
||||
'approve',
|
||||
'transfer',
|
||||
'transfer_from',
|
||||
'mint',
|
||||
'burn'
|
||||
]);
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (multisig_plain)', (): void => {
|
||||
compare('multisigPlain', [
|
||||
'add_owner',
|
||||
'remove_owner',
|
||||
'replace_owner',
|
||||
'change_requirement',
|
||||
'submit_transaction',
|
||||
'cancel_transaction',
|
||||
'confirm_transaction',
|
||||
'revoke_confirmation',
|
||||
'invoke_transaction',
|
||||
'eval_transaction'
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
it('stores base project as JSON', (): void => {
|
||||
expect(new Abi(abis.dns).json).toEqual(abis.dns);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,33 +1,39 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { AnyJson } from '@polkadot/types/types';
|
||||
import { ChainProperties, InkConstructorSpec, InkMessageSpec } from '@polkadot/types/interfaces';
|
||||
import { AbiConstructor, AbiMessage, AbiMessageParam } from './types';
|
||||
import { AnyJson, Codec, CodecArg } from '@polkadot/types/types';
|
||||
import { ChainProperties, ContractConstructorSpec, ContractEventSpec, ContractMessageSpec, ContractMessageParamSpec, ContractProject } from '@polkadot/types/interfaces';
|
||||
import { AbiConstructor, AbiEvent, AbiMessage, AbiParam, DecodedEvent, DecodedMessage } from './types';
|
||||
|
||||
import { assert, isNumber, isObject, isString, stringCamelCase } from '@polkadot/util';
|
||||
import { assert, assertReturn, compactAddLength, compactStripLength, isNumber, isObject, isString, logger, stringCamelCase, u8aConcat, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import ContractRegistry from './ContractRegistry';
|
||||
import MetaRegistry from './MetaRegistry';
|
||||
|
||||
const l = logger('Abi');
|
||||
|
||||
function findMessage <T extends AbiMessage> (list: T[], messageOrId: T | string | number): T {
|
||||
const message = isNumber(messageOrId)
|
||||
? list[messageOrId]
|
||||
: isString(messageOrId)
|
||||
? list.find(({ identifier }: T) => identifier === messageOrId.toString())
|
||||
? list.find(({ identifier }) => [identifier, stringCamelCase(identifier)].includes(messageOrId.toString()))
|
||||
: messageOrId;
|
||||
|
||||
assert(message, `Attempted to call an invalid contract interface, ${JSON.stringify(messageOrId)}`);
|
||||
|
||||
return message;
|
||||
return assertReturn(message, `Attempted to call an invalid contract interface, ${JSON.stringify(messageOrId)}`);
|
||||
}
|
||||
|
||||
export default class Abi extends ContractRegistry {
|
||||
export default class Abi {
|
||||
readonly #events: AbiEvent[];
|
||||
|
||||
public readonly constructors: AbiConstructor[];
|
||||
|
||||
public readonly json: AnyJson;
|
||||
|
||||
public readonly messages: AbiMessage[];
|
||||
|
||||
public readonly project: ContractProject;
|
||||
|
||||
public readonly registry: MetaRegistry;
|
||||
|
||||
constructor (abiJson: AnyJson, chainProperties?: ChainProperties) {
|
||||
const json = isString(abiJson)
|
||||
? JSON.parse(abiJson) as AnyJson
|
||||
@@ -35,27 +41,62 @@ export default class Abi extends ContractRegistry {
|
||||
|
||||
assert(isObject(json) && !Array.isArray(json) && json.metadataVersion && isObject(json.spec) && !Array.isArray(json.spec) && Array.isArray(json.spec.constructors) && Array.isArray(json.spec.messages), 'Invalid JSON ABI structure supplied, expected a recent metadata version');
|
||||
|
||||
super(json, chainProperties);
|
||||
|
||||
this.json = json;
|
||||
this.constructors = this.project.spec.constructors.map((spec: InkConstructorSpec, index) =>
|
||||
this._createBase(spec, index, {
|
||||
this.registry = new MetaRegistry(chainProperties);
|
||||
this.project = this.registry.createType('ContractProject', json);
|
||||
|
||||
this.registry.setMetaTypes(this.project.types);
|
||||
|
||||
this.project.types.forEach((_, index) =>
|
||||
this.registry.getMetaTypeDef({ type: this.registry.createType('SiLookupTypeId', index + 1) })
|
||||
);
|
||||
this.constructors = this.project.spec.constructors.map((spec: ContractConstructorSpec, index) =>
|
||||
this.#createMessage(spec, index, {
|
||||
isConstructor: true
|
||||
})
|
||||
);
|
||||
this.messages = this.project.spec.messages.map((spec: InkMessageSpec, index): AbiMessage => {
|
||||
this.#events = this.project.spec.events.map((spec: ContractEventSpec, index) =>
|
||||
this.#createEvent(spec, index)
|
||||
);
|
||||
this.messages = this.project.spec.messages.map((spec: ContractMessageSpec, index): AbiMessage => {
|
||||
const typeSpec = spec.returnType.unwrapOr(null);
|
||||
|
||||
return this._createBase(spec, index, {
|
||||
return this.#createMessage(spec, index, {
|
||||
isMutating: spec.mutates.isTrue,
|
||||
isPayable: spec.payable.isTrue,
|
||||
returnType: typeSpec
|
||||
? this.typeDefAt(typeSpec.type, { displayName: typeSpec.displayName.map((s) => s.toString()).join('::') || undefined })
|
||||
? this.registry.getMetaTypeDef(typeSpec)
|
||||
: null
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Warning: Unstable API, bound to change
|
||||
*/
|
||||
public decodeEvent (data: Uint8Array): DecodedEvent {
|
||||
const index = data[0];
|
||||
const event = this.#events[index];
|
||||
|
||||
assert(event, `Unable to find event with index ${index}`);
|
||||
|
||||
return event.fromU8a(data.subarray(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Warning: Unstable API, bound to change
|
||||
*/
|
||||
public decodeConstructor (data: Uint8Array): DecodedMessage {
|
||||
return this.#decodeMessage('message', this.constructors, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Warning: Unstable API, bound to change
|
||||
*/
|
||||
public decodeMessage (data: Uint8Array): DecodedMessage {
|
||||
return this.#decodeMessage('message', this.messages, data);
|
||||
}
|
||||
|
||||
public findConstructor (constructorOrId: AbiConstructor | string | number): AbiConstructor {
|
||||
return findMessage(this.constructors, constructorOrId);
|
||||
}
|
||||
@@ -64,30 +105,91 @@ export default class Abi extends ContractRegistry {
|
||||
return findMessage(this.messages, messageOrId);
|
||||
}
|
||||
|
||||
private _createBase (spec: InkMessageSpec | InkConstructorSpec, index: number, add: Partial<AbiMessage> = {}): AbiMessage {
|
||||
const identifier = spec.name.toString();
|
||||
const args = spec.args.map((arg, index): AbiMessageParam => {
|
||||
#createArgs = (args: ContractMessageParamSpec[], spec: unknown): AbiParam[] => {
|
||||
return args.map((arg, index): AbiParam => {
|
||||
try {
|
||||
assert(isObject(arg.type), 'Invalid type definition found');
|
||||
|
||||
return {
|
||||
name: stringCamelCase(arg.name.toString()),
|
||||
type: this.typeDefAt(arg.type.type)
|
||||
name: stringCamelCase(arg.name),
|
||||
type: this.registry.getMetaTypeDef(arg.type)
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`Error expanding argument ${index} in ${JSON.stringify(spec)}`);
|
||||
l.error(`Error expanding argument ${index} in ${JSON.stringify(spec)}`);
|
||||
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
#createEvent = (spec: ContractEventSpec, index: number): AbiEvent => {
|
||||
const args = this.#createArgs(spec.args, spec);
|
||||
const event = {
|
||||
args,
|
||||
docs: spec.docs.map((doc) => doc.toString()),
|
||||
fromU8a: (data: Uint8Array): DecodedEvent => ({
|
||||
args: this.#decodeArgs(args, data),
|
||||
event
|
||||
}),
|
||||
identifier: spec.name.toString(),
|
||||
index
|
||||
};
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
#createMessage = (spec: ContractMessageSpec | ContractConstructorSpec, index: number, add: Partial<AbiMessage> = {}): AbiMessage => {
|
||||
const args = this.#createArgs(spec.args, spec);
|
||||
const message = {
|
||||
...add,
|
||||
args,
|
||||
docs: spec.docs.map((doc) => doc.toString()),
|
||||
identifier,
|
||||
fromU8a: (data: Uint8Array): DecodedMessage => ({
|
||||
args: this.#decodeArgs(args, data),
|
||||
message
|
||||
}),
|
||||
identifier: spec.name.toString(),
|
||||
index,
|
||||
selector: spec.selector
|
||||
selector: spec.selector,
|
||||
toU8a: (params: CodecArg[]) =>
|
||||
this.#encodeArgs(spec, args, params)
|
||||
};
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
#decodeArgs = (args: AbiParam[], data: Uint8Array): Codec[] => {
|
||||
// for decoding we expect the input to be just the arg data, no selectors
|
||||
// no length added (this allows use with events as well)
|
||||
let offset = 0;
|
||||
|
||||
return args.map(({ type }): Codec => {
|
||||
const value = this.registry.createType(type.type as 'Text', data.subarray(offset));
|
||||
|
||||
offset += value.encodedLength;
|
||||
|
||||
return value;
|
||||
});
|
||||
}
|
||||
|
||||
#decodeMessage = (type: 'constructor' | 'message', list: AbiMessage[], data: Uint8Array): DecodedMessage => {
|
||||
const [, trimmed] = compactStripLength(data);
|
||||
const selector = trimmed.subarray(0, 4);
|
||||
const message = list.find((m) => m.selector.eq(selector));
|
||||
|
||||
assert(message, `Unable to find ${type} with selector ${u8aToHex(selector)}`);
|
||||
|
||||
return message.fromU8a(trimmed.subarray(4));
|
||||
}
|
||||
|
||||
#encodeArgs = ({ name, selector }: ContractMessageSpec | ContractConstructorSpec, args: AbiParam[], data: CodecArg[]): Uint8Array => {
|
||||
assert(data.length === args.length, `Expected ${args.length} arguments to contract message '${name.toString()}', found ${data.length}`);
|
||||
|
||||
return compactAddLength(
|
||||
u8aConcat(
|
||||
this.registry.createType('ContractSelector', selector).toU8a(),
|
||||
...args.map(({ type }, index) => this.registry.createType(type.type as 'Text', data[index]).toU8a())
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import ContractRegistry from './ContractRegistry';
|
||||
|
||||
import * as testContracts from '../test/contracts';
|
||||
|
||||
const abis: Record<string, any> = { ...testContracts };
|
||||
|
||||
function compare (name: string): void {
|
||||
const inkRegistry = new ContractRegistry(abis[name]);
|
||||
|
||||
try {
|
||||
const cmpPath = path.join(__dirname, `../test/compare/${name}.test.json`);
|
||||
|
||||
if (!fs.existsSync(cmpPath)) {
|
||||
fs.writeFileSync(cmpPath, JSON.stringify(inkRegistry.typeDefs, null, 2), { flag: 'w' });
|
||||
}
|
||||
|
||||
expect(inkRegistry.typeDefs).toEqual(require(cmpPath));
|
||||
} catch (error) {
|
||||
console.error(JSON.stringify(inkRegistry.typeDefs));
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
describe('ContractRegistry', (): void => {
|
||||
describe('construction', (): void => {
|
||||
it('initializes from a contract ABI (flipper)', (): void => {
|
||||
compare('flipper');
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (incrementer)', (): void => {
|
||||
compare('incrementer');
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (erc20)', (): void => {
|
||||
compare('erc20');
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (dns)', (): void => {
|
||||
compare('dns');
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (erc721)', (): void => {
|
||||
compare('erc721');
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (multisig_plain)', (): void => {
|
||||
compare('multisigPlain');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,226 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ChainProperties, InkProject, MtField, MtLookupTypeId, MtType, MtTypeDefArray, MtTypeDefVariant, MtTypeDefSequence, MtTypeDefTuple, MtVariant } from '@polkadot/types/interfaces';
|
||||
import { AnyJson, Registry, TypeDef, TypeDefInfo } from '@polkadot/types/types';
|
||||
|
||||
import { assert, isUndefined } from '@polkadot/util';
|
||||
import { TypeRegistry, withTypeString } from '@polkadot/types';
|
||||
|
||||
// convert the offset into project-specific, index-1
|
||||
export function getRegistryOffset (id: MtLookupTypeId): number {
|
||||
return id.toNumber() - 1;
|
||||
}
|
||||
|
||||
export default class ContractRegistry {
|
||||
public readonly typeDefs: TypeDef[] = [];
|
||||
|
||||
public readonly registry: Registry;
|
||||
|
||||
public readonly project: InkProject;
|
||||
|
||||
constructor (json: Record<string, AnyJson>, chainProperties?: ChainProperties) {
|
||||
this.registry = new TypeRegistry();
|
||||
|
||||
if (chainProperties) {
|
||||
this.registry.setChainProperties(chainProperties);
|
||||
}
|
||||
|
||||
this.project = this.registry.createType('InkProject', json);
|
||||
|
||||
// Generate TypeDefs for each provided registry type
|
||||
this.project.types.forEach((_, index) => this.setTypeDef(this.registry.createType('MtLookupTypeId', index + 1)));
|
||||
}
|
||||
|
||||
public getAbiType (id: MtLookupTypeId): MtType {
|
||||
const offset = getRegistryOffset(id);
|
||||
const type = this.project.types[offset];
|
||||
|
||||
assert(!isUndefined(type), `getAbiType:: Unable to find ${id.toNumber()} in type values`);
|
||||
|
||||
return this.registry.createType('MtType', type);
|
||||
}
|
||||
|
||||
public typeDefAt (id: MtLookupTypeId, extra: Pick<TypeDef, never> = {}): TypeDef {
|
||||
if (!this.typeDefs[getRegistryOffset(id)]) {
|
||||
this.setTypeDef(id);
|
||||
}
|
||||
|
||||
return {
|
||||
...this.typeDefs[getRegistryOffset(id)],
|
||||
...extra
|
||||
};
|
||||
}
|
||||
|
||||
public setTypeDef (id: MtLookupTypeId): void {
|
||||
const typeDef = this.extractType(this.getAbiType(id), id) as TypeDef;
|
||||
|
||||
this.typeDefs[getRegistryOffset(id)] = typeDef;
|
||||
|
||||
// We have a displayName for non-primitives and non-results
|
||||
// FIXME here we protect against "Option: 'Option<...something...>' definitions (same with result)
|
||||
if (typeDef.displayName && typeDef.info !== TypeDefInfo.Plain && !typeDef.type.startsWith(typeDef.displayName)) {
|
||||
this.registry.register({ [typeDef.displayName]: typeDef.type });
|
||||
}
|
||||
}
|
||||
|
||||
private extractType (inkType: MtType, id: MtLookupTypeId): Pick<TypeDef, never> {
|
||||
const path = [...inkType.path];
|
||||
let typeDef;
|
||||
|
||||
if (inkType.path.join('::').startsWith('ink_env::types::') || inkType.def.isPrimitive) {
|
||||
typeDef = this.extractPrimitive(inkType);
|
||||
} else if (inkType.def.isComposite) {
|
||||
typeDef = this.extractFields(inkType.def.asComposite.fields);
|
||||
} else if (inkType.def.isVariant) {
|
||||
typeDef = this.extractVariant(inkType.def.asVariant, id);
|
||||
} else if (inkType.def.isArray) {
|
||||
typeDef = this.extractArray(inkType.def.asArray);
|
||||
} else if (inkType.def.isSequence) {
|
||||
typeDef = this.extractSequence(inkType.def.asSequence, id);
|
||||
} else if (inkType.def.isTuple) {
|
||||
typeDef = this.extractTuple(inkType.def.asTuple);
|
||||
} else {
|
||||
throw new Error(`Invalid ink! type at index ${id.toString()}`);
|
||||
}
|
||||
|
||||
const displayName = path.pop()?.toString();
|
||||
|
||||
return withTypeString({
|
||||
...(displayName
|
||||
? { displayName }
|
||||
: {}
|
||||
),
|
||||
...(path.length > 1
|
||||
? { namespace: path.map((segment) => segment.toString()).join('::') }
|
||||
: {}
|
||||
),
|
||||
...(inkType.params.length > 0
|
||||
? { params: inkType.params.map((id) => this.typeDefAt(id)) }
|
||||
: {}
|
||||
),
|
||||
...typeDef
|
||||
});
|
||||
}
|
||||
|
||||
private extractVariant ({ variants }: MtTypeDefVariant, id: MtLookupTypeId): Pick<TypeDef, never> {
|
||||
const { params, path } = this.getAbiType(id);
|
||||
const specialVariant = path[0].toString();
|
||||
|
||||
if (specialVariant === 'Option') {
|
||||
return {
|
||||
info: TypeDefInfo.Option,
|
||||
sub: this.typeDefAt(params[0])
|
||||
};
|
||||
} else if (specialVariant === 'Result') {
|
||||
return {
|
||||
info: TypeDefInfo.Result,
|
||||
sub: params.map((param, index) => ({
|
||||
name: ['Ok', 'Error'][index],
|
||||
...this.typeDefAt(param)
|
||||
}))
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
info: TypeDefInfo.Enum,
|
||||
sub: this.extractVariantSub(variants)
|
||||
};
|
||||
}
|
||||
|
||||
private extractVariantSub (variants: MtVariant[]): Pick<TypeDef, any>[] {
|
||||
const isAllUnitVariants = variants.every(({ fields }) => fields.length === 0);
|
||||
|
||||
if (isAllUnitVariants) {
|
||||
return variants.map(({ discriminant, name }) => ({
|
||||
...(
|
||||
discriminant.isSome
|
||||
? { ext: { discriminant: discriminant.unwrap().toNumber() } }
|
||||
: {}
|
||||
),
|
||||
info: TypeDefInfo.Plain,
|
||||
name: name.toString(),
|
||||
type: 'Null'
|
||||
}));
|
||||
}
|
||||
|
||||
return variants.map(({ fields, name }) => ({
|
||||
...this.extractFields(fields),
|
||||
name: name.toString()
|
||||
}));
|
||||
}
|
||||
|
||||
private extractFields (fields: MtField[]): Pick<TypeDef, any> {
|
||||
const [isStruct, isTuple] = fields.reduce(([isAllNamed, isAllUnnamed], { name }) => ([
|
||||
isAllNamed && name.isSome,
|
||||
isAllUnnamed && name.isNone
|
||||
]),
|
||||
[true, true]);
|
||||
|
||||
let info;
|
||||
|
||||
// check for tuple first (no fields may be available)
|
||||
if (isTuple) {
|
||||
info = TypeDefInfo.Tuple;
|
||||
} else if (isStruct) {
|
||||
info = TypeDefInfo.Struct;
|
||||
} else {
|
||||
throw new Error('Invalid fields type detected, expected either Tuple or Struct');
|
||||
}
|
||||
|
||||
const sub = fields.map(({ name, type }) => {
|
||||
return {
|
||||
...this.typeDefAt(type),
|
||||
...(name.isSome ? { name: name.unwrap().toString() } : {})
|
||||
};
|
||||
});
|
||||
|
||||
return isTuple && sub.length === 1
|
||||
? sub[0]
|
||||
: { info, sub };
|
||||
}
|
||||
|
||||
private extractArray ({ len: length, type }: MtTypeDefArray): Pick<TypeDef, never> {
|
||||
assert(!length || length.toNumber() <= 256, 'ContractRegistry: Only support for [Type; <length>], where length > 256');
|
||||
|
||||
return {
|
||||
info: TypeDefInfo.VecFixed,
|
||||
length: length.toNumber(),
|
||||
sub: this.typeDefAt(type)
|
||||
};
|
||||
}
|
||||
|
||||
private extractSequence ({ type }: MtTypeDefSequence, id: MtLookupTypeId): Pick<TypeDef, never> {
|
||||
assert(!!type, `ContractRegistry: Invalid sequence type found at id ${id.toString()}`);
|
||||
|
||||
return {
|
||||
info: TypeDefInfo.Vec,
|
||||
sub: this.typeDefAt(type)
|
||||
};
|
||||
}
|
||||
|
||||
private extractTuple (ids: MtTypeDefTuple): Pick<TypeDef, never> {
|
||||
return ids.length === 1
|
||||
? this.typeDefAt(ids[0])
|
||||
: {
|
||||
info: TypeDefInfo.Tuple,
|
||||
sub: ids.map((id) => this.typeDefAt(id))
|
||||
};
|
||||
}
|
||||
|
||||
private extractPrimitive (inkType: MtType): Pick<TypeDef, never> {
|
||||
if (inkType.def.isPrimitive) {
|
||||
return {
|
||||
info: TypeDefInfo.Plain,
|
||||
type: inkType.def.asPrimitive.type.toLowerCase()
|
||||
};
|
||||
} else if (inkType.path.length > 1) {
|
||||
return {
|
||||
info: TypeDefInfo.Plain,
|
||||
type: inkType.path[inkType.path.length - 1].toString()
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error('Invalid primitive type');
|
||||
}
|
||||
}
|
||||
38
packages/api-contract/src/MetaRegistry.spec.ts
Normal file
38
packages/api-contract/src/MetaRegistry.spec.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import Abi from './Abi';
|
||||
|
||||
import * as testContracts from '../test/contracts';
|
||||
|
||||
const abis: Record<string, any> = { ...testContracts };
|
||||
|
||||
describe('MetaRegistry', (): void => {
|
||||
Object.keys(abis).forEach((abiName) => {
|
||||
it(`initializes from a contract ABI (${abiName})`, (): void => {
|
||||
const abi = new Abi(abis[abiName]);
|
||||
|
||||
try {
|
||||
const cmpPath = path.join(__dirname, `../test/compare/${abiName}.test.json`);
|
||||
|
||||
if (!fs.existsSync(cmpPath)) {
|
||||
fs.writeFileSync(cmpPath, JSON.stringify(abi.registry.metaTypeDefs, null, 2), { flag: 'w' });
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
expect(abi.registry.metaTypeDefs).toEqual(require(cmpPath));
|
||||
} catch (error) {
|
||||
console.error(JSON.stringify(abi.registry.metaTypeDefs));
|
||||
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('stores base project as JSON', (): void => {
|
||||
expect(new Abi(abis.dns).json).toEqual(abis.dns);
|
||||
});
|
||||
});
|
||||
251
packages/api-contract/src/MetaRegistry.ts
Normal file
251
packages/api-contract/src/MetaRegistry.ts
Normal file
@@ -0,0 +1,251 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ContractDisplayName, ChainProperties, SiField, SiLookupTypeId, SiType, SiTypeDefArray, SiTypeDefVariant, SiTypeDefSequence, SiTypeDefTuple, SiVariant } from '@polkadot/types/interfaces';
|
||||
import { InterfaceTypes, TypeDef, TypeDefInfo } from '@polkadot/types/types';
|
||||
|
||||
import { assert, isUndefined } from '@polkadot/util';
|
||||
import { TypeRegistry, withTypeString } from '@polkadot/types';
|
||||
|
||||
interface PartialTypeSpec {
|
||||
readonly type: SiLookupTypeId;
|
||||
readonly displayName?: ContractDisplayName;
|
||||
}
|
||||
|
||||
// convert the offset into project-specific, index-1
|
||||
export function getRegistryOffset (id: SiLookupTypeId): number {
|
||||
return id.toNumber() - 1;
|
||||
}
|
||||
|
||||
const PRIMITIVE_ALIAS: Record<string, keyof InterfaceTypes> = {
|
||||
Char: 'u32', // Rust char is 4-bytes
|
||||
Str: 'Text'
|
||||
};
|
||||
|
||||
const PRIMITIVE_ALWAYS = ['AccountId', 'AccountIndex', 'Address', 'Balance'];
|
||||
|
||||
export default class MetaRegistry extends TypeRegistry {
|
||||
public readonly metaTypeDefs: TypeDef[] = [];
|
||||
|
||||
#siTypes: SiType[] = [];
|
||||
|
||||
constructor (chainProperties?: ChainProperties) {
|
||||
super();
|
||||
|
||||
if (chainProperties) {
|
||||
this.setChainProperties(chainProperties);
|
||||
}
|
||||
}
|
||||
|
||||
public setMetaTypes (metaTypes: SiType[]): void {
|
||||
this.#siTypes = metaTypes;
|
||||
}
|
||||
|
||||
public getMetaTypeDef (typeSpec: PartialTypeSpec): TypeDef {
|
||||
const offset = getRegistryOffset(typeSpec.type);
|
||||
let typeDef = this.metaTypeDefs[offset];
|
||||
|
||||
if (!typeDef) {
|
||||
typeDef = this.#extract(this.#getMetaType(typeSpec.type), typeSpec.type);
|
||||
|
||||
this.metaTypeDefs[offset] = typeDef;
|
||||
}
|
||||
|
||||
if (typeSpec.displayName && typeSpec.displayName.length) {
|
||||
const displayName = typeSpec.displayName[typeSpec.displayName.length - 1].toString();
|
||||
|
||||
if (!typeDef.type.startsWith(displayName)) {
|
||||
typeDef = {
|
||||
...typeDef,
|
||||
displayName,
|
||||
type: PRIMITIVE_ALWAYS.includes(displayName)
|
||||
? displayName
|
||||
: typeDef.type
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Here we protect against the following cases
|
||||
// - No displayName present, these are generally known primitives
|
||||
// - displayName === type, these generate circular references
|
||||
// - displayName Option & type Option<...something...>
|
||||
if (typeDef.displayName && !this.hasType(typeDef.displayName) && !(typeDef.type === typeDef.displayName || typeDef.type.startsWith(`${typeDef.displayName}<`))) {
|
||||
this.register({ [typeDef.displayName]: typeDef.type });
|
||||
}
|
||||
|
||||
return typeDef;
|
||||
}
|
||||
|
||||
#getMetaType = (id: SiLookupTypeId): SiType => {
|
||||
const type = this.#siTypes[getRegistryOffset(id)];
|
||||
|
||||
assert(!isUndefined(type), `getMetaType:: Unable to find ${id.toNumber()} in type values`);
|
||||
|
||||
return this.createType('SiType', type);
|
||||
}
|
||||
|
||||
#extract = (type: SiType, id: SiLookupTypeId): TypeDef => {
|
||||
const path = [...type.path];
|
||||
const pathFinal = path.length ? path[path.length - 1].toString() : '';
|
||||
let typeDef: Omit<TypeDef, 'type'>;
|
||||
|
||||
if (type.path.join('::').startsWith('ink_env::types::') || PRIMITIVE_ALWAYS.includes(pathFinal)) {
|
||||
typeDef = this.#extractPrimitivePath(type);
|
||||
} else if (type.def.isPrimitive) {
|
||||
typeDef = this.#extractPrimitive(type);
|
||||
} else if (type.def.isComposite) {
|
||||
typeDef = this.#extractFields(type.def.asComposite.fields);
|
||||
} else if (type.def.isVariant) {
|
||||
typeDef = this.#extractVariant(type.def.asVariant, id);
|
||||
} else if (type.def.isArray) {
|
||||
typeDef = this.#extractArray(type.def.asArray);
|
||||
} else if (type.def.isSequence) {
|
||||
typeDef = this.#extractSequence(type.def.asSequence, id);
|
||||
} else if (type.def.isTuple) {
|
||||
typeDef = this.#extractTuple(type.def.asTuple);
|
||||
} else {
|
||||
throw new Error(`Invalid ink! type at index ${id.toString()}`);
|
||||
}
|
||||
|
||||
const displayName = path.pop()?.toString();
|
||||
|
||||
return withTypeString({
|
||||
...(displayName
|
||||
? { displayName }
|
||||
: {}
|
||||
),
|
||||
...(path.length > 1
|
||||
? { namespace: path.map((segment) => segment.toString()).join('::') }
|
||||
: {}
|
||||
),
|
||||
...(type.params.length > 0
|
||||
? { params: type.params.map((type) => this.getMetaTypeDef({ type })) }
|
||||
: {}
|
||||
),
|
||||
...typeDef
|
||||
});
|
||||
}
|
||||
|
||||
#extractArray = ({ len: length, type }: SiTypeDefArray): Omit<TypeDef, 'type'> => {
|
||||
assert(!length || length.toNumber() <= 256, 'MetaRegistry: Only support for [Type; <length>], where length > 256');
|
||||
|
||||
return {
|
||||
info: TypeDefInfo.VecFixed,
|
||||
length: length.toNumber(),
|
||||
sub: this.getMetaTypeDef({ type })
|
||||
};
|
||||
}
|
||||
|
||||
#extractFields = (fields: SiField[]): Omit<TypeDef, 'type'> => {
|
||||
const [isStruct, isTuple] = fields.reduce(([isAllNamed, isAllUnnamed], { name }) => ([
|
||||
isAllNamed && name.isSome,
|
||||
isAllUnnamed && name.isNone
|
||||
]),
|
||||
[true, true]);
|
||||
|
||||
let info;
|
||||
|
||||
// check for tuple first (no fields may be available)
|
||||
if (isTuple) {
|
||||
info = TypeDefInfo.Tuple;
|
||||
} else if (isStruct) {
|
||||
info = TypeDefInfo.Struct;
|
||||
} else {
|
||||
throw new Error('Invalid fields type detected, expected either Tuple or Struct');
|
||||
}
|
||||
|
||||
const sub = fields.map(({ name, type }) => {
|
||||
return {
|
||||
...this.getMetaTypeDef({ type }),
|
||||
...(name.isSome ? { name: name.unwrap().toString() } : {})
|
||||
};
|
||||
});
|
||||
|
||||
return isTuple && sub.length === 1
|
||||
? sub[0]
|
||||
: { info, sub };
|
||||
}
|
||||
|
||||
#extractPrimitive = (type: SiType): TypeDef => {
|
||||
const typeStr = type.def.asPrimitive.type.toString();
|
||||
|
||||
return {
|
||||
info: TypeDefInfo.Plain,
|
||||
type: PRIMITIVE_ALIAS[typeStr] || typeStr.toLowerCase()
|
||||
};
|
||||
}
|
||||
|
||||
#extractPrimitivePath = (type: SiType): TypeDef => {
|
||||
return {
|
||||
info: TypeDefInfo.Plain,
|
||||
type: type.path[type.path.length - 1].toString()
|
||||
};
|
||||
}
|
||||
|
||||
#extractSequence = ({ type }: SiTypeDefSequence, id: SiLookupTypeId): Omit<TypeDef, 'type'> => {
|
||||
assert(!!type, `ContractRegistry: Invalid sequence type found at id ${id.toString()}`);
|
||||
|
||||
return {
|
||||
info: TypeDefInfo.Vec,
|
||||
sub: this.getMetaTypeDef({ type })
|
||||
};
|
||||
}
|
||||
|
||||
#extractTuple = (ids: SiTypeDefTuple): Omit<TypeDef, 'type'> => {
|
||||
return ids.length === 1
|
||||
? this.getMetaTypeDef({ type: ids[0] })
|
||||
: {
|
||||
info: TypeDefInfo.Tuple,
|
||||
sub: ids.map((type) => this.getMetaTypeDef({ type }))
|
||||
};
|
||||
}
|
||||
|
||||
#extractVariant = ({ variants }: SiTypeDefVariant, id: SiLookupTypeId): Omit<TypeDef, 'type'> => {
|
||||
const { params, path } = this.#getMetaType(id);
|
||||
const specialVariant = path[0].toString();
|
||||
|
||||
if (specialVariant === 'Option') {
|
||||
return {
|
||||
info: TypeDefInfo.Option,
|
||||
sub: this.getMetaTypeDef({ type: params[0] })
|
||||
};
|
||||
} else if (specialVariant === 'Result') {
|
||||
return {
|
||||
info: TypeDefInfo.Result,
|
||||
sub: params.map((type, index) => ({
|
||||
name: ['Ok', 'Error'][index],
|
||||
...this.getMetaTypeDef({ type })
|
||||
}))
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
info: TypeDefInfo.Enum,
|
||||
sub: this.#extractVariantSub(variants)
|
||||
};
|
||||
}
|
||||
|
||||
#extractVariantSub = (variants: SiVariant[]): TypeDef[] => {
|
||||
const isAllUnitVariants = variants.every(({ fields }) => fields.length === 0);
|
||||
|
||||
if (isAllUnitVariants) {
|
||||
return variants.map(({ discriminant, name }) => ({
|
||||
...(
|
||||
discriminant.isSome
|
||||
? { ext: { discriminant: discriminant.unwrap().toNumber() } }
|
||||
: {}
|
||||
),
|
||||
info: TypeDefInfo.Plain,
|
||||
name: name.toString(),
|
||||
type: 'Null'
|
||||
}));
|
||||
}
|
||||
|
||||
return variants.map(({ fields, name }) =>
|
||||
withTypeString({
|
||||
...this.#extractFields(fields),
|
||||
name: name.toString()
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,15 @@ import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
||||
import { AccountId, EventRecord, Hash } from '@polkadot/types/interfaces';
|
||||
import { AnyJson, CodecArg, ISubmittableResult } from '@polkadot/types/types';
|
||||
import { AbiConstructor } from '../types';
|
||||
import { MapConstructorExec } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { SubmittableResult } from '@polkadot/api';
|
||||
import ApiBase from '@polkadot/api/base';
|
||||
import { isUndefined, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import Abi from '../Abi';
|
||||
import { applyOnEvent, encodeMessage } from '../util';
|
||||
import { applyOnEvent } from '../util';
|
||||
import Base from './Base';
|
||||
import Contract from './Contract';
|
||||
|
||||
@@ -29,19 +31,38 @@ export class BlueprintSubmittableResult<ApiType extends ApiTypes> extends Submit
|
||||
export default class Blueprint<ApiType extends ApiTypes> extends Base<ApiType> {
|
||||
public readonly codeHash: Hash;
|
||||
|
||||
readonly #tx: MapConstructorExec<ApiType> = {};
|
||||
|
||||
constructor (api: ApiBase<ApiType>, abi: AnyJson | Abi, codeHash: string | Hash | Uint8Array, decorateMethod: DecorateMethod<ApiType>) {
|
||||
super(api, abi, decorateMethod);
|
||||
|
||||
this.codeHash = this.registry.createType('Hash', codeHash);
|
||||
|
||||
this.abi.constructors.forEach((c): void => {
|
||||
const messageName = stringCamelCase(c.identifier);
|
||||
|
||||
if (isUndefined(this.#tx[messageName])) {
|
||||
this.#tx[messageName] = (endowment: BigInt | string | number | BN, gasLimit: BigInt | string | number | BN, ...params: CodecArg[]) =>
|
||||
this.#deploy(c, endowment, gasLimit, params);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public createContract (constructorOrId: AbiConstructor | string| number, endowment: BigInt | string | number | BN, gasLimit: BigInt | string | number | BN, ...params: CodecArg[]): SubmittableExtrinsic<ApiType, BlueprintSubmittableResult<ApiType>> {
|
||||
public get tx (): MapConstructorExec<ApiType> {
|
||||
return this.#tx;
|
||||
}
|
||||
|
||||
#deploy = (constructorOrId: AbiConstructor | string| number, endowment: BigInt | string | number | BN, gasLimit: BigInt | string | number | BN, params: CodecArg[]): SubmittableExtrinsic<ApiType, BlueprintSubmittableResult<ApiType>> => {
|
||||
return this.api.tx.contracts
|
||||
.instantiate(endowment, gasLimit, this.codeHash, encodeMessage(this.registry, this.abi.findConstructor(constructorOrId), params))
|
||||
.instantiate(endowment, gasLimit, this.codeHash, this.abi.findConstructor(constructorOrId).toU8a(params))
|
||||
.withResultTransform((result: ISubmittableResult) =>
|
||||
new BlueprintSubmittableResult(result, applyOnEvent(result, 'Instantiated', (record: EventRecord) =>
|
||||
new BlueprintSubmittableResult(result, applyOnEvent(result, 'Instantiated', ([record]: EventRecord[]) =>
|
||||
new Contract<ApiType>(this.api, this.abi, record.event.data[1] as AccountId, this._decorateMethod)
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
public createContract (constructorOrId: AbiConstructor | string| number, endowment: BigInt | string | number | BN, gasLimit: BigInt | string | number | BN, ...params: CodecArg[]): SubmittableExtrinsic<ApiType, BlueprintSubmittableResult<ApiType>> {
|
||||
return this.#deploy(constructorOrId, endowment, gasLimit, params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ export default class Code<ApiType extends ApiTypes> extends Base<ApiType> {
|
||||
return this.api.tx.contracts
|
||||
.putCode(compactAddLength(this.code))
|
||||
.withResultTransform((result: ISubmittableResult) =>
|
||||
new CodeSubmittableResult(result, applyOnEvent(result, 'CodeStored', (record: EventRecord) =>
|
||||
new CodeSubmittableResult(result, applyOnEvent(result, 'CodeStored', ([record]: EventRecord[]) =>
|
||||
new Blueprint<ApiType>(this.api, this.abi, record.event.data[0] as Hash, this._decorateMethod)
|
||||
))
|
||||
);
|
||||
|
||||
@@ -1,79 +1,167 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ApiTypes, DecorateMethod, ObsInnerType } from '@polkadot/api/types';
|
||||
import { ApiTypes, DecorateMethod } from '@polkadot/api/types';
|
||||
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
||||
import { AccountId, ContractExecResult } from '@polkadot/types/interfaces';
|
||||
import { AnyJson, CodecArg, IKeyringPair } from '@polkadot/types/types';
|
||||
import { AbiMessage, ContractCallOutcome } from '../types';
|
||||
import { AccountId, ContractExecResult, EventRecord } from '@polkadot/types/interfaces';
|
||||
import { AnyJson, CodecArg, ISubmittableResult, Registry } from '@polkadot/types/types';
|
||||
import { AbiMessage, ContractCallOutcome, DecodedEvent } from '../types';
|
||||
import { ContractRead, MapMessageExec, MapMessageRead } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { SubmittableResult } from '@polkadot/api';
|
||||
import ApiBase from '@polkadot/api/base';
|
||||
import { assert, isFunction } from '@polkadot/util';
|
||||
import { assert, bnToBn, isFunction, isUndefined, logger, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import Abi from '../Abi';
|
||||
import { encodeMessage, formatData } from '../util';
|
||||
import { applyOnEvent, formatData } from '../util';
|
||||
import Base from './Base';
|
||||
|
||||
type ContractCallTypes = 'tx' | 'rpc';
|
||||
const ERROR_NO_CALL = 'Your node does not expose the contracts.call RPC. This is most probably due to a runtime configuration.';
|
||||
const l = logger('Contract');
|
||||
|
||||
type ContractCallResultSubscription<ApiType extends ApiTypes, CallType extends ContractCallTypes> = ApiType extends 'rxjs'
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
? Observable<ContractCallResult<CallType>>
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
: Promise<ObsInnerType<ContractCallResult<CallType>>>;
|
||||
export class ContractSubmittableResult extends SubmittableResult {
|
||||
public readonly contractEvents?: DecodedEvent[];
|
||||
|
||||
export interface ContractRead<ApiType extends ApiTypes> {
|
||||
send (account: IKeyringPair | string | AccountId): ContractCallResultSubscription<ApiType, 'rpc'>;
|
||||
constructor (result: ISubmittableResult, contractEvents?: DecodedEvent[]) {
|
||||
super(result);
|
||||
|
||||
this.contractEvents = contractEvents;
|
||||
}
|
||||
}
|
||||
|
||||
export type ContractCallResult<CallType extends ContractCallTypes> = CallType extends 'rpc'
|
||||
? Observable<ContractCallOutcome>
|
||||
: Observable<SubmittableResult>;
|
||||
// map from a JSON result to current-style ContractExecResult
|
||||
function mapExecResult (registry: Registry, json: Record<string, AnyJson>): ContractExecResult {
|
||||
if (!Object.keys(json).some((key) => ['error', 'success'].includes(key))) {
|
||||
return registry.createType('ContractExecResult', json);
|
||||
}
|
||||
|
||||
const from = registry.createType('ContractExecResultTo260', json);
|
||||
|
||||
if (from.isSuccess) {
|
||||
const s = from.asSuccess;
|
||||
|
||||
return registry.createType('ContractExecResult', {
|
||||
gasConsumed: s.gasConsumed,
|
||||
result: {
|
||||
ok: {
|
||||
data: s.data,
|
||||
flags: s.flags
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// in the old format error has no additional information,
|
||||
// map it as-is with an "unknown" error
|
||||
return registry.createType('ContractExecResult', { result: { err: { other: 'unknown' } } });
|
||||
}
|
||||
|
||||
export default class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
|
||||
public readonly address: AccountId;
|
||||
|
||||
readonly #tx: MapMessageExec<ApiType> = {};
|
||||
|
||||
readonly #query: MapMessageRead<ApiType> = {};
|
||||
|
||||
constructor (api: ApiBase<ApiType>, abi: AnyJson | Abi, address: string | AccountId, decorateMethod: DecorateMethod<ApiType>) {
|
||||
super(api, abi, decorateMethod);
|
||||
|
||||
this.address = this.registry.createType('AccountId', address);
|
||||
|
||||
this.abi.messages.forEach((m): void => {
|
||||
const messageName = stringCamelCase(m.identifier);
|
||||
|
||||
if (isUndefined(this.#tx[messageName])) {
|
||||
this.#tx[messageName] = (value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]) =>
|
||||
this.#exec(m, value, gasLimit, params);
|
||||
}
|
||||
|
||||
if (isUndefined(this.#query[messageName])) {
|
||||
this.#query[messageName] = (origin: string | AccountId | Uint8Array, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]) =>
|
||||
this.#read(m, value, gasLimit, params).send(origin);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public get hasRpcContractsCall (): boolean {
|
||||
return isFunction(this.api.rx.rpc.contracts?.call);
|
||||
}
|
||||
|
||||
public exec (messageOrId: AbiMessage | string | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]): SubmittableExtrinsic<ApiType> {
|
||||
return this.api.tx.contracts.call(this.address, value, gasLimit, encodeMessage(this.registry, this.abi.findMessage(messageOrId), params));
|
||||
public get query (): MapMessageRead<ApiType> {
|
||||
assert(this.hasRpcContractsCall, ERROR_NO_CALL);
|
||||
|
||||
return this.#query;
|
||||
}
|
||||
|
||||
public read (messageOrId: AbiMessage | string | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]): ContractRead<ApiType> {
|
||||
assert(this.hasRpcContractsCall, 'Your node does not support contract RPC read calls');
|
||||
public get tx (): MapMessageExec<ApiType> {
|
||||
return this.#tx;
|
||||
}
|
||||
|
||||
#getGas = (_gasLimit: BigInt | BN | string | number): BN => {
|
||||
const gasLimit = bnToBn(_gasLimit);
|
||||
|
||||
return gasLimit.lten(0)
|
||||
? this.api.consts.system.maximumBlockWeight.muln(64).divn(100)
|
||||
: gasLimit;
|
||||
}
|
||||
|
||||
#exec = (messageOrId: AbiMessage | string | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, params: CodecArg[]): SubmittableExtrinsic<ApiType> => {
|
||||
return this.api.tx.contracts
|
||||
.call(this.address, value, this.#getGas(gasLimit), this.abi.findMessage(messageOrId).toU8a(params))
|
||||
.withResultTransform((result: ISubmittableResult) =>
|
||||
new ContractSubmittableResult(result, applyOnEvent(result, 'ContractExecution', (records: EventRecord[]) =>
|
||||
records
|
||||
.map(({ event: { data: [, data] } }): DecodedEvent | null => {
|
||||
try {
|
||||
return this.abi.decodeEvent(data.toU8a());
|
||||
} catch (error) {
|
||||
l.error(`Unable to decode contract event: ${(error as Error).message}`);
|
||||
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter((decoded): decoded is DecodedEvent => !!decoded)
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#read = (messageOrId: AbiMessage | string | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, params: CodecArg[]): ContractRead<ApiType> => {
|
||||
assert(this.hasRpcContractsCall, ERROR_NO_CALL);
|
||||
|
||||
const message = this.abi.findMessage(messageOrId);
|
||||
|
||||
return {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
send: this._decorateMethod((account: string | AccountId | Uint8Array): ContractCallResult<'rpc'> =>
|
||||
this.api.rx.rpc.contracts.call({
|
||||
send: this._decorateMethod((origin: string | AccountId | Uint8Array) =>
|
||||
this.api.rx.rpc.contracts.call.json({
|
||||
dest: this.address,
|
||||
gasLimit,
|
||||
inputData: encodeMessage(this.registry, message, params),
|
||||
origin: account,
|
||||
gasLimit: this.#getGas(gasLimit),
|
||||
inputData: message.toU8a(params),
|
||||
origin,
|
||||
value
|
||||
}).pipe(
|
||||
map((result: ContractExecResult): ContractCallOutcome => ({
|
||||
output: result.isSuccess && message.returnType
|
||||
? formatData(this.registry, result.asSuccess.data, message.returnType)
|
||||
}).pipe(map((json): ContractCallOutcome => {
|
||||
const { debugMessage, gasConsumed, result } = mapExecResult(this.registry, json.toJSON());
|
||||
|
||||
return {
|
||||
debugMessage,
|
||||
gasConsumed,
|
||||
output: result.isOk && message.returnType
|
||||
? formatData(this.registry, result.asOk.data, message.returnType)
|
||||
: null,
|
||||
result
|
||||
}))
|
||||
)
|
||||
};
|
||||
}))
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
public exec (messageOrId: AbiMessage | string | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]): SubmittableExtrinsic<ApiType> {
|
||||
return this.#exec(messageOrId, value, gasLimit, params);
|
||||
}
|
||||
|
||||
public read (messageOrId: AbiMessage | string | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]): ContractRead<ApiType> {
|
||||
return this.#read(messageOrId, value, gasLimit, params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/api authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { default as Blueprint } from './Blueprint';
|
||||
export { default as Code } from './Code';
|
||||
export { default as Blueprint, BlueprintSubmittableResult } from './Blueprint';
|
||||
export { default as Code, CodeSubmittableResult } from './Code';
|
||||
export { default as Contract } from './Contract';
|
||||
|
||||
31
packages/api-contract/src/base/types.ts
Normal file
31
packages/api-contract/src/base/types.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ApiTypes, ObsInnerType } from '@polkadot/api/types';
|
||||
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
|
||||
import { AccountId } from '@polkadot/types/interfaces';
|
||||
import { CodecArg } from '@polkadot/types/types';
|
||||
import { ContractCallOutcome } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
type ContractCallResult<ApiType extends ApiTypes, T> = ApiType extends 'rxjs'
|
||||
? Observable<Observable<T>>
|
||||
: Promise<ObsInnerType<Observable<T>>>;
|
||||
|
||||
export interface ContractRead<ApiType extends ApiTypes> {
|
||||
send (account: string | AccountId | Uint8Array): ContractCallResult<ApiType, ContractCallOutcome>;
|
||||
}
|
||||
|
||||
export interface MapConstructorExec<ApiType extends ApiTypes> {
|
||||
[message: string]: (endowment: BigInt | string | number | BN, gasLimit: BigInt | string | number | BN, ...params: CodecArg[]) => SubmittableExtrinsic<ApiType>;
|
||||
}
|
||||
|
||||
export interface MapMessageExec<ApiType extends ApiTypes> {
|
||||
[message: string]: (value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]) => SubmittableExtrinsic<ApiType>;
|
||||
}
|
||||
|
||||
export interface MapMessageRead<ApiType extends ApiTypes> {
|
||||
[message: string]: (origin: AccountId | string | Uint8Array, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, ...params: CodecArg[]) => ContractCallResult<ApiType, ContractCallOutcome>;
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { CodeSubmittableResult } from './base/Code';
|
||||
export { BlueprintSubmittableResult } from './base/Blueprint';
|
||||
|
||||
export { default as Abi } from './Abi';
|
||||
|
||||
export * from './rx';
|
||||
export * from './promise';
|
||||
export { Blueprint as BlueprintRx, Code as CodeRx, Contract as ContractRx } from './rx';
|
||||
export { Blueprint as BlueprintPromise, Code as CodePromise, Contract as ContractPromise } from './promise';
|
||||
|
||||
@@ -8,9 +8,9 @@ import { ApiPromise } from '@polkadot/api';
|
||||
import { decorateMethod } from '@polkadot/api/promise';
|
||||
|
||||
import Abi from '../Abi';
|
||||
import Blueprint from '../base/Blueprint';
|
||||
import BaseBlueprint from '../base/Blueprint';
|
||||
|
||||
export default class PromiseBlueprint extends Blueprint<'promise'> {
|
||||
export default class Blueprint extends BaseBlueprint<'promise'> {
|
||||
constructor (api: ApiPromise, abi: AnyJson | Abi, codeHash: string | Hash) {
|
||||
super(api, abi, codeHash, decorateMethod);
|
||||
}
|
||||
@@ -6,9 +6,9 @@ import { decorateMethod } from '@polkadot/api/promise';
|
||||
import { AnyJson } from '@polkadot/types/types';
|
||||
|
||||
import Abi from '../Abi';
|
||||
import Code from '../base/Code';
|
||||
import BaseCode from '../base/Code';
|
||||
|
||||
export default class PromiseCode extends Code<'promise'> {
|
||||
export default class Code extends BaseCode<'promise'> {
|
||||
constructor (api: ApiPromise, abi: AnyJson | Abi, wasm: Uint8Array | string | Buffer) {
|
||||
super(api, abi, wasm, decorateMethod);
|
||||
}
|
||||
@@ -8,9 +8,9 @@ import { ApiPromise } from '@polkadot/api';
|
||||
import { decorateMethod } from '@polkadot/api/promise';
|
||||
|
||||
import Abi from '../Abi';
|
||||
import Contract from '../base/Contract';
|
||||
import BaseContract from '../base/Contract';
|
||||
|
||||
export default class PromiseContract extends Contract<'promise'> {
|
||||
export default class Contract extends BaseContract<'promise'> {
|
||||
constructor (api: ApiPromise, abi: AnyJson | Abi, address: string | AccountId) {
|
||||
super(api, abi, address, decorateMethod);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { default as BlueprintPromise } from './PromiseBlueprint';
|
||||
export { default as CodePromise } from './PromiseCode';
|
||||
export { default as ContractPromise } from './PromiseContract';
|
||||
export { default as Blueprint } from './Blueprint';
|
||||
export { default as Code } from './Code';
|
||||
export { default as Contract } from './Contract';
|
||||
|
||||
7
packages/api-contract/src/promise/types.ts
Normal file
7
packages/api-contract/src/promise/types.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { BlueprintSubmittableResult as BaseBlueprintSubmittableResult, CodeSubmittableResult as BaseCodeSubmittableResult } from '../base';
|
||||
|
||||
export type BlueprintSubmittableResult = BaseBlueprintSubmittableResult<'promise'>;
|
||||
export type CodeSubmittableResult = BaseCodeSubmittableResult<'promise'>;
|
||||
@@ -8,9 +8,9 @@ import { ApiRx } from '@polkadot/api';
|
||||
import { decorateMethod } from '@polkadot/api/rx';
|
||||
|
||||
import Abi from '../Abi';
|
||||
import Blueprint from '../base/Blueprint';
|
||||
import BaseBlueprint from '../base/Blueprint';
|
||||
|
||||
export default class RxBlueprint extends Blueprint<'rxjs'> {
|
||||
export default class Blueprint extends BaseBlueprint<'rxjs'> {
|
||||
constructor (api: ApiRx, abi: AnyJson | Abi, codeHash: string | Hash) {
|
||||
super(api, abi, codeHash, decorateMethod);
|
||||
}
|
||||
@@ -7,9 +7,9 @@ import { ApiRx } from '@polkadot/api';
|
||||
import { decorateMethod } from '@polkadot/api/rx';
|
||||
|
||||
import Abi from '../Abi';
|
||||
import Code from '../base/Code';
|
||||
import BaseCode from '../base/Code';
|
||||
|
||||
export default class RxCode extends Code<'rxjs'> {
|
||||
export default class Code extends BaseCode<'rxjs'> {
|
||||
constructor (api: ApiRx, abi: AnyJson | Abi, wasm: Uint8Array | string | Buffer) {
|
||||
super(api, abi, wasm, decorateMethod);
|
||||
}
|
||||
@@ -8,9 +8,9 @@ import { ApiRx } from '@polkadot/api';
|
||||
import { decorateMethod } from '@polkadot/api/rx';
|
||||
|
||||
import Abi from '../Abi';
|
||||
import Contract from '../base/Contract';
|
||||
import BaseContract from '../base/Contract';
|
||||
|
||||
export default class RxContract extends Contract<'rxjs'> {
|
||||
export default class Contract extends BaseContract<'rxjs'> {
|
||||
constructor (api: ApiRx, abi: AnyJson | Abi, address: string | AccountId) {
|
||||
super(api, abi, address, decorateMethod);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { default as BlueprintRx } from './RxBlueprint';
|
||||
export { default as CodeRx } from './RxCode';
|
||||
export { default as ContractRx } from './RxContract';
|
||||
export { default as Blueprint } from './Blueprint';
|
||||
export { default as Code } from './Code';
|
||||
export { default as Contract } from './Contract';
|
||||
|
||||
7
packages/api-contract/src/rx/types.ts
Normal file
7
packages/api-contract/src/rx/types.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { BlueprintSubmittableResult as BaseBlueprintSubmittableResult, CodeSubmittableResult as BaseCodeSubmittableResult } from '../base';
|
||||
|
||||
export type BlueprintSubmittableResult = BaseBlueprintSubmittableResult<'promise'>;
|
||||
export type CodeSubmittableResult = BaseCodeSubmittableResult<'promise'>;
|
||||
@@ -2,10 +2,12 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ApiTypes } from '@polkadot/api/types';
|
||||
import { ContractExecResult, InkSelector } from '@polkadot/types/interfaces';
|
||||
import { Codec, TypeDef } from '@polkadot/types/types';
|
||||
import { ContractExecResultResult, ContractSelector } from '@polkadot/types/interfaces';
|
||||
import { Codec, CodecArg, TypeDef } from '@polkadot/types/types';
|
||||
|
||||
import ApiBase from '@polkadot/api/base';
|
||||
import { Text, u64 } from '@polkadot/types';
|
||||
|
||||
import Abi from './Abi';
|
||||
|
||||
export interface ContractBase<ApiType extends ApiTypes> {
|
||||
@@ -16,21 +18,31 @@ export interface ContractBase<ApiType extends ApiTypes> {
|
||||
messages: AbiMessage[];
|
||||
}
|
||||
|
||||
export interface AbiMessageParam {
|
||||
export interface AbiParam {
|
||||
name: string;
|
||||
type: TypeDef;
|
||||
}
|
||||
|
||||
export interface AbiMessage {
|
||||
args: AbiMessageParam[];
|
||||
export interface AbiEvent {
|
||||
args: AbiParam[];
|
||||
docs: string[];
|
||||
fromU8a: (data: Uint8Array) => DecodedEvent;
|
||||
identifier: string;
|
||||
index: number;
|
||||
}
|
||||
|
||||
export interface AbiMessage {
|
||||
args: AbiParam[];
|
||||
docs: string[];
|
||||
fromU8a: (data: Uint8Array) => DecodedMessage;
|
||||
identifier: string;
|
||||
index: number;
|
||||
isConstructor?: boolean;
|
||||
isMutating?: boolean;
|
||||
isPayable?: boolean;
|
||||
returnType?: TypeDef | null;
|
||||
selector: InkSelector;
|
||||
selector: ContractSelector;
|
||||
toU8a: (params: CodecArg[]) => Uint8Array;
|
||||
}
|
||||
|
||||
export type AbiConstructor = AbiMessage;
|
||||
@@ -41,6 +53,18 @@ export interface InterfaceContractCalls {
|
||||
}
|
||||
|
||||
export interface ContractCallOutcome {
|
||||
debugMessage: Text;
|
||||
gasConsumed: u64;
|
||||
output: Codec | null;
|
||||
result: ContractExecResult;
|
||||
result: ContractExecResultResult;
|
||||
}
|
||||
|
||||
export interface DecodedEvent {
|
||||
args: Codec[];
|
||||
event: AbiEvent;
|
||||
}
|
||||
|
||||
export interface DecodedMessage {
|
||||
args: Codec[];
|
||||
message: AbiMessage;
|
||||
}
|
||||
|
||||
@@ -3,44 +3,24 @@
|
||||
|
||||
import { SubmittableResult } from '@polkadot/api';
|
||||
import { EventRecord } from '@polkadot/types/interfaces';
|
||||
import { Codec, CodecArg, Registry, TypeDef } from '@polkadot/types/types';
|
||||
import { AbiConstructor, AbiMessage } from './types';
|
||||
import { Codec, Registry, TypeDef } from '@polkadot/types/types';
|
||||
|
||||
import { Raw, createClass, createTypeUnsafe, encodeTypeDef } from '@polkadot/types';
|
||||
import { Raw, createTypeUnsafe } from '@polkadot/types';
|
||||
|
||||
import { assert, compactAddLength } from '@polkadot/util';
|
||||
type ContractEvents = 'CodeStored' | 'ContractExecution' | 'Instantiated';
|
||||
|
||||
export function formatData (registry: Registry, data: Raw, { type }: TypeDef): Codec {
|
||||
return createTypeUnsafe(registry, type, [data], true);
|
||||
}
|
||||
|
||||
export function applyOnEvent <T> (result: SubmittableResult, type: 'CodeStored' | 'Instantiated', fn: (record: EventRecord) => T): T | undefined {
|
||||
export function applyOnEvent <T> (result: SubmittableResult, type: ContractEvents, fn: (records: EventRecord[]) => T): T | undefined {
|
||||
if (result.isInBlock || result.isFinalized) {
|
||||
const record = result.findRecord('contracts', type);
|
||||
const records = result.filterRecords('contracts', type);
|
||||
|
||||
if (record) {
|
||||
return fn(record);
|
||||
if (records.length) {
|
||||
return fn(records);
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function encodeMessage (registry: Registry, message: AbiMessage | AbiConstructor, params: CodecArg[]): Uint8Array {
|
||||
assert(message, 'Attempted to call an invalid contract message');
|
||||
assert(params.length === message.args.length, `Expected ${message.args.length} arguments to contract message '${message.identifier}', found ${params.length}`);
|
||||
|
||||
const Clazz = createClass(registry, JSON.stringify(
|
||||
message.args.reduce((r: Record<string, any>, { name, type }): Record<string, any> => {
|
||||
r[name] = type.displayName || encodeTypeDef(type);
|
||||
|
||||
return r;
|
||||
}, { __selector: 'InkSelector' })
|
||||
));
|
||||
|
||||
return compactAddLength(new Clazz(registry, message.args.reduce((r: Record<string, CodecArg>, { name }, index): Record<string, CodecArg> => {
|
||||
r[name] = params[index];
|
||||
|
||||
return r;
|
||||
}, { __selector: message.selector })).toU8a());
|
||||
}
|
||||
|
||||
28
packages/api-contract/test/compare/assetTransfer.test.json
Normal file
28
packages/api-contract/test/compare/assetTransfer.test.json
Normal file
@@ -0,0 +1,28 @@
|
||||
[
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u256"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u8"
|
||||
},
|
||||
{
|
||||
"info": 12,
|
||||
"length": 32,
|
||||
"sub": {
|
||||
"info": 6,
|
||||
"type": "u8"
|
||||
},
|
||||
"type": "[u8;32]"
|
||||
},
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "Text"
|
||||
}
|
||||
]
|
||||
31
packages/api-contract/test/compare/delegator.test.json
Normal file
31
packages/api-contract/test/compare/delegator.test.json
Normal file
@@ -0,0 +1,31 @@
|
||||
[
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
},
|
||||
{
|
||||
"info": 12,
|
||||
"length": 32,
|
||||
"sub": {
|
||||
"info": 6,
|
||||
"type": "u8"
|
||||
},
|
||||
"type": "[u8;32]"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u8"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "i32"
|
||||
},
|
||||
{
|
||||
"displayName": "Hash",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "Hash"
|
||||
}
|
||||
]
|
||||
6
packages/api-contract/test/compare/ints256.test.json
Normal file
6
packages/api-contract/test/compare/ints256.test.json
Normal file
@@ -0,0 +1,6 @@
|
||||
[
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u256"
|
||||
}
|
||||
]
|
||||
@@ -488,7 +488,8 @@
|
||||
{
|
||||
"info": 10,
|
||||
"sub": [],
|
||||
"name": "Confirmed"
|
||||
"name": "Confirmed",
|
||||
"type": "()"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
@@ -509,7 +510,8 @@
|
||||
{
|
||||
"info": 10,
|
||||
"sub": [],
|
||||
"name": "Confirmed"
|
||||
"name": "Confirmed",
|
||||
"type": "()"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
@@ -677,4 +679,4 @@
|
||||
},
|
||||
"type": "Option<Vec<u8>>"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
247
packages/api-contract/test/compare/withString.test.json
Normal file
247
packages/api-contract/test/compare/withString.test.json
Normal file
@@ -0,0 +1,247 @@
|
||||
[
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u128"
|
||||
},
|
||||
{
|
||||
"displayName": "Header",
|
||||
"namespace": "ink_storage::collections::stash",
|
||||
"info": 9,
|
||||
"sub": [
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32",
|
||||
"name": "last_vacant"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32",
|
||||
"name": "len"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32",
|
||||
"name": "len_entries"
|
||||
}
|
||||
],
|
||||
"type": "{\"last_vacant\":\"u32\",\"len\":\"u32\",\"len_entries\":\"u32\"}"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32"
|
||||
},
|
||||
{
|
||||
"displayName": "Entry",
|
||||
"namespace": "ink_storage::collections::stash",
|
||||
"params": [
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
}
|
||||
],
|
||||
"info": 3,
|
||||
"sub": [
|
||||
{
|
||||
"displayName": "VacantEntry",
|
||||
"namespace": "ink_storage::collections::stash",
|
||||
"info": 9,
|
||||
"sub": [
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32",
|
||||
"name": "next"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32",
|
||||
"name": "prev"
|
||||
}
|
||||
],
|
||||
"type": "{\"next\":\"u32\",\"prev\":\"u32\"}",
|
||||
"name": "Vacant"
|
||||
},
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId",
|
||||
"name": "Occupied"
|
||||
}
|
||||
],
|
||||
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"AccountId\"}}"
|
||||
},
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
},
|
||||
{
|
||||
"info": 12,
|
||||
"length": 32,
|
||||
"sub": {
|
||||
"info": 6,
|
||||
"type": "u8"
|
||||
},
|
||||
"type": "[u8;32]"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u8"
|
||||
},
|
||||
{
|
||||
"displayName": "VacantEntry",
|
||||
"namespace": "ink_storage::collections::stash",
|
||||
"info": 9,
|
||||
"sub": [
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32",
|
||||
"name": "next"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32",
|
||||
"name": "prev"
|
||||
}
|
||||
],
|
||||
"type": "{\"next\":\"u32\",\"prev\":\"u32\"}"
|
||||
},
|
||||
{
|
||||
"displayName": "ValueEntry",
|
||||
"namespace": "ink_storage::collections::hashmap",
|
||||
"params": [
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u128"
|
||||
}
|
||||
],
|
||||
"info": 9,
|
||||
"sub": [
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u128",
|
||||
"name": "value"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32",
|
||||
"name": "key_index"
|
||||
}
|
||||
],
|
||||
"type": "{\"value\":\"u128\",\"key_index\":\"u32\"}"
|
||||
},
|
||||
{
|
||||
"displayName": "Entry",
|
||||
"namespace": "ink_storage::collections::stash",
|
||||
"params": [
|
||||
{
|
||||
"info": 10,
|
||||
"sub": [
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
},
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
}
|
||||
],
|
||||
"type": "(AccountId, AccountId)"
|
||||
}
|
||||
],
|
||||
"info": 3,
|
||||
"sub": [
|
||||
{
|
||||
"displayName": "VacantEntry",
|
||||
"namespace": "ink_storage::collections::stash",
|
||||
"info": 9,
|
||||
"sub": [
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32",
|
||||
"name": "next"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "u32",
|
||||
"name": "prev"
|
||||
}
|
||||
],
|
||||
"type": "{\"next\":\"u32\",\"prev\":\"u32\"}",
|
||||
"name": "Vacant"
|
||||
},
|
||||
{
|
||||
"info": 10,
|
||||
"sub": [
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
},
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
}
|
||||
],
|
||||
"type": "(AccountId, AccountId)",
|
||||
"name": "Occupied"
|
||||
}
|
||||
],
|
||||
"type": "{\"_enum\":{\"Vacant\":\"VacantEntry\",\"Occupied\":\"(AccountId, AccountId)\"}}"
|
||||
},
|
||||
{
|
||||
"info": 10,
|
||||
"sub": [
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
},
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
}
|
||||
],
|
||||
"type": "(AccountId, AccountId)"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "Text"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
"type": "bool"
|
||||
},
|
||||
{
|
||||
"displayName": "Option",
|
||||
"params": [
|
||||
{
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
}
|
||||
],
|
||||
"info": 5,
|
||||
"sub": {
|
||||
"displayName": "AccountId",
|
||||
"namespace": "ink_env::types",
|
||||
"info": 6,
|
||||
"type": "AccountId"
|
||||
},
|
||||
"type": "Option<AccountId>"
|
||||
}
|
||||
]
|
||||
@@ -1,10 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { default as delegator } from './delegator.json';
|
||||
export { default as dns } from './dns.json';
|
||||
export { default as erc20 } from './erc20.json';
|
||||
export { default as erc721 } from './erc721.json';
|
||||
export { default as flipper } from './flipper.json';
|
||||
export { default as incrementer } from './incrementer.json';
|
||||
export { default as multisigPlain } from './multisig_plain.json';
|
||||
export * from './ink';
|
||||
export * from './solang';
|
||||
export * from './user';
|
||||
|
||||
10
packages/api-contract/test/contracts/ink/index.ts
Normal file
10
packages/api-contract/test/contracts/ink/index.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { default as delegator } from './delegator.json';
|
||||
export { default as dns } from './dns.json';
|
||||
export { default as erc20 } from './erc20.json';
|
||||
export { default as erc721 } from './erc721.json';
|
||||
export { default as flipper } from './flipper.json';
|
||||
export { default as incrementer } from './incrementer.json';
|
||||
export { default as multisigPlain } from './multisig_plain.json';
|
||||
4
packages/api-contract/test/contracts/solang/index.ts
Normal file
4
packages/api-contract/test/contracts/solang/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { default as ints256 } from './ints256.json';
|
||||
113
packages/api-contract/test/contracts/solang/ints256.json
Normal file
113
packages/api-contract/test/contracts/solang/ints256.json
Normal file
@@ -0,0 +1,113 @@
|
||||
{
|
||||
"contract": {
|
||||
"authors": [
|
||||
"Sean Young <sean@mess.org>"
|
||||
],
|
||||
"description": "Test 256 bits types",
|
||||
"name": "ints256",
|
||||
"version": "0.0.1"
|
||||
},
|
||||
"metadataVersion": "0.1.0",
|
||||
"source": {
|
||||
"compiler": "solang 0.1.4",
|
||||
"hash": "0xa85fb5c61a09d71dce84c15a8bd87e6f4eafac498a1aec3869e8cf7ea4389697",
|
||||
"language": "Solidity 0.1.4"
|
||||
},
|
||||
"spec": {
|
||||
"constructors": [
|
||||
{
|
||||
"args": [],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"name": "new",
|
||||
"selector": "0x861731d5"
|
||||
}
|
||||
],
|
||||
"events": [],
|
||||
"messages": [
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "a",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"u256"
|
||||
],
|
||||
"type": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "b",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"u256"
|
||||
],
|
||||
"type": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [
|
||||
"Multiply two 256 bit values\n\n"
|
||||
],
|
||||
"mutates": false,
|
||||
"name": "multiply",
|
||||
"payable": false,
|
||||
"return_type": {
|
||||
"display_name": [
|
||||
"u256"
|
||||
],
|
||||
"type": 1
|
||||
},
|
||||
"selector": "0x165c4a16"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "a",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"u256"
|
||||
],
|
||||
"type": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "b",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"u256"
|
||||
],
|
||||
"type": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [
|
||||
"Add two 256 bit values\n\n"
|
||||
],
|
||||
"mutates": false,
|
||||
"name": "add",
|
||||
"payable": false,
|
||||
"return_type": {
|
||||
"display_name": [
|
||||
"u256"
|
||||
],
|
||||
"type": 1
|
||||
},
|
||||
"selector": "0x771602f7"
|
||||
}
|
||||
]
|
||||
},
|
||||
"storage": {
|
||||
"struct": {
|
||||
"fields": []
|
||||
}
|
||||
},
|
||||
"types": [
|
||||
{
|
||||
"def": {
|
||||
"primitive": "u256"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
13
packages/api-contract/test/contracts/solang/ints256.sol
Normal file
13
packages/api-contract/test/contracts/solang/ints256.sol
Normal file
@@ -0,0 +1,13 @@
|
||||
/// @title Test 256 bits types
|
||||
/// @author Sean Young <sean@mess.org>
|
||||
contract ints256 {
|
||||
/// Multiply two 256 bit values
|
||||
function multiply(uint256 a, uint256 b) public pure returns (uint256) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
/// Add two 256 bit values
|
||||
function add(uint256 a, uint256 b) public pure returns (uint256) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
BIN
packages/api-contract/test/contracts/solang/ints256.wasm
Normal file
BIN
packages/api-contract/test/contracts/solang/ints256.wasm
Normal file
Binary file not shown.
299
packages/api-contract/test/contracts/user/assetTransfer.json
Normal file
299
packages/api-contract/test/contracts/user/assetTransfer.json
Normal file
@@ -0,0 +1,299 @@
|
||||
{
|
||||
"contract": {
|
||||
"authors": [
|
||||
"unknown"
|
||||
],
|
||||
"name": "AssetTransfer",
|
||||
"version": "0.0.1"
|
||||
},
|
||||
"metadataVersion": "0.1.0",
|
||||
"source": {
|
||||
"compiler": "solang 0.1.4",
|
||||
"hash": "0x7d163f445bec3d33e90d0f60bda40acd42f2760dbc1b731616af70a6001e05c7",
|
||||
"language": "Solidity 0.1.4"
|
||||
},
|
||||
"spec": {
|
||||
"constructors": [
|
||||
{
|
||||
"args": [],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"name": "new",
|
||||
"selector": "0x861731d5"
|
||||
}
|
||||
],
|
||||
"events": [
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_newUser",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 4
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"name": "successfulRegistration"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "sender",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_hexAsset",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"String"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"name": "addedAsset"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "sender",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "receiver",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "hexOfAsset",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"String"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"name": "assetSent"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "_user",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 4
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"mutates": false,
|
||||
"name": "viewBalance",
|
||||
"payable": false,
|
||||
"return_type": {
|
||||
"display_name": [
|
||||
"u256"
|
||||
],
|
||||
"type": 1
|
||||
},
|
||||
"selector": "0xc1a13d1a"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "_assetHex",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"String"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"mutates": false,
|
||||
"name": "findOwner",
|
||||
"payable": false,
|
||||
"return_type": {
|
||||
"display_name": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 4
|
||||
},
|
||||
"selector": "0x63560a8e"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "_hexAsset",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"String"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"mutates": true,
|
||||
"name": "addAsset",
|
||||
"payable": false,
|
||||
"return_type": null,
|
||||
"selector": "0xa7c6b52e"
|
||||
},
|
||||
{
|
||||
"args": [],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"mutates": true,
|
||||
"name": "receiveAsset",
|
||||
"payable": false,
|
||||
"return_type": {
|
||||
"display_name": [
|
||||
"String"
|
||||
],
|
||||
"type": 5
|
||||
},
|
||||
"selector": "0xb2f3b5bc"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "_assetHex",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"String"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "_receiver",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 4
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"mutates": true,
|
||||
"name": "sendAsset",
|
||||
"payable": false,
|
||||
"return_type": null,
|
||||
"selector": "0xd6e5288f"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "_newUser",
|
||||
"type": {
|
||||
"display_name": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 4
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [
|
||||
""
|
||||
],
|
||||
"mutates": true,
|
||||
"name": "register",
|
||||
"payable": false,
|
||||
"return_type": null,
|
||||
"selector": "0x4420e486"
|
||||
}
|
||||
]
|
||||
},
|
||||
"storage": {
|
||||
"struct": {
|
||||
"fields": []
|
||||
}
|
||||
},
|
||||
"types": [
|
||||
{
|
||||
"def": {
|
||||
"primitive": "u256"
|
||||
}
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"primitive": "u8"
|
||||
}
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"array": {
|
||||
"len": 32,
|
||||
"type": 2
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"composite": {
|
||||
"fields": [
|
||||
{
|
||||
"type": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"path": [
|
||||
"AccountId"
|
||||
]
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"primitive": "str"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
BIN
packages/api-contract/test/contracts/user/assetTransfer.wasm
Normal file
BIN
packages/api-contract/test/contracts/user/assetTransfer.wasm
Normal file
Binary file not shown.
5
packages/api-contract/test/contracts/user/index.ts
Normal file
5
packages/api-contract/test/contracts/user/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { default as assetTransfer } from './assetTransfer.json';
|
||||
export { default as withString } from './withString.json';
|
||||
777
packages/api-contract/test/contracts/user/withString.json
Normal file
777
packages/api-contract/test/contracts/user/withString.json
Normal file
@@ -0,0 +1,777 @@
|
||||
{
|
||||
"metadataVersion": "0.1.0",
|
||||
"source": {
|
||||
"hash": "0xafcd0acf1a747cca1febe5bad81a6c0244a381ceb02635ab1069a1a396adda3f",
|
||||
"language": "ink! 3.0.0-rc1",
|
||||
"compiler": "rustc 1.49.0-nightly"
|
||||
},
|
||||
"contract": {
|
||||
"name": "erc20",
|
||||
"version": "0.1.0",
|
||||
"authors": [
|
||||
"[your_name] <[your_email]>"
|
||||
]
|
||||
},
|
||||
"spec": {
|
||||
"constructors": [
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "initial_supply",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"Balance"
|
||||
],
|
||||
"type": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "_name",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"String"
|
||||
],
|
||||
"type": 12
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "_symbol",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"String"
|
||||
],
|
||||
"type": 12
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [],
|
||||
"name": [
|
||||
"new"
|
||||
],
|
||||
"selector": "0xd183512b"
|
||||
}
|
||||
],
|
||||
"docs": [],
|
||||
"events": [
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"docs": [],
|
||||
"indexed": true,
|
||||
"name": "from",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"Option"
|
||||
],
|
||||
"type": 14
|
||||
}
|
||||
},
|
||||
{
|
||||
"docs": [],
|
||||
"indexed": true,
|
||||
"name": "to",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"Option"
|
||||
],
|
||||
"type": 14
|
||||
}
|
||||
},
|
||||
{
|
||||
"docs": [],
|
||||
"indexed": true,
|
||||
"name": "value",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"Balance"
|
||||
],
|
||||
"type": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [],
|
||||
"name": "Transfer"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"docs": [],
|
||||
"indexed": true,
|
||||
"name": "owner",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"docs": [],
|
||||
"indexed": true,
|
||||
"name": "spender",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"docs": [],
|
||||
"indexed": true,
|
||||
"name": "value",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"Balance"
|
||||
],
|
||||
"type": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [],
|
||||
"name": "Approval"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"args": [],
|
||||
"docs": [],
|
||||
"mutates": false,
|
||||
"name": [
|
||||
"total_supply"
|
||||
],
|
||||
"payable": false,
|
||||
"returnType": {
|
||||
"displayName": [
|
||||
"Balance"
|
||||
],
|
||||
"type": 1
|
||||
},
|
||||
"selector": "0xdcb736b5"
|
||||
},
|
||||
{
|
||||
"args": [],
|
||||
"docs": [],
|
||||
"mutates": false,
|
||||
"name": [
|
||||
"name"
|
||||
],
|
||||
"payable": false,
|
||||
"returnType": {
|
||||
"displayName": [
|
||||
"String"
|
||||
],
|
||||
"type": 12
|
||||
},
|
||||
"selector": "0xa0a95494"
|
||||
},
|
||||
{
|
||||
"args": [],
|
||||
"docs": [],
|
||||
"mutates": false,
|
||||
"name": [
|
||||
"symbol"
|
||||
],
|
||||
"payable": false,
|
||||
"returnType": {
|
||||
"displayName": [
|
||||
"String"
|
||||
],
|
||||
"type": 12
|
||||
},
|
||||
"selector": "0x57178a4a"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "owner",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [],
|
||||
"mutates": false,
|
||||
"name": [
|
||||
"balance_of"
|
||||
],
|
||||
"payable": false,
|
||||
"returnType": {
|
||||
"displayName": [
|
||||
"Balance"
|
||||
],
|
||||
"type": 1
|
||||
},
|
||||
"selector": "0x56e929b2"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "owner",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spender",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [],
|
||||
"mutates": false,
|
||||
"name": [
|
||||
"allowance"
|
||||
],
|
||||
"payable": false,
|
||||
"returnType": {
|
||||
"displayName": [
|
||||
"Balance"
|
||||
],
|
||||
"type": 1
|
||||
},
|
||||
"selector": "0xf3cfff66"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "to",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "value",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"Balance"
|
||||
],
|
||||
"type": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [],
|
||||
"mutates": true,
|
||||
"name": [
|
||||
"transfer"
|
||||
],
|
||||
"payable": false,
|
||||
"returnType": {
|
||||
"displayName": [
|
||||
"bool"
|
||||
],
|
||||
"type": 13
|
||||
},
|
||||
"selector": "0xfae3a09d"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "spender",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "value",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"Balance"
|
||||
],
|
||||
"type": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [],
|
||||
"mutates": true,
|
||||
"name": [
|
||||
"approve"
|
||||
],
|
||||
"payable": false,
|
||||
"returnType": {
|
||||
"displayName": [
|
||||
"bool"
|
||||
],
|
||||
"type": 13
|
||||
},
|
||||
"selector": "0x03d0e114"
|
||||
},
|
||||
{
|
||||
"args": [
|
||||
{
|
||||
"name": "from",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "to",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"AccountId"
|
||||
],
|
||||
"type": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "value",
|
||||
"type": {
|
||||
"displayName": [
|
||||
"Balance"
|
||||
],
|
||||
"type": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"docs": [],
|
||||
"mutates": true,
|
||||
"name": [
|
||||
"transfer_from"
|
||||
],
|
||||
"payable": false,
|
||||
"returnType": {
|
||||
"displayName": [
|
||||
"bool"
|
||||
],
|
||||
"type": 13
|
||||
},
|
||||
"selector": "0xfcfb2ccd"
|
||||
}
|
||||
]
|
||||
},
|
||||
"storage": {
|
||||
"struct": {
|
||||
"fields": [
|
||||
{
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0000000000000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 1
|
||||
}
|
||||
},
|
||||
"name": "total_supply"
|
||||
},
|
||||
{
|
||||
"layout": {
|
||||
"struct": {
|
||||
"fields": [
|
||||
{
|
||||
"layout": {
|
||||
"struct": {
|
||||
"fields": [
|
||||
{
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0100000000000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 2
|
||||
}
|
||||
},
|
||||
"name": "header"
|
||||
},
|
||||
{
|
||||
"layout": {
|
||||
"struct": {
|
||||
"fields": [
|
||||
{
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0200000000000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 3
|
||||
}
|
||||
},
|
||||
"name": "len"
|
||||
},
|
||||
{
|
||||
"layout": {
|
||||
"array": {
|
||||
"cellsPerElem": 1,
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0200000001000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 4
|
||||
}
|
||||
},
|
||||
"len": 4294967295,
|
||||
"offset": "0x0300000000000000000000000000000000000000000000000000000000000000"
|
||||
}
|
||||
},
|
||||
"name": "elems"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"name": "entries"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"name": "keys"
|
||||
},
|
||||
{
|
||||
"layout": {
|
||||
"hash": {
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0300000001000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 9
|
||||
}
|
||||
},
|
||||
"offset": "0x0200000001000000000000000000000000000000000000000000000000000000",
|
||||
"strategy": {
|
||||
"hasher": "Blake2x256",
|
||||
"postfix": "",
|
||||
"prefix": "0x696e6b20686173686d6170"
|
||||
}
|
||||
}
|
||||
},
|
||||
"name": "values"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"name": "balances"
|
||||
},
|
||||
{
|
||||
"layout": {
|
||||
"struct": {
|
||||
"fields": [
|
||||
{
|
||||
"layout": {
|
||||
"struct": {
|
||||
"fields": [
|
||||
{
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0300000001000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 2
|
||||
}
|
||||
},
|
||||
"name": "header"
|
||||
},
|
||||
{
|
||||
"layout": {
|
||||
"struct": {
|
||||
"fields": [
|
||||
{
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0400000001000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 3
|
||||
}
|
||||
},
|
||||
"name": "len"
|
||||
},
|
||||
{
|
||||
"layout": {
|
||||
"array": {
|
||||
"cellsPerElem": 1,
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0400000002000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 10
|
||||
}
|
||||
},
|
||||
"len": 4294967295,
|
||||
"offset": "0x0500000001000000000000000000000000000000000000000000000000000000"
|
||||
}
|
||||
},
|
||||
"name": "elems"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"name": "entries"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"name": "keys"
|
||||
},
|
||||
{
|
||||
"layout": {
|
||||
"hash": {
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0500000002000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 9
|
||||
}
|
||||
},
|
||||
"offset": "0x0400000002000000000000000000000000000000000000000000000000000000",
|
||||
"strategy": {
|
||||
"hasher": "Blake2x256",
|
||||
"postfix": "",
|
||||
"prefix": "0x696e6b20686173686d6170"
|
||||
}
|
||||
}
|
||||
},
|
||||
"name": "values"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"name": "allowances"
|
||||
},
|
||||
{
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0500000002000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 12
|
||||
}
|
||||
},
|
||||
"name": "name"
|
||||
},
|
||||
{
|
||||
"layout": {
|
||||
"cell": {
|
||||
"key": "0x0600000002000000000000000000000000000000000000000000000000000000",
|
||||
"ty": 12
|
||||
}
|
||||
},
|
||||
"name": "symbol"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"types": [
|
||||
{
|
||||
"def": {
|
||||
"primitive": "u128"
|
||||
}
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"composite": {
|
||||
"fields": [
|
||||
{
|
||||
"name": "last_vacant",
|
||||
"type": 3
|
||||
},
|
||||
{
|
||||
"name": "len",
|
||||
"type": 3
|
||||
},
|
||||
{
|
||||
"name": "len_entries",
|
||||
"type": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"path": [
|
||||
"ink_storage",
|
||||
"collections",
|
||||
"stash",
|
||||
"Header"
|
||||
]
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"primitive": "u32"
|
||||
}
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"variant": {
|
||||
"variants": [
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": 8
|
||||
}
|
||||
],
|
||||
"name": "Vacant"
|
||||
},
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": 5
|
||||
}
|
||||
],
|
||||
"name": "Occupied"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
5
|
||||
],
|
||||
"path": [
|
||||
"ink_storage",
|
||||
"collections",
|
||||
"stash",
|
||||
"Entry"
|
||||
]
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"composite": {
|
||||
"fields": [
|
||||
{
|
||||
"type": 6
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"path": [
|
||||
"ink_env",
|
||||
"types",
|
||||
"AccountId"
|
||||
]
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"array": {
|
||||
"len": 32,
|
||||
"type": 7
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"primitive": "u8"
|
||||
}
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"composite": {
|
||||
"fields": [
|
||||
{
|
||||
"name": "next",
|
||||
"type": 3
|
||||
},
|
||||
{
|
||||
"name": "prev",
|
||||
"type": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"path": [
|
||||
"ink_storage",
|
||||
"collections",
|
||||
"stash",
|
||||
"VacantEntry"
|
||||
]
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"composite": {
|
||||
"fields": [
|
||||
{
|
||||
"name": "value",
|
||||
"type": 1
|
||||
},
|
||||
{
|
||||
"name": "key_index",
|
||||
"type": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
1
|
||||
],
|
||||
"path": [
|
||||
"ink_storage",
|
||||
"collections",
|
||||
"hashmap",
|
||||
"ValueEntry"
|
||||
]
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"variant": {
|
||||
"variants": [
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": 8
|
||||
}
|
||||
],
|
||||
"name": "Vacant"
|
||||
},
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": 11
|
||||
}
|
||||
],
|
||||
"name": "Occupied"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
11
|
||||
],
|
||||
"path": [
|
||||
"ink_storage",
|
||||
"collections",
|
||||
"stash",
|
||||
"Entry"
|
||||
]
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"tuple": [
|
||||
5,
|
||||
5
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"primitive": "str"
|
||||
}
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"primitive": "bool"
|
||||
}
|
||||
},
|
||||
{
|
||||
"def": {
|
||||
"variant": {
|
||||
"variants": [
|
||||
{
|
||||
"name": "None"
|
||||
},
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": 5
|
||||
}
|
||||
],
|
||||
"name": "Some"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
5
|
||||
],
|
||||
"path": [
|
||||
"Option"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/api-derive",
|
||||
"version": "2.2.1",
|
||||
"version": "2.5.1",
|
||||
"description": "Common functions used across Polkadot, derived from RPC calls and storage queries.",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -27,18 +27,18 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-derive#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/api": "2.2.1",
|
||||
"@polkadot/rpc-core": "2.2.1",
|
||||
"@polkadot/rpc-provider": "2.2.1",
|
||||
"@polkadot/types": "2.2.1",
|
||||
"@polkadot/util": "^3.5.1",
|
||||
"@polkadot/util-crypto": "^3.5.1",
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/api": "^2.5.1",
|
||||
"@polkadot/rpc-core": "^2.5.1",
|
||||
"@polkadot/rpc-provider": "^2.5.1",
|
||||
"@polkadot/types": "^2.5.1",
|
||||
"@polkadot/util": "^3.7.1",
|
||||
"@polkadot/util-crypto": "^3.7.1",
|
||||
"bn.js": "^5.1.3",
|
||||
"memoizee": "^0.4.14",
|
||||
"rxjs": "^6.6.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/keyring": "^3.5.1"
|
||||
"@polkadot/keyring": "^3.7.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { isU8a, assert } from '@polkadot/util';
|
||||
import { isU8a, assertReturn } from '@polkadot/util';
|
||||
import { decodeAddress } from '@polkadot/util-crypto';
|
||||
|
||||
import { memo } from '../util';
|
||||
@@ -23,11 +23,7 @@ function retrieve (api: ApiInterfaceRx, address: Address | AccountId | AccountIn
|
||||
const accountIndex = api.registry.createType('AccountIndex', decoded);
|
||||
|
||||
return api.derive.accounts.indexToId(accountIndex.toString()).pipe(
|
||||
map((accountId) => {
|
||||
assert(accountId, 'Unable to retrieve accountId');
|
||||
|
||||
return accountId;
|
||||
})
|
||||
map((accountId) => assertReturn(accountId, 'Unable to retrieve accountId'))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,9 +20,17 @@ type FlagsIntermediate = [
|
||||
AccountId | undefined
|
||||
];
|
||||
|
||||
function isIncludedFn (accountId: AccountId | Address | string): (_: AccountId | Address |string) => boolean {
|
||||
return function (id: AccountId | Address | string): boolean {
|
||||
return id.toString() === accountId.toString();
|
||||
function parseFlags (address: AccountId | Address | string | null | undefined, [electionsMembers, councilMembers, technicalCommitteeMembers, societyMembers, sudoKey]: FlagsIntermediate): DeriveAccountFlags {
|
||||
const isIncluded = (id: AccountId | Address | string) =>
|
||||
address
|
||||
? id.toString() === address.toString()
|
||||
: false;
|
||||
|
||||
return {
|
||||
isCouncil: (electionsMembers?.map(([id]: ITuple<[AccountId, Balance]>) => id) || councilMembers || []).some(isIncluded),
|
||||
isSociety: (societyMembers || []).some(isIncluded),
|
||||
isSudo: sudoKey?.toString() === address?.toString(),
|
||||
isTechCommittee: (technicalCommitteeMembers || []).some(isIncluded)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -34,35 +42,24 @@ export function flags (instanceId: string, api: ApiInterfaceRx): (address?: Acco
|
||||
return memo(instanceId, (address?: AccountId | Address | string | null): Observable<DeriveAccountFlags> => {
|
||||
const councilSection = api.query.electionsPhragmen ? 'electionsPhragmen' : 'elections';
|
||||
|
||||
return (
|
||||
combineLatest([
|
||||
address && api.query[councilSection]?.members
|
||||
? api.query[councilSection].members()
|
||||
: of(undefined),
|
||||
address && api.query.council?.members
|
||||
? api.query.council.members()
|
||||
: of([]),
|
||||
address && api.query.technicalCommittee?.members
|
||||
? api.query.technicalCommittee.members()
|
||||
: of([]),
|
||||
address && api.query.society?.members
|
||||
? api.query.society.members()
|
||||
: of([]),
|
||||
address && api.query.sudo?.key
|
||||
? api.query.sudo.key()
|
||||
: of(undefined)
|
||||
]) as Observable<FlagsIntermediate>
|
||||
).pipe(
|
||||
map(([electionsMembers, councilMembers, technicalCommitteeMembers, societyMembers, sudoKey]): DeriveAccountFlags => {
|
||||
const isIncluded = address ? isIncludedFn(address) : (): boolean => false;
|
||||
|
||||
return {
|
||||
isCouncil: (electionsMembers?.map(([id]: ITuple<[AccountId, Balance]>) => id) || councilMembers || []).some(isIncluded),
|
||||
isSociety: (societyMembers || []).some(isIncluded),
|
||||
isSudo: sudoKey?.toString() === address?.toString(),
|
||||
isTechCommittee: (technicalCommitteeMembers || []).some(isIncluded)
|
||||
};
|
||||
})
|
||||
return combineLatest<FlagsIntermediate>([
|
||||
address && api.query[councilSection]?.members
|
||||
? api.query[councilSection].members()
|
||||
: of(undefined),
|
||||
address && api.query.council?.members
|
||||
? api.query.council.members()
|
||||
: of([]),
|
||||
address && api.query.technicalCommittee?.members
|
||||
? api.query.technicalCommittee.members()
|
||||
: of([]),
|
||||
address && api.query.society?.members
|
||||
? api.query.society.members()
|
||||
: of([]),
|
||||
address && api.query.sudo?.key
|
||||
? api.query.sudo.key()
|
||||
: of(undefined)
|
||||
]).pipe(
|
||||
map((result) => parseFlags(address, result))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { AccountId, AccountIndex, Address, Balance, Registration } from '@polkadot/types/interfaces';
|
||||
import { AccountId, AccountIndex, Address, Balance, IdentityInfoAdditional, Registration } from '@polkadot/types/interfaces';
|
||||
import { ITuple } from '@polkadot/types/types';
|
||||
import { DeriveAccountInfo, DeriveAccountRegistration } from '../types';
|
||||
|
||||
@@ -13,6 +13,8 @@ import { u8aToString } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
const UNDEF_HEX = { toHex: () => undefined };
|
||||
|
||||
function dataAsString (data: Data): string | undefined {
|
||||
return data.isRaw
|
||||
? u8aToString(data.asRaw.toU8a(true))
|
||||
@@ -35,6 +37,19 @@ function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable<s
|
||||
);
|
||||
}
|
||||
|
||||
function extractOther (additional: IdentityInfoAdditional[]): Record<string, string> {
|
||||
return additional.reduce((other: Record<string, string>, [_key, _value]): Record<string, string> => {
|
||||
const key = dataAsString(_key);
|
||||
const value = dataAsString(_value);
|
||||
|
||||
if (key && value) {
|
||||
other[key] = value;
|
||||
}
|
||||
|
||||
return other;
|
||||
}, {});
|
||||
}
|
||||
|
||||
function extractIdentity (identityOfOpt?: Option<Registration>, superOf?: [AccountId, Data]): DeriveAccountRegistration {
|
||||
if (!identityOfOpt?.isSome) {
|
||||
return { judgements: [] };
|
||||
@@ -44,32 +59,15 @@ function extractIdentity (identityOfOpt?: Option<Registration>, superOf?: [Accou
|
||||
const topDisplay = dataAsString(info.display);
|
||||
|
||||
return {
|
||||
display: superOf
|
||||
? dataAsString(superOf[1]) || topDisplay
|
||||
: topDisplay,
|
||||
displayParent: superOf
|
||||
? topDisplay
|
||||
: undefined,
|
||||
display: (superOf && dataAsString(superOf[1])) || topDisplay,
|
||||
displayParent: superOf && topDisplay,
|
||||
email: dataAsString(info.email),
|
||||
image: dataAsString(info.image),
|
||||
judgements,
|
||||
legal: dataAsString(info.legal),
|
||||
other: info.additional.reduce((other: Record<string, string>, [_key, _value]): Record<string, string> => {
|
||||
const key = dataAsString(_key);
|
||||
const value = dataAsString(_value);
|
||||
|
||||
if (key && value) {
|
||||
other[key] = value;
|
||||
}
|
||||
|
||||
return other;
|
||||
}, {}),
|
||||
parent: superOf
|
||||
? superOf[0]
|
||||
: undefined,
|
||||
pgp: info.pgpFingerprint.isSome
|
||||
? info.pgpFingerprint.unwrap().toHex()
|
||||
: undefined,
|
||||
other: extractOther(info.additional),
|
||||
parent: superOf && superOf[0],
|
||||
pgp: info.pgpFingerprint.unwrapOr(UNDEF_HEX).toHex(),
|
||||
riot: dataAsString(info.riot),
|
||||
twitter: dataAsString(info.twitter),
|
||||
web: dataAsString(info.web)
|
||||
|
||||
@@ -16,9 +16,16 @@ import { memo } from '../util';
|
||||
type ResultBalance = [VestingInfo | null, (BalanceLock | BalanceLockTo212)[]];
|
||||
type Result = [DeriveBalancesAccount, BlockNumber, ResultBalance];
|
||||
|
||||
interface AllLocked {
|
||||
allLocked: boolean,
|
||||
lockedBalance: Balance,
|
||||
lockedBreakdown: (BalanceLock | BalanceLockTo212)[],
|
||||
vestingLocked: Balance
|
||||
}
|
||||
|
||||
const VESTING_ID = '0x76657374696e6720';
|
||||
|
||||
function calcBalances (api: ApiInterfaceRx, [{ accountId, accountNonce, freeBalance, frozenFee, frozenMisc, reservedBalance, votingBalance }, bestNumber, [vesting, locks]]: Result): DeriveBalancesAll {
|
||||
function calcLocked (api: ApiInterfaceRx, bestNumber: BlockNumber, locks: (BalanceLock | BalanceLockTo212)[]): AllLocked {
|
||||
let lockedBalance = api.registry.createType('Balance');
|
||||
let lockedBreakdown: (BalanceLock | BalanceLockTo212)[] = [];
|
||||
let vestingLocked = api.registry.createType('Balance');
|
||||
@@ -27,18 +34,22 @@ function calcBalances (api: ApiInterfaceRx, [{ accountId, accountNonce, freeBala
|
||||
if (Array.isArray(locks)) {
|
||||
// only get the locks that are valid until passed the current block
|
||||
lockedBreakdown = (locks as BalanceLockTo212[]).filter(({ until }): boolean => !until || (bestNumber && until.gt(bestNumber)));
|
||||
|
||||
const notAll = lockedBreakdown.filter(({ amount }) => !amount.isMax());
|
||||
|
||||
allLocked = lockedBreakdown.some(({ amount }) => amount.isMax());
|
||||
vestingLocked = api.registry.createType('Balance', lockedBreakdown.filter(({ id }) => id.eq(VESTING_ID)).reduce((result: BN, { amount }) => result.iadd(amount), new BN(0)));
|
||||
|
||||
// get the maximum of the locks according to https://github.com/paritytech/substrate/blob/master/srml/balances/src/lib.rs#L699
|
||||
const notAll = lockedBreakdown.filter(({ amount }) => !amount.isMax());
|
||||
|
||||
if (notAll.length) {
|
||||
lockedBalance = api.registry.createType('Balance', bnMax(...notAll.map(({ amount }): Balance => amount)));
|
||||
}
|
||||
}
|
||||
|
||||
return { allLocked, lockedBalance, lockedBreakdown, vestingLocked };
|
||||
}
|
||||
|
||||
function calcBalances (api: ApiInterfaceRx, [{ accountId, accountNonce, freeBalance, frozenFee, frozenMisc, reservedBalance, votingBalance }, bestNumber, [vesting, locks]]: Result): DeriveBalancesAll {
|
||||
const { allLocked, lockedBalance, lockedBreakdown, vestingLocked } = calcLocked(api, bestNumber, locks);
|
||||
// Calculate the vesting balances,
|
||||
// - offset = balance locked at startingBlock
|
||||
// - perBlock is the unlock amount
|
||||
|
||||
@@ -5,4 +5,5 @@ export * from './bestNumber';
|
||||
export * from './bestNumberFinalized';
|
||||
export * from './bestNumberLag';
|
||||
export * from './getHeader';
|
||||
export * from './subscribeNewBlocks';
|
||||
export * from './subscribeNewHeads';
|
||||
|
||||
32
packages/api-derive/src/chain/subscribeNewBlocks.ts
Normal file
32
packages/api-derive/src/chain/subscribeNewBlocks.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { EventRecord, Hash, SignedBlock } from '@polkadot/types/interfaces';
|
||||
import { FullNewBlock } from '../types';
|
||||
|
||||
import { Observable, combineLatest, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
/**
|
||||
* @name subscribeNewBlocks
|
||||
* @returns The latest block & events for that block
|
||||
*/
|
||||
export function subscribeNewBlocks (instanceId: string, api: ApiInterfaceRx): () => Observable<FullNewBlock> {
|
||||
return memo(instanceId, (): Observable<FullNewBlock> =>
|
||||
api.derive.chain.subscribeNewHeads().pipe(
|
||||
switchMap((header): Observable<[Hash, EventRecord[], SignedBlock]> => {
|
||||
const blockHash = header.hash;
|
||||
|
||||
return combineLatest(
|
||||
of(blockHash),
|
||||
api.query.system.events.at(blockHash),
|
||||
api.rpc.chain.getBlock(header.hash)
|
||||
);
|
||||
}),
|
||||
map(([blockHash, events, block]) => ({ block: block.block, blockHash, blockNumber: block.block.header.number.unwrap(), events, justification: block.justification }))
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import { memo } from '../util';
|
||||
|
||||
/**
|
||||
* @name subscribeNewHeads
|
||||
* @returns An array containing the block header and the block author
|
||||
* @returns A header with the current header (including extracted author)
|
||||
* @description An observable of the current block header and it's author
|
||||
* @example
|
||||
* <BR>
|
||||
|
||||
12
packages/api-derive/src/chain/types.ts
Normal file
12
packages/api-derive/src/chain/types.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Block, BlockNumber, EventRecord, Hash, Justification } from '@polkadot/types/interfaces';
|
||||
|
||||
export interface FullNewBlock {
|
||||
block: Block;
|
||||
blockHash: Hash;
|
||||
blockNumber: BlockNumber;
|
||||
events: EventRecord[];
|
||||
justification: Justification;
|
||||
}
|
||||
@@ -12,14 +12,14 @@ import { isFunction } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
type Result = [Hash[], Option<Proposal>[], Option<Votes>[]];
|
||||
type Result = [(Hash | Uint8Array | string)[], Option<Proposal>[], Option<Votes>[]];
|
||||
|
||||
function parse ([hashes, proposals, votes]: Result): DeriveCollectiveProposal[] {
|
||||
function parse (api: ApiInterfaceRx, [hashes, proposals, votes]: Result): DeriveCollectiveProposal[] {
|
||||
return proposals
|
||||
.map((proposalOpt, index): DeriveCollectiveProposal | null =>
|
||||
proposalOpt.isSome
|
||||
? {
|
||||
hash: hashes[index],
|
||||
hash: api.registry.createType('Hash', hashes[index]),
|
||||
proposal: proposalOpt.unwrap(),
|
||||
votes: votes[index].unwrapOr(null)
|
||||
}
|
||||
@@ -28,21 +28,41 @@ function parse ([hashes, proposals, votes]: Result): DeriveCollectiveProposal[]
|
||||
.filter((proposal): proposal is DeriveCollectiveProposal => !!proposal);
|
||||
}
|
||||
|
||||
export function proposals (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): () => Observable<DeriveCollectiveProposal[]> {
|
||||
function _proposalsFrom (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee' = 'council'): (hashes: (Hash | Uint8Array | string)[]) => Observable<DeriveCollectiveProposal[]> {
|
||||
return memo(instanceId, (hashes: (Hash | Uint8Array | string)[]): Observable<DeriveCollectiveProposal[]> =>
|
||||
(isFunction(api.query[section]?.proposals) && hashes.length
|
||||
? combineLatest<Result>([
|
||||
of(hashes),
|
||||
api.query[section].proposalOf.multi<Option<Proposal>>(hashes),
|
||||
api.query[section].voting.multi<Option<Votes>>(hashes)
|
||||
])
|
||||
: of([[], [], []] as Result)
|
||||
).pipe(
|
||||
map((result) => parse(api, result))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function proposals (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee' = 'council'): () => Observable<DeriveCollectiveProposal[]> {
|
||||
const proposalsFrom = _proposalsFrom(instanceId, api, section);
|
||||
|
||||
return memo(instanceId, (): Observable<DeriveCollectiveProposal[]> =>
|
||||
isFunction(api.query[section]?.proposals)
|
||||
? api.query[section].proposals().pipe(
|
||||
switchMap((hashes: Hash[]): Observable<Result> =>
|
||||
hashes.length
|
||||
? combineLatest([
|
||||
of(hashes),
|
||||
api.query[section].proposalOf.multi<Option<Proposal>>(hashes),
|
||||
api.query[section].voting.multi<Option<Votes>>(hashes)
|
||||
])
|
||||
: of([[], [], []])
|
||||
),
|
||||
map(parse)
|
||||
switchMap(proposalsFrom)
|
||||
)
|
||||
: of([] as DeriveCollectiveProposal[])
|
||||
);
|
||||
}
|
||||
|
||||
export function proposal (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee' = 'council'): (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
|
||||
const proposalsFrom = _proposalsFrom(instanceId, api, section);
|
||||
|
||||
return memo(instanceId, (hash: Hash | Uint8Array | string): Observable<DeriveCollectiveProposal | null> =>
|
||||
isFunction(api.query[section]?.proposals)
|
||||
? proposalsFrom([hash]).pipe(
|
||||
map(([proposal]) => proposal)
|
||||
)
|
||||
: of(null)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,12 +2,17 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { DeriveCollectiveProposals } from '../types';
|
||||
import { Hash } from '@polkadot/types/interfaces';
|
||||
import { DeriveCollectiveProposal } from '../types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { proposals as collectiveProposals } from '../collective';
|
||||
import { proposal as collectiveProposal, proposals as collectiveProposals } from '../collective';
|
||||
import { memo } from '../util';
|
||||
|
||||
export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposals> {
|
||||
return memo(instanceId, collectiveProposals(instanceId, api, 'council'));
|
||||
export function proposal (instanceId: string, api: ApiInterfaceRx): (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
|
||||
return memo(instanceId, collectiveProposal(instanceId, api));
|
||||
}
|
||||
|
||||
export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposal[]> {
|
||||
return memo(instanceId, collectiveProposals(instanceId, api));
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { AccountId, ReferendumInfo, ReferendumInfoTo239, VotingDelegating, VotingDirect, VotingDirectVote } from '@polkadot/types/interfaces';
|
||||
import { AccountId, ReferendumInfo, ReferendumInfoFinished, ReferendumInfoTo239, Vote, VotingDelegating, VotingDirect, VotingDirectVote } from '@polkadot/types/interfaces';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { DeriveDemocracyLock } from '../types';
|
||||
|
||||
@@ -9,27 +9,26 @@ import BN from 'bn.js';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
import { Option } from '@polkadot/types';
|
||||
import { isUndefined } from '@polkadot/util';
|
||||
import { BN_ZERO, isUndefined } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
const LOCKUPS = [0, 1, 2, 4, 8, 16, 32];
|
||||
const ZERO = new BN(0);
|
||||
|
||||
function parseEnd (api: ApiInterfaceRx, vote: Vote, { approved, end }: ReferendumInfoFinished): [BN, BN] {
|
||||
return [
|
||||
end,
|
||||
(approved.isTrue && vote.isAye) || (approved.isFalse && vote.isNay)
|
||||
? end.add(api.consts.democracy.enactmentPeriod.muln(LOCKUPS[vote.conviction.index]))
|
||||
: BN_ZERO
|
||||
];
|
||||
}
|
||||
|
||||
function parseLock (api: ApiInterfaceRx, [referendumId, accountVote]: VotingDirectVote, referendum: ReferendumInfo): DeriveDemocracyLock {
|
||||
const { balance, vote } = accountVote.asStandard;
|
||||
let unlockAt = ZERO;
|
||||
let referendumEnd = ZERO;
|
||||
|
||||
if (referendum.isFinished) {
|
||||
const { approved, end } = referendum.asFinished;
|
||||
|
||||
referendumEnd = end;
|
||||
|
||||
if ((approved.isTrue && vote.isAye) || (approved.isFalse && vote.isNay)) {
|
||||
unlockAt = end.add(api.consts.democracy.enactmentPeriod.muln(LOCKUPS[vote.conviction.index]));
|
||||
}
|
||||
}
|
||||
const [referendumEnd, unlockAt] = referendum.isFinished
|
||||
? parseEnd(api, vote, referendum.asFinished)
|
||||
: [BN_ZERO, BN_ZERO];
|
||||
|
||||
return { balance, isDelegated: false, isFinished: referendum.isFinished, referendumEnd, referendumId, unlockAt, vote };
|
||||
}
|
||||
|
||||
@@ -40,31 +40,35 @@ function votesPrev (api: ApiInterfaceRx, referendumId: BN): Observable<DeriveRef
|
||||
);
|
||||
}
|
||||
|
||||
function extractVotes (mapped: [AccountId, Voting][], referendumId: BN): DeriveReferendumVote[] {
|
||||
return mapped
|
||||
.filter(([, voting]) => voting.isDirect)
|
||||
.map(([accountId, voting]): [AccountId, VotingDirectVote[]] => [
|
||||
accountId,
|
||||
voting.asDirect.votes.filter(([idx]) => idx.eq(referendumId))
|
||||
])
|
||||
.filter(([, directVotes]) => !!directVotes.length)
|
||||
.reduce((result: DeriveReferendumVote[], [accountId, votes]) =>
|
||||
// FIXME We are ignoring split votes
|
||||
votes.reduce((result: DeriveReferendumVote[], [, vote]): DeriveReferendumVote[] => {
|
||||
if (vote.isStandard) {
|
||||
result.push({
|
||||
accountId,
|
||||
isDelegating: false,
|
||||
...vote.asStandard
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}, result), []
|
||||
);
|
||||
}
|
||||
|
||||
function votesCurr (api: ApiInterfaceRx, referendumId: BN): Observable<DeriveReferendumVote[]> {
|
||||
return api.query.democracy.votingOf.entries<Voting>().pipe(
|
||||
map((allVoting): DeriveReferendumVote[] => {
|
||||
const mapped = allVoting.map(([key, voting]): [AccountId, Voting] => [key.args[0] as AccountId, voting]);
|
||||
const votes = mapped
|
||||
.filter(([, voting]) => voting.isDirect)
|
||||
.map(([accountId, voting]): [AccountId, VotingDirectVote[]] => [
|
||||
accountId,
|
||||
voting.asDirect.votes.filter(([idx]) => idx.eq(referendumId))
|
||||
])
|
||||
.filter(([, directVotes]) => !!directVotes.length)
|
||||
.reduce((result: DeriveReferendumVote[], [accountId, votes]) =>
|
||||
// FIXME We are ignoring split votes
|
||||
votes.reduce((result: DeriveReferendumVote[], [, vote]): DeriveReferendumVote[] => {
|
||||
if (vote.isStandard) {
|
||||
result.push({
|
||||
accountId,
|
||||
isDelegating: false,
|
||||
...vote.asStandard
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}, result), []
|
||||
);
|
||||
const votes = extractVotes(mapped, referendumId);
|
||||
const delegations = mapped
|
||||
.filter(([, voting]) => voting.isDelegating)
|
||||
.map(([accountId, voting]): [AccountId, VotingDelegating] => [accountId, voting.asDelegating]);
|
||||
|
||||
@@ -58,16 +58,20 @@ export function compareRationals (n1: BN, d1: BN, n2: BN, d2: BN): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
export function calcPassing (threshold: VoteThreshold, sqrtElectorate: BN, { votedAye, votedNay, votedTotal }: ApproxState): boolean {
|
||||
function calcPassingOther (threshold: VoteThreshold, sqrtElectorate: BN, { votedAye, votedNay, votedTotal }: ApproxState): boolean {
|
||||
const sqrtVoters = bnSqrt(votedTotal);
|
||||
|
||||
return sqrtVoters.isZero()
|
||||
? false
|
||||
: threshold.isSimplemajority
|
||||
? votedAye.gt(votedNay)
|
||||
: threshold.isSupermajorityapproval
|
||||
? compareRationals(votedNay, sqrtVoters, votedAye, sqrtElectorate)
|
||||
: compareRationals(votedNay, sqrtElectorate, votedAye, sqrtVoters);
|
||||
: threshold.isSupermajorityapproval
|
||||
? compareRationals(votedNay, sqrtVoters, votedAye, sqrtElectorate)
|
||||
: compareRationals(votedNay, sqrtElectorate, votedAye, sqrtVoters);
|
||||
}
|
||||
|
||||
export function calcPassing (threshold: VoteThreshold, sqrtElectorate: BN, state: ApproxState): boolean {
|
||||
return threshold.isSimplemajority
|
||||
? state.votedAye.gt(state.votedNay)
|
||||
: calcPassingOther(threshold, sqrtElectorate, state);
|
||||
}
|
||||
|
||||
function calcVotesPrev (votesFor: DeriveReferendumVote[]): DeriveReferendumVoteState {
|
||||
@@ -92,16 +96,7 @@ function calcVotesPrev (votesFor: DeriveReferendumVote[]): DeriveReferendumVoteS
|
||||
state.votedTotal.iadd(counted);
|
||||
|
||||
return state;
|
||||
}, {
|
||||
allAye: [],
|
||||
allNay: [],
|
||||
voteCount: 0,
|
||||
voteCountAye: 0,
|
||||
voteCountNay: 0,
|
||||
votedAye: new BN(0),
|
||||
votedNay: new BN(0),
|
||||
votedTotal: new BN(0)
|
||||
});
|
||||
}, { allAye: [], allNay: [], voteCount: 0, voteCountAye: 0, voteCountNay: 0, votedAye: new BN(0), votedNay: new BN(0), votedTotal: new BN(0) });
|
||||
}
|
||||
|
||||
function calcVotesCurrent (tally: Tally, votes: DeriveReferendumVote[]): DeriveReferendumVoteState {
|
||||
|
||||
@@ -10,6 +10,7 @@ import { map } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { memo } from '../util';
|
||||
import { didUpdateToBool } from './util';
|
||||
|
||||
type Result = [
|
||||
Active,
|
||||
@@ -23,7 +24,7 @@ type Result = [
|
||||
];
|
||||
|
||||
function parseActive (id: ParaId, active: Active): DeriveParachainActive | null {
|
||||
const found = active.find(([paraId]): boolean => paraId === id);
|
||||
const found = active.find(([paraId]) => paraId === id);
|
||||
|
||||
if (found && found[1].isSome) {
|
||||
const [collatorId, retriable] = found[1].unwrap();
|
||||
@@ -48,12 +49,11 @@ function parseActive (id: ParaId, active: Active): DeriveParachainActive | null
|
||||
}
|
||||
|
||||
function parseCollators (id: ParaId, collatorQueue: SelectedThreads | RetryQueue): (CollatorId | null)[] {
|
||||
return collatorQueue
|
||||
.map((queue): CollatorId | null => {
|
||||
const found = queue.find(([paraId]): boolean => paraId === id);
|
||||
return collatorQueue.map((queue): CollatorId | null => {
|
||||
const found = queue.find(([paraId]) => paraId === id);
|
||||
|
||||
return found ? found[1] : null;
|
||||
});
|
||||
return found ? found[1] : null;
|
||||
});
|
||||
}
|
||||
|
||||
function parse (id: ParaId, [active, retryQueue, selectedThreads, didUpdate, info, pendingSwap, heads, relayDispatchQueue]: Result): DeriveParachainFull | null {
|
||||
@@ -63,9 +63,7 @@ function parse (id: ParaId, [active, retryQueue, selectedThreads, didUpdate, inf
|
||||
|
||||
return {
|
||||
active: parseActive(id, active),
|
||||
didUpdate: didUpdate.isSome
|
||||
? !!didUpdate.unwrap().some((paraId): boolean => paraId.eq(id))
|
||||
: false,
|
||||
didUpdate: didUpdateToBool(didUpdate, id),
|
||||
heads,
|
||||
id,
|
||||
info: { id, ...info.unwrap() } as DeriveParachainInfo,
|
||||
|
||||
@@ -10,6 +10,7 @@ import { map, switchMap } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { memo } from '../util';
|
||||
import { didUpdateToBool } from './util';
|
||||
|
||||
type Result = [
|
||||
ParaId[],
|
||||
@@ -20,17 +21,13 @@ type Result = [
|
||||
];
|
||||
|
||||
function parse ([ids, didUpdate, infos, pendingSwaps, relayDispatchQueueSizes]: Result): DeriveParachain[] {
|
||||
return ids.map((id, index): DeriveParachain => {
|
||||
return {
|
||||
didUpdate: didUpdate.isSome
|
||||
? !!didUpdate.unwrap().some((paraId): boolean => paraId.eq(id))
|
||||
: false,
|
||||
id,
|
||||
info: { id, ...infos[index].unwrapOr(null) } as DeriveParachainInfo,
|
||||
pendingSwapId: pendingSwaps[index].unwrapOr(null),
|
||||
relayDispatchQueueSize: relayDispatchQueueSizes[index][0].toNumber()
|
||||
};
|
||||
});
|
||||
return ids.map((id, index): DeriveParachain => ({
|
||||
didUpdate: didUpdateToBool(didUpdate, id),
|
||||
id,
|
||||
info: { id, ...infos[index].unwrapOr(null) } as DeriveParachainInfo,
|
||||
pendingSwapId: pendingSwaps[index].unwrapOr(null),
|
||||
relayDispatchQueueSize: relayDispatchQueueSizes[index][0].toNumber()
|
||||
}));
|
||||
}
|
||||
|
||||
export function overview (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveParachain[]> {
|
||||
@@ -46,9 +43,7 @@ export function overview (instanceId: string, api: ApiInterfaceRx): () => Observ
|
||||
api.query.parachains.relayDispatchQueueSize.multi<RelayDispatchQueueSize>(paraIds)
|
||||
])
|
||||
),
|
||||
map((result: Result): DeriveParachain[] =>
|
||||
parse(result)
|
||||
)
|
||||
map(parse)
|
||||
)
|
||||
: of([] as DeriveParachain[])
|
||||
);
|
||||
|
||||
11
packages/api-derive/src/parachains/util.ts
Normal file
11
packages/api-derive/src/parachains/util.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ParaId } from '@polkadot/types/interfaces';
|
||||
import { DidUpdate } from './types';
|
||||
|
||||
export function didUpdateToBool (didUpdate: DidUpdate, id: ParaId): boolean {
|
||||
return didUpdate.isSome
|
||||
? didUpdate.unwrap().some((paraId) => paraId.eq(id))
|
||||
: false;
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import { map, switchMap } from 'rxjs/operators';
|
||||
import { BN_ZERO } from '@polkadot/util';
|
||||
|
||||
import { deriveCache, memo } from '../util';
|
||||
import { filterEras } from './util';
|
||||
|
||||
const CACHE_KEY = 'eraPoints';
|
||||
|
||||
@@ -42,7 +43,7 @@ export function _erasPoints (instanceId: string, api: ApiInterfaceRx): (eras: Er
|
||||
: eras
|
||||
.map((era) => deriveCache.get<DeriveEraPoints>(`${CACHE_KEY}-${era.toString()}`))
|
||||
.filter((value): value is DeriveEraPoints => !!value);
|
||||
const remaining = eras.filter((era) => !cached.some((cached) => era.eq(cached.era)));
|
||||
const remaining = filterEras(eras, cached);
|
||||
|
||||
return !remaining.length
|
||||
? of(cached)
|
||||
|
||||
@@ -10,6 +10,7 @@ import { map, switchMap } from 'rxjs/operators';
|
||||
import { Option } from '@polkadot/types';
|
||||
|
||||
import { deriveCache, memo } from '../util';
|
||||
import { filterEras } from './util';
|
||||
|
||||
const CACHE_KEY = 'eraRewards';
|
||||
|
||||
@@ -31,7 +32,7 @@ export function _erasRewards (instanceId: string, api: ApiInterfaceRx): (eras: E
|
||||
: eras
|
||||
.map((era) => deriveCache.get<DeriveEraRewards>(`${CACHE_KEY}-${era.toString()}`))
|
||||
.filter((value): value is DeriveEraRewards => !!value);
|
||||
const remaining = eras.filter((era) => !cached.some((cached) => era.eq(cached.era)));
|
||||
const remaining = filterEras(eras, cached);
|
||||
|
||||
if (!remaining.length) {
|
||||
return of(cached);
|
||||
|
||||
@@ -12,6 +12,7 @@ import { Option } from '@polkadot/types';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
import { DeriveStakingQuery } from './types';
|
||||
|
||||
type ErasResult = [{ unwrapOr: (value: BN) => BN }, DeriveEraPoints[], DeriveEraPrefs[], DeriveEraRewards[]];
|
||||
|
||||
@@ -99,24 +100,37 @@ function filterEras (eras: EraIndex[], stakingLedger: StakingLedger): EraIndex[]
|
||||
return eras.filter((era) => filterEra(era, stakingLedger));
|
||||
}
|
||||
|
||||
function filterRewards (api: ApiInterfaceRx, eras: EraIndex[], migrateEra: BN, rewards: DeriveStakerReward[], stakingLedger: StakingLedger, withActive: boolean): Observable<DeriveStakerReward[]> {
|
||||
if (withActive) {
|
||||
return of(rewards);
|
||||
}
|
||||
function removeClaimed (validators: string[], queryValidators: DeriveStakingQuery[], reward: DeriveStakerReward): void {
|
||||
const rm: string[] = [];
|
||||
|
||||
Object.keys(reward.validators).forEach((validatorId): void => {
|
||||
const index = validators.indexOf(validatorId);
|
||||
|
||||
if (index !== -1) {
|
||||
const valLedger = queryValidators[index].stakingLedger;
|
||||
|
||||
if (valLedger?.claimedRewards.some((era) => reward.era.eq(era))) {
|
||||
rm.push(validatorId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
rm.forEach((validatorId): void => {
|
||||
delete reward.validators[validatorId];
|
||||
});
|
||||
}
|
||||
|
||||
function filterRewards (api: ApiInterfaceRx, eras: EraIndex[], { migrateEra, rewards, stakingLedger }: { migrateEra: BN, rewards: DeriveStakerReward[]; stakingLedger: StakingLedger }): Observable<DeriveStakerReward[]> {
|
||||
const validators = uniqValidators(rewards);
|
||||
const filter = filterEras(eras, stakingLedger);
|
||||
|
||||
return (
|
||||
isFunction(api.tx.staking.payoutStakers)
|
||||
? api.derive.staking.queryMulti(validators)
|
||||
: of([])
|
||||
).pipe(
|
||||
map((queryValidators): DeriveStakerReward[] => {
|
||||
const filter = withActive
|
||||
? eras
|
||||
: filterEras(eras, stakingLedger);
|
||||
|
||||
return rewards
|
||||
map((queryValidators): DeriveStakerReward[] =>
|
||||
rewards
|
||||
.filter(({ isEmpty }) => !isEmpty)
|
||||
.filter((reward): boolean => {
|
||||
if (!filter.some((filter) => reward.era.eq(filter))) {
|
||||
@@ -128,32 +142,16 @@ function filterRewards (api: ApiInterfaceRx, eras: EraIndex[], migrateEra: BN, r
|
||||
|
||||
reward.isStakerPayout = true;
|
||||
|
||||
const rm: string[] = [];
|
||||
|
||||
Object.keys(reward.validators).forEach((validatorId): void => {
|
||||
const index = validators.indexOf(validatorId);
|
||||
|
||||
if (index !== -1) {
|
||||
const valLedger = queryValidators[index].stakingLedger;
|
||||
|
||||
if (valLedger?.claimedRewards.some((era) => reward.era.eq(era))) {
|
||||
rm.push(validatorId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
rm.forEach((validatorId): void => {
|
||||
delete reward.validators[validatorId];
|
||||
});
|
||||
removeClaimed(validators, queryValidators, reward);
|
||||
|
||||
return true;
|
||||
})
|
||||
.filter(({ validators }) => Object.keys(validators).length !== 0)
|
||||
.map((reward) => ({
|
||||
...reward,
|
||||
nominators: reward.nominating.filter(({ validatorId }) => !!reward.validators[validatorId])
|
||||
}));
|
||||
})
|
||||
nominators: reward.nominating.filter((n) => !!reward.validators[n.validatorId])
|
||||
}))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -178,13 +176,15 @@ export function _stakerRewards (instanceId: string, api: ApiInterfaceRx): (accou
|
||||
api.derive.staking._stakerRewardsEras(eras, withActive)
|
||||
]).pipe(
|
||||
switchMap(([{ stakingLedger, stashId }, exposures, erasResult]): Observable<DeriveStakerReward[]> => {
|
||||
const migrateEra: BN = erasResult[0].unwrapOr(ZERO);
|
||||
|
||||
if (!stashId || !stakingLedger) {
|
||||
return of([]);
|
||||
}
|
||||
|
||||
return filterRewards(api, eras, migrateEra, parseRewards(api, stashId, erasResult, exposures), stakingLedger, withActive);
|
||||
const rewards = parseRewards(api, stashId, erasResult, exposures);
|
||||
|
||||
return withActive
|
||||
? of(rewards)
|
||||
: filterRewards(api, eras, { migrateEra: erasResult[0].unwrapOr(ZERO), rewards, stakingLedger });
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
8
packages/api-derive/src/staking/util.ts
Normal file
8
packages/api-derive/src/staking/util.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { EraIndex } from '@polkadot/types/interfaces';
|
||||
|
||||
export function filterEras <T extends { era: EraIndex }> (eras: EraIndex[], list: T[]): EraIndex[] {
|
||||
return eras.filter((era) => !list.some((entry) => era.eq(entry.era)));
|
||||
}
|
||||
@@ -2,12 +2,12 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { DeriveCollectiveProposals } from '../types';
|
||||
import { DeriveCollectiveProposal } from '../types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { proposals as collectiveProposals } from '../collective';
|
||||
import { memo } from '../util';
|
||||
|
||||
export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposals> {
|
||||
export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposal[]> {
|
||||
return memo(instanceId, collectiveProposals(instanceId, api, 'technicalCommittee'));
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { ProposalIndex, TreasuryProposal } from '@polkadot/types/interfaces';
|
||||
import { DeriveCollectiveProposals, DeriveTreasuryProposal, DeriveTreasuryProposals } from '../types';
|
||||
import { DeriveCollectiveProposal, DeriveTreasuryProposal, DeriveTreasuryProposals } from '../types';
|
||||
|
||||
import { Observable, combineLatest, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
@@ -15,7 +15,7 @@ interface Result {
|
||||
allIds: ProposalIndex[];
|
||||
allProposals: Option<TreasuryProposal>[];
|
||||
approvalIds: ProposalIndex[];
|
||||
councilProposals: DeriveCollectiveProposals;
|
||||
councilProposals: DeriveCollectiveProposal[];
|
||||
proposalCount: ProposalIndex;
|
||||
}
|
||||
|
||||
@@ -51,9 +51,7 @@ function retrieveProposals (api: ApiInterfaceRx, proposalCount: ProposalIndex, a
|
||||
const count = proposalCount.toNumber();
|
||||
|
||||
for (let index = 0; index < count; index++) {
|
||||
const isApproval = approvalIds.some((id): boolean => id.eqn(index));
|
||||
|
||||
if (!isApproval) {
|
||||
if (!approvalIds.some((id) => id.eqn(index))) {
|
||||
proposalIds.push(api.registry.createType('ProposalIndex', index));
|
||||
}
|
||||
}
|
||||
@@ -64,7 +62,7 @@ function retrieveProposals (api: ApiInterfaceRx, proposalCount: ProposalIndex, a
|
||||
api.query.treasury.proposals.multi<Option<TreasuryProposal>>(allIds),
|
||||
api.derive.council.proposals()
|
||||
]).pipe(
|
||||
map(([allProposals, councilProposals]: [Option<TreasuryProposal>[], DeriveCollectiveProposals]): DeriveTreasuryProposals =>
|
||||
map(([allProposals, councilProposals]: [Option<TreasuryProposal>[], DeriveCollectiveProposal[]]): DeriveTreasuryProposals =>
|
||||
parseResult(api, { allIds, allProposals, approvalIds, councilProposals, proposalCount })
|
||||
)
|
||||
);
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user