Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import json example #936

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions scripts/examples/asset.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FPNumber, api } from '@sora-substrate/sdk';

import { delay, withConnectedAccount } from './util';
import { delay, withConnectedAccount, withImportedFromJsonAccount } from './util';

async function main(): Promise<void> {
await withConnectedAccount(async () => {
await withImportedFromJsonAccount(async () => {
console.info('RegisterAsset fee', FPNumber.fromCodecValue(api.NetworkFee.RegisterAsset).toString());
console.info('Transfer fee', FPNumber.fromCodecValue(api.NetworkFee.Transfer).toString());
console.info('XorlessTransfer fee', FPNumber.fromCodecValue(api.NetworkFee.XorlessTransfer).toString());
Expand Down
34 changes: 34 additions & 0 deletions scripts/examples/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { api, connection } from '@sora-substrate/sdk';
import { SORA_ENV } from '@sora-substrate/types/scripts/consts';

// import { mnemonicGenerate } from '@polkadot/util-crypto'; // TODO: use it within the faucet

const TST_MNEMONIC = 'street firm worth record skin taste legend lobster magnet stove drive side';

const TST_JSON_ACCOUNT_PASSWORD = 'soratest';
const TST_JSON_ACCOUNT = {
encoded:
'7jzkrA8197VeKooU7peSfCNtgsgLlhA13Fk6lHdQLKMAgAAAAQAAAAgAAADe2IIB9bellrk/4JsL6BZqDtpHHv9GfrP1gA+S5L91m9A2QnoE3pITBaUTcWGWlFJVuS1oizMPsrlyilAC08q3AmCdCxcZr6FqXBVBXlpL/9gcS6h+rHB6cuQTvWaYXnLOolRctaB9oIKbaJ2z3bOZ62T7UGkU5trZaKvGF2gCH5TE7oEhCcmGQbQBGUm56ThhU+F9qqIacLcbIOwx',
encoding: { content: ['pkcs8', 'sr25519'], type: ['scrypt', 'xsalsa20-poly1305'], version: '3' },
address: '5GLDeyxgNzsnm4NeSHZd9imbSMaV2RUPGRSkchxsUqSbfBpu',
meta: { isHidden: false, name: 'soratest', whenCreated: 1691752486854 },
};

export async function delay(ms = 40_000): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, ms));
}
Expand All @@ -21,6 +31,22 @@ export async function connectAndImportAccount(
api.importAccount(mnemonic ?? TST_MNEMONIC, 'name', 'pass');
}

export async function connectAndImportAccountJson(
env: SORA_ENV | string = SORA_ENV.stage,
json: any,
password: string,
withKeyringLoading = true
): Promise<void> {
await connection.open(env);
console.info('Connected: ' + env);
await api.initialize(withKeyringLoading);
await api.calcStaticNetworkFees();

api.restoreAccountFromJson(json, password);

await api.loginAccount(json.address);
}

export async function disconnect(): Promise<void> {
api.logout();
await connection.close();
Expand All @@ -36,3 +62,11 @@ export async function withConnectedAccount(
await fn();
await disconnect();
}

export async function withImportedFromJsonAccount(fn: Function, env: SORA_ENV | string = SORA_ENV.test): Promise<void> {
await connectAndImportAccountJson(env, TST_JSON_ACCOUNT, TST_JSON_ACCOUNT_PASSWORD, true);
api.unlockPair(TST_JSON_ACCOUNT_PASSWORD);
await fn();
api.lockPair();
await disconnect();
}