Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
switchMap(({ accountId }): Observable =>
(accountId
? combineLatest([
of(accountId),
api.derive.chain.bestNumber(),
queryBalances(api, accountId),
// FIXME This is having issues with Kusama, only use accountNonce atm
// api.rpc.account && api.rpc.account.nextIndex
// ? api.rpc.account.nextIndex(accountId)
// // otherwise we end up with this: type 'Codec | Index' is not assignable to type 'Index'.
// : api.query.system.accountNonce(accountId)
api.query.system.accountNonce(accountId)
])
: of([createType(api.registry, 'AccountId'), createType(api.registry, 'BlockNumber'), [createType(api.registry, 'Balance'), createType(api.registry, 'Balance'), createType(api.registry, 'Vec'), createType(api.registry, 'Option', null)], createType(api.registry, 'Index')])
)
),
const alice = keyring.addFromUri('//Alice');
// Get nonce for account
const nonce = await api.query.system.accountNonce(alice.address);
// Get current block
const signedBlock = await api.rpc.chain.getBlock();
// Get current block height and hash
const currentHeight = signedBlock.block.header.number;
const blockHash = signedBlock.block.header.hash;
// NOTE By default the API will send mortal transactions, only explicitly construct
// if you wish to override the defaults
// construct a mortal era
const era = createType('ExtrinsicEra', { current: currentHeight, period: 10 });
// Create an extrinsic, transferring 12345 units to Bob
const transfer = api.tx.balances.transfer(BOB, 12345);
// Sign and send the transaction using our account with a nonce and the length of blocks the transaction is valid for
const hash = await transfer.signAndSend(alice, { blockHash, era, nonce });
console.log('Transfer sent with hash', hash.toHex());
}
function handleBlockAnnounce ({ sync }: P2pInterface, peer: PeerInterface, message: BlockAnnounce): void {
// self.l.debug(() => [peer.shortId, 'BlockAnnounce', JSON.stringify(message)]);
const header = message.header;
const blockNumber = header.number.unwrap();
if (peer.bestNumber.lt(blockNumber)) {
peer.setBest(blockNumber, createType('Header', header).hash);
}
sync.requestBlocks(peer);
}
.subscribe(([chain, health, name, properties, version]) => {
if (!keyringInitialized) {
// keyring with Schnorrkel support
keyring.loadAll({
ss58Format: properties.ss58Format.unwrapOr(createType('u8', SS58_PREFIX)).toNumber(),
genesisHash: api.genesisHash,
isDevelopment: isTestChain(chain.toString()),
type: 'ed25519'
});
keyringInitialized = true;
} else {
// The keyring can only be initialized once. To make sure that the
// keyring values are up-to-date in case the node has changed settings
// we need to reinitialize it.
window.location.reload();
return;
}
l.log(`Api connected to ${WS_URL}`);
l.log(`Api ready, connected to chain "${chain}" with properties ${JSON.stringify(properties)}`);
function createDerivedLatest (api: ApiInterfaceRx, [[hasBabe, epochDuration, sessionsPerEra], { currentIndex, currentEra, validatorCount }, [currentSlot, epochIndex, epochOrGenesisStartSlot, currentEraStartSessionIndex]]: Result): DerivedSessionInfo {
const epochStartSlot = epochIndex.mul(epochDuration).add(epochOrGenesisStartSlot);
const sessionProgress = currentSlot.sub(epochStartSlot);
const eraProgress = currentIndex.sub(currentEraStartSessionIndex).mul(epochDuration).add(sessionProgress);
return {
currentEra,
currentIndex,
eraLength: createType(api.registry, 'BlockNumber', sessionsPerEra.mul(epochDuration)),
eraProgress: createType(api.registry, 'BlockNumber', eraProgress),
isEpoch: hasBabe,
lastEraLengthChange: createType(api.registry, 'BlockNumber'),
lastLengthChange: createType(api.registry, 'BlockNumber', epochStartSlot),
sessionLength: epochDuration,
sessionsPerEra,
sessionProgress: createType(api.registry, 'BlockNumber', sessionProgress),
validatorCount
};
}
function createDerivedLatest (api: ApiInterfaceRx, [[hasBabe, epochDuration, sessionsPerEra], { currentIndex, currentEra, validatorCount }, [currentSlot, epochIndex, epochOrGenesisStartSlot, currentEraStartSessionIndex]]: Result): DerivedSessionInfo {
const epochStartSlot = epochIndex.mul(epochDuration).add(epochOrGenesisStartSlot);
const sessionProgress = currentSlot.sub(epochStartSlot);
const eraProgress = currentIndex.sub(currentEraStartSessionIndex).mul(epochDuration).add(sessionProgress);
return {
currentEra,
currentIndex,
eraLength: createType(api.registry, 'BlockNumber', sessionsPerEra.mul(epochDuration)),
eraProgress: createType(api.registry, 'BlockNumber', eraProgress),
isEpoch: hasBabe,
lastEraLengthChange: createType(api.registry, 'BlockNumber'),
lastLengthChange: createType(api.registry, 'BlockNumber', epochStartSlot),
sessionLength: epochDuration,
sessionsPerEra,
sessionProgress: createType(api.registry, 'BlockNumber', sessionProgress),
validatorCount
};
}
votersFor.map((accountId, index): DerivedReferendumVote => ({
accountId,
balance: balances[index].votingBalance || createType(api.registry, 'Balance'),
vote: votes[index] || createType(api.registry, 'Vote')
} as DerivedReferendumVote))
)
modules: modules.map(({ calls, constants, events, name, prefix, storage }): ModuleMetadataV7 =>
createType(registry, 'ModuleMetadataV7', {
calls,
constants,
events,
name,
storage: storage.isSome
? createType(registry, 'StorageMetadataV7', { prefix, items: storage.unwrap() })
: null
})
)
function createDerivedLatest (api: ApiInterfaceRx, [[hasBabe, epochDuration, sessionsPerEra], { currentIndex, currentEra, validatorCount }, [currentSlot, epochIndex, epochOrGenesisStartSlot, currentEraStartSessionIndex]]: Result): DerivedSessionInfo {
const epochStartSlot = epochIndex.mul(epochDuration).add(epochOrGenesisStartSlot);
const sessionProgress = currentSlot.sub(epochStartSlot);
const eraProgress = currentIndex.sub(currentEraStartSessionIndex).mul(epochDuration).add(sessionProgress);
return {
currentEra,
currentIndex,
eraLength: createType(api.registry, 'BlockNumber', sessionsPerEra.mul(epochDuration)),
eraProgress: createType(api.registry, 'BlockNumber', eraProgress),
isEpoch: hasBabe,
lastEraLengthChange: createType(api.registry, 'BlockNumber'),
lastLengthChange: createType(api.registry, 'BlockNumber', epochStartSlot),
sessionLength: epochDuration,
sessionsPerEra,
sessionProgress: createType(api.registry, 'BlockNumber', sessionProgress),
validatorCount
};
}
import BN from 'bn.js';
import { registry } from '@polkadot/react-api';
import { createType } from '@polkadot/types';
const ZERO_BALANCE: DerivedBalances = {
accountId: createType(registry, 'AccountId'),
accountNonce: createType(registry, 'Index'),
availableBalance: createType(registry, 'Balance'),
freeBalance: createType(registry, 'Balance'),
isVesting: false,
lockedBalance: createType(registry, 'Balance'),
lockedBreakdown: [],
reservedBalance: createType(registry, 'Balance'),
vestedBalance: createType(registry, 'Balance'),
vestingTotal: createType(registry, 'Balance'),
votingBalance: createType(registry, 'Balance')
};
const ZERO_FEES_BALANCES: DerivedFees = {
creationFee: createType(registry, 'Balance'),
existentialDeposit: createType(registry, 'Balance'),
transactionBaseFee: createType(registry, 'Balance'),
transactionByteFee: createType(registry, 'Balance'),
transferFee: createType(registry, 'Balance')
};
const ZERO_FEES = ZERO_FEES_BALANCES;
const ZERO_FEES_CONTRACT: DerivedContractFees = {
callBaseFee: new BN(0),
contractFee: new BN(0),
createBaseFee: new BN(0),