generated from NomicFoundation/hardhat-ts-plugin-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
e2fc068
commit cc84ca1
Showing
3 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,3 +61,8 @@ typings/ | |
|
||
# environment variables | ||
.env | ||
|
||
# Emacs backups | ||
*~ | ||
\#* | ||
.\#* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
@@ -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)); | ||
|
@@ -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; | ||
|
@@ -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", | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |