Web3 sending EIP-1559 Transaction Completed Successfully but got back an Error: invalid remainder
Web3 sending EIP-1559 Transaction Completed Successfully but got back an Error: invalid remainder
概览
我能够使用 Web3 通过 Rinkeby Testnet 发送 EIP-1559 交易,交易完成没有任何问题,我确实在我的 Metamask Rinkeby 账户中获得了我的以太币。
问题是我在交易完成后收到 Error: invalid remainder 并且我的程序立即停止。
代码
// EIP-1559 Transanction
// Loading dependencies
const fs = require( 'fs' ).promises;
const Web3 = require( 'web3' );
const HDWalletProvider = require( '@truffle/hdwallet-provider' );
const { mnemonicGenerate } = require( '@polkadot/util-crypto' );
const { FeeMarketEIP1559Transaction } = require( '@ethereumjs/tx' );
const Common = require( '@ethereumjs/common' ).default;
async function main () {
// Infura rinkeby's url
const infuraRinkeby = "https://rinkeby.infura.io/v3/4fa53ccf01504cc69f0dcbdfdaa38acf";
// Generating bip39 mnemonic
// const mnemonic = mnemonicGenerate();
// Loading previously generated mnemonic
const mnemonic = ( JSON.parse( await fs.readFile( "./private/mnemonic.json" , "utf8" ) ) ).mnemonic;
// Creating wallet from mnemonic and saving it as a web3 provider with infura
const provider = new HDWalletProvider( mnemonic , infuraRinkeby );
// Passing the web3 provider to web3
const web3 = new Web3( provider );
// Getting sending address and balance
const sendingAddress = ( await web3.eth.getAccounts() )[0];
var sendingAddressBalance = await web3.eth.getBalance( sendingAddress );
console.log( " " );
console.log( Array( 75 ).join( "=" ) );
console.log( "Before Transaction" );
console.log( "Address: " + sendingAddress );
console.log( "Balance: " + sendingAddressBalance );
// Setting receiving address
const receivingAddress = "0x24620ddf8474c89C0Fc0c916acBcF4029C4eB47F";
// Getting the private key for the sending address account
const preKey = ( provider.wallets )[ sendingAddress.toLowerCase() ].privateKey.toString( 'hex' );
const privateKey = Buffer.from( preKey , 'hex' );
// Setting chain
var chain = new Common( { chain : 'rinkeby', hardfork : 'london' } );
// Constructing the raw transaction
// To 'Speed Up' a transaction like Metamask just send a new transaction with the same nonce but with higher gas
const rawTx = {
"from" : web3.utils.toHex( sendingAddress ),
"to" : web3.utils.toHex( receivingAddress ),
"gasLimit" : web3.utils.toHex( 210000 ),
"maxFeePerGas" : web3.utils.toHex( web3.utils.toWei( '1.5' , 'gwei' ) ),
"maxPriorityFeePerGas" : web3.utils.toHex( web3.utils.toWei( '1.5' , 'gwei' ) ),
"value" : web3.utils.toHex( web3.utils.toWei( '0.1' , 'ether' ) ),
"data" : web3.utils.toHex( 'This is my example' ),
"nonce" : web3.utils.toHex( await web3.eth.getTransactionCount( sendingAddress , 'pending' ) ),
"chainId" : "0x04",
"accessList" : [],
"type" : "0x02"
};
// Creating a new transaction
const tx = FeeMarketEIP1559Transaction.fromTxData( rawTx , { chain } );
// Signing the transaction
const signedTransaction = tx.sign( privateKey );
// Generating transaction Hash
const txHash = await web3.utils.sha3( '0x' + signedTransaction.serialize().toString( 'hex' ) );
console.log( "Tx Hash: " + txHash );
// Sending transaction
await web3.eth.sendSignedTransaction( '0x' + signedTransaction.serialize().toString( 'hex' ) )
.on( 'error' , function( error ) {
console.error( error )
} );
console.log( 'After Transaction' );
sendingAddressBalance = await web3.eth.getBalance( sendingAddress );
console.log( "Address: " + sendingAddress );
console.log( "Balance: " + sendingAddressBalance );
console.log( Array( 75 ).join( "=" ) );
console.log( " " );
};
main();
错误截图
问题
如何解决这个错误?
我正在尝试将此示例作为作品集添加到项目存储库中以在未来的个人网站中展示,但我做不到,因为它在发送交易后崩溃(交易成功)。 如何在不使整个程序崩溃的情况下捕获错误?(已经尝试过 try catch 但没有解决任何问题)。
如果有导致错误的库,我可以使用什么其他库来替换导致程序崩溃的库?
交易Link交易完成证明
Rinkeby Transaction from rinkeby.etherscan.io
如果您转到事务 link 中的数据并将十六进制转换为字符串,您将看到消息 这是我的示例。
所以,由于 Rakz 的评论,我已经找到了解决这个问题的方法。
这是 EIP-1559 交易的工作代码。
// Loading dependencies
const fs = require( 'fs' ).promises;
const Web3 = require( 'web3' );
const HDWalletProvider = require( '@truffle/hdwallet-provider' );
const { mnemonicGenerate } = require( '@polkadot/util-crypto' );
const { FeeMarketEIP1559Transaction } = require( '@ethereumjs/tx' );
const Common = require( '@ethereumjs/common' ).default;
async function main () {
// Infura rinkeby's url
const infuraRinkeby = "https://rinkeby.infura.io/v3/4fa53ccf01504cc69f0dcbdfdaa38acf";
// Generating bip39 mnemonic
// const mnemonic = mnemonicGenerate();
// Loading from a previously generated mnemonic
const mnemonic = ( JSON.parse( await fs.readFile( "./private/mnemonic.json" , "utf8" ) ) ).mnemonic;
// Creating a wallet from mnemonic with 10 accounts
const hdWallet = new HDWalletProvider( mnemonic , infuraRinkeby );
// Getting the addresses of the 10 accounts
const accountsGenerated = Object.keys( hdWallet.wallets );
// Initialize the provider
const web3 = new Web3( infuraRinkeby );
// Initialize a wallet
const wallet = web3.eth.accounts.wallet;
// Migrating the accounts generated from HDWallet to web3
for ( const account of accountsGenerated ) {
const privateKey = ( hdWallet.wallets )[ account.toLowerCase() ].privateKey.toString( 'hex' );
wallet.add( web3.eth.accounts.privateKeyToAccount( privateKey ) );
}
// Initializating the sending and receiving addresses
const sendingAddress = wallet[ 0 ].address;
const receivingAddress = "0x24620ddf8474c89C0Fc0c916acBcF4029C4eB47F";
// Getting the private key for the sending address account
const privateKey = Buffer.from( wallet[ 0 ].privateKey.replace( '0x' , '' ) , 'hex' );
// Setting chain
var chain = new Common( { chain : 'rinkeby', hardfork : 'london' } );
// Constructing the raw transaction
// To 'Speed Up' a transaction like Metamask just send a new transaction with the same nonce but with higher gas
const rawTx = {
"to" : web3.utils.toHex( receivingAddress ),
"gasLimit" : web3.utils.toHex( 210000 ),
"maxFeePerGas" : web3.utils.toHex( web3.utils.toWei( '1.5' , 'gwei' ) ),
"maxPriorityFeePerGas" : web3.utils.toHex( web3.utils.toWei( '1.5' , 'gwei' ) ),
"value" : web3.utils.toHex( web3.utils.toWei( '0.1' , 'ether' ) ),
"data" : web3.utils.toHex( 'TESTING TODAY' ),
"nonce" : web3.utils.toHex( await web3.eth.getTransactionCount( sendingAddress , 'pending' ) ),
"chainId" : "0x04",
"accessList" : [],
"type" : "0x02"
};
// Creating a new transaction
const tx = FeeMarketEIP1559Transaction.fromTxData( rawTx , { chain } );
// Signing the transaction
const signedTransaction = tx.sign( privateKey );
// Serializing the transaction
const serializedTransaction = '0x' + signedTransaction.serialize().toString( 'hex' );
// Getting the Sending Address Balance
var sendingAddressBalance = await web3.eth.getBalance( sendingAddress );
console.log( " " );
console.log( Array( 75 ).join( "=" ) );
console.log( "Before Transaction" );
console.log( "Address: " + sendingAddress );
console.log( "Balance: " + sendingAddressBalance );
console.log( Array( 75 ).join( "=" ) );
// Generating transaction Hash
const txHash = await web3.utils.sha3( serializedTransaction );
console.log( "Tx Hash: " + txHash );
// Sending transaction
await web3.eth.sendSignedTransaction( serializedTransaction )
.on( 'error' , function( error ) {
console.error( error )
} );
console.log( Array( 75 ).join( "=" ) );
console.log( 'After Transaction' );
sendingAddressBalance = await web3.eth.getBalance( sendingAddress );
console.log( "Address: " + sendingAddress );
console.log( "Balance: " + sendingAddressBalance );
console.log( Array( 75 ).join( "=" ) );
};
main();
概览
我能够使用 Web3 通过 Rinkeby Testnet 发送 EIP-1559 交易,交易完成没有任何问题,我确实在我的 Metamask Rinkeby 账户中获得了我的以太币。 问题是我在交易完成后收到 Error: invalid remainder 并且我的程序立即停止。
代码
// EIP-1559 Transanction
// Loading dependencies
const fs = require( 'fs' ).promises;
const Web3 = require( 'web3' );
const HDWalletProvider = require( '@truffle/hdwallet-provider' );
const { mnemonicGenerate } = require( '@polkadot/util-crypto' );
const { FeeMarketEIP1559Transaction } = require( '@ethereumjs/tx' );
const Common = require( '@ethereumjs/common' ).default;
async function main () {
// Infura rinkeby's url
const infuraRinkeby = "https://rinkeby.infura.io/v3/4fa53ccf01504cc69f0dcbdfdaa38acf";
// Generating bip39 mnemonic
// const mnemonic = mnemonicGenerate();
// Loading previously generated mnemonic
const mnemonic = ( JSON.parse( await fs.readFile( "./private/mnemonic.json" , "utf8" ) ) ).mnemonic;
// Creating wallet from mnemonic and saving it as a web3 provider with infura
const provider = new HDWalletProvider( mnemonic , infuraRinkeby );
// Passing the web3 provider to web3
const web3 = new Web3( provider );
// Getting sending address and balance
const sendingAddress = ( await web3.eth.getAccounts() )[0];
var sendingAddressBalance = await web3.eth.getBalance( sendingAddress );
console.log( " " );
console.log( Array( 75 ).join( "=" ) );
console.log( "Before Transaction" );
console.log( "Address: " + sendingAddress );
console.log( "Balance: " + sendingAddressBalance );
// Setting receiving address
const receivingAddress = "0x24620ddf8474c89C0Fc0c916acBcF4029C4eB47F";
// Getting the private key for the sending address account
const preKey = ( provider.wallets )[ sendingAddress.toLowerCase() ].privateKey.toString( 'hex' );
const privateKey = Buffer.from( preKey , 'hex' );
// Setting chain
var chain = new Common( { chain : 'rinkeby', hardfork : 'london' } );
// Constructing the raw transaction
// To 'Speed Up' a transaction like Metamask just send a new transaction with the same nonce but with higher gas
const rawTx = {
"from" : web3.utils.toHex( sendingAddress ),
"to" : web3.utils.toHex( receivingAddress ),
"gasLimit" : web3.utils.toHex( 210000 ),
"maxFeePerGas" : web3.utils.toHex( web3.utils.toWei( '1.5' , 'gwei' ) ),
"maxPriorityFeePerGas" : web3.utils.toHex( web3.utils.toWei( '1.5' , 'gwei' ) ),
"value" : web3.utils.toHex( web3.utils.toWei( '0.1' , 'ether' ) ),
"data" : web3.utils.toHex( 'This is my example' ),
"nonce" : web3.utils.toHex( await web3.eth.getTransactionCount( sendingAddress , 'pending' ) ),
"chainId" : "0x04",
"accessList" : [],
"type" : "0x02"
};
// Creating a new transaction
const tx = FeeMarketEIP1559Transaction.fromTxData( rawTx , { chain } );
// Signing the transaction
const signedTransaction = tx.sign( privateKey );
// Generating transaction Hash
const txHash = await web3.utils.sha3( '0x' + signedTransaction.serialize().toString( 'hex' ) );
console.log( "Tx Hash: " + txHash );
// Sending transaction
await web3.eth.sendSignedTransaction( '0x' + signedTransaction.serialize().toString( 'hex' ) )
.on( 'error' , function( error ) {
console.error( error )
} );
console.log( 'After Transaction' );
sendingAddressBalance = await web3.eth.getBalance( sendingAddress );
console.log( "Address: " + sendingAddress );
console.log( "Balance: " + sendingAddressBalance );
console.log( Array( 75 ).join( "=" ) );
console.log( " " );
};
main();
错误截图
问题
如何解决这个错误?
我正在尝试将此示例作为作品集添加到项目存储库中以在未来的个人网站中展示,但我做不到,因为它在发送交易后崩溃(交易成功)。 如何在不使整个程序崩溃的情况下捕获错误?(已经尝试过 try catch 但没有解决任何问题)。
如果有导致错误的库,我可以使用什么其他库来替换导致程序崩溃的库?
交易Link交易完成证明
Rinkeby Transaction from rinkeby.etherscan.io
如果您转到事务 link 中的数据并将十六进制转换为字符串,您将看到消息 这是我的示例。
所以,由于 Rakz 的评论,我已经找到了解决这个问题的方法。 这是 EIP-1559 交易的工作代码。
// Loading dependencies
const fs = require( 'fs' ).promises;
const Web3 = require( 'web3' );
const HDWalletProvider = require( '@truffle/hdwallet-provider' );
const { mnemonicGenerate } = require( '@polkadot/util-crypto' );
const { FeeMarketEIP1559Transaction } = require( '@ethereumjs/tx' );
const Common = require( '@ethereumjs/common' ).default;
async function main () {
// Infura rinkeby's url
const infuraRinkeby = "https://rinkeby.infura.io/v3/4fa53ccf01504cc69f0dcbdfdaa38acf";
// Generating bip39 mnemonic
// const mnemonic = mnemonicGenerate();
// Loading from a previously generated mnemonic
const mnemonic = ( JSON.parse( await fs.readFile( "./private/mnemonic.json" , "utf8" ) ) ).mnemonic;
// Creating a wallet from mnemonic with 10 accounts
const hdWallet = new HDWalletProvider( mnemonic , infuraRinkeby );
// Getting the addresses of the 10 accounts
const accountsGenerated = Object.keys( hdWallet.wallets );
// Initialize the provider
const web3 = new Web3( infuraRinkeby );
// Initialize a wallet
const wallet = web3.eth.accounts.wallet;
// Migrating the accounts generated from HDWallet to web3
for ( const account of accountsGenerated ) {
const privateKey = ( hdWallet.wallets )[ account.toLowerCase() ].privateKey.toString( 'hex' );
wallet.add( web3.eth.accounts.privateKeyToAccount( privateKey ) );
}
// Initializating the sending and receiving addresses
const sendingAddress = wallet[ 0 ].address;
const receivingAddress = "0x24620ddf8474c89C0Fc0c916acBcF4029C4eB47F";
// Getting the private key for the sending address account
const privateKey = Buffer.from( wallet[ 0 ].privateKey.replace( '0x' , '' ) , 'hex' );
// Setting chain
var chain = new Common( { chain : 'rinkeby', hardfork : 'london' } );
// Constructing the raw transaction
// To 'Speed Up' a transaction like Metamask just send a new transaction with the same nonce but with higher gas
const rawTx = {
"to" : web3.utils.toHex( receivingAddress ),
"gasLimit" : web3.utils.toHex( 210000 ),
"maxFeePerGas" : web3.utils.toHex( web3.utils.toWei( '1.5' , 'gwei' ) ),
"maxPriorityFeePerGas" : web3.utils.toHex( web3.utils.toWei( '1.5' , 'gwei' ) ),
"value" : web3.utils.toHex( web3.utils.toWei( '0.1' , 'ether' ) ),
"data" : web3.utils.toHex( 'TESTING TODAY' ),
"nonce" : web3.utils.toHex( await web3.eth.getTransactionCount( sendingAddress , 'pending' ) ),
"chainId" : "0x04",
"accessList" : [],
"type" : "0x02"
};
// Creating a new transaction
const tx = FeeMarketEIP1559Transaction.fromTxData( rawTx , { chain } );
// Signing the transaction
const signedTransaction = tx.sign( privateKey );
// Serializing the transaction
const serializedTransaction = '0x' + signedTransaction.serialize().toString( 'hex' );
// Getting the Sending Address Balance
var sendingAddressBalance = await web3.eth.getBalance( sendingAddress );
console.log( " " );
console.log( Array( 75 ).join( "=" ) );
console.log( "Before Transaction" );
console.log( "Address: " + sendingAddress );
console.log( "Balance: " + sendingAddressBalance );
console.log( Array( 75 ).join( "=" ) );
// Generating transaction Hash
const txHash = await web3.utils.sha3( serializedTransaction );
console.log( "Tx Hash: " + txHash );
// Sending transaction
await web3.eth.sendSignedTransaction( serializedTransaction )
.on( 'error' , function( error ) {
console.error( error )
} );
console.log( Array( 75 ).join( "=" ) );
console.log( 'After Transaction' );
sendingAddressBalance = await web3.eth.getBalance( sendingAddress );
console.log( "Address: " + sendingAddress );
console.log( "Balance: " + sendingAddressBalance );
console.log( Array( 75 ).join( "=" ) );
};
main();