Skip to content

Commit

Permalink
Merge pull request #57 from skalenetwork/schain-ima
Browse files Browse the repository at this point in the history
Add predeployed IMA contracts to the typescript library
  • Loading branch information
DimaStebaev committed May 24, 2024
2 parents bce7be2 + 8302c74 commit 2666b4f
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dictionary
Submodule dictionary updated 1 files
+1 −0 libraries.txt
1 change: 1 addition & 0 deletions typescript/base/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
"error",
"separate-lines"
],
"no-warning-comments": ["warn"],
"object-curly-spacing": [
"error",
"always"
Expand Down
4 changes: 3 additions & 1 deletion typescript/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"types": "lib/index.d.ts",
"version": "1.0.1",
"dependencies": {
"axios": "^1.4.0"
"@renovatebot/pep440": "^3.0.20",
"axios": "^1.4.0",
"semver": "^7.6.0"
}
}
1 change: 1 addition & 0 deletions typescript/base/src/domain/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const PREDEPLOYED_ALIAS = "predeployed";
export const REPOSITORY_URL = "https://skalenetwork.github.io/skale-contracts/";
export const METADATA_FILENAME = "metadata.json";
48 changes: 45 additions & 3 deletions typescript/base/src/instance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as semver from "semver";
import {
ContractAddress,
ContractName,
MainContractAddress,
SkaleABIFile
} from "./domain/types";
import { parse, stringify } from "@renovatebot/pep440/lib/version";
import { Pep440Version } from "@renovatebot/pep440";
import { Project } from "./project";

export type InstanceData = {
Expand All @@ -27,6 +30,37 @@ const defaultVersionAbi = [
}
];

const processSemver = (semVersion: semver.SemVer) => {
if (!semVersion.prerelease.length) {
const defaultPrerelease = 0;
semVersion.prerelease = [
"stable",
defaultPrerelease
];
}
return semVersion.format();
};

const processPep440 = (pyVersion: Pep440Version) => {
const replaceMap = new Map<string, string>([
[
"a",
"develop"
],
[
"b",
"beta"
]
]);
pyVersion.pre = pyVersion.pre.map((value: string | number) => {
if (typeof value === "string" && replaceMap.has(value)) {
return `-${replaceMap.get(value)!}.`;
}
return value;
});
return stringify(pyVersion)!;
};

