Skip to content

Commit

Permalink
Add core/base to new sdk directory
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Nov 7, 2023
1 parent 54a8fe7 commit 335fc22
Show file tree
Hide file tree
Showing 60 changed files with 10,316 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdk/js-connect/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
1 change: 1 addition & 0 deletions sdk/js-connect/__tests__/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Integration tests for the entire SDK
4 changes: 4 additions & 0 deletions sdk/js-connect/core/base/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SDK Base
--------

Provides Constants and utils
36 changes: 36 additions & 0 deletions sdk/js-connect/core/base/__tests__/amount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { normalizeAmount } from "../src/";

describe("Amount Tests", function () {
const cases: [number | string, bigint, bigint][] = [
[1, 18n, BigInt(1 + "0".repeat(18))],
[0, 18n, BigInt(0)],
[1, 2n, BigInt(1 + "0".repeat(2))],
[3.2, 2n, BigInt(320)],
["1.4", 12n, BigInt(1400000000000)],
["0.0001", 12n, BigInt(100000000)],
["0", 2n, BigInt(0)],
// should we throw on negative?
[-3, 2n, BigInt(-300)],
["-3", 2n, BigInt(-300)],
];

const badCases: [number | string, bigint][] = [
["0.000001", 2n],
["-0.000001", 2n],
["3", -2n],
];

it("should correctly normalize values", function () {
for (const [amt, dec, expected] of cases) {
const actual = normalizeAmount(amt, dec);
expect(actual).toEqual(expected);
}
});

it("should correctly fail on unexpected values", function () {
for (const [amt, dec] of badCases) {
const actual = () => normalizeAmount(amt, dec);
expect(actual).toThrow();
}
});
});
77 changes: 77 additions & 0 deletions sdk/js-connect/core/base/__tests__/encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { test, describe, expect } from "@jest/globals";
import { toUint8Array, hex, b64, b58 } from "../src/utils/encoding";

// A table of base64 encoded strings and their decoded string equivalents
const base64Table = [
["", ""],
["f", "Zg=="],
["fo", "Zm8="],
["foo", "Zm9v"],
["foob", "Zm9vYg=="],
["fooba", "Zm9vYmE="],
["foobar", "Zm9vYmFy"]
];

// A table of hex encoded strings and their decoded string equivalents
const hexTable = [
["", ""],
["f", "66"],
["fo", "666f"],
["foo", "666f6f"],
["foob", "666f6f62"],
["fooba", "666f6f6261"],
["foobar", "666f6f626172"]
];

// A table of base58 encoded strings and their decoded string equivalents
const base58Table = [
["", ""],
["f", "2m"],
["fo", "8o8"],
["foo", "bQbp"],
["foob", "3csAg9"],
["fooba", "CZJRhmz"],
["foobar", "t1Zv2yaZ"]
];


describe("codec Tests", function () {

describe("base64", function () {
test.each(base64Table)("encodes properly", function (plain, expected) {
const actual = b64.encode(plain)
expect(actual).toEqual(expected)
});

test.each(base64Table)("decodes properly", function (expected, encoded) {
const actual = b64.decode(encoded)
expect(actual).toEqual(toUint8Array(expected))
});
})

describe("hex", function () {
test.each(hexTable)("encodes properly", function (plain, expected) {
const actual = hex.encode(plain)
expect(actual).toEqual(expected)
});

test.each(hexTable)("decodes properly", function (expected, encoded) {
const actual = hex.decode(encoded)
expect(actual).toEqual(toUint8Array(expected))
});
})


describe("base58", function () {
test.each(base58Table)("encodes properly", function (plain, expected) {
const actual = b58.encode(plain)
expect(actual).toEqual(expected)
});

test.each(base58Table)("decodes properly", function (expected, encoded) {
const actual = b58.decode(encoded)
expect(actual).toEqual(toUint8Array(expected))
});
})
});

9 changes: 9 additions & 0 deletions sdk/js-connect/core/base/__tests__/finality.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { finalityThreshold } from "../src/constants/finality";

describe("Finality tests", function () {
const mainnetFinality = finalityThreshold("Mainnet", "Ethereum");
it("should correctly access values", function () {
expect(mainnetFinality).toEqual(64);
});
});

Loading

0 comments on commit 335fc22

Please sign in to comment.