Skip to content

Commit

Permalink
Refactor address handling to accept unknown types and add type guard …
Browse files Browse the repository at this point in the history
…for ContractAddressMap
  • Loading branch information
yavrsky committed Jan 8, 2025
1 parent b4c4a65 commit 20395d6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion typescript/base/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export interface Adapter<ContractType> {

getChainId(): Promise<bigint>;

isAddress(value: string): boolean;
isAddress(value: unknown): boolean;
}
18 changes: 18 additions & 0 deletions typescript/base/src/domain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ export type ContractAddressMap = {
[contractName: ContractName]: ContractAddress
}

export const isContractAddressMap = (
obj: unknown
): obj is ContractAddressMap => {
if (typeof obj !== "object" || obj === null) {
return false;
}

return Object.entries(obj).every(
([
key,
value
]) => typeof key === "string" &&
typeof value === "string" &&
(/^0x[a-fA-F0-9]{40}$/u).test(value)

);
};

export interface AbiFragmentType {
readonly name?: string;
readonly indexed?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions typescript/base/src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ export abstract class Instance<ContractType> {

constructor (
project: Project<ContractType>,
addressOrAddressContainer: MainContractAddress | ContractAddressMap
address: MainContractAddress | ContractAddressMap
) {
this.project = project;
if (typeof addressOrAddressContainer === "string") {
if (typeof address === "string") {
this.addressContainer = {
[this.project.mainContractName]: addressOrAddressContainer
[this.project.mainContractName]: address
};
} else {
this.addressContainer = addressOrAddressContainer;
this.addressContainer = address;
}
}

Expand Down
11 changes: 6 additions & 5 deletions typescript/base/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as semver from "semver";
import {
ContractAddressMap,
MainContractAddress,
SkaleABIFile
SkaleABIFile,
isContractAddressMap
} from "./domain/types";
import { Instance, InstanceData } from "./instance";
import axios, { HttpStatusCode } from "axios";
Expand Down Expand Up @@ -49,10 +50,10 @@ export abstract class Project<ContractType> {
}

getInstance (target: string | MainContractAddress | ContractAddressMap) {
const isStringAndAddress =
typeof target === "string" &&
this.network.adapter.isAddress(target);
if (isStringAndAddress || typeof target === "object") {
if (
this.network.adapter.isAddress(target) ||
isContractAddressMap(target)
) {
return this.getInstanceByAddress(
target as MainContractAddress | ContractAddressMap
);
Expand Down
4 changes: 2 additions & 2 deletions typescript/base/src/projects/ima/mainnet/MainnetImaProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export class MainnetImaProject<ContractType> extends
return `${this.metadata.name}-${version}-abi.json`;
}

createInstance (target: MainContractAddress | ContractAddressMap)
createInstance (address: MainContractAddress | ContractAddressMap)
: Instance<ContractType> {
return new MainnetImaInstance(
this,
target
address
);
}
}
10 changes: 5 additions & 5 deletions typescript/base/src/projects/ima/schain/SchainImaProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ export class SchainImaProject<ContractType> extends
return `${this.metadata.name}-${version}-abi.json`;
}

getInstance (target: string | MainContractAddress | ContractAddressMap) {
if (target === PREDEPLOYED_ALIAS) {
getInstance (address: string | MainContractAddress | ContractAddressMap) {
if (address === PREDEPLOYED_ALIAS) {
return this.createInstance(SchainImaInstance.PREDEPLOYED.
get("MessageProxyForSchain")! as ContractAddress);
}
return super.getInstance(target);
return super.getInstance(address);
}

createInstance (target: MainContractAddress | ContractAddressMap)
createInstance (address: MainContractAddress | ContractAddressMap)
: Instance<ContractType> {
return new SchainImaInstance(
this,
target
address
);
}
}

0 comments on commit 20395d6

Please sign in to comment.