use abi from abi store url
MODIFY
src/config.ts
src/config.ts
-import MetaNetwork from '@maticnetwork/meta/network';
export const config = {
- networkApiUrl: MetaNetwork
+ abiStoreUrl: 'https://static.matic.network/network/'
}
\ No newline at end of file

ADD
src/helpers/abi_helper.ts
+import { service } from "../services";
+import { resolve } from "../utils";
+
+type T_ABI_CACHE = {
+ [networkName: string]: {
+ [version: string]: {
+ address: any,
+ abi: {
+ [bridgeType: string]: {
+ [contractName: string]: any
+ }
+ }
+ }
+ }
+};
+
+let cache: T_ABI_CACHE = {};
+
+export class ABIHelper {
+ constructor(public networkName: string, public version: string) {
+
+ }
+
+ init() {
+ return service.abi.getAddress(
+ this.networkName, this.version
+ ).then(result => {
+ cache[this.networkName] = {
+ [this.version]: {
+ address: result,
+ abi: {}
+ }
+ };
+ });
+ }
+
+ getAddress(path: string) {
+ return resolve(
+ cache[this.networkName][this.version].address,
+ path
+ );
+ }
+
+ getABI(contractName: string, bridgeType = 'plasma'): Promise<any> {
+ let targetBridgeABICache = cache[this.networkName][this.version].
+ abi[bridgeType];
+ const abiForContract = targetBridgeABICache[contractName];
+ if (targetBridgeABICache && abiForContract) {
+ return abiForContract;
+ }
+ return service.abi.getABI(
+ this.networkName,
+ this.version,
+ bridgeType,
+ contractName
+ ).then(result => {
+ targetBridgeABICache = {
+ [contractName]: result
+ };
+ return result;
+ });
+ }
+
+ static set cache(value: T_ABI_CACHE) {
+ cache = value;
+ }
+
+ static get cache() {
+ return cache;
+ }
+}
\ No newline at end of file

MODIFY
src/helpers/index.ts
src/helpers/index.ts
export * from "./do_nothing";
export * from "./contract_write_result";
+export * from "./abi_helper";
\ No newline at end of file

MODIFY
src/index.ts
src/index.ts
@@ -9,7 +9,6 @@ export * from "./types";
export * from "./constant";
export * from "./abstracts";
export * from "./ethers";
-export { initService } from "./services";
export default defaultExport;

MODIFY
src/pos/exit_manager.ts
src/pos/exit_manager.ts
@@ -105,7 +105,7 @@ export class ExitManager {
private async getRootBlockInfoFromAPI(txBlockNumber: number) {
try {
console.log("block info from API 1");
- const headerBlock = await service.getBlockIncluded(txBlockNumber);
+ const headerBlock = await service.network.getBlockIncluded(txBlockNumber);
console.log("block info from API 2", headerBlock);
if (!headerBlock || !headerBlock.start || !headerBlock.end || !headerBlock.headerBlockNumber) {
throw Error('Network API Error');
@@ -129,7 +129,7 @@ export class ExitManager {
private async getBlockProofFromAPI(txBlockNumber: number, rootBlockInfo: { start, end }) {
try {
- const blockProof = await service.getProof(
+ const blockProof = await service.network.getProof(
rootBlockInfo.start, rootBlockInfo.end,
txBlockNumber
);

MODIFY
src/pos/index.ts
src/pos/index.ts
@@ -5,7 +5,6 @@ import { IPOSClientConfig } from "../interfaces";
import { LOGGER } from "../constant";
import { ExitManager } from "./exit_manager";
import { RootChain } from "./root_chain";
-import { initService } from "../services";
import { ERC721 } from "./erc721";
export * from "./exit_manager";
@@ -26,7 +25,6 @@ export class POSClient {
const mainPOSContracts = this.client_.mainPOSContracts;
- initService(this.client_.metaNetwork.Matic.NetworkAPI);
config = Object.assign(
{

ADD
src/services/abi_service.ts
+import { HttpRequest } from "../utils";
+
+export class ABIService {
+ httpRequest: HttpRequest;
+
+ constructor(baseUrl: string) {
+ this.httpRequest = new HttpRequest(baseUrl);
+ }
+
+ getABI(network: string, version: string, bridgeType: string, contractName: string) {
+ const url = `${network}/${version}/artifacts/${bridgeType}/${contractName}.json`;
+ return this.httpRequest.get(url).then((result: any) => {
+ return result.abi;
+ });
+ }
+
+ getAddress(network: string, version: string) {
+ const url = `${network}/${version}/index.json`;
+ return this.httpRequest.get(url);
+ }
+}
\ No newline at end of file

MODIFY
src/services/index.ts
src/services/index.ts
-import { HttpRequest } from "../utils";
-import BN from "bn.js";
+import { ABIService } from "./abi_service";
+import { config } from "../config";
+import { NetworkService } from "./network_service";
-class NetworkService {
- httpRequest: HttpRequest;
+export * from "./network_service";
- constructor(baseUrl: string) {
- this.httpRequest = new HttpRequest(baseUrl);
+class Service {
+ network: NetworkService;
+ abi: ABIService;
}
- getBlockIncluded(blockNumber: number) {
- const url = `/block-included/${blockNumber}`;
- return this.httpRequest.get<{
- start: string;
- end: string;
- headerBlockNumber: string;
- blockNumber: BN
- }>(url).then(result => {
- result['blockNumber'] = result.headerBlockNumber as any;
- return result;
- });
- }
-
- getProof(start, end, blockNumber) {
- const url = `/fast-merkle-proof?start=${start}&end=${end}&number=${blockNumber}`;
- return this.httpRequest.get<any>(url).then(result => {
- return result.proof;
- });
- }
-}
-
-export let service: NetworkService;
+export const service = new Service();
+service.abi = new ABIService(config.abiStoreUrl);
-export const initService = (url: string) => {
- service = new NetworkService(url);
-};

ADD
src/services/network_service.ts
+import { HttpRequest } from "../utils";
+import BN from "bn.js";
+
+export class NetworkService {
+ httpRequest: HttpRequest;
+
+ constructor(baseUrl: string) {
+ this.httpRequest = new HttpRequest(baseUrl);
+ }
+
+ getBlockIncluded(blockNumber: number) {
+ const url = `/block-included/${blockNumber}`;
+ return this.httpRequest.get<{
+ start: string;
+ end: string;
+ headerBlockNumber: string;
+ blockNumber: BN
+ }>(url).then(result => {
+ result['blockNumber'] = result.headerBlockNumber as any;
+ return result;
+ });
+ }
+
+ getProof(start, end, blockNumber) {
+ const url = `/fast-merkle-proof?start=${start}&end=${end}&number=${blockNumber}`;
+ return this.httpRequest.get<any>(url).then(result => {
+ return result.proof;
+ });
+ }
+}
\ No newline at end of file

MODIFY
src/utils/index.ts
src/utils/index.ts
@@ -10,3 +10,5 @@ export * from "./http_request";
export * from "./converter";
export * from "./web3_side_chain_client";
export * from "./base_token";
+export * from "./set_proof_api_url";
+export * from "./resolve";
\ No newline at end of file

ADD
src/utils/resolve.ts
+export function resolve(obj, path) {
+ const properties = Array.isArray(path) ? path : path.split(".");
+ return properties.reduce((prev, curr) => prev && prev[curr], obj);
+}
\ No newline at end of file

ADD
src/utils/set_proof_api_url.ts
+import { service, NetworkService } from "../services";
+
+export const setProofApi = (url: string) => {
+ service.network = new NetworkService(url) as any;
+};
\ No newline at end of file

use abi from abi store url