首先呈现坚实的编译代码Compile.js
const path = require('path');
const solc = require('solc');
const fs = require('fs-extra');
const buildPath = path.resolve(__dirname, 'build');
fs.removeSync(buildPath);
const campaignPath = path.resolve(__dirname, 'contracts', 'Campaign.sol');
const source = fs.readFileSync(campaignPath, 'UTF-8');
var input = {
language: 'Solidity',
sources: {
'Campaign.sol' : {
content: source
}
},
settings: {
outputSelection: {
'*': {
'*': [ '*' ]
}
}
}
};
var output = JSON.parse(solc.compile(JSON.stringify(input)));
fs.ensureDirSync(buildPath);
for(contractName in output.contracts['Campaign.sol']){
fs.outputJSONSync(
path.resolve(buildPath, contractName + '.json'),
output.contracts['Campaign.sol'][contractName]
);
}
我的哪个编译代码是对的还是错的?
这是我的deploy.js
const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const compiledFactory = require('./build/CampaignFactory.json');
const provider = new HDWalletProvider(
// process.env.NEXT_PUBLIC_META_MASK,
// process.env.NEXT_PUBLIC_INFURA_API
'dove poppih solid recycle impose piano leisure twin pyramid afraid spolik nose',
'https://sepolia.infura.io/v3/fcuijy7e97ee2f475da508dc6d1dc42701'
);
const web3 = new Web3(provider);
const deploy = async () => {
const accounts = await web3.eth.getAccounts();
console.log('attempting to deploy from account: ', accounts[0]);
const result = await new web3.eth.Contract(compiledFactory.abi)
.deploy({data: compiledFactory.evm.bytecode.object})
.send( {from:accounts[0], gas:'125653'});
console.log('Contract deployed to: ', result.options.address);
provider.engine.stop();
};
deploy();
上面的代码得到错误
attempting to deploy from account: 0x909CD220D8707e052C0a6E94FA167227fFfc7865
node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
Error: insufficient funds for gas * price + value
这可能是一个bug,请记住,如果您的帐户中没有足够的ETH
,如果您的node
与网络不同步,或者如果您没有指定天然气价格,则此错误很常见或者是其他命令有一点错误。
gas * price + value实际上意味着MAXGas * price
您可以尝试此解决方案将所有值转换为hex
(如果使用bash则使用bc),并确保同时指定(gas和gasPrice),并将所有值包含在单引号中:
eth.sendTransaction({from:'0x123456', to:'0x123456', value: '0x8AC4270ACC4B7FF7', gas: '0x5208', gasPrice: '0x4A817C800'});"
其他解决方案可以是'降级版本,因为有时它会意外地导致这个问题。