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

feat: add syncProgress #127

Merged
merged 6 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]
### Added
- [\#127](https://github.com/Manta-Network/sdk/pull/127) Add ledger sync progress
- [\#126](https://github.com/Manta-Network/sdk/pull/126) Add Reproduce extra balance example && Update SDK example && && Update extension example
- [\#119](https://github.com/Manta-Network/sdk/pull/119) Add HttpProvider && Fix private-transfer.lfs file download failure in Manta Wallet && Fix the error that the download failed on Ledger api
- [\#118](https://github.com/Manta-Network/sdk/pull/118) Add pruning function && Fix initial sync bug && Refactor walletIsBusy logic && Add get transactionDatas from posts
Expand Down
2 changes: 1 addition & 1 deletion manta-js/package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "manta-extension-sdk",
"version": "1.0.22",
"version": "1.0.23",
"description": "manta.js sdk",
"main": "./dist/browser/index.js",
"exports": {
Expand Down
6 changes: 6 additions & 0 deletions manta-js/package/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export interface ILedgerApi {
pull(checkpoint: any): any;
}

export type LedgerSyncProgress = {
current: number,
total: number,
syncType: 'initial' | 'normal',
}

export type SignedTransaction = {
posts: any;
transactionData: any;
Expand Down
29 changes: 25 additions & 4 deletions manta-js/package/src/ledger-api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Polkadot-JS Ledger Integration
import { base64Decode } from '@polkadot/util-crypto';
import { nToBigInt, u8aToU8a } from '@polkadot/util';
import { nToBigInt, u8aToBn, u8aToU8a } from '@polkadot/util';
import { MAX_RECEIVERS_PULL_SIZE, MAX_SENDERS_PULL_SIZE } from '../constants';
import $, {
$Utxos,
Expand All @@ -12,14 +12,15 @@ import $, {
utxoToJson,
currentPathToJson,
} from './decodeUtils';
import type { ILedgerApi, PalletName } from '../interfaces';
import { log } from '../utils';
import type { ILedgerApi, LedgerSyncProgress, PalletName } from '../interfaces';
import { getLedgerSyncedCount, log } from '../utils';
import { ApiPromise } from '@polkadot/api';

export default class LedgerApi implements ILedgerApi {
api: ApiPromise;
palletName: PalletName;
loggingEnabled: boolean;
syncProgress: LedgerSyncProgress;

constructor(
api: ApiPromise,
Expand All @@ -29,6 +30,11 @@ export default class LedgerApi implements ILedgerApi {
this.api = api;
this.palletName = palletName;
this.loggingEnabled = Boolean(loggingEnabled);
this.syncProgress = {
current: 0,
total: 0,
syncType: 'normal',
};
}

_log(message: string) {
Expand Down Expand Up @@ -59,8 +65,12 @@ export default class LedgerApi implements ILedgerApi {
const decodedMemberShipProofData = $CurrentPaths.decode(
base64Decode(result.membership_proof_data.toString()),
);
let sendersReceiversTotal = 0;
const membershipProofData = decodedMemberShipProofData.map(
(currentPath) => currentPathToJson(currentPath),
(currentPath) => {
sendersReceiversTotal += currentPath.leaf_index || 0;
return currentPathToJson(currentPath);
},
);

const pull_result = {
Expand All @@ -75,6 +85,12 @@ export default class LedgerApi implements ILedgerApi {
// JSON.stringify does not support bigint
pull_result.nullifier_count = nToBigInt(result.nullifier_count);

this.syncProgress = {
current: getLedgerSyncedCount(checkpoint),
total: sendersReceiversTotal,
syncType: 'initial',
};

return pull_result;
} catch (err) {
console.error(err);
Expand Down Expand Up @@ -121,6 +137,11 @@ export default class LedgerApi implements ILedgerApi {
if (this.loggingEnabled) {
this._log('pull response: ' + JSON.stringify(pull_result));
}
this.syncProgress = {
current: getLedgerSyncedCount(checkpoint),
total: u8aToBn(result.senders_receivers_total).toNumber(),
syncType: 'normal',
};
return pull_result;
} catch (err) {
console.error(err);
Expand Down
14 changes: 14 additions & 0 deletions manta-js/package/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,17 @@ export async function fetchFile(url: string): Promise<Uint8Array | null> {
export function wrapWasmError(error: Error | string) {
return typeof error === 'string' ? new Error(error) : error;
}

export function getLedgerSyncedCount(checkpoint: any) {
if (!checkpoint) {
return 0;
}
let receiverTotal = 0;
if (checkpoint.receiver_index instanceof Array) {
receiverTotal = checkpoint.receiver_index.reduce(
(total: number, value: number) => total + value,
0,
);
}
return (checkpoint.sender_index || 0) + (receiverTotal || 0);
}