Skip to content

Commit

Permalink
Update contract address handling and types across projects
Browse files Browse the repository at this point in the history
  • Loading branch information
yavrsky committed Dec 30, 2024
1 parent 77f5f33 commit 248069f
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 39 deletions.
5 changes: 4 additions & 1 deletion typescript/base/src/domain/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type ContractAddress = string;
export type ContractAddress = `0x${string}`;
export type ContractName = string;
export type MainContractAddress = ContractAddress;
export type ContractAddressMap = {
[contractName: ContractName]: ContractAddress
}

export interface AbiFragmentType {
readonly name?: string;
Expand Down
19 changes: 15 additions & 4 deletions typescript/base/src/instance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as semver from "semver";
import {
ContractAddress,
ContractAddressMap,
ContractName,
MainContractAddress,
SkaleABIFile
Expand Down Expand Up @@ -64,18 +65,24 @@ const processPep440 = (pyVersion: Pep440Version) => {
export abstract class Instance<ContractType> {
protected project: Project<ContractType>;

address: MainContractAddress;
addressContainer: ContractAddressMap;

abi: SkaleABIFile | undefined;

version: string | undefined;

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

get adapter () {
Expand Down Expand Up @@ -103,7 +110,7 @@ export abstract class Instance<ContractType> {
return this.project.network.adapter.makeCall(
{
"abi": defaultVersionAbi,
"address": this.address
"address": this.mainContractAddress
},
{
"args": [],
Expand All @@ -117,6 +124,10 @@ export abstract class Instance<ContractType> {
return abi[contractName];
}

get mainContractAddress () {
return this.addressContainer[this.project.mainContractName];
}

// Private

private async getVersion () {
Expand Down
21 changes: 14 additions & 7 deletions typescript/base/src/project.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as semver from "semver";
import {
ContractAddressMap,
MainContractAddress,
SkaleABIFile
} from "./domain/types";
import { Instance, InstanceData } from "./instance";
import { MainContractAddress, SkaleABIFile } from "./domain/types";
import axios, { HttpStatusCode } from "axios";
import { InstanceNotFound } from "./domain/errors/instance/instanceNotFound";
import { ListedNetwork } from "./listedNetwork";
Expand Down Expand Up @@ -34,6 +38,8 @@ export abstract class Project<ContractType> {

abstract githubRepo: string;

abstract mainContractName: string;

constructor (
network: Network<ContractType>,
metadata: ProjectMetadata
Expand All @@ -42,11 +48,12 @@ export abstract class Project<ContractType> {
this.metadata = metadata;
}

getInstance (aliasOrAddress: string) {
if (this.network.adapter.isAddress(aliasOrAddress)) {
return this.getInstanceByAddress(aliasOrAddress);
getInstance (target: string | MainContractAddress | ContractAddressMap) {
if (typeof target === "string" && this.network.adapter.isAddress(target) || typeof target === "object") {

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

This line has a length of 113. Maximum allowed is 80

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

This line has a length of 113. Maximum allowed is 80

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

This line has a length of 113. Maximum allowed is 80

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (16)

This line has a length of 113. Maximum allowed is 80

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (16)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (16)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

This line has a length of 113. Maximum allowed is 80

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

This line has a length of 113. Maximum allowed is 80

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

This line has a length of 113. Maximum allowed is 80

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check failure on line 52 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations
return this.getInstanceByAddress(

Check failure on line 53 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

Unexpected newline after '('

Check failure on line 53 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

Unexpected newline after '('

Check failure on line 53 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

Unexpected newline after '('

Check failure on line 53 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (16)

Unexpected newline after '('

Check failure on line 53 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

Unexpected newline after '('

Check failure on line 53 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

Unexpected newline after '('

Check failure on line 53 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

Unexpected newline after '('
target as MainContractAddress | ContractAddressMap);
}
return this.getInstanceByAlias(aliasOrAddress);
return this.getInstanceByAlias(target);
}

async downloadAbiFile (version: string) {
Expand Down Expand Up @@ -89,12 +96,12 @@ export abstract class Project<ContractType> {
throw new NetworkNotFoundError("Network is unknown");
}

abstract createInstance(address: MainContractAddress):
abstract createInstance(address: MainContractAddress | ContractAddressMap):
Instance<ContractType>;

// Private

private getInstanceByAddress (address: string) {
private getInstanceByAddress (address: MainContractAddress | ContractAddressMap) {

Check failure on line 104 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

This line has a length of 86. Maximum allowed is 80

Check failure on line 104 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

This line has a length of 86. Maximum allowed is 80

Check failure on line 104 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

This line has a length of 86. Maximum allowed is 80

Check failure on line 104 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (16)

This line has a length of 86. Maximum allowed is 80

Check failure on line 104 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

This line has a length of 86. Maximum allowed is 80

Check failure on line 104 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

This line has a length of 86. Maximum allowed is 80

Check failure on line 104 in typescript/base/src/project.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

This line has a length of 86. Maximum allowed is 80
return this.createInstance(address);
}

Expand Down
2 changes: 1 addition & 1 deletion typescript/base/src/projects/ima/ImaInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export abstract class ImaInstance<ContractType> extends
return this.project.network.adapter.makeCall(
{
"abi": messageProxyAbi,
"address": this.address
"address": this.mainContractAddress
},
{
args,
Expand Down
11 changes: 6 additions & 5 deletions typescript/base/src/projects/ima/mainnet/MainnetImaInstance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ContractData } from "../../../adapter";
import { ContractAddress } from "../../../domain/types";

Check failure on line 2 in typescript/base/src/projects/ima/mainnet/MainnetImaInstance.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

Imports should be sorted alphabetically

Check failure on line 2 in typescript/base/src/projects/ima/mainnet/MainnetImaInstance.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

Imports should be sorted alphabetically

Check failure on line 2 in typescript/base/src/projects/ima/mainnet/MainnetImaInstance.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

Imports should be sorted alphabetically

Check failure on line 2 in typescript/base/src/projects/ima/mainnet/MainnetImaInstance.ts

View workflow job for this annotation

GitHub Actions / typescript (16)

Imports should be sorted alphabetically

Check failure on line 2 in typescript/base/src/projects/ima/mainnet/MainnetImaInstance.ts

View workflow job for this annotation

GitHub Actions / typescript (20)

Imports should be sorted alphabetically

Check failure on line 2 in typescript/base/src/projects/ima/mainnet/MainnetImaInstance.ts

View workflow job for this annotation

GitHub Actions / typescript (22)

Imports should be sorted alphabetically

Check failure on line 2 in typescript/base/src/projects/ima/mainnet/MainnetImaInstance.ts

View workflow job for this annotation

GitHub Actions / typescript (18)

Imports should be sorted alphabetically
import { ImaInstance } from "../ImaInstance";


Expand Down Expand Up @@ -29,9 +30,9 @@ export class MainnetImaInstance<ContractType> extends
ImaInstance<ContractType> {
private contractManager: ContractData | undefined;

async getContractAddress (name: string): Promise<string> {
async getContractAddress (name: string): Promise<ContractAddress> {
if (name === "MessageProxyForMainnet") {
return Promise.resolve(this.address);
return Promise.resolve(this.mainContractAddress);
} else if (name === "CommunityPool") {
return this.project.network.adapter.makeCall(
{
Expand All @@ -43,15 +44,15 @@ export class MainnetImaInstance<ContractType> extends
"args": [],
"functionName": "communityPool"
}
) as Promise<string>;
) as Promise<ContractAddress>;
}
return this.project.network.adapter.makeCall(
await this.getContractManager(),
{
"args": [name],
"functionName": "getContract"
}
) as Promise<string>;
) as Promise<ContractAddress>;
}

// Private
Expand All @@ -63,7 +64,7 @@ export class MainnetImaInstance<ContractType> extends
{
"abi":
await this.getContractAbi("MessageProxyForMainnet"),
"address": this.address
"address": this.mainContractAddress
},
{
"args": [],
Expand Down
8 changes: 6 additions & 2 deletions typescript/base/src/projects/ima/mainnet/MainnetImaProject.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { ContractAddressMap, MainContractAddress } from "../../../domain/types";
import { ImaProject } from "../ImaProject";
import { Instance } from "../../../instance";
import { MainnetImaInstance } from "./MainnetImaInstance";

export class MainnetImaProject<ContractType> extends
ImaProject<ContractType> {
mainContractName = "MessageProxyForMainnet";

getAbiFilename (version: string) {
return `${this.metadata.name}-${version}-abi.json`;
}

createInstance (address: string): Instance<ContractType> {
createInstance (target: MainContractAddress | ContractAddressMap)
: Instance<ContractType> {
return new MainnetImaInstance(
this,
address
target
);
}
}
9 changes: 5 additions & 4 deletions typescript/base/src/projects/ima/schain/SchainImaInstance.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ContractAddress } from "../../../domain/types";
import { ImaInstance } from "../ImaInstance";


Expand Down Expand Up @@ -46,13 +47,13 @@ export class SchainImaInstance<ContractType> extends
]
]);

getContractAddress (name: string): Promise<string> {
if (name === "MessageProxyForSchain") {
return Promise.resolve(this.address);
getContractAddress (name: string): Promise<ContractAddress> {
if (name in this.addressContainer) {
return Promise.resolve(this.addressContainer[name]);
}
if (SchainImaInstance.PREDEPLOYED.has(name)) {
return Promise.resolve(SchainImaInstance.PREDEPLOYED.
get(name) as string);
get(name) as ContractAddress);
}
throw new Error(`Can't get address of ${name} contract`);
}
Expand Down
20 changes: 14 additions & 6 deletions typescript/base/src/projects/ima/schain/SchainImaProject.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import {
ContractAddress,
ContractAddressMap,
MainContractAddress
} from "../../../domain/types";
import { ImaProject } from "../ImaProject";
import { Instance } from "../../../instance";
import { PREDEPLOYED_ALIAS } from "../../../domain/constants";
import { SchainImaInstance } from "./SchainImaInstance";

export class SchainImaProject<ContractType> extends
ImaProject<ContractType> {
mainContractName = "MessageProxyForSchain";

getAbiFilename (version: string) {
return `${this.metadata.name}-${version}-abi.json`;
}

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

createInstance (address: string): Instance<ContractType> {
createInstance (target: MainContractAddress | ContractAddressMap)
: Instance<ContractType> {
return new SchainImaInstance(
this,
address
target
);
}
}
4 changes: 2 additions & 2 deletions typescript/base/src/projects/paymaster/paymasterInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class PaymasterInstance<ContractType> extends
"Paymaster",
"FastForwardPaymaster"
].includes(name)) {
return this.address;
return this.mainContractAddress;
}
if (name === "PaymasterAccessManager") {
return await this.callPaymaster(
Expand All @@ -29,7 +29,7 @@ export class PaymasterInstance<ContractType> extends
return this.project.network.adapter.makeCall(
{
"abi": await this.getContractAbi("Paymaster"),
"address": this.address
"address": this.mainContractAddress
},
{
args,
Expand Down
6 changes: 5 additions & 1 deletion typescript/base/src/projects/paymaster/paymasterProject.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ContractAddressMap, MainContractAddress } from "../../domain/types";
import { Instance } from "../../instance";
import { PaymasterInstance } from "./paymasterInstance";
import { Project } from "../../project";
Expand All @@ -6,7 +7,10 @@ export class PaymasterProject<ContractType> extends
Project<ContractType> {
githubRepo = "https://github.com/skalenetwork/paymaster/";

createInstance (address: string): Instance<ContractType> {
mainContractName = "Paymaster";

createInstance (address: MainContractAddress | ContractAddressMap)
: Instance<ContractType> {
return new PaymasterInstance(
this,
address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class SkaleAllocatorInstance<ContractType> extends
args?: unknown[]
): Promise<ContractAddress> {
if (name === "Allocator") {
return Promise.resolve(this.address);
return Promise.resolve(this.mainContractAddress);
}
if (name === "Escrow") {
const firstArgument = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ContractAddressMap, MainContractAddress } from "../../domain/types";
import { Instance } from "../../instance";
import { Project } from "../../project";
import { SkaleAllocatorInstance } from "./skaleAllocatorInstance";
Expand All @@ -6,7 +7,10 @@ export class SkaleAllocatorProject<ContractType> extends
Project<ContractType> {
githubRepo = "https://github.com/skalenetwork/skale-allocator/";

createInstance (address: string): Instance<ContractType> {
mainContractName = "SkaleAllocator";

createInstance (address: MainContractAddress | ContractAddressMap)
: Instance<ContractType> {
return new SkaleAllocatorInstance(
this,
address
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ContractAddress,
ContractName
ContractName,
MainContractAddress
} from "../../domain/types";
import { Instance } from "../../instance";

Expand Down Expand Up @@ -72,7 +73,7 @@ export class SkaleManagerInstance<ContractType> extends
"args": [this.actualName(name)],
"functionName": "getContract"
}
) as string;
) as MainContractAddress;
}

// Private
Expand All @@ -88,7 +89,7 @@ export class SkaleManagerInstance<ContractType> extends
return this.project.network.adapter.makeCall(
{
"abi": skaleManagerAbi,
"address": this.address
"address": this.mainContractAddress
},
{
args,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ContractAddressMap, MainContractAddress } from "../../domain/types";
import { Instance } from "../../instance";
import { Project } from "../../project";
import { SkaleManagerInstance } from "./skaleManagerInstance";
Expand All @@ -6,7 +7,10 @@ export class SkaleManagerProject<ContractType> extends
Project<ContractType> {
githubRepo = "https://github.com/skalenetwork/skale-manager/";

createInstance (address: string): Instance<ContractType> {
mainContractName = "SkaleManager";

createInstance (address: MainContractAddress | ContractAddressMap)
: Instance<ContractType> {
return new SkaleManagerInstance(
this,
address
Expand Down

0 comments on commit 248069f

Please sign in to comment.