Skip to content

Commit

Permalink
fix: add wait feature with fix
Browse files Browse the repository at this point in the history
  • Loading branch information
QEDK committed Nov 15, 2023
1 parent 292f2fd commit fc1da63
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 39 deletions.
56 changes: 18 additions & 38 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
'use strict'
import { Argument, Command, Option } from 'commander'
import { initialize, formatNumberToBalance, getKeyringFromSeed, isValidAddress } from 'avail-js-sdk'
import { ApiPromise } from '@polkadot/api';
import { ISubmittableResult, SignatureOptions } from '@polkadot/types/types';
import { ISubmittableResult } from '@polkadot/types/types';
import { KeyringPair } from '@polkadot/keyring/types';
import { H256 } from '@polkadot/types/interfaces';
import { spawn } from 'child_process'
// import { version } from './package.json' assert { type: "json" };
const program = new Command()

enum NetworkNames {
Expand All @@ -25,6 +22,7 @@ const NETWORK_RPC_URLS: { kate: string, goldberg: string, local: string } = {
program
.name('avail')
.description('A simple CLI for Avail network utilities')
.version('0.1.9')

const transfer = async (to: string, value: number, options: {
seed: string
Expand Down Expand Up @@ -60,34 +58,30 @@ const transfer = async (to: string, value: number, options: {
}
}

const sendTx = async (api: any, blob: string, keyring: KeyringPair, opt: Partial<any>): Promise<void> => {
const sendBlobTx = async (api: any, blob: string, keyring: KeyringPair, opt: Partial<any>, network: NetworkNames): Promise<void> => {
return new Promise((resolve, reject) => {
api.tx.dataAvailability.submitData(blob)
.signAndSend(keyring, opt, (result: ISubmittableResult) => {

if (result.status.isInBlock) {
console.log(`Transaction included at blockHash ${result.status.asInBlock}`);
}
if (result.status.isFinalized) {
console.log(`Transaction included at blockHash ${result.status.asFinalized}`);
if (result.status.isInBlock || result.status.isFinalized) {
console.log(`✅ Blob included at block hash: ${result.status.asInBlock ?? result.status.asFinalized}`);
if (typeof(network) !== 'undefined') {
console.log(`🧭 Link to explorer: https://${network}.avail.tools/#/explorer/query/${result.status.asInBlock ?? result.status.asFinalized}`)
}
resolve();
}
})
.catch((error: any) => {
console.error('Transaction failed:', error);
reject(error); // Reject the promise on error
reject('❌ Transaction failed: ' + error);
});
});
};




async function data(blob: string, options: {
seed: string;
network: NetworkNames;
rpc: string;
appId: number;
wait: boolean;
}): Promise<void> {
try {
const seed = options.seed;
Expand All @@ -98,34 +92,19 @@ async function data(blob: string, options: {
} else {
rpcUrl = NETWORK_RPC_URLS[options.network];
}
interface SignatureOptionsNew extends SignatureOptions {
app_id: number;
}

const tempConsoleWarn = console.warn;
console.warn = () => { };
const api = await initialize(rpcUrl, { noInitWarn: true });
console.warn = tempConsoleWarn;
const keyring = getKeyringFromSeed(seed);
const opt: Partial<any> = { app_id: options.appId, nonce: -1 };


// await api.tx.dataAvailability.submitData(blob).signAndSend(
// keyring, // sender
// opt, // options
// (result: ISubmittableResult) => {
// //uncomment the below line👇 to see the whole status flow of the transaction
// // console.log(`Tx status: ${result.status}`);
// if (result.status.isReady) {
// console.log(`result is ready`)
// }
// if (result.status.isInBlock) {
// let block_hash = result.status.asInBlock;
// let extrinsic_hash = result.txHash;
// console.log(`\nExtrinsic hash: ${result.txHash} is in block`);
// }
// })
await sendTx(api, blob, keyring, opt);
console.log('✅ Data blob sent to Avail');
if (options.wait) {
await sendBlobTx(api, blob, keyring, opt, options.network);
} else {
await api.tx.dataAvailability.submitData(blob).signAndSend(keyring, opt);
console.log('✅ Data blob sent to Avail');
}
process.exit(0);
} catch (err) {
console.error(err);
Expand Down Expand Up @@ -171,6 +150,7 @@ program
.addOption(new Option('-r, --rpc <RPC url>', 'the RPC url to connect to').env('AVAIL_RPC_URL').default(NETWORK_RPC_URLS.goldberg))
.addOption(new Option('-s, --seed <seed phrase>', 'the seed phrase for the Avail account').env('AVAIL_SEED').makeOptionMandatory())
.addOption(new Option('-a, --app-id <app ID>', 'the blob will be submitted with this app ID').default(0))
.option('-w, --wait', 'wait for extrinsic inclusion')
.addArgument(new Argument('<blob>', 'the data blob to submit'))
.action(data)

Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"typescript",
"cli"
],
"type":"module",
"type": "module",
"scripts": {
"build": "tsc",
"build:publish": "tsc && npm publish --access=public",
Expand All @@ -35,6 +35,7 @@
"commander": "^11.1.0"
},
"devDependencies": {
"@polkadot/types": "^10.10.1",
"@types/node": "^20.9.0",
"ts-standard": "^12.0.2",
"typescript": "^5.2.2"
Expand Down

0 comments on commit fc1da63

Please sign in to comment.