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

Add account info getter #111

Closed
wants to merge 11 commits into from
4 changes: 2 additions & 2 deletions packages/types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default {
};
```

A subsequent `yarn generate:defs` updates the types.
A subsequent `yarn generate:types` updates the types.

To update the polkadot types, upgrade the `@polkadot/types` package and generate the types again.

The type generation process is elaborated on in the polkadot.{js} [docs](https://polkadot.js.org/docs/api/examples/promise/typegen#types-setup).
The type generation process is elaborated on in the polkadot.{js} [docs](https://polkadot.js.org/docs/api/examples/promise/typegen#types-setup).
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export default {
},
IntegriteeTrustedGetter: {
_enum: {
free_balance: 'AccountId',
reserved_balance: 'AccountId',
nonce: 'AccountId',
account_info: 'AccountId',
}
},
IntegriteeTrustedGetterSigned: {
Expand Down
10 changes: 3 additions & 7 deletions packages/types/src/interfaces/integriteeWorker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,9 @@ export interface IntegriteeTrustedCallSigned extends Struct {

/** @name IntegriteeTrustedGetter */
export interface IntegriteeTrustedGetter extends Enum {
readonly isFreeBalance: boolean;
readonly asFreeBalance: AccountId;
readonly isReservedBalance: boolean;
readonly asReservedBalance: AccountId;
readonly isNonce: boolean;
readonly asNonce: AccountId;
readonly type: 'FreeBalance' | 'ReservedBalance' | 'Nonce';
readonly isAccountInfo: boolean;
readonly asAccountInfo: AccountId;
readonly type: 'AccountInfo';
Comment on lines -78 to +94
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 has been generated by a subsequent yarn generated:types

}

/** @name IntegriteeTrustedGetterSigned */
Expand Down
35 changes: 13 additions & 22 deletions packages/worker-api/src/integriteeWorker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Keyring } from '@polkadot/api';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { paseoNetwork} from './testUtils/networks.js';
import {localDockerNetwork} from './testUtils/networks.js';
import { IntegriteeWorker } from './integriteeWorker.js';
import WS from 'websocket';
import {type KeyringPair} from "@polkadot/keyring/types";

const {w3cwebsocket: WebSocket} = WS;

describe('worker', () => {
const network = paseoNetwork();
const network = localDockerNetwork();
let keyring: Keyring;
let worker: IntegriteeWorker;
let alice: KeyringPair;
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('worker', () => {
// skip it, as this requires a worker (and hence a node) to be running
// To my knowledge jest does not have an option to run skipped tests specifically, does it?
// Todo: add proper CI to test this too.
describe.skip('needs worker and node running', () => {
describe('needs worker and node running', () => {
describe('getWorkerPubKey', () => {
it('should return value', async () => {
const result = await worker.getShieldingKey();
Expand All @@ -56,38 +56,29 @@ describe('worker', () => {
});
});

describe('getBalance', () => {
describe('getNonce', () => {
it('should return value', async () => {
const result = await worker.getBalance(charlie, network.mrenclave);
console.log('getBalance toNumber:', result.toString(10));
const result = await worker.getNonce(alice, network.mrenclave);
console.log('getAccountInfo', result);
expect(result).toBeDefined();
});
});

describe('getBalanceGetter', () => {
it('should return value', async () => {
const getter = await worker.getBalanceGetter(charlie, network.mrenclave);
console.log(`BalanceGetter: ${JSON.stringify(getter)}`);
const result = await getter.send();
console.log('getBalance toNumber:', result.toString(10));
expect(result).toBeDefined();
});
});

describe('getNonce', () => {
describe('getAccountInfo', () => {
it('should return value', async () => {
const result = await worker.getNonce(alice, network.mrenclave);
console.log('getNonce', result);
const result = await worker.getAccountInfo(alice, network.mrenclave);
console.log('getAccountInfo', result);
expect(result).toBeDefined();
});
});

describe('getNonceGetter', () => {
describe('getAccountInfoGetter', () => {
it('should return value', async () => {
const getter = await worker.getNonceGetter(charlie, network.mrenclave);
console.log(`NonceGetter: ${JSON.stringify(getter)}`);
const getter = await worker.getAccountInfoGetter(charlie, network.mrenclave);
console.log(`AccountInfoGetter: ${JSON.stringify(getter)}`);
const result = await getter.send();
console.log('getNonce', result);
console.log('getAccountInfo', result);
expect(result).toBeDefined();
});
});
Expand Down
39 changes: 14 additions & 25 deletions packages/worker-api/src/integriteeWorker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type {u32} from '@polkadot/types';

import type {Balance, Hash} from '@polkadot/types/interfaces/runtime';
import type {Hash} from '@polkadot/types/interfaces/runtime';
import type {
ShardIdentifier, IntegriteeTrustedCallSigned, IntegriteeGetter,
} from '@encointer/types';
Expand All @@ -20,41 +18,31 @@ import {
} from "./requests.js";
import bs58 from "bs58";
import type {AddressOrPair} from "@polkadot/api-base/types/submittable";
import type { AccountInfo } from "@polkadot/types/interfaces/system";
import type {u32} from "@polkadot/types-codec";

export class IntegriteeWorker extends Worker {

public async getNonce(accountOrPubKey: AddressOrPair, shard: string, singerOptions?: TrustedSignerOptions, requestOptions?: RequestOptions): Promise<u32> {
return await callGetter<u32>(this, [Request.TrustedGetter, 'nonce', 'u32'], {
shard: shard,
account: accountOrPubKey,
signer: singerOptions?.signer
}, requestOptions)
const info = await this.getAccountInfo(accountOrPubKey, shard, singerOptions, requestOptions);
return info.nonce;
}

public async getNonceGetter(accountOrPubKey: AddressOrPair, shard: string, signerOptions?: TrustedSignerOptions): Promise<SubmittableGetter<IntegriteeWorker, Balance>> {
const trustedGetterArgs = {
public async getAccountInfo(accountOrPubKey: AddressOrPair, shard: string, singerOptions?: TrustedSignerOptions, requestOptions?: RequestOptions): Promise<AccountInfo> {
return await callGetter<AccountInfo>(this, [Request.TrustedGetter, 'account_info', 'AccountInfo'], {
shard: shard,
account: accountOrPubKey,
signer: signerOptions?.signer,
}
return await submittableGetter<IntegriteeWorker, Balance>(this, 'nonce', trustedGetterArgs,'u32');
}

public async getBalance(accountOrPubKey: AddressOrPair, shard: string, signerOptions?: TrustedSignerOptions, requestOptions?: RequestOptions): Promise<Balance> {
return await callGetter<Balance>(this, [Request.TrustedGetter, 'free_balance', 'Balance'], {
shard: shard,
account: accountOrPubKey,
signer: signerOptions?.signer
signer: singerOptions?.signer
}, requestOptions)
}

public async getBalanceGetter(accountOrPubKey: AddressOrPair, shard: string, signerOptions?: TrustedSignerOptions): Promise<SubmittableGetter<IntegriteeWorker, Balance>> {
public async getAccountInfoGetter(accountOrPubKey: AddressOrPair, shard: string, signerOptions?: TrustedSignerOptions): Promise<SubmittableGetter<IntegriteeWorker, AccountInfo>> {
const trustedGetterArgs = {
shard: shard,
account: accountOrPubKey,
signer: signerOptions?.signer
signer: signerOptions?.signer,
}
return await submittableGetter<IntegriteeWorker, Balance>(this, 'free_balance', trustedGetterArgs,'Balance');
return await submittableGetter<IntegriteeWorker, AccountInfo>(this, 'account_info', trustedGetterArgs,'AccountInfo');
}

public async trustedBalanceTransfer(
Expand All @@ -67,7 +55,7 @@ export class IntegriteeWorker extends Worker {
signerOptions?: TrustedSignerOptions,
requestOptions?: RequestOptions,
): Promise<Hash> {
const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions, requestOptions);
const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions, requestOptions)
const shardT = this.createType('ShardIdentifier', bs58.decode(shard));
const params = this.createType('BalanceTransferArgs', [from, to, amount])
const call = createTrustedCall(this, ['balance_transfer', 'BalanceTransferArgs'], params);
Expand All @@ -85,7 +73,8 @@ export class IntegriteeWorker extends Worker {
signerOptions?: TrustedSignerOptions,
requestOptions?: RequestOptions,
): Promise<Hash> {
const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions, requestOptions);
const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions, requestOptions)

const shardT = this.createType('ShardIdentifier', bs58.decode(shard));
const params = this.createType('BalanceUnshieldArgs', [fromIncognitoAddress, toPublicAddress, amount, shardT])
const call = createTrustedCall(this, ['balance_unshield', 'BalanceUnshieldArgs'], params);
Expand Down
Loading