Skip to content

Commit

Permalink
implement guess the number public getters
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed Oct 9, 2024
1 parent f199c11 commit 6ef39c2
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/types/src/interfaces/augment-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { Assignment, AssignmentCount, AssignmentParams, Attestation, Attest
import type { FixedI64F64, IpfsCid, PalletString } from '@encointer/types/interfaces/common';
import type { AnnouncementSigner, Bip340, CidDigest, CidName, CommunityCeremony, CommunityIdentifier, CommunityMetadataType, CommunityRules, DegreeFixed, DegreeRpc, GeoHash, Location, LocationRpc, NominalIncomeType } from '@encointer/types/interfaces/community';
import type { EncointerBalanceTransferArgs, EncointerGetter, EncointerGetterArgs, EncointerPublicGetter, EncointerTrustedCall, EncointerTrustedCallSigned, EncointerTrustedGetter, EncointerTrustedGetterSigned, GrantReputationArgs, RegisterAttestationsArgs, RegisterParticipantArgs } from '@encointer/types/interfaces/encointerWorker';
import type { BalanceSetBalanceArgs, BalanceShieldArgs, BalanceTransferArgs, BalanceUnshieldArgs, GuessTheNumberArgs, GuessTheNumberSetWinningsArgs, GuessType, IntegriteeGetter, IntegriteePublicGetter, IntegriteeTrustedCall, IntegriteeTrustedCallSigned, IntegriteeTrustedGetter, IntegriteeTrustedGetterSigned, IntegriteeTrustedOperation, TimestampSetArgs } from '@encointer/types/interfaces/integriteeWorker';
import type { BalanceSetBalanceArgs, BalanceShieldArgs, BalanceTransferArgs, BalanceUnshieldArgs, GuessTheNumberArgs, GuessTheNumberInfo, GuessTheNumberSetWinningsArgs, GuessType, IntegriteeGetter, IntegriteePublicGetter, IntegriteeTrustedCall, IntegriteeTrustedCallSigned, IntegriteeTrustedGetter, IntegriteeTrustedGetterSigned, IntegriteeTrustedOperation, TimestampSetArgs } from '@encointer/types/interfaces/integriteeWorker';
import type { SchedulerState, SystemNumber } from '@encointer/types/interfaces/scheduler';
import type { DirectRequestStatus, Enclave, EnclaveFingerprint, GetterArgs, ParentchainId, Request, RpcReturnValue, ShardIdentifier, TrustedOperationStatus, Vault, WorkerEncoded } from '@encointer/types/interfaces/worker';
import type { Data, StorageKey } from '@polkadot/types';
Expand Down Expand Up @@ -595,6 +595,7 @@ declare module '@polkadot/types/types/registry' {
GroupIndex: GroupIndex;
GroupRotationInfo: GroupRotationInfo;
GuessTheNumberArgs: GuessTheNumberArgs;
GuessTheNumberInfo: GuessTheNumberInfo;
GuessTheNumberSetWinningsArgs: GuessTheNumberSetWinningsArgs;
GuessType: GuessType;
H1024: H1024;
Expand Down
9 changes: 9 additions & 0 deletions packages/types/src/interfaces/integriteeWorker/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ export default {
}
},
GuessType: 'u32',
GuessTheNumberInfo: {
account: 'AccountId',
balance: 'Balance',
winnings: 'Balance',
next_round_timestamp: 'Moment',
last_winners: 'Vec<AccountId>',
maybe_last_lucky_number: 'Option<GuessType>',
maybe_last_winning_distance: 'Option<GuessType>',
},
GuessTheNumberSetWinningsArgs: '(AccountId, Balance)',
GuessTheNumberArgs: '(AccountId, GuessType)',
TimestampSetArgs: '(AccountId, H160, BalanceType)',
Expand Down
15 changes: 13 additions & 2 deletions packages/types/src/interfaces/integriteeWorker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import type { BalanceType } from '@encointer/types/interfaces/balances';
import type { ParentchainId, ShardIdentifier } from '@encointer/types/interfaces/worker';
import type { Enum, Struct, u32 } from '@polkadot/types-codec';
import type { Enum, Option, Struct, Vec, u32 } from '@polkadot/types-codec';
import type { ITuple } from '@polkadot/types-codec/types';
import type { MultiSignature } from '@polkadot/types/interfaces/extrinsics';
import type { AccountId, Balance, H160 } from '@polkadot/types/interfaces/runtime';
import type { AccountId, Balance, H160, Moment } from '@polkadot/types/interfaces/runtime';

/** @name BalanceSetBalanceArgs */
export interface BalanceSetBalanceArgs extends ITuple<[AccountId, AccountId, BalanceType, BalanceType]> {}
Expand All @@ -23,6 +23,17 @@ export interface BalanceUnshieldArgs extends ITuple<[AccountId, AccountId, Balan
/** @name GuessTheNumberArgs */
export interface GuessTheNumberArgs extends ITuple<[AccountId, GuessType]> {}

/** @name GuessTheNumberInfo */
export interface GuessTheNumberInfo extends Struct {
readonly account: AccountId;
readonly balance: Balance;
readonly winnings: Balance;
readonly next_round_timestamp: Moment;
readonly last_winners: Vec<AccountId>;
readonly maybe_last_lucky_number: Option<GuessType>;
readonly maybe_last_winning_distance: Option<GuessType>;
}

/** @name GuessTheNumberSetWinningsArgs */
export interface GuessTheNumberSetWinningsArgs extends ITuple<[AccountId, Balance]> {}

Expand Down
35 changes: 32 additions & 3 deletions packages/worker-api/src/integriteeWorker.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type {Hash} from '@polkadot/types/interfaces/runtime';
import type {
ShardIdentifier, IntegriteeTrustedCallSigned, IntegriteeGetter,
ShardIdentifier, IntegriteeTrustedCallSigned, IntegriteeGetter, GuessType, GuessTheNumberArgs, GuessTheNumberInfo,
} from '@encointer/types';
import {
type RequestOptions,
type ISubmittableGetter,
Request,
type JsonRpcRequest, type TrustedGetterArgs, type TrustedSignerOptions,
type JsonRpcRequest, type TrustedGetterArgs, type TrustedSignerOptions, type PublicGetterArgs,
} from './interface.js';
import {Worker} from "./worker.js";
import {callGetter, sendTrustedCall, sendWorkerRequest} from './sendRequest.js';
import {
createGetterRpc,
createGetterRpc, createIntegriteeGetterPublic,
createSignedGetter,
createTrustedCall,
signTrustedCall,
Expand Down Expand Up @@ -46,6 +46,28 @@ export class IntegriteeWorker extends Worker {
return await submittableGetter<IntegriteeWorker, AccountInfo>(this, 'account_info', trustedGetterArgs,'AccountInfo');
}

public getGuessTheNumberLastLuckyNumberGetter(shard: string): SubmittableGetter<IntegriteeWorker, GuessType> {
const publicGetterArgs = {
shard: shard,
}
return submittablePublicGetter<IntegriteeWorker, GuessType>(this, 'guess_the_number_last_lucky_number', publicGetterArgs,'GuessType');
}

public getGuessTheNumberWinningDistanceGetter(shard: string): SubmittableGetter<IntegriteeWorker, GuessType> {
const publicGetterArgs = {
shard: shard,
}
return submittablePublicGetter<IntegriteeWorker, GuessType>(this, 'guess_the_number_last_winning_distance', publicGetterArgs,'GuessType');
}


public getGuessTheNumberInfoGetter(shard: string): SubmittableGetter<IntegriteeWorker, GuessTheNumberInfo> {
const publicGetterArgs = {
shard: shard,
}
return submittablePublicGetter<IntegriteeWorker, GuessTheNumberInfo>(this, 'guess_the_number_info', publicGetterArgs,'GuessTheNumberInfo');
}

public async trustedBalanceTransfer(
account: AddressOrPair,
shard: string,
Expand Down Expand Up @@ -139,3 +161,10 @@ export const submittableGetter = async <W extends Worker, T>(self: W, request: s
const signedGetter = await createSignedGetter(self, request, account, { signer: args?.signer })
return new SubmittableGetter<W, T>(self, shardT, signedGetter, returnType);
}

export const submittablePublicGetter = <W extends Worker, T>(self: W, request: string, args: PublicGetterArgs, returnType: string)=> {
const {shard} = args;
const shardT = self.createType('ShardIdentifier', bs58.decode(shard));
const signedGetter = createIntegriteeGetterPublic(self, request)
return new SubmittableGetter<W, T>(self, shardT, signedGetter, returnType);
}
2 changes: 1 addition & 1 deletion packages/worker-api/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface TrustedSignerOptions {
}

export interface PublicGetterArgs {
cid: string;
shard: string;
}

export type RequestArgs = PublicGetterArgs | TrustedGetterArgs | { }
Expand Down
14 changes: 9 additions & 5 deletions packages/worker-api/src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ import type {u32} from "@polkadot/types";
import bs58 from "bs58";
import type {AddressOrPair} from "@polkadot/api-base/types/submittable";

// Todo: Properly resolve cid vs shard
export const clientRequestGetterRpc = (self: IWorker, request: string, args: PublicGetterArgs) => {
const {cid} = args;
const {shard} = args;
const getter = createIntegriteeGetterPublic(self, request);
const shardT = self.createType('ShardIdentifier', bs58.decode(shard));
return createGetterRpc(self, getter, shardT);
}

export const createIntegriteeGetterPublic = (self: IWorker, request: string) => {
const getter = self.createType('IntegriteePublicGetter', {
[request]: cid
[request]: null
});

const g = self.createType('IntegriteeGetter', {
public: {
getter,
}
});
const shardT = self.createType('ShardIdentifier', bs58.decode(cid));

return createGetterRpc(self, g, shardT);
return g;
}

export const clientRequestTrustedGetterRpc = async (self: IWorker, request: string, args: TrustedGetterArgs) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/worker-api/src/worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('worker', () => {
undefined,
// Allow the worker's self-signed certificate, needed in non-reverse proxy setups
// where we talk to the worker directly.
// { rejectUnauthorized: false }
{ rejectUnauthorized: false }
),
api: null,
});
Expand Down

0 comments on commit 6ef39c2

Please sign in to comment.