web3dart
web3dart copied to clipboard
client.sendTransaction not tranferring any ether in private chain
Hi, I am very new to web3dart or blockchain. I have some problem with web3dart, I am unable to send transaction using the dart code attached in this issue. The code executed correctly however it is not transferring any amount. It returned a transaction hash however whenever I checked it in the metamask transactions does not show up neither value changes. It may be due to non specifying a network_id or hardfork along with the transaction. The code equivalent that work correctly in the web3js is also attached.
//Dart Code
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';
//import 'package:web3dart/transaction.dart'
const String privateKey =
'12.......21.....12......21.........21';
const String rpcUrl = 'https://examplechain.org/v1/undefined';
Future<void> main() async {
// start a client we can use to send transactions
final client = Web3Client(rpcUrl, Client());
final credentials = await client.credentialsFromPrivateKey(privateKey);
final address = await credentials.extractAddress();
print(address.hexEip55);
print(await client.getBalance(address));
Transaction transaction = Transaction(
to: EthereumAddress.fromHex('0xFEED12......fdaE12'),
gasPrice: EtherAmount.inWei(BigInt.one),
maxGas: 100000,
nonce: 0x101,
value: EtherAmount.fromUnitAndValue(EtherUnit.ether, 101),
);
//await client.sendTransaction(credentials, transaction, chainId: 69696969);
final signed =
await client.signTransaction(credentials, transaction, chainId: 69696969);
print(await client.sendRawTransaction(
signed,
));
/*await client.sendTransaction(
credentials,
Transaction(
to: EthereumAddress.fromHex('0xBEACH123..........1F02'),
gasPrice: EtherAmount.inWei(BigInt.one),
maxGas: 100000,
value: EtherAmount.fromUnitAndValue(EtherUnit.ether, 101),
),
chainId: 108801,
*/
// fetchChainIdFromNetworkId: true,
await client.dispose();
}
Using the following code I can clearly able to transfer any amount to any target wallet address
//Web3js Code
const Web3 = require('web3');
const Tx = require('ethereumjs-tx').Transaction;
const Common = require('ethereumjs-common');
let web3 = new Web3("https://examplechain.org/v1/undefined");
const privKey =
Buffer.from('91919021.........................91d3767ed32112','hex'); // Genesis private key
const addressFrom = '0xFEED132ec........f8c5b6f8CfFFFF';
const addressTo = '0xFEED213...........2d8C449C';
web3.eth.getTransactionCount(addressFrom, (err, txCount) => {
const txObject = {
nonce: web3.utils.toHex(txCount),
to: addressTo,
value: web3.utils.toHex(web3.utils.toWei('400', 'ether')),
gasLimit: web3.utils.toHex(100000),
gasPrice: web3.utils.toHex(web3.utils.toWei('1', 'gwei'))
};
const common = Common.default.forCustomChain('mainnet', {
name: 'newchain',
networkId: 69696969,
chainId: 69696969
}, 'petersburg');
const tx = new Tx(txObject, {common});
//const tx = new Tx(txObject, {'chain':'56'});
//const tx = new Tx(txObject, {'chain':'binance'});
tx.sign(privKey);
const serializedTrans = tx.serialize();
const raw = '0x' + serializedTrans.toString('hex');
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
console.log('txHash:', txHash)
});
I have the same problem
我也有同样的问题
After the transfer is successful, I can't find the transaction in the browser.
me to any solution, After the transfer is successful, I can't find the transaction?
The transaction returns hash but doesn't appear on Etherscan or change the values. Please help :(
final sendFunction = contract.function('transfer');
await client.sendTransaction( credentials, Transaction.callContract ( from: ownAddress, contract: contract, function: sendFunction, parameters: [receiver, BigInt.from(10)], gasPrice: EtherAmount.inWei(BigInt.from(1000000)), maxGas: 100000,
),
chainId: 4,
).then((value) async{
print("DONE");
print(value);
final balance = await client.call(
contract: contract, function: balanceFunction, params: [ownAddress]);
print('We have ${balance.first} EC');
});
in callContract: nonce: await provider.getTransactionCount(credentials!.address),
"gasPrice" in a network with eip1559 activated must not be included "from" is not needed if you send it from the same signer(credentials)