Update AggregatorSetConfigParams fields to be optional

This commit is contained in:
Jackson Jessup 2022-09-21 13:02:16 -04:00
parent 5c9127d9d8
commit fb6acf8d8b
2 changed files with 38 additions and 36 deletions

View File

@ -56,6 +56,7 @@
"chan": "^0.6.1", "chan": "^0.6.1",
"crypto-js": "^4.0.0", "crypto-js": "^4.0.0",
"glob": "^8.0.3", "glob": "^8.0.3",
"lodash": "^4.17.21",
"long": "^4.0.0", "long": "^4.0.0",
"mocha": "^9.1.1", "mocha": "^9.1.1",
"node-fetch": "^3.2.6" "node-fetch": "^3.2.6"

View File

@ -23,6 +23,7 @@ import { OracleJob } from "@switchboard-xyz/common";
import assert from "assert"; import assert from "assert";
import Big from "big.js"; import Big from "big.js";
import * as crypto from "crypto"; import * as crypto from "crypto";
import lodash from "lodash";
export type SwitchboardProgram = anchor.Program; export type SwitchboardProgram = anchor.Program;
@ -711,42 +712,22 @@ export class AggregatorHistoryRow {
} }
} }
export interface AggregatorSetConfigParams { export type AggregatorSetConfigParams = Partial<{
name?: Buffer; name: Buffer;
metadata?: Buffer; metadata: Buffer;
batchSize: number; batchSize: number;
minOracleResults: number; minOracleResults: number;
minJobResults: number; minJobResults: number;
minUpdateDelaySeconds: number; minUpdateDelaySeconds: number;
forceReportPeriod?: number; forceReportPeriod: number;
varianceThreshold?: number; varianceThreshold: number;
} }>;
export interface AggregatorSetBatchSizeParams {
batchSize: number;
authority?: Keypair;
}
export interface AggregatorSetMinJobsParams {
minJobResults: number;
authority?: Keypair;
}
export interface AggregatorSetMinOraclesParams {
minOracleResults: number;
authority?: Keypair;
}
export interface AggregatorSetQueueParams { export interface AggregatorSetQueueParams {
queueAccount: OracleQueueAccount; queueAccount: OracleQueueAccount;
authority?: Keypair; authority?: Keypair;
} }
export interface AggregatorSetUpdateIntervalParams {
newInterval: number;
authority?: Keypair;
}
/** /**
* Account type representing an aggregator (data feed). * Account type representing an aggregator (data feed).
*/ */
@ -1168,16 +1149,16 @@ export class AggregatorAccount {
params.authority ?? this.keypair ?? programWallet(this.program); params.authority ?? this.keypair ?? programWallet(this.program);
return program.methods return program.methods
.aggregatorSetConfig({ .aggregatorSetConfig({
name: (params.name ?? Buffer.from("")).slice(0, 32), name: findKeyInObject(params, "name"),
metadata: (params.metadata ?? Buffer.from("")).slice(0, 128), metadata: findKeyInObject(params, "metadata"),
batchSize: params.batchSize, batchSize: findKeyInObject(params, "batchSize"),
minOracleResults: params.minOracleResults, minOracleResults: findKeyInObject(params, "minOracleResults"),
minUpdateDelaySeconds: params.minUpdateDelaySeconds, minUpdateDelaySeconds: findKeyInObject(params, "minUpdateDelaySeconds"),
minJobResults: params.minJobResults, minJobResults: findKeyInObject(params, "minJobResults"),
forceReportPeriod: new anchor.BN(params.forceReportPeriod ?? 0), forceReportPeriod: findKeyInObject(params, "forceReportPeriod"),
varianceThreshold: params.varianceThreshold varianceThreshold: findKeyInObject(params, "varianceThreshold", (o) =>
? SwitchboardDecimal.fromBig(new Big(params.varianceThreshold)) SwitchboardDecimal.fromBig(new Big(o))
: undefined, ),
}) })
.accounts({ .accounts({
aggregator: this.publicKey, aggregator: this.publicKey,
@ -4340,6 +4321,26 @@ function safeDiv(number_: Big, denominator: Big, decimals = 20): Big {
return result; 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<T extends Record<string, unknown>>(
object: T,
key: keyof T,
transform?: (obj: any) => any
) {
const obj = lodash.get<T, keyof T, null>(object, key, null);
return lodash.isNull(obj) || lodash.isUndefined(transform)
? obj
: transform(obj);
}
export class AnchorWallet implements anchor.Wallet { export class AnchorWallet implements anchor.Wallet {
constructor(readonly payer: Keypair) { constructor(readonly payer: Keypair) {
this.payer = payer; this.payer = payer;