-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
p-939 Add balances moving script (#2982)
* Optimize initApi * add move scripts * add prettier&&gitignore * rococo * add preimage hash * format json file * using forceBatch * adding totalissuance * changing to balances.transfer * rename script * add account_index
- Loading branch information
Showing
11 changed files
with
532 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Ignore all JSON files | ||
*.json | ||
|
||
# But don't ignore tsconfig.json | ||
!tsconfig.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"singleQuote": true, | ||
"printWidth": 120, | ||
"tabWidth": 4, | ||
"semi": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// run: pnpm exec ts-node batch-transfer.ts | ||
|
||
import { initApi } from './initApis'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const prettier = require('prettier'); | ||
import colors from 'colors'; | ||
|
||
// maximal calls are 1000 per batch | ||
const BATCH_SIZE = 750; | ||
async function encodeExtrinsic() { | ||
// params: source chain endpoint, destination chain endpoint | ||
const { sourceApi, destinationAPI } = await initApi( | ||
'wss://rpc.litmus-parachain.litentry.io', | ||
'wss://rpc.litentry-parachain.litentry.io' | ||
); | ||
console.log(colors.green('Fetching system accounts entries...')); | ||
|
||
const entries = await sourceApi.query.system.account.entries(); | ||
console.log(colors.green('system account entries length:'), entries.length); | ||
|
||
let totalIssuance = BigInt(0); | ||
let account_index = 0; | ||
|
||
const data = entries.map((res: any) => { | ||
const account = res[0].toHuman(); | ||
const data = res[1].toHuman(); | ||
const free = BigInt(data.data.free.replace(/,/g, '')); | ||
const reserved = BigInt(data.data.reserved.replace(/,/g, '')); | ||
const totalBalance = free + reserved; | ||
totalIssuance += totalBalance; | ||
account_index++; | ||
return { | ||
index: account_index, | ||
account: account, | ||
free: free.toString(), | ||
reserved: reserved.toString(), | ||
totalBalance: totalBalance.toString(), | ||
}; | ||
}); | ||
|
||
console.log('totalIssuance:', totalIssuance.toString()); | ||
|
||
const filename = `system-accounts-entries-litmus-${new Date().toISOString().slice(0, 10)}.json`; | ||
const filepath = path.join(__dirname, filename); | ||
const formattedData = prettier.format(JSON.stringify(data), { | ||
parser: 'json', | ||
printWidth: 120, | ||
tabWidth: 2, | ||
singleQuote: true, | ||
trailingComma: 'es5', | ||
}); | ||
fs.writeFileSync(filepath, formattedData); | ||
console.log(colors.green(`Data saved to ${filename} successfully.`)); | ||
|
||
let txs: any[] = []; | ||
let i = 0; | ||
let hexData = []; | ||
const extrinsicsData = []; | ||
|
||
while (data.length > 0) { | ||
const batch = data.splice(0, BATCH_SIZE); | ||
const batchTxs = batch.map((entry: any) => | ||
destinationAPI.tx.balances.transfer(entry.account[0], entry.totalBalance) | ||
); | ||
txs = txs.concat(batchTxs); | ||
if (data.length === 0 || txs.length >= BATCH_SIZE) { | ||
i++; | ||
const extrinsics = destinationAPI.tx.utility.forceBatch(batchTxs); | ||
extrinsicsData.push({ batch: i, extrinsics: extrinsics.toHex() }); | ||
|
||
hexData = [ | ||
[ | ||
{ | ||
totalIssuance: totalIssuance.toString(), | ||
}, | ||
], | ||
extrinsicsData, | ||
]; | ||
txs = []; | ||
if (data.length === 0) { | ||
const extrinsicsFilename = `extrinsics-${new Date().toISOString().slice(0, 10)}.json`; | ||
const extrinsicsFilepath = path.join(__dirname, extrinsicsFilename); | ||
|
||
const formattedHexData = prettier.format(JSON.stringify(hexData), { | ||
parser: 'json', | ||
printWidth: 120, | ||
tabWidth: 2, | ||
singleQuote: true, | ||
trailingComma: 'es5', | ||
}); | ||
|
||
fs.writeFileSync(extrinsicsFilepath, formattedHexData); | ||
console.log(colors.green(`Extrinsics saved to ${extrinsicsFilename} successfully.`)); | ||
} | ||
} | ||
} | ||
process.exit(); | ||
} | ||
|
||
encodeExtrinsic(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
const { ApiPromise, WsProvider } = require("@polkadot/api"); | ||
const { ApiPromise, WsProvider } = require('@polkadot/api'); | ||
const { cryptoWaitReady } = require('@polkadot/util-crypto'); | ||
const { fetchEndpoint, defaultEndpoint } = require('./endpoint.json'); | ||
|
||
import colors from "colors"; | ||
//fetchApi is used to fetch data from the chain | ||
const wsFetchProvider = new WsProvider(fetchEndpoint); | ||
import colors from 'colors'; | ||
|
||
//defaultAPI is used to send transactions to the chain | ||
const wsDefaultProvider = new WsProvider(defaultEndpoint); | ||
/* | ||
* @param fromEndpoint - the endpoint of the source chain | ||
* @param toEndpoint - the endpoint of the destination chain | ||
*/ | ||
|
||
export const initApi = async () => { | ||
console.log(colors.green("init api...")) | ||
const fetchApi = await ApiPromise.create({ provider: wsFetchProvider }); | ||
const defaultAPI = await ApiPromise.create({ provider: wsDefaultProvider }); | ||
await cryptoWaitReady(); | ||
console.log(colors.green("api is ready")) | ||
|
||
return { fetchApi, defaultAPI }; | ||
} | ||
export const initApi = async (fromEndpoint: string, toEndpoint: string) => { | ||
const sourceProvider = new WsProvider(fromEndpoint); | ||
const destinationProvider = new WsProvider(toEndpoint); | ||
|
||
const sourceApi = await ApiPromise.create({ provider: sourceProvider }); | ||
const destinationAPI = await ApiPromise.create({ provider: destinationProvider }); | ||
await cryptoWaitReady(); | ||
console.log(colors.green('api is ready')); | ||
|
||
return { sourceApi, destinationAPI }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,75 @@ | ||
// run: pnpm exec ts-node move-vcregistry-snapshot.ts | ||
|
||
import { initApi } from "./initApis"; | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const prettier = require("prettier"); | ||
import colors from "colors"; | ||
import { initApi } from './initApis'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const prettier = require('prettier'); | ||
import colors from 'colors'; | ||
|
||
//set the maximal calls are 500 per batch | ||
const BATCH_SIZE = 500; | ||
async function encodeExtrinsic() { | ||
const { fetchApi, defaultAPI } = await initApi(); | ||
console.log(colors.green("get vcRegistry entries...")); | ||
// params: source chain endpoint, destination chain endpoint | ||
const { sourceApi, destinationAPI } = await initApi('ws://localhost:9944', 'ws://localhost:9944'); | ||
console.log(colors.green('get vcRegistry entries...')); | ||
|
||
const entries = await fetchApi.query.vcManagement.vcRegistry.entries(); | ||
const data = entries.map((res: any) => { | ||
return { index: res[0].toHuman(), vc: res[1].toHuman() }; | ||
}); | ||
const entries = await sourceApi.query.vcManagement.vcRegistry.entries(); | ||
const data = entries.map((res: any) => { | ||
return { index: res[0].toHuman(), vc: res[1].toHuman() }; | ||
}); | ||
|
||
const filename = `VCRegistry-${new Date().toISOString().slice(0, 10)}.json`; | ||
const filepath = path.join(__dirname, filename); | ||
const formattedData = prettier.format(JSON.stringify(data), { | ||
parser: "json", | ||
printWidth: 120, | ||
tabWidth: 2, | ||
singleQuote: true, | ||
trailingComma: "es5", | ||
}); | ||
fs.writeFileSync(filepath, formattedData); | ||
console.log(colors.green(`Data saved to ${filename} successfully.`)); | ||
const filename = `VCRegistry-${new Date().toISOString().slice(0, 10)}.json`; | ||
const filepath = path.join(__dirname, filename); | ||
const formattedData = prettier.format(JSON.stringify(data), { | ||
parser: 'json', | ||
printWidth: 120, | ||
tabWidth: 2, | ||
singleQuote: true, | ||
trailingComma: 'es5', | ||
}); | ||
fs.writeFileSync(filepath, formattedData); | ||
console.log(colors.green(`Data saved to ${filename} successfully.`)); | ||
|
||
let txs: any[] = []; | ||
console.log(colors.green("vcRegistry data length"), data.length); | ||
let i = 0; | ||
const hexData = []; | ||
let txs: any[] = []; | ||
console.log(colors.green('vcRegistry data length'), data.length); | ||
let i = 0; | ||
const hexData = []; | ||
|
||
while (data.length > 0) { | ||
const batch = data.splice(0, BATCH_SIZE); | ||
const batchTxs = batch.map((entry: any) => | ||
defaultAPI.tx.vcManagement.addVcRegistryItem( | ||
entry.index[0], | ||
entry.vc.subject, | ||
entry.vc.assertion, | ||
entry.vc.hash_ | ||
) | ||
); | ||
txs = txs.concat(batchTxs); | ||
if (data.length === 0 || txs.length >= BATCH_SIZE) { | ||
i++; | ||
const extrinsics = defaultAPI.tx.utility.batch(batchTxs); | ||
hexData.push({ batch: i, extrinsics: extrinsics.toHex() }); | ||
// console.log(colors.green(`extrinsic ${i} encode`), extrinsics.toHex()); | ||
txs = []; | ||
if (data.length === 0) { | ||
const extrinsicsFilename = `extrinsics-${new Date() | ||
.toISOString() | ||
.slice(0, 10)}.json`; | ||
const extrinsicsFilepath = path.join(__dirname, extrinsicsFilename); | ||
while (data.length > 0) { | ||
const batch = data.splice(0, BATCH_SIZE); | ||
const batchTxs = batch.map((entry: any) => | ||
destinationAPI.tx.vcManagement.addVcRegistryItem( | ||
entry.index[0], | ||
entry.vc.subject, | ||
entry.vc.assertion, | ||
entry.vc.hash_ | ||
) | ||
); | ||
txs = txs.concat(batchTxs); | ||
if (data.length === 0 || txs.length >= BATCH_SIZE) { | ||
i++; | ||
const extrinsics = destinationAPI.tx.utility.batch(batchTxs); | ||
hexData.push({ batch: i, extrinsics: extrinsics.toHex() }); | ||
// console.log(colors.green(`extrinsic ${i} encode`), extrinsics.toHex()); | ||
txs = []; | ||
if (data.length === 0) { | ||
const extrinsicsFilename = `extrinsics-${new Date().toISOString().slice(0, 10)}.json`; | ||
const extrinsicsFilepath = path.join(__dirname, extrinsicsFilename); | ||
|
||
const formattedHexData = prettier.format(JSON.stringify(hexData), { | ||
parser: "json", | ||
printWidth: 120, | ||
tabWidth: 2, | ||
singleQuote: true, | ||
trailingComma: "es5", | ||
}); | ||
const formattedHexData = prettier.format(JSON.stringify(hexData), { | ||
parser: 'json', | ||
printWidth: 120, | ||
tabWidth: 2, | ||
singleQuote: true, | ||
trailingComma: 'es5', | ||
}); | ||
|
||
fs.writeFileSync(extrinsicsFilepath, formattedHexData); | ||
console.log( | ||
colors.green( | ||
`Extrinsics saved to ${extrinsicsFilename} successfully.` | ||
) | ||
); | ||
} | ||
fs.writeFileSync(extrinsicsFilepath, formattedHexData); | ||
console.log(colors.green(`Extrinsics saved to ${extrinsicsFilename} successfully.`)); | ||
} | ||
} | ||
} | ||
} | ||
process.exit(); | ||
process.exit(); | ||
} | ||
|
||
encodeExtrinsic(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
{ | ||
"name": "ts-utils", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@polkadot/api": "^10.3.1", | ||
"colors": "^1.4.0", | ||
"exceljs": "^4.3.0", | ||
"prettier": "^2.8.7", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.4" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.15.11" | ||
}, | ||
"packageManager": "[email protected]" | ||
"name": "ts-utils", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"format": "pnpm exec prettier --write '**.ts'" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@polkadot/api": "^10.3.1", | ||
"@polkadot/util-crypto": "^13.0.2", | ||
"colors": "^1.4.0", | ||
"ethers": "^6.13.2", | ||
"exceljs": "^4.3.0", | ||
"prettier": "^2.8.7", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.4" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.15.11" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
Oops, something went wrong.