From fb6acf8d8b92270c51ebc62d26b13154ac25fa61 Mon Sep 17 00:00:00 2001 From: Jackson Jessup Date: Wed, 21 Sep 2022 13:02:16 -0400 Subject: [PATCH] Update AggregatorSetConfigParams fields to be optional --- libraries/ts/package.json | 1 + libraries/ts/src/sbv2.ts | 73 ++++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/libraries/ts/package.json b/libraries/ts/package.json index f182300..d130f14 100644 --- a/libraries/ts/package.json +++ b/libraries/ts/package.json @@ -56,6 +56,7 @@ "chan": "^0.6.1", "crypto-js": "^4.0.0", "glob": "^8.0.3", + "lodash": "^4.17.21", "long": "^4.0.0", "mocha": "^9.1.1", "node-fetch": "^3.2.6" diff --git a/libraries/ts/src/sbv2.ts b/libraries/ts/src/sbv2.ts index 209dbb2..46fdf65 100644 --- a/libraries/ts/src/sbv2.ts +++ b/libraries/ts/src/sbv2.ts @@ -23,6 +23,7 @@ import { OracleJob } from "@switchboard-xyz/common"; import assert from "assert"; import Big from "big.js"; import * as crypto from "crypto"; +import lodash from "lodash"; export type SwitchboardProgram = anchor.Program; @@ -711,42 +712,22 @@ export class AggregatorHistoryRow { } } -export interface AggregatorSetConfigParams { - name?: Buffer; - metadata?: Buffer; +export type AggregatorSetConfigParams = Partial<{ + name: Buffer; + metadata: Buffer; batchSize: number; minOracleResults: number; minJobResults: number; minUpdateDelaySeconds: number; - forceReportPeriod?: number; - varianceThreshold?: number; -} - -export interface AggregatorSetBatchSizeParams { - batchSize: number; - authority?: Keypair; -} - -export interface AggregatorSetMinJobsParams { - minJobResults: number; - authority?: Keypair; -} - -export interface AggregatorSetMinOraclesParams { - minOracleResults: number; - authority?: Keypair; -} + forceReportPeriod: number; + varianceThreshold: number; +}>; export interface AggregatorSetQueueParams { queueAccount: OracleQueueAccount; authority?: Keypair; } -export interface AggregatorSetUpdateIntervalParams { - newInterval: number; - authority?: Keypair; -} - /** * Account type representing an aggregator (data feed). */ @@ -1168,16 +1149,16 @@ export class AggregatorAccount { params.authority ?? this.keypair ?? programWallet(this.program); return program.methods .aggregatorSetConfig({ - name: (params.name ?? Buffer.from("")).slice(0, 32), - metadata: (params.metadata ?? Buffer.from("")).slice(0, 128), - batchSize: params.batchSize, - minOracleResults: params.minOracleResults, - minUpdateDelaySeconds: params.minUpdateDelaySeconds, - minJobResults: params.minJobResults, - forceReportPeriod: new anchor.BN(params.forceReportPeriod ?? 0), - varianceThreshold: params.varianceThreshold - ? SwitchboardDecimal.fromBig(new Big(params.varianceThreshold)) - : undefined, + name: findKeyInObject(params, "name"), + metadata: findKeyInObject(params, "metadata"), + batchSize: findKeyInObject(params, "batchSize"), + minOracleResults: findKeyInObject(params, "minOracleResults"), + minUpdateDelaySeconds: findKeyInObject(params, "minUpdateDelaySeconds"), + minJobResults: findKeyInObject(params, "minJobResults"), + forceReportPeriod: findKeyInObject(params, "forceReportPeriod"), + varianceThreshold: findKeyInObject(params, "varianceThreshold", (o) => + SwitchboardDecimal.fromBig(new Big(o)) + ), }) .accounts({ aggregator: this.publicKey, @@ -4340,6 +4321,26 @@ function safeDiv(number_: Big, denominator: Big, decimals = 20): Big { return result; } +/** + * Given an {@linkcode object} and a {@linkcode key}, try to produce a value. + * + * @param object _REQUIRED_: The object search for value output. + * @param key _REQUIRED_: A key of {@linkcode object} to check for value output. + * @param transform _OPTIONAL_: A hook that can be used to transform the result value if it is found. + * + * @returns The (optionally transformed) keyed object found in {@linkcode object}. + */ +function findKeyInObject>( + object: T, + key: keyof T, + transform?: (obj: any) => any +) { + const obj = lodash.get(object, key, null); + return lodash.isNull(obj) || lodash.isUndefined(transform) + ? obj + : transform(obj); +} + export class AnchorWallet implements anchor.Wallet { constructor(readonly payer: Keypair) { this.payer = payer;