Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Feat: Refine many things
Browse files Browse the repository at this point in the history
  • Loading branch information
lightwalker-eth committed Apr 25, 2024
1 parent 958b22d commit 6097846
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 319 deletions.
76 changes: 76 additions & 0 deletions src/address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

import { checksumAddress } from "viem";
import { describe, it, expect } from "vitest";
import { buildAddress, isAddressEqual, truncateAddress } from "./address";
import { MAINNET } from "./blockchain";

describe("buildAddress() function", () => {

it("doesn't begin with 0x", () => {
const address = "0000000000000000000000000000000000000000";

expect(() => buildAddress(address)).toThrow();
});

it("too few digits", () => {
const address = "0x000000000000000000000000000000000000000";

expect(() => buildAddress(address)).toThrow();
});

it("too many digits", () => {
const address = "0x00000000000000000000000000000000000000000";

expect(() => buildAddress(address)).toThrow();
});

it("valid address, checksum wrong but not strict", () => {
const address = "0x80C9A4104F594029F3F7F9E4FDE5BC25E9E64CDF";
const expectedAddress = checksumAddress(address, MAINNET.chainId);

const result = buildAddress(address);
expect(result.address).toBe(expectedAddress);
});

it("valid address, checksum wrong and strict", () => {
const address = "0x80C9A4104F594029F3F7F9E4FDE5BC25E9E64CDF";

expect(() => buildAddress(address, true, MAINNET)).toThrow();
});

it("valid address, checksum right and strict", () => {
const address = "0x80C9a4104F594029F3F7f9e4Fde5bc25E9E64cdf";

const result = buildAddress(address, true, MAINNET);
expect(result.address).toBe(address);
});
});

describe("truncateAddress() function", () => {

it("truncate address", () => {
const address = buildAddress("0x80C9A4104F594029F3F7F9E4FDE5BC25E9E64CDF");

const result = truncateAddress(address);
expect(result).toBe("0x80C9...4cdf");
});
});

describe("isAddressEqual() function", () => {

it("equal addresses", () => {
const a = buildAddress("0x80C9A4104F594029F3F7F9E4FDE5BC25E9E64CDF");
const b = buildAddress("0x80c9a4104f594029f3f7f9e4fde5bc25e9e64cdf");

const result = isAddressEqual(a, b);
expect(result).toBe(true);
});

it("non-equal addresses", () => {
const a = buildAddress("0x80C9A4104F594029F3F7F9E4FDE5BC25E9E64CDF");
const b = buildAddress("0x80c9a4104f594029f3f7f9e4fde5bc25e9e64cd0");

const result = isAddressEqual(a, b);
expect(result).toBe(false);
});
});
29 changes: 29 additions & 0 deletions src/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { checksumAddress, isAddress } from "viem";
import { ChainId, MAINNET } from "./blockchain";

export interface Address {
address: `0x${string}`;
}

export const buildAddress = (maybeAddress: string, strict: boolean = false, chain: ChainId = MAINNET): Address => {

if (!isAddress(maybeAddress, { strict: false }))
throw new Error(`Invalid address: ${maybeAddress}`);

const address = checksumAddress(maybeAddress, chain.chainId);

if (strict && address !== maybeAddress)
throw new Error(`Invalid address checksum: ${maybeAddress} expected ${address} on chainId ${chain.chainId}`);

return {
address: address
};
}

export const truncateAddress = (address: Address): string => {
return address.address.slice(0, 6) + "..." + address.address.slice(-4);
};

export const isAddressEqual = (a: Address, b: Address): boolean => {
return a.address.toLowerCase() === b.address.toLowerCase()
}
66 changes: 30 additions & 36 deletions src/blockchain.test.ts
Original file line number Diff line number Diff line change
@@ -1,108 +1,102 @@
import { describe, it, expect } from "vitest";
import { buildBlockchainReference, getBlockchainByName, getBlockchainMetadata } from "./blockchain";
import { MAINNET, buildChainId, getChainByName, getChainMetadata } from "./blockchain";

