I have been having horrible problems catching events in Rinkeby test net using Metamask. I know things work because I can use Metamask though localhost (testrpc) and it works fine, but it is extremely intermittent with Metamask.
This seems to be a really common issue. I have solved it by instead logging the tx hash that is returned when you make a tx, and then calling web3.eth.getTransactionReceipt(txhash) until it returns not null, and parsing the events from there.
Hopefully this helps. Of course this isn’t viable in all situations.
I can see the price that you are using is rather low already (it needs to be converted from Gwei to Wei), but you will also benefit of estimating the gas of the transaction, and use other features of Nethereum.
First of all, we can just have a TransferFunction. This is mainly an object that describes the Transfer function from the ERC20 contract and the different parameters. This inherits from ContractMessage, which includes other properties like AddressFrom, Gas, GasPrice, Value (Ether value) which are part of a normal Transaction.
[Function("transfer", "bool")]
public class TransferFunction : ContractMessage
{
[Parameter("address", "_to", 1)]
public string To { get; set; }
[Parameter("uint256", "_value", 2)]
public BigInteger TokenAmount { get; set; }
}
Now let's define our custom variables:
var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
var receiverAddress = "0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe";
var privatekey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
var url = "https://rinkeby.infura.io/";
and create a new instance of Web3.
var web3 = new Web3.Web3(new Account(privatekey), url);
This instance of Web3 has been created using a new Account object using the private key. This mainly enables Web3 to sign any transactions "offline" before sending them, so you don't need to craft everything yourself.
The next step is to create an instance of the message we are going to send.
var transactionMessage = new TransferFunction()
{
FromAddress = senderAddress,
To = receiverAddress,
TokenAmount = 100,
//Set our own price
GasPrice = Web3.Web3.Convert.ToWei(25, UnitConversion.EthUnit.Gwei)
};
Here it has been populated the FromAddress (this is not necessary as this will be picked from the Account already set), the receiver (To) from the smart contract, the amount and finally the GasPrice.
This GasPrice is a custom price (not using the Nethereum default which might be too expensive due to price fluctuation, and it is now).
To set the price we have converted it from Gwei which is the unit is normally used for pricing to Wei.
Now that we have our "message" / transaction ready we can create a transaction handler, which will be responsible to send the transaction.
var transferHandler = web3.Eth.GetContractTransactionHandler<TransferFunction>();
Another step (which is optional) as this is done automatically for you is to estimate the cost (in gas) of the transaction.
var estimate = await transferHandler.EstimateGasAsync(transactionMessage, ContractAddress);
transactionMessage.Gas = estimate.Value;
And finally send the transaction:
var transactionHash = await transferHandler.SendRequestAsync(transactionMessage, ContractAddress);
You can find the full sample here:
https://github.com/Nethereum/Nethereum.CQS.SimpleTokenTransfer/blob/master/Nethereum.CQS.SimpleTokenTransfer/Program.cs#L111-L155
Best Answer
Password in this case is used to unlock keystore file, stored locally on your machine (where node is running). Usually it reside in ethereum client folder under the subfolder
keystore
. Metamask doesn't store your keystore this way, it uses browser data instead.If you wan't to unlock your wallet you need to create another account somewhere where keystore file is generated by default after account creation (with password).
Or you can use your MetaMask account and
Export Private Key
to further use some web3py (for example) to generate your own keystore file:After you generate your keystore file just put it in the keystore folder in your client folder and the client should automatically detect it and next time you try to unlock account with the correct password it should work as expected.