Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 3 additions & 78 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Build
lib/

# Logs
logs
*.log
Expand All @@ -6,40 +9,8 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/
Expand All @@ -50,55 +21,9 @@ typings/
# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# runtime-state-export-tools
Scripts for exporting runtime state
# Runtime state export tools
Scripts to export state from a running chain over rpc.


## Run export scripts

```
yarn
yarn export-members > members.json
yarn export-forum > forum.json
yarn export-content > content.json
```
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@joystream/api-examples",
"version": "1.0.0",
"license": "GPL-3.0-only",
"scripts": {
"postinstall": "yarn tsc",
"export-members": "node lib/export_members",
"export-forum": "node lib/export_forum",
"export-content": "node lib/export_content",
"export-balances": "node lib/export_balances"
},
"dependencies": {
"@joystream/types": "^0.12.0",
"@polkadot/api": "^0.96.1",
"@polkadot/keyring": "^1.7.0-beta.5",
"@polkadot/types": "^0.96.1",
"@polkadot/util": "^1.7.0-beta.5",
"@polkadot/util-crypto": "^1.7.0-beta.5",
"@types/bn.js": "^4.11.5",
"bn.js": "^4.11.8"
},
"devDependencies": {
"@polkadot/ts": "^0.1.56",
"typescript": "3.7.2"
}
}
21 changes: 21 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @ts-check

import { ApiPromise, WsProvider } from '@polkadot/api';
import { registerJoystreamTypes } from '@joystream/types';

export default async function create_api () {
// Get URL to websocket endpoint from environment or connect to local node by default
const WS_URL = process.env['WS_URL'] || 'ws://127.0.0.1:9944'

// Initialise the provider
const provider = new WsProvider(WS_URL);

// register types before creating the api
registerJoystreamTypes();

// Create the API and wait until ready
let api = await ApiPromise.create({ provider });
await api.isReady;

return api;
}
67 changes: 67 additions & 0 deletions src/export_balances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import create_api from './api'
import { ApiPromise } from '@polkadot/api'
import { Profile, MemberId } from '@joystream/types/members'
import { Option } from '@polkadot/types/'
import { AccountId } from '@polkadot/types/interfaces'

main()

async function main () {
const api = await create_api()
// WIP
console.log(await enumerate_member_accounts(api))
api.disconnect()
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still a Work in progress will update in later PR today hopefully.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basic implementation done in 0b787aa
will improve in future PR

// member accounts: get freebalance and reserved balances (council participation)
async function enumerate_member_accounts(api: ApiPromise) : Promise<AccountId[]> {
const first = 0
const next = (await api.query.members.membersCreated() as MemberId).toNumber();

let accounts: AccountId[] = [];

for (let id = first; id < next; id++ ) {
const profile = await api.query.members.memberProfile(id) as Option<Profile>;

if (profile.isSome) {
const p = profile.unwrap();
if (!accounts.includes(p.root_account)) {
accounts.push(p.root_account)
}
if (!accounts.includes(p.controller_account)) {
accounts.push(p.controller_account)
}
}
}

return accounts;
}

// TODO:
// staked amounts: we will computer the staked amount associated with various roles and
// add it to the free balance to be the starting freebalance on new chain.

// * enumerate council seat accounts and backers
// calculated stake amount from council/election structures or just reserved balance from balances module?
// * working groups
// storage -> stake_id -> staked amount (staking account)
// role account
// identify the member by member id and balances to the member account balance
// content -> curators -> stake_id (role and application) -> staked amount?
// role account
// identify the member by member id and add balances to the member account balance
// * Stake behind an active proposal - proposal created by a member -> add to their root account balance
// * Validators -> stash and controller accounts - can't identify an associated member so just include the
// the accounts as is, read lock information real balance in the stash account, controller just free balance.

// since we have not enabled the account indices OnNewAccount hook, we cannot enumerate accounts.
// there may be accounts used in past roles that have balances but at snapshot time are not associated
// with a member or active role.
// To enumerate these accounts we must iterate over past Events from the system
// transfer event and or account created events
// assume no locks, reservation or stake is associated, so just lookup the freebalance for these accounts

// to avoid doing this search for accounts, we can advise members if they have any balance in accounts
// not associated with a member or role to transfer that balance to one of their member accounts

// compare the computed balance with total issuance as a sanity check.
Loading