11import async from 'async'
22import { execution } from '@remix-project/remix-lib'
33import { compilationInterface } from './types'
4- import { BrowserProvider , ContractFactory , ethers } from 'ethers'
4+ import { BaseContract , BrowserProvider , Contract , ContractFactory , ethers , TransactionReceipt , TransactionResponse } from 'ethers'
55
66/**
77 * @dev Deploy all contracts from compilation result
@@ -57,27 +57,26 @@ export function deployAll (compileResult: compilationInterface, provider: Browse
5757 next ( null , contractsToDeploy )
5858 } ,
5959 function deployContracts ( contractsToDeploy : string [ ] , next ) {
60- const deployRunner = ( deployObject , contractObject , contractName , filename , callback ) => {
61- deployObject . estimateGas ( undefined ) . then ( ( gasValue ) => {
62- const gasBase = Math . ceil ( gasValue * 1.2 )
63- const gas = withDoubleGas ? gasBase * 2 : gasBase
64- deployObject . send ( {
65- from : accounts [ 0 ] ,
66- gas : gas
67- } ) . on ( 'receipt' , async function ( receipt ) {
68- contractObject . options . address = receipt . contractAddress
69- contractObject . options . from = accounts [ 0 ]
70- contractObject . options . gas = 5000 * 1000
71- compiledObject [ contractName ] . deployedAddress = receipt . contractAddress
60+ const deployRunner = ( deployObject , { abi, signer} , contractName , filename , callback ) => {
61+ deployObject . getDeployTransaction ( ) . then ( ( tx : TransactionResponse ) => {
62+ provider . estimateGas ( tx ) . then ( ( gasValue ) => {
63+ const gasBase = Math . ceil ( Number ( gasValue ) * 1.2 )
64+ const gas = withDoubleGas ? gasBase * 2 : gasBase
65+ deployObject . deploy ( {
66+ from : accounts [ 0 ] ,
67+ gasLimit : gas
68+ } ) . then ( async function ( deployContractObj : BaseContract ) {
69+ const deployTx = deployContractObj . deploymentTransaction ( )
70+ const receipt : TransactionReceipt = await provider . getTransactionReceipt ( deployTx . hash )
71+ const contractObject : Contract = new ethers . Contract ( receipt . contractAddress , abi , signer )
72+ compiledObject [ contractName ] . deployedAddress = receipt . contractAddress
7273
73- contracts [ contractName ] = contractObject
74- contracts [ contractName ] . filename = filename
74+ contracts [ contractName ] = contractObject
75+ contracts [ contractName ] . filename = filename
7576
76- if ( deployCb ) await deployCb ( filename , receipt . contractAddress )
77- callback ( null , { receipt : { contractAddress : receipt . contractAddress } } ) // TODO this will only work with JavaScriptV VM
78- } ) . on ( 'error' , function ( err ) {
79- console . error ( err )
80- callback ( err )
77+ if ( deployCb ) await deployCb ( filename , receipt . contractAddress )
78+ callback ( null , { receipt : { contractAddress : receipt . contractAddress } } ) // TODO this will only work with JavaScriptV VM
79+ } )
8180 } )
8281 } ) . catch ( ( err ) => {
8382 console . error ( err )
@@ -90,10 +89,8 @@ export function deployAll (compileResult: compilationInterface, provider: Browse
9089 const encodeDataFinalCallback = ( error , contractDeployData ) => {
9190 if ( error ) return nextEach ( error )
9291 provider . getSigner ( ) . then ( ( signer ) => {
93- const contractObject : ContractFactory = new ethers . ContractFactory ( contract . abi , '0x' + contractDeployData . dataHex , signer )
94- contractObject . deploy ( ) . then ( ( deployObject ) => {
95- deployRunner ( deployObject , contractObject , contractName , contract . filename , ( error ) => { nextEach ( error ) } )
96- } )
92+ const deployObject : ContractFactory = new ethers . ContractFactory ( contract . abi , '0x' + contractDeployData . dataHex , signer )
93+ deployRunner ( deployObject , { abi : contract . abi , signer} , contractName , contract . filename , ( error ) => { nextEach ( error ) } )
9794 } )
9895 }
9996
@@ -103,10 +100,8 @@ export function deployAll (compileResult: compilationInterface, provider: Browse
103100 const abi = compiledObject [ libData . data . contractName ] . abi
104101 const code = compiledObject [ libData . data . contractName ] . code
105102 provider . getSigner ( ) . then ( ( signer ) => {
106- const libraryObject = new ethers . ContractFactory ( abi , '0x' + code , signer )
107- contract . deploy ( ) . then ( ( deployObject ) => {
108- deployRunner ( deployObject , libraryObject , libData . data . contractName , contract . filename , callback )
109- } )
103+ const deployObject : ContractFactory = new ethers . ContractFactory ( abi , '0x' + code , signer )
104+ deployRunner ( deployObject , { abi, signer} , libData . data . contractName , contract . filename , callback )
110105 } )
111106 }
112107
0 commit comments