ocn-registry
Version:
Oli-systems OCN registry smart contracts
80 lines (77 loc) • 2.96 kB
JavaScript
;
/*
Copyright 2019-2020 eMobilify GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContractWrapper = void 0;
const ethers_1 = require("ethers");
const networks_1 = require("../networks");
class ContractWrapper {
constructor(contract, environment, signer, overrides, specifContractAddress, verbose = true) {
if (!networks_1.networks[environment]) {
throw new Error(`Option \"${environment}\" not found in configured networks.`);
}
const provider = {
...networks_1.networks[environment].provider,
...overrides?.provider,
};
if (verbose) {
console.log(`connecting to ${provider.protocol}://${provider.host}:${provider.port}`);
}
this.provider = new ethers_1.ethers.JsonRpcProvider(`${provider.protocol}://${provider.host}:${provider.port}`);
if (signer) {
this.wallet = new ethers_1.ethers.Wallet(signer, this.provider);
this.mode = "r+w";
}
else {
this.mode = "r";
}
if (specifContractAddress) {
this.verifyAddress(specifContractAddress);
this.contract = new ethers_1.ethers.Contract(specifContractAddress, contract.abi, this.wallet || this.provider);
}
else {
this.contract = new ethers_1.ethers.Contract(contract.address, contract.abi, this.wallet || this.provider);
}
}
verifyAddress(address) {
try {
ethers_1.ethers.getAddress(address);
}
catch (err) {
throw Error(`Invalid address. Expected Ethereum address, got "${address}".`);
}
}
verifyWritable() {
if (this.mode !== "r+w") {
throw Error("No signer provided. Unable to send transaction.");
}
}
verifyStringLen(str, len) {
if (str.length !== len) {
throw Error(`Invalid string length. Wanted ${len}, got "${str}" (${str.length})`);
}
}
verifyUrl(url) {
try {
new URL(url);
}
catch (err) {
throw Error(`Invalid URL. Expected valid URL, got "${url}".`);
}
}
toBytes(str) {
return (0, ethers_1.hexlify)((0, ethers_1.toUtf8Bytes)(str.toUpperCase()));
}
}
exports.ContractWrapper = ContractWrapper;
//# sourceMappingURL=contract-wrapper.js.map