Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Commit

Permalink
fix(types)!: polkadot/api to v4 w. esm; Use substrate/dev for build (#…
Browse files Browse the repository at this point in the history
…404)

* fix(types)!: polkadot/api to v4 w. esm; Use substrate/dev for build

BREAKING CHANGE
Emitted .js is now es2020, requiring node >= 12.

## Changes
- Use @substrate/dev for build configuration
    - new tsconfig.json targets es2019
    - prettier is now uses hard tabs and 2 spaces, which causes the huge diff
    - some slight type annotations where necessary for claims.ts and method.ts in order to pass the stricter tsconfig
    - Babel is now used to transpile the @polkadot/* packages `.js` from ESM to CJS for jest
  • Loading branch information
emostov committed Mar 12, 2021
1 parent c7b56d7 commit 2280218
Show file tree
Hide file tree
Showing 115 changed files with 3,346 additions and 3,306 deletions.
31 changes: 1 addition & 30 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'prettier',
'plugin:prettier/recommended'
],
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json'
},
plugins: [
'@typescript-eslint',
'prettier',
'simple-import-sort'
],
rules: {
// Sort imports and exports
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',
// https://github.com/eslint/eslint/issues/2321#issuecomment-134665757
// 'no-unused-vars': [2, { args: 'all', argsIgnorePattern: '^_' }],
'@typescript-eslint/no-unused-vars': [
2,
{ args: 'all', argsIgnorePattern: '^_' }
],
"@typescript-eslint/ban-types": 0,
},
};
module.exports = require('@substrate/dev/config/eslint');
7 changes: 1 addition & 6 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
module.exports = {
semi: true,
singleQuote: true,
tabWidth: 2,
useTabs: false,
};
module.exports = require('@substrate/dev/config/prettier');
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@substrate/dev/config/babel");
236 changes: 118 additions & 118 deletions examples/polkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { Keyring } from '@polkadot/api';
import { cryptoWaitReady } from '@polkadot/util-crypto';

import {
createSignedTx,
createSigningPayload,
decode,
deriveAddress,
getRegistry,
getTxHash,
methods,
POLKADOT_SS58_FORMAT,
createSignedTx,
createSigningPayload,
decode,
deriveAddress,
getRegistry,
getTxHash,
methods,
POLKADOT_SS58_FORMAT,
} from '../src';
import { rpcToNode, signWith } from './util';

Expand All @@ -25,128 +25,128 @@ import { rpcToNode, signWith } from './util';
* locally on `http://localhost:9933`.
*/
async function main(): Promise<void> {
// Wait for the promise to resolve async WASM
await cryptoWaitReady();
// Create a new keyring, and add an "Alice" account
const keyring = new Keyring();
const alice = keyring.addFromUri('//Alice', { name: 'Alice' }, 'sr25519');
console.log(
"Alice's SS58-Encoded Address:",
deriveAddress(alice.publicKey, POLKADOT_SS58_FORMAT)
);
// Wait for the promise to resolve async WASM
await cryptoWaitReady();
// Create a new keyring, and add an "Alice" account
const keyring = new Keyring();
const alice = keyring.addFromUri('//Alice', { name: 'Alice' }, 'sr25519');
console.log(
"Alice's SS58-Encoded Address:",
deriveAddress(alice.publicKey, POLKADOT_SS58_FORMAT)
);

// Construct a balance transfer transaction offline.
// To construct the tx, we need some up-to-date information from the node.
// `txwrapper` is offline-only, so does not care how you retrieve this info.
// In this tutorial, we simply send RPC requests to the node.
const { block } = await rpcToNode('chain_getBlock');
const blockHash = await rpcToNode('chain_getBlockHash');
const genesisHash = await rpcToNode('chain_getBlockHash', [0]);
const metadataRpc = await rpcToNode('state_getMetadata');
const { specVersion, transactionVersion } = await rpcToNode(
'state_getRuntimeVersion'
);
// Construct a balance transfer transaction offline.
// To construct the tx, we need some up-to-date information from the node.
// `txwrapper` is offline-only, so does not care how you retrieve this info.
// In this tutorial, we simply send RPC requests to the node.
const { block } = await rpcToNode('chain_getBlock');
const blockHash = await rpcToNode('chain_getBlockHash');
const genesisHash = await rpcToNode('chain_getBlockHash', [0]);
const metadataRpc = await rpcToNode('state_getMetadata');
const { specVersion, transactionVersion } = await rpcToNode(
'state_getRuntimeVersion'
);

// Create Polkadot's type registry.
const registry = getRegistry('Polkadot', 'polkadot', specVersion);
// Create Polkadot's type registry.
const registry = getRegistry('Polkadot', 'polkadot', specVersion);

// Now we can create our `balances.transfer` unsigned tx. The following
// function takes the above data as arguments, so can be performed offline
// if desired.
const unsigned = methods.balances.transfer(
{
value: '90071992547409910',
dest: '14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3', // Bob
},
{
address: deriveAddress(alice.publicKey, POLKADOT_SS58_FORMAT),
blockHash,
blockNumber: registry
.createType('BlockNumber', block.header.number)
.toNumber(),
eraPeriod: 64,
genesisHash,
metadataRpc,
nonce: 0, // Assuming this is Alice's first tx on the chain
specVersion,
tip: 0,
transactionVersion,
},
{
metadataRpc,
registry,
}
);
// Now we can create our `balances.transfer` unsigned tx. The following
// function takes the above data as arguments, so can be performed offline
// if desired.
const unsigned = methods.balances.transfer(
{
value: '90071992547409910',
dest: '14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3', // Bob
},
{
address: deriveAddress(alice.publicKey, POLKADOT_SS58_FORMAT),
blockHash,
blockNumber: registry
.createType('BlockNumber', block.header.number)
.toNumber(),
eraPeriod: 64,
genesisHash,
metadataRpc,
nonce: 0, // Assuming this is Alice's first tx on the chain
specVersion,
tip: 0,
transactionVersion,
},
{
metadataRpc,
registry,
}
);

// Decode an unsigned transaction.
const decodedUnsigned = decode(
unsigned,
{
metadataRpc,
registry,
},
true
);
console.log(
`\nDecoded Transaction\n To: ${decodedUnsigned.method.args.dest}\n` +
` Amount: ${decodedUnsigned.method.args.value}`
);
// Decode an unsigned transaction.
const decodedUnsigned = decode(
unsigned,
{
metadataRpc,
registry,
},
true
);
console.log(
`\nDecoded Transaction\n To: ${decodedUnsigned.method.args.dest}\n` +
` Amount: ${decodedUnsigned.method.args.value}`
);

// Construct the signing payload from an unsigned transaction.
const signingPayload = createSigningPayload(unsigned, { registry });
console.log(`\nPayload to Sign: ${signingPayload}`);
// Construct the signing payload from an unsigned transaction.
const signingPayload = createSigningPayload(unsigned, { registry });
console.log(`\nPayload to Sign: ${signingPayload}`);

// Decode the information from a signing payload.
const payloadInfo = decode(
signingPayload,
{
metadataRpc,
registry,
},
true
);
console.log(
`\nDecoded Transaction\n To: ${payloadInfo.method.args.dest}\n` +
` Amount: ${payloadInfo.method.args.value}`
);
// Decode the information from a signing payload.
const payloadInfo = decode(
signingPayload,
{
metadataRpc,
registry,
},
true
);
console.log(
`\nDecoded Transaction\n To: ${payloadInfo.method.args.dest}\n` +
` Amount: ${payloadInfo.method.args.value}`
);

// Sign a payload. This operation should be performed on an offline device.
const signature = signWith(alice, signingPayload, {
metadataRpc,
registry,
});
console.log(`\nSignature: ${signature}`);
// Sign a payload. This operation should be performed on an offline device.
const signature = signWith(alice, signingPayload, {
metadataRpc,
registry,
});
console.log(`\nSignature: ${signature}`);

// Serialize a signed transaction.
const tx = createSignedTx(unsigned, signature, { metadataRpc, registry });
console.log(`\nTransaction to Submit: ${tx}`);
// Serialize a signed transaction.
const tx = createSignedTx(unsigned, signature, { metadataRpc, registry });
console.log(`\nTransaction to Submit: ${tx}`);

// Derive the tx hash of a signed transaction offline.
const expectedTxHash = getTxHash(tx);
console.log(`\nExpected Tx Hash: ${expectedTxHash}`);
// Derive the tx hash of a signed transaction offline.
const expectedTxHash = getTxHash(tx);
console.log(`\nExpected Tx Hash: ${expectedTxHash}`);

// Send the tx to the node. Again, since `txwrapper` is offline-only, this
// operation should be handled externally. Here, we just send a JSONRPC
// request directly to the node.
const actualTxHash = await rpcToNode('author_submitExtrinsic', [tx]);
console.log(`Actual Tx Hash: ${actualTxHash}`);
// Send the tx to the node. Again, since `txwrapper` is offline-only, this
// operation should be handled externally. Here, we just send a JSONRPC
// request directly to the node.
const actualTxHash = await rpcToNode('author_submitExtrinsic', [tx]);
console.log(`Actual Tx Hash: ${actualTxHash}`);

// Decode a signed payload.
const txInfo = decode(
tx,
{
metadataRpc,
registry,
},
true
);
console.log(
`\nDecoded Transaction\n To: ${txInfo.method.args.dest}\n` +
` Amount: ${txInfo.method.args.value}\n`
);
// Decode a signed payload.
const txInfo = decode(
tx,
{
metadataRpc,
registry,
},
true
);
console.log(
`\nDecoded Transaction\n To: ${txInfo.method.args.dest}\n` +
` Amount: ${txInfo.method.args.value}\n`
);
}

main().catch((error) => {
console.error(error);
process.exit(1);
console.error(error);
process.exit(1);
});

0 comments on commit 2280218

Please sign in to comment.