Skip to content

Commit

Permalink
(feat) #5
Browse files Browse the repository at this point in the history
 * contracts that fail to deploy now throw, with an explanatory error message.
 * it's now possible to obtain the transaction id (and other data) from a contract deployment
 * you can now specify attempts, gas params, etc. for the Zilliqa provider object
  • Loading branch information
rrw-zilliqa committed Mar 9, 2023
1 parent e2fc068 commit cc84ca1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ typings/

# environment variables
.env

# Emacs backups
*~
\#*
.\#*
37 changes: 26 additions & 11 deletions src/ScillaContractDeployer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// This is necessary so that tsc can resolve some of the indirect types for
// sc_call, otherwise it errors out - [email protected] 2023-03-09
import { Transaction } from "@zilliqa-js/account";
import { Contract, Init, Value } from "@zilliqa-js/contract";
import { BN, bytes, Long, units } from "@zilliqa-js/util";
import { Zilliqa } from "@zilliqa-js/zilliqa";
import fs from "fs";
import { HardhatPluginError } from "hardhat/plugins";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { stringifyTransactionErrors } from "./ZilliqaUtils";

import { ContractInfo } from "./ScillaContractsInfoUpdater";
import { Fields, isNumeric, TransitionParam } from "./ScillaParser";
Expand All @@ -19,18 +23,23 @@ export interface Setup {

export let setup: Setup | null = null;

// The optional params are listed in popularity order.
export const initZilliqa = (
zilliqaNetworkUrl: string,
chainId: number,
privateKeys: string[]
zilliqaNetworkUrl: string,
chainId: number,
privateKeys: string[],
attempts: number = 10,
timeoutMs: number = 1000,
gasPriceQa: number = 2000,
gasLimit: number = 50000,
): Setup => {
setup = {
zilliqa: new Zilliqa(zilliqaNetworkUrl),
version: bytes.pack(chainId, 1),
gasPrice: units.toQa("2000", units.Units.Li),
gasLimit: Long.fromNumber(50000),
attempts: 10,
timeout: 1000,
gasPrice: units.toQa(gasPriceQa.toString(), units.Units.Li),
gasLimit: Long.fromNumber(gasLimit),
attempts: attempts,
timeout: timeoutMs,
};

privateKeys.forEach((pk) => setup!.zilliqa.wallet.addByPrivateKey(pk));
Expand Down Expand Up @@ -60,13 +69,15 @@ export async function deploy(
}

let sc: ScillaContract;
let tx: Transaction;
const init: Init = fillInit(
contractName,
contractInfo.parsedContract.constructorParams,
...args
);

sc = await deploy_from_file(contractInfo.path, init);
[tx,sc] = await deploy_from_file(contractInfo.path, init);
sc['deployed_by'] = tx
contractInfo.parsedContract.transitions.forEach((transition) => {
sc[transition.name] = async (...args: any[]) => {
let amount = 0;
Expand Down Expand Up @@ -142,7 +153,7 @@ const fillInit = (
async function deploy_from_file(
path: string,
init: Init
): Promise<ScillaContract> {
): Promise<[Transaction, ScillaContract]> {
if (setup === null) {
throw new HardhatPluginError(
"hardhat-scilla-plugin",
Expand All @@ -152,14 +163,18 @@ async function deploy_from_file(

const code = read(path);
const contract = setup.zilliqa.contracts.new(code, init);
const [_, sc] = await contract.deploy(
const [tx, sc] = await contract.deploy(
{ ...setup },
setup.attempts,
setup.timeout,
false
);

return sc;
if (!sc.isDeployed()) {
let txnErrors = stringifyTransactionErrors(tx);
throw new Error(`Scilla contract was not deployed - status ${sc.status} from ${tx.id}, errors: ${txnErrors}`)
}
return [tx, sc];
}

// call a smart contract's transition with given args and an amount to send from a given public key
Expand Down
41 changes: 41 additions & 0 deletions src/ZilliqaUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { TransactionError } from "@zilliqa-js/core";
import { Transaction } from "@zilliqa-js/account";

interface ErrorDict {
[index: string]: TransactionError[];
}

/** Given a transaction, return a string[][] array containing a list of errors, decoded into their
* symbolic values, for each level of the transaction.
*/
export function decodeTransactionErrors(txn : Transaction) : string[][] {
let receipt = txn.getReceipt();
if (receipt === undefined) {
return [ ]
}
if (receipt.errors === undefined) {
return [ ]
}
var rv : string[][] = [ ]
var errors = (receipt.errors as ErrorDict)
for (let key in errors) {
var our_errors : string[] = []
for (let err in errors[key]) {
our_errors.push(`${err} (${TransactionError[err]})`)
}
rv.push(our_errors)
}
return rv
}

/** Given a transaction, get the errors from the receipt and return a human-readable string that
* can be used to display them.
*/
export function stringifyTransactionErrors(txn : Transaction): string {
let errors : string[][] = decodeTransactionErrors(txn)
let result = ""
for (let level in errors) {
result = result + "[" + errors[level].join(",") + "] "
}
return result
}

0 comments on commit cc84ca1

Please sign in to comment.