hardhat-scilla-plugin
Version:
Hardhat TypeScript plugin for scilla testing
122 lines • 4.92 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZilliqaHardhatObject = void 0;
exports.loadZilliqaHardhatObject = loadZilliqaHardhatObject;
const account_1 = require("@zilliqa-js/account");
const zcrypto = __importStar(require("@zilliqa-js/crypto"));
const util_1 = require("@zilliqa-js/util");
const plugins_1 = require("hardhat/plugins");
const ScillaContractDeployer = __importStar(require("./deployer/ScillaContractDeployer"));
// We carefully don't cache the setup object, in case it changes underneath us.
class ZilliqaHardhatObject {
getEventLog(tx) {
const receipt = tx.getReceipt();
const event_logs = receipt.event_logs;
return event_logs;
}
getZilliqaSetup() {
return ScillaContractDeployer.setup;
}
getZilliqaJSObject() {
return this.getZilliqaSetup().zilliqa;
}
getAccounts() {
return this.getZilliqaSetup().accounts;
}
// Retrieve the default account used to sign transactions.
getDefaultAccount() {
const wallet = this.getZilliqaSetup().zilliqa.wallet;
const defaultAccount = wallet.defaultAccount;
return defaultAccount;
}
/** Push a private key onto the accounts array, returning a pair of the
* account and the index at which it can be found
*/
pushPrivateKey(privKey) {
const account = new account_1.Account(privKey);
const val = this.getZilliqaSetup().accounts.push(account) - 1;
this.getZilliqaJSObject().wallet.addByPrivateKey(privKey);
return [account, val];
}
createPrivateKey() {
const privateKey = zcrypto.schnorr.generatePrivateKey();
return privateKey;
}
async transferTo(toAccount, value, txParams = {}) {
return this.transferToAddress(toAccount.address, value, txParams);
}
async transferToAddress(toAddress, value, txParams = {}) {
const setup = this.getZilliqaSetup();
const zjs = this.getZilliqaJSObject();
// Forcibly checksum the address, since otherwise we will get into a mess
// because eth addresses are checksummed differently from zil.
const flat = toAddress.toLowerCase();
const summed = zcrypto.toChecksumAddress(flat);
const txDefault = {
version: setup.version,
gasPrice: setup.gasPrice,
gasLimit: setup.gasLimit,
amount: value,
toAddr: summed,
};
const callParams = { ...txDefault, ...txParams };
const tx1 = zjs.transactions.new(callParams);
const tx = zjs.blockchain.createTransaction(tx1, setup.attempts, setup.timeout);
return tx;
}
async getBalance(account) {
return this.getBalanceForAddress(account.address);
}
async getBalanceForAddress(addressToQuery) {
const rpc = await this.getZilliqaJSObject().blockchain.getBalance(addressToQuery);
if (rpc.error !== undefined) {
if (rpc.error.code === -5) {
// Account not created. Simulate it.
return [new util_1.BN(0, 10), -1];
}
else {
throw new plugins_1.HardhatPluginError(`RPC failed - ${JSON.stringify(rpc.error)}`);
}
}
const data = rpc.result;
return [new util_1.BN(data.balance, 10), data.nonce];
}
}
exports.ZilliqaHardhatObject = ZilliqaHardhatObject;
function loadZilliqaHardhatObject(_hre) {
return new ZilliqaHardhatObject();
}
//# sourceMappingURL=ZilliqaHardhatObject.js.map