export abstract class Instance<ContractType> {
protected project: Project<ContractType>;

Expand Down Expand Up @@ -87,9 +121,17 @@ export abstract class Instance<ContractType> {

private async getVersion () {
if (typeof this.version === "undefined") {
this.version = await this.queryVersion();
if (!this.version.includes("-")) {
this.version = `${this.version}-stable.0`;
const rawVersion = await this.queryVersion();
const semVersion = semver.parse(rawVersion);
if (semVersion) {
this.version = processSemver(semVersion);
} else {
const pyVersion = parse(rawVersion);
if (pyVersion) {
this.version = processPep440(pyVersion);
} else {
throw new Error(`Can't parse version ${rawVersion}`);
}
}
}
return this.version;
Expand Down
6 changes: 3 additions & 3 deletions typescript/base/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export abstract class Project<ContractType> {
}

async downloadAbiFile (version: string) {
const exceptions = [];
const exceptions: string[] = [];
for (const abiUrl of this.getAbiUrls(version)) {
try {
// Await expression should be executed only
Expand All @@ -42,10 +42,10 @@ export abstract class Project<ContractType> {
const response = await axios.get(abiUrl);
return response.data as SkaleABIFile;
} catch (exception) {
exceptions.push(exception);
exceptions.push(`\nDownloading from ${abiUrl} - ${exception}`);
}
}
throw new Error(exceptions.toString());
throw new Error(exceptions.join(""));
}

getAbiUrls (version: string) {
Expand Down
10 changes: 10 additions & 0 deletions typescript/base/src/projects/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Project } from "../project";
import {
ProjectNotFoundError
} from "../domain/errors/project/projectNotFoundError";
import { SchainImaProject } from "./ima/schain/SchainImaProject";
import { SkaleAllocatorProject } from "./skale-allocator/skaleAllocatorProject";
import { SkaleManagerProject } from "./skale-manager/skaleManagerProject";

Expand All @@ -13,6 +14,10 @@ export const projects = {
"name": "mainnet-ima",
"path": "mainnet-ima"
},
"schainIma": {
"name": "schain-ima",
"path": "schain-ima"
},
"skaleAllocator": {
"name": "skale-allocator",
"path": "skale-allocator"
Expand All @@ -38,6 +43,11 @@ export const createProject =
network,
projects.mainnetIma
);
} else if (name === projects.schainIma.name) {
return new SchainImaProject<ContractType>(
network,
projects.schainIma
);
} else if (name === projects.skaleAllocator.name) {
return new SkaleAllocatorProject<ContractType>(
network,
Expand Down
59 changes: 59 additions & 0 deletions typescript/base/src/projects/ima/schain/SchainImaInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ImaInstance } from "../ImaInstance";


export class SchainImaInstance<ContractType> extends
ImaInstance<ContractType> {
static PREDEPLOYED = new Map<string, string>([
[
"CommunityLocker",
"0xD2aaa00300000000000000000000000000000000"
],
[
"KeyStorage",
"0xd2aaa00200000000000000000000000000000000"
],
[
"MessageProxyForSchain",
"0xd2AAa00100000000000000000000000000000000"
],
[
"ProxyAdmin",
"0xd2aAa00000000000000000000000000000000000"
],
[
"TokenManagerERC1155",
"0xD2aaA00900000000000000000000000000000000"
],
[
"TokenManagerERC20",
"0xD2aAA00500000000000000000000000000000000"
],
[
"TokenManagerERC721",
"0xD2aaa00600000000000000000000000000000000"
],
[
"TokenManagerERC721WithMetadata",
"0xd2AaA00a00000000000000000000000000000000"
],
[
"TokenManagerEth",
"0xd2AaA00400000000000000000000000000000000"
],
[
"TokenManagerLinker",
"0xD2aAA00800000000000000000000000000000000"
]
]);

getContractAddress (name: string): Promise<string> {
if (name === "MessageProxyForSchain") {
return Promise.resolve(this.address);
}
if (SchainImaInstance.PREDEPLOYED.has(name)) {
return Promise.resolve(SchainImaInstance.PREDEPLOYED.
get(name) as string);
}
throw new Error(`Can't get address of ${name} contract`);
}
}
26 changes: 26 additions & 0 deletions typescript/base/src/projects/ima/schain/SchainImaProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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> {
getAbiFilename (version: string) {
return `${this.metadata.name}-${version}-abi.json`;
}

getInstance (aliasOrAddress: string) {
if (aliasOrAddress === PREDEPLOYED_ALIAS) {
return this.createInstance(SchainImaInstance.PREDEPLOYED.
get("MessageProxyForSchain")!);
}
return super.getInstance(aliasOrAddress);
}

createInstance (address: string): Instance<ContractType> {
return new SchainImaInstance(
this,
address
);
}
}
20 changes: 20 additions & 0 deletions typescript/base/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,25 @@ __metadata:
languageName: node
linkType: hard

"@renovatebot/pep440@npm:^3.0.20":
version: 3.0.20
resolution: "@renovatebot/pep440@npm:3.0.20"
checksum: f7014e1774bd7df0db8d08ea9aa8432a668f829f5f6233aee28dc598dc43a4011e5a65962a8d5f295b39b1bb550fd172b85ecb5bb47b8e9cd8ab5db69dabb8e0
languageName: node
linkType: hard

"@skalenetwork/skale-contracts@workspace:.":
version: 0.0.0-use.local
resolution: "@skalenetwork/skale-contracts@workspace:."
dependencies:
"@renovatebot/pep440": ^3.0.20
"@tsconfig/recommended": ^1.0.2
"@types/node": ^20.2.5
"@typescript-eslint/eslint-plugin": ^6.0.0
"@typescript-eslint/parser": ^6.0.0
axios: ^1.4.0
eslint: ^8.45.0
semver: ^7.6.0
typescript: ^5.1.6
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -1163,6 +1172,17 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^7.6.0":
version: 7.6.0
resolution: "semver@npm:7.6.0"
dependencies:
lru-cache: ^6.0.0
bin:
semver: bin/semver.js
checksum: 7427f05b70786c696640edc29fdd4bc33b2acf3bbe1740b955029044f80575fc664e1a512e4113c3af21e767154a94b4aa214bf6cd6e42a1f6dba5914e0b208c
languageName: node
linkType: hard

"shebang-command@npm:^2.0.0":
version: 2.0.0
resolution: "shebang-command@npm:2.0.0"
Expand Down
20 changes: 20 additions & 0 deletions typescript/ethers-v5/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,13 @@ __metadata:
languageName: node
linkType: hard

"@renovatebot/pep440@npm:^3.0.20":
version: 3.0.20
resolution: "@renovatebot/pep440@npm:3.0.20"
checksum: f7014e1774bd7df0db8d08ea9aa8432a668f829f5f6233aee28dc598dc43a4011e5a65962a8d5f295b39b1bb550fd172b85ecb5bb47b8e9cd8ab5db69dabb8e0
languageName: node
linkType: hard

"@skalenetwork/skale-contracts-ethers-v5@workspace:.":
version: 0.0.0-use.local
resolution: "@skalenetwork/skale-contracts-ethers-v5@workspace:."
Expand All @@ -526,7 +533,9 @@ __metadata:
version: 0.0.0-use.local
resolution: "@skalenetwork/skale-contracts@portal:../base/::locator=%40skalenetwork%2Fskale-contracts-ethers-v5%40workspace%3A."
dependencies:
"@renovatebot/pep440": ^3.0.20
axios: ^1.4.0
semver: ^7.6.0
languageName: node
linkType: soft

Expand Down Expand Up @@ -1703,6 +1712,17 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^7.6.0":
version: 7.6.0
resolution: "semver@npm:7.6.0"
dependencies:
lru-cache: ^6.0.0
bin:
semver: bin/semver.js
checksum: 7427f05b70786c696640edc29fdd4bc33b2acf3bbe1740b955029044f80575fc664e1a512e4113c3af21e767154a94b4aa214bf6cd6e42a1f6dba5914e0b208c
languageName: node
linkType: hard

"shebang-command@npm:^2.0.0":
version: 2.0.0
resolution: "shebang-command@npm:2.0.0"
Expand Down
20 changes: 20 additions & 0 deletions typescript/ethers-v6/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ __metadata:
languageName: node
linkType: hard

"@renovatebot/pep440@npm:^3.0.20":
version: 3.0.20
resolution: "@renovatebot/pep440@npm:3.0.20"
checksum: f7014e1774bd7df0db8d08ea9aa8432a668f829f5f6233aee28dc598dc43a4011e5a65962a8d5f295b39b1bb550fd172b85ecb5bb47b8e9cd8ab5db69dabb8e0
languageName: node
linkType: hard

"@skalenetwork/skale-contracts-ethers-v6@workspace:.":
version: 0.0.0-use.local
resolution: "@skalenetwork/skale-contracts-ethers-v6@workspace:."
Expand All @@ -145,7 +152,9 @@ __metadata:
version: 0.0.0-use.local
resolution: "@skalenetwork/skale-contracts@portal:../base/::locator=%40skalenetwork%2Fskale-contracts-ethers-v6%40workspace%3A."
dependencies:
"@renovatebot/pep440": ^3.0.20
axios: ^1.4.0
semver: ^7.6.0
languageName: node
linkType: soft

Expand Down Expand Up @@ -1214,6 +1223,17 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^7.6.0":
version: 7.6.0
resolution: "semver@npm:7.6.0"
dependencies:
lru-cache: ^6.0.0
bin:
semver: bin/semver.js
checksum: 7427f05b70786c696640edc29fdd4bc33b2acf3bbe1740b955029044f80575fc664e1a512e4113c3af21e767154a94b4aa214bf6cd6e42a1f6dba5914e0b208c
languageName: node
linkType: hard

"shebang-command@npm:^2.0.0":
version: 2.0.0
resolution: "shebang-command@npm:2.0.0"
Expand Down

0 comments on commit 2666b4f

Please sign in to comment.