First commit
This commit is contained in:
93
scripts/1_deploy_on_mainnet.js
Normal file
93
scripts/1_deploy_on_mainnet.js
Normal file
@@ -0,0 +1,93 @@
|
||||
require('dotenv').config()
|
||||
|
||||
// Mainnet
|
||||
process.env.DEBUG = '0'
|
||||
|
||||
const fs = require('fs')
|
||||
const { logMessage, errorMessage } = require('../lib/logger')
|
||||
const {
|
||||
tronWeb,
|
||||
setDefaultPrivateKey,
|
||||
contractData,
|
||||
feeLimit,
|
||||
sunToTrx,
|
||||
addressToBase58
|
||||
} = require('../lib/tron')
|
||||
const { sleep } = require('../lib/utils')
|
||||
|
||||
// Binary data of the contract and ABI
|
||||
const bytecode = contractData.bytecode
|
||||
const abi = contractData.abi
|
||||
logMessage('1_deploy_on_mainnet', 'Bytecode and ABI loaded')
|
||||
|
||||
// Get TRX balance
|
||||
const getTrxBalance = async (address) => {
|
||||
const balance = sunToTrx(await tronWeb.trx.getBalance(address))
|
||||
logMessage('1_deploy_on_mainnet', `TRX balance: ${balance} TRX`)
|
||||
return balance
|
||||
}
|
||||
|
||||
async function deployContract() {
|
||||
try {
|
||||
setDefaultPrivateKey()
|
||||
|
||||
logMessage('1_deploy_on_mainnet', 'Deploying contract...')
|
||||
|
||||
// Get TRX balance
|
||||
const address = tronWeb.defaultAddress.base58
|
||||
const balance = await getTrxBalance(address)
|
||||
if (balance < 200) {
|
||||
throw new Error('Insufficient TRX balance to deploy the contract')
|
||||
}
|
||||
|
||||
// Sign transaction
|
||||
const unsignedTxn = await tronWeb.transactionBuilder.createSmartContract({
|
||||
abi,
|
||||
bytecode,
|
||||
feeLimit,
|
||||
callValue: 0, // TRX sent to the contract
|
||||
parameters: [process.env.CHAINLINK_USD_PRICE_FEED]
|
||||
})
|
||||
const signedTxn = await tronWeb.trx.sign(unsignedTxn)
|
||||
logMessage('1_deploy_on_mainnet', 'Transaction signed')
|
||||
|
||||
// Broadcast transaction
|
||||
const tx = await tronWeb.trx.sendRawTransaction(signedTxn)
|
||||
logMessage('1_deploy_on_mainnet', `Transaction ID: ${tx.txid}`)
|
||||
logMessage('1_deploy_on_mainnet', 'Contract deployed')
|
||||
|
||||
// Get contract address
|
||||
let contractAddress = {}
|
||||
do {
|
||||
await sleep(5) // Wait 5 seconds
|
||||
contractAddress = await tronWeb.trx.getTransactionInfo(tx.txid)
|
||||
} while (!contractAddress.contract_address)
|
||||
logMessage(
|
||||
'1_deploy_on_mainnet',
|
||||
`Contract address: ${contractAddress.contract_address}`
|
||||
)
|
||||
|
||||
// Add/Change into .env
|
||||
let envContent = fs.readFileSync('.env', 'utf8')
|
||||
if (/CONTRACT_ADDRESS=/.test(envContent)) {
|
||||
envContent = envContent.replace(
|
||||
/CONTRACT_ADDRESS=.*/,
|
||||
`CONTRACT_ADDRESS=${addressToBase58(contractAddress.contract_address)}`
|
||||
)
|
||||
} else {
|
||||
envContent += `\n\n# Contract address\nCONTRACT_ADDRESS=${addressToBase58(
|
||||
contractAddress.contract_address
|
||||
)}`
|
||||
}
|
||||
fs.writeFileSync('.env', envContent)
|
||||
|
||||
process.exit(0)
|
||||
} catch (error) {
|
||||
errorMessage(
|
||||
'1_deploy_on_mainnet',
|
||||
`ERROR: ${error.message || JSON.stringify(error)}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
deployContract()
|
Reference in New Issue
Block a user