import * as path from 'path' ; import { readFile as readFileCallback , exists as existsCallback } from 'fs' ; import { promisify } from 'util' ; import { Serialize } from 'eosjs' ; import * as ecc from 'eosjs-ecc' ; const exists = promisify ( existsCallback ) ; const readFile = promisify ( readFileCallback ) ; import { Contract } from './contract' ; import { Account , AccountManager } from '../accounts' ; import { EOSManager } from '../eosManager' ; import { ConfigManager } from '../configManager' ; import { ContractLoader } from './contractLoader' ; /**
 * Provides a set of methods to manage contract deployment
 */ export class ContractDeployer { author Kevin Brown <github.com/thekevinbrown>
     *  param contractIdentifier param account returns public static async deployToAccount < T extends Contract > ( contractIdentifier , account ) { EOSManager . addSigningAccountIfMissing ( account ) ; const buffer = new Serialize . SerialBuffer ( { textEncoder : EOSManager . api . textEncoder , textDecoder : EOSManager . api . textDecoder , } ) ; const abiPath = path . join ( ConfigManager . outDir , 'compiled_contracts' , `${ contractIdentifier }.abi` ) ; if ( ! ( await exists ( abiPath ) ) ) { throw new Error ( `Couldn't find ABI at ${ abiPath }. Are you sure you used the correct contract identifier?` ) ; } const wasmPath = path . join ( ConfigManager . outDir , 'compiled_contracts' , `${ contractIdentifier }.wasm` ) ; if ( ! ( await exists ( wasmPath ) ) ) { throw new Error ( `Couldn't find WASM file at ${ wasmPath }. Are you sure you used the correct contract identifier?` ) ; } let abi = JSON . parse ( await readFile ( abiPath , 'utf8' ) ) ; const wasm = await readFile ( wasmPath ) ; const abiDefinition = EOSManager . api . abiTypes . get ( `abi_def` ) ; if ( ! abiDefinition ) throw new Error ( 'Could not retrieve abiDefinition from EOS API when flattening ABIs.' ) ; abi = abiDefinition . fields . reduce ( ( acc , { name : fieldName } ) => Object . assign ( acc , { [ fieldName ] : acc [ fieldName ] || [ ] } ) , abi ) ; abiDefinition . serialize ( buffer , abi ) ; await EOSManager . transact ( { actions : [ { account : 'eosio' , name : 'setcode' , authorization : account . active , data : { account : account . name , vmtype : 0 , vmversion : 0 , code : wasm . toString ( 'hex' ) , } , } , { account : 'eosio' , name : 'setabi' , authorization : account . active , data : { account : account . name , abi : Buffer . from ( buffer . asUint8Array ( ) ) . toString ( `hex` ) , } , } , ] , } ) ; return await ContractLoader . at < T > ( account ) ; } author Kevin Brown <github.com/thekevinbrown>
     *  param contractIdentifier returns public static async deploy < T extends Contract > ( contractIdentifier ) { const account = await AccountManager . createAccount ( ) ; return await ContractDeployer . deployToAccount < T > ( contractIdentifier , account ) ; } note author Mitch Pierias <github.com/MitchPierias>
     *  param contractIdentifier param accountName returns public static async deployWithName < T extends Contract > ( contractIdentifier , accountName ) { const privateKey = await ecc . unsafeRandomKey ( ) ; const account = new Account ( accountName , privateKey ) ; await AccountManager . setupAccount ( account ) ; return await ContractDeployer . deployToAccount < T > ( contractIdentifier , account ) ; } }