const Web3 = require ( 'web3' ) ; const BigNumber = require ( 'bignumber.js' ) ; require ( 'dotenv' ) . config ( { path : './.env' } ) ; import { version } from '../package.json' ; import { Address } from 'set-protocol-utils' ; import { DEFAULT_GAS , NULL_ADDRESS } from './constants' ; const contract = require ( '@truffle/contract' ) ; export const getWeb3 = ( ) => { let myWeb3 ; if ( typeof web3 !== 'undefined' ) { myWeb3 = new Web3 ( web3 . currentProvider ) ; } else { myWeb3 = new Web3 ( new Web3 . providers . HttpProvider ( 'http://localhost:8545' ) ) ; } return myWeb3 ; } ; export const getContractInstance = ( artifact , contractAddress = artifact . address ) => { const web3 = getWeb3 ( ) ; return new web3 . eth . Contract ( artifact . abi , contractAddress ) ; } ; export const getGasUsageInEth = async ( txHash ) => { const web3 = getWeb3 ( ) ; const txReceipt = await web3 . eth . getTransactionReceipt ( txHash ) ; const txn = await web3 . eth . getTransaction ( txHash ) ; const { gasPrice } = txn ; const { gasUsed } = txReceipt ; return new BigNumber ( gasPrice ) . mul ( gasUsed ) ; } ; export const txnFrom = ( from ) => { return { from , gas : DEFAULT_GAS } ; } ; export const blankTxn = async ( from ) => { const web3 = getWeb3 ( ) ; await web3 . eth . sendTransaction ( { from , to : NULL_ADDRESS , value : '1' , } ) ; } ; export const linkLibrariesToDeploy = async ( contract , libraries , from ) => { contract . setNetwork ( 50 ) ; await Promise . all ( libraries . map ( async ( library ) => { const truffleLibrary = await library . new ( { from } ) ; if ( process . env . IS_BUIDLER === 'true' ) { try { await contract . link ( truffleLibrary ) ; } finally { return ; } } else { await contract . link ( library . contractName , truffleLibrary . address ) ; } } ) ) ; } ; export const importFromContracts = ( contractName ) => { return importFromRepo ( 'set-protocol-contracts' , contractName ) ; } ; const importFromRepo = ( repoName , contractName ) => { const data = require ( repoName + '/dist/artifacts/ts/' + contractName ) [ contractName ] ; const instance = contract ( data ) ; instance . setProvider ( web3 . currentProvider ) ; return instance ; } ; export const importArtifactsFromSource = ( contractName ) => { const web3 = getWeb3 ( ) ; let instance ; try { instance = artifacts . require ( contractName ) ; return instance ; } catch ( e ) { } try { const data = require ( 'set-protocol-strategies/dist/artifacts/ts/' + contractName ) [ contractName ] ; instance = contract ( data ) ; instance . setProvider ( web3 . currentProvider ) ; return instance ; } catch ( e ) { } try { const filePath = 'set-protocol-strategies-' + version + '/dist/artifacts/ts/' + contractName ; const data = require ( filePath ) [ contractName ] ; instance = contract ( data ) ; instance . setProvider ( web3 . currentProvider ) ; return instance ; } catch ( e ) { } } ;