UNPKG

@neo-one/smart-contract-test-common-esnext-esm

Version:

NEO•ONE TypeScript smart contract test harness common code.

65 lines (63 loc) 3.15 kB
import { setupWallets } from '@neo-one/cli-common-esnext-esm'; import { common, crypto, scriptHashToAddress, } from '@neo-one/client-common-esnext-esm'; import { setGlobalLogLevel } from '@neo-one/logger-esnext-esm'; import { compileContract } from '@neo-one/smart-contract-compiler-esnext-esm'; import { camel } from '@neo-one/utils-esnext-esm'; import BigNumber from 'bignumber.js'; export const withContracts = async (contracts, test, createCompilerHost, getDataProvider, { ignoreWarnings = false, deploy = true, autoConsensus = true, autoSystemFee = true, logging = false, } = {}) => { setGlobalLogLevel(logging ? 'info' : 'silent'); const { dataProvider, cleanup, privateKey } = await getDataProvider(); const { client, developerClient, masterWallet, accountIDs } = await setupWallets(dataProvider, privateKey); const networkName = dataProvider.network; try { if (autoSystemFee) { client.hooks.beforeRelay.tapPromise('AutoSystemFee', async (options) => { options.systemFee = new BigNumber(-1); }); } if (autoConsensus) { client.hooks.beforeConfirmed.tapPromise('DeveloperClient', async () => { await developerClient.runConsensusNow(); }); } const mutableLinked = {}; const mutableSourceMaps = {}; const deployedContracts = await contracts.reduce(async (accIn, { filePath, name }) => { const acc = await accIn; const { contract, sourceMap, abi } = compileContract(filePath, name, createCompilerHost(), mutableLinked, ignoreWarnings); const address = scriptHashToAddress(common.uInt160ToString(crypto.toScriptHash(Buffer.from(contract.script, 'hex')))); mutableSourceMaps[address] = await sourceMap; let result; if (deploy) { result = await client.publishAndDeploy(contract, abi, [], { systemFee: new BigNumber(-1) }, mutableSourceMaps); } else { result = await client.publish(contract, { systemFee: new BigNumber(-1) }); } const [receipt] = await Promise.all([result.confirmed({ timeoutMS: 2500 }), developerClient.runConsensusNow()]); if (receipt.result.state === 'FAULT') { throw new Error(receipt.result.message); } const smartContract = client.smartContract({ networks: { [networkName]: { address } }, abi, sourceMaps: mutableSourceMaps, }); mutableLinked[filePath] = { [name]: address }; return Object.assign({}, acc, { [camel(name)]: smartContract }); }, Promise.resolve({})); const contractOptions = Object.assign({}, deployedContracts, { client, developerClient, masterAccountID: masterWallet.userAccount.id, masterPrivateKey: privateKey, networkName, accountIDs, }); await test(contractOptions); } finally { await cleanup(); } }; //# sourceMappingURL=withContracts.js.map