3 运行一个不断调用合约更新价格方法的js脚本,之前运行良好,现在一直报错 Error: Returned error: already known,from地址现在有11Matic,余额充足,直接remix调用可以成功

运行一个不断调用合约更新价格方法的js脚本,之前运行良好,现在一直报错#### Error: Returned error: already known,from地址现在有11Matic,余额充足,直接remix-ide调用,可以成功

#!/usr/bin/env node

const Web3 = require('web3');
const web3 = new Web3('https://matic-mainnet.chainstacklabs.com');

const abi = [
    {
        "inputs": [],
        "name": "update",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    }

];
const address = '合约地址';
let Instance= new web3.eth.Contract(abi,address);

/**
 * Matic 合约调用
 */
//web3.eth.getGasPrice().then(console.log);
// (async () =>{
//     let gasPrice = await web3.eth.getGasPrice()
//     console.log(parseInt(gasPrice.toString())+5000000000)
// })()

async function update(index) {
    let gasPrice = await web3.eth.getGasPrice()
    gasPrice = parseInt(gasPrice.toString())+1000000000;
    let code = web3.eth.abi.encodeFunctionCall({
        name: 'update',
        type: 'function',
        inputs: [],
    }, []);
    await web3.eth.accounts.signTransaction({
        to: '合约地址',
        value: '0',                 // 0 ether
        gas: '3000000',             // 3000000 个
        gasPrice: gasPrice.toString(),     // curGasPrice + 1 gwei
        data: code
    }, 'from地址私钥')
        .then(res => {
            web3.eth.sendSignedTransaction(res.rawTransaction)
                .on('transactionHash', hash => {
                    console.log('### hash+index: '+index +'- ' + hash);
                })
                .on('receipt', receipt => {
                    console.log('### receipt: ' + JSON.stringify(receipt));
                })
                .on('confirmation', (confirmationNumber, receipt) => {
                    console.log('### confirmation: '+ confirmationNumber);
                })
                .on('error', err => {
                    console.error('### error+index: ' +index + '- ' + err);
                });
        });
}

let accIndex = 0;

setInterval(async ()=>{

    let indexTmp =accIndex
    await update(indexTmp);
    accIndex++;

},1000*60*20);
请先 登录 后评论

1 个回答

Tiny熊
  擅长:智能合约,以太坊
请先 登录 后评论
  • 1 关注
  • 1 收藏,4593 浏览
  • Ethereal 提出于 2021-11-18 16:22