describe("buildBlockchainReference() function", () => {
describe("buildChainId() function", () => {

it("Build from string values", () => {
it("Build from string", () => {
const chainId = "1";

const result = buildBlockchainReference(chainId);
const result = buildChainId(chainId);

expect(result).toStrictEqual({
chainId: 1,
});
expect(result).toStrictEqual(MAINNET);
});

it("Build from non-string values", () => {
it("Build from number", () => {
const chainId = 1;

const result = buildBlockchainReference(chainId);
const result = buildChainId(chainId);

expect(result).toStrictEqual({
chainId: 1,
});
expect(result).toStrictEqual(MAINNET);
});

it("Invalid chainId: non-number", () => {
const chainId = "q";

expect(() => buildBlockchainReference(chainId)).toThrow("Invalid chain ID: q. All chain IDs must be numbers.");
expect(() => buildChainId(chainId)).toThrow();
});

it("Invalid chainId: chainId non-positive", () => {
const chainId = 0;

expect(() => buildBlockchainReference(chainId)).toThrow("Invalid chain ID: 0. All chain IDs must be positive integers.");
expect(() => buildChainId(chainId)).toThrow();
});

it("Invalid chainId: chainId negative", () => {
const chainId = -1;

expect(() => buildBlockchainReference(chainId)).toThrow("Invalid chain ID: -1. All chain IDs must be positive integers.");
expect(() => buildChainId(chainId)).toThrow();
});

it("Invalid chainId: chainId non-integer", () => {
const chainId = 1.5;

expect(() => buildBlockchainReference(chainId)).toThrow("Invalid chain ID: 1.5. All chain IDs must be integers.");
expect(() => buildChainId(chainId)).toThrow();
});

it("Invalid chainId: chainId non-integer string", () => {
const chainId = "1.5";

expect(() => buildChainId(chainId)).toThrow();
});

it("Invalid chainId: chainId NaN", () => {
const chainId = NaN;

expect(() => buildBlockchainReference(chainId)).toThrow("Invalid chain ID: NaN. All chain IDs must be numbers.");
expect(() => buildChainId(chainId)).toThrow();
});

it("Invalid chainId: chainId Infinity", () => {
const chainId = Infinity;

expect(() => buildBlockchainReference(chainId)).toThrow("Invalid chain ID: Infinity. All chain IDs must be finite numbers.");
expect(() => buildChainId(chainId)).toThrow();
});

});

describe("getBlockchainByName() function", () => {
describe("getChainByName() function", () => {

it("known chain", () => {
const name = "mainnet";

const result = getBlockchainByName(name);
const result = getChainByName(name);

expect(result).toStrictEqual({
chainId: 1,
});
expect(result).toStrictEqual(MAINNET);
});

it("case-insensitive unknown chain", () => {
const name = "Mainnet";

const result = getBlockchainByName(name);

expect(result).toBeNull();
expect(() => getChainByName(name)).toThrow();
});

it("general unknown chain", () => {
const name = "unknown";

const result = getBlockchainByName(name);

expect(result).toBeNull();
expect(() => getChainByName(name)).toThrow();
});
});

describe("getBlockchainMetadata() function", () => {
describe("getChainMetadata() function", () => {

it("known chain", () => {
const ref = buildBlockchainReference(1);
const result = getBlockchainMetadata(ref);
const result = getChainMetadata(MAINNET);

expect(result).toBeDefined();
});

it("unknown chain", () => {
const ref = buildBlockchainReference(1234567890);
const result = getBlockchainMetadata(ref);

expect(result).toBeNull();
const chainId = buildChainId(1234567890);

expect(() => getChainMetadata(chainId)).toThrow();
});
});
Loading

0 comments on commit 6097846

Please sign in to comment.