4

I am trying to write tests for a smart contract and am having trouble transferring funds to the contract's address.

I'm using web3.py app which (if I understand correctly) runs its own TestRPC, like this:

w3 = Web3(TestRPCProvider())

Then I deploy contract, after which I try to send funds to it:

ACCT_PASSPHRASE = 'KEYSMASH FJAFJKLDSKF7JKFDJ 1530'
acct_addr = w3.personal.newAccount(ACCT_PASSPHRASE)
w3.personal.unlockAccount(acct_addr, ACCT_PASSPHRASE, 1000)
txn_hash = w3.eth.sendTransaction(transaction={
    "from": acct_addr,
    "to": crowdsale_contract_address,
    "value": 5,
    "gas": 3000000,
})
txn = w3.eth.getTransaction(txn_hash)

Result is of course an exception:

ethereum.exceptions.InsufficientBalance: <Transaction(b'30c4')>: 'balance' actual:0 target:3000005

So, how do I create an account with sufficient balance?

Alternatively, how do I use one of the preexisting accounts? What are their passwords? Do they have any balance?

1
  • You can allocate balance in the genesis block at the time of instantiation of a node. You will create accounts first using web3 api. At the time of creation, you will set the password. It will create keystore and account address. Then, at the time of node instantiation, you will add those accounts with your needed balance in the genesis file. That is how, you can have accounts with pre allocated balance. Sep 18, 2019 at 4:31

2 Answers 2

1

Alternatively, how do I use one of the preexisting accounts? What are their passwords? Do they have any balance?

TestRPC includes accounts with pre-existing balances in w3.eth.accounts. They are unlocked, so you don't need a password,

Alternatively, you can set the gasPrice to 0, and invoke contracts from an unfunded account.

0

I solved it by using external TestRPC (Ganache really) installation and using HTTPProvider to interact with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.