Export SwitchboardDecimal from @switchboard-xyz/common

This commit is contained in:
Jackson Jessup 2022-11-02 00:51:43 +00:00
parent 459472caf7
commit 9db4921dae
2 changed files with 2052 additions and 3578 deletions

View File

@ -18,7 +18,7 @@ import {
Transaction,
TransactionSignature,
} from "@solana/web3.js";
import { OracleJob } from "@switchboard-xyz/common";
import { OracleJob, SwitchboardDecimal } from "@switchboard-xyz/common";
import assert from "assert";
import Big from "big.js";
import * as crypto from "crypto";
@ -26,6 +26,8 @@ import lodash from "lodash";
export type SwitchboardProgram = anchor.Program;
export type { SwitchboardDecimal } from "@switchboard-xyz/common";
/**
* Switchboard Devnet Program ID
* 2TfB33aLaneQb5TNVwyDz3jSZXS6jdW2ARw1Dgf84XCG
@ -146,109 +148,6 @@ export function watchSwitchboardAccount(
);
}
/**
* Switchboard precisioned representation of numbers.
*/
export class SwitchboardDecimal {
public constructor(
public readonly mantissa: anchor.BN,
public readonly scale: number
) {}
/**
* Convert untyped object to a Switchboard decimal, if possible.
* @param obj raw object to convert from
* @return SwitchboardDecimal
*/
public static from(obj: any): SwitchboardDecimal {
return new SwitchboardDecimal(new anchor.BN(obj.mantissa), obj.scale);
}
/**
* Convert a Big.js decimal to a Switchboard decimal.
* @param big a Big.js decimal
* @return a SwitchboardDecimal
*/
public static fromBig(big: Big): SwitchboardDecimal {
// Round to fit in Switchboard Decimal
// TODO: smarter logic.
big = big.round(20);
let mantissa: anchor.BN = new anchor.BN(big.c.join(""), 10);
// Set the scale. Big.exponenet sets scale from the opposite side
// SwitchboardDecimal does.
let scale = big.c.slice(1).length - big.e;
if (scale < 0) {
mantissa = mantissa.mul(
new anchor.BN(10, 10).pow(new anchor.BN(Math.abs(scale), 10))
);
scale = 0;
}
if (scale < 0) {
throw new Error(`SwitchboardDecimal: Unexpected negative scale.`);
}
if (scale >= 28) {
throw new Error("SwitchboardDecimalExcessiveScaleError");
}
// Set sign for the coefficient (mantissa)
mantissa = mantissa.mul(new anchor.BN(big.s, 10));
const result = new SwitchboardDecimal(mantissa, scale);
if (big.sub(result.toBig()).abs().gt(new Big(0.00005))) {
throw new Error(
`SwitchboardDecimal: Converted decimal does not match original:\n` +
`out: ${result.toBig().toNumber()} vs in: ${big.toNumber()}\n` +
`-- result mantissa and scale: ${result.mantissa.toString()} ${result.scale.toString()}\n` +
`${result} ${result.toBig()}`
);
}
return result;
}
/**
* SwitchboardDecimal equality comparator.
* @param other object to compare to.
* @return true iff equal
*/
public eq(other: SwitchboardDecimal): boolean {
return this.mantissa.eq(other.mantissa) && this.scale === other.scale;
}
/**
* Convert SwitchboardDecimal to big.js Big type.
* @return Big representation
*/
public toBig(): Big {
let mantissa: anchor.BN = new anchor.BN(this.mantissa, 10);
let s = 1;
const c: Array<number> = [];
const ZERO = new anchor.BN(0, 10);
const TEN = new anchor.BN(10, 10);
if (mantissa.lt(ZERO)) {
s = -1;
mantissa = mantissa.abs();
}
while (mantissa.gt(ZERO)) {
c.unshift(mantissa.mod(TEN).toNumber());
mantissa = mantissa.div(TEN);
}
const e = c.length - this.scale - 1;
const result = new Big(0);
if (c.length === 0) {
return result;
}
result.s = s;
result.c = c;
result.e = e;
return result;
}
toString() {
return this.toBig().toString();
}
}
/**
* Input parameters for constructing wrapped representations of Switchboard accounts.
*/

File diff suppressed because it is too large Load Diff