-1

I am new to ethereum block-chain. I created a ERC20 token and crow-sale smart contract in solidity. I also deployed these smart contract with the help of web3.py. I created a contract instance.

Crowed-sell solidity code like this:-

    pragma solidity ^0.4.2;

    import "./DappToken.sol";

    contract DappTokenSale {
        address admin;
        DappToken public tokenContract;
        uint256 public tokenPrice;
        uint256 public tokensSold;

        event Sell(address _buyer, uint256 _amount);

        function DappTokenSale(DappToken _tokenContract, uint256 _tokenPrice) public {
            admin = msg.sender;
            tokenContract = _tokenContract;
            tokenPrice = _tokenPrice;
        }

        function multiply(uint x, uint y) internal pure returns (uint z) {
            require(y == 0 || (z = x * y) / y == x);
        }

        function buyTokens(uint256 _numberOfTokens) public payable {
            require(msg.value == multiply(_numberOfTokens, tokenPrice));
            require(tokenContract.balanceOf(this) >= _numberOfTokens);
            require(tokenContract.transfer(msg.sender, _numberOfTokens));

            tokensSold += _numberOfTokens;

            emit Sell(msg.sender, _numberOfTokens);
        }

      // Ending Token DappTokenSale

        function endSale() public {
                // Require admin
                require (msg.sender == admin);

                 // Transfer remaing DappToken to admin
                require(tokenContract.transfer(admin,tokenContract.balanceOf(this)));


                // Destroy Contract
                selfdestruct(admin);
        }
    }

And python code with web3.py like this:-

 from web3 import Web3
    from solc import  compile_files
    from web3.contract import ConciseContract
    from users.common.ERC20 import token_contract_address,contract_files_list,w3,token_instance
    import time

    token_sell_compile = compile_files(contract_files_list)
    #Compiled crowdsale solidity file
    crowdsale_intrface = token_sell_compile['DappTokenSale.sol:DappTokenSale']


    nonce = w3.eth.getTransactionCount(w3.eth.accounts[0])

    # Instantiate and deploy token-sale contract
    token_sale_contract = w3.eth.contract(abi=crowdsale_intrface['abi'], bytecode=crowdsale_intrface['bin'])
    unsigned_transcation = token_sale_contract.functions.buyTokens(100).buildTransaction({
            'chainId': 1,
           'gas': 70000,
           'to':'0xBefd3a017D364E29955b7502Ed480735144A5Bb1',
           'gasPrice': w3.toWei('1', 'gwei'),
          'nonce': nonce,
       })

    # print(unsigned_transcation)

    # Get transaction hash from deployed crowd-sale contract
    crowd_sale_tx_hash =  token_sale_contract.constructor(token_contract_address,300000000000000000).transact({'from': w3.eth.accounts[0]})
    # print(crowd_sale_tx_hash)

    #Get tx receipt to get crowd-sale contract address
    crowdsale_tx_receipt = w3.eth.getTransactionReceipt(crowd_sale_tx_hash)

    # Crowd-sale contract address of solidity smart contract
    crowdsale_contract_address = crowdsale_tx_receipt['contractAddress']
    # print(crowdsale_contract_address)

    # Crowdsale instance in concise mode
    crowd_sale_abi = crowdsale_intrface['abi']
    crowdsale_instance = w3.eth.contract(address=crowdsale_contract_address,
                                                    abi=crowd_sale_abi, ContractFactoryClass=ConciseContract)
    tokensAvailable = 750000
    print(token_instance.transfer(crowdsale_instance.address, tokensAvailable, w3.eth.accounts[0]))
    print(dir(crowdsale_instance))

    print(crowdsale_instance.buyTokens(1))

    def handle_event(event):
        # print(event)
        receipt = w3.eth.waitForTransactionReceipt(event['transactionHash'])
        print(receipt)
        result = crowdsale_instance.events.Sell.processReceipt(receipt)
        # print(result[0]['args'])
        # print(result)

    def log_loop(event_filter, poll_interval):
        while True:
            for event in event_filter.get_new_entries():
                # print(handle_event(event))
                time.sleep(poll_interval)

    def main():
        block_filter = w3.eth.filter('latest')
        log_loop(block_filter, 2)

    if __name__ == '__main__':
        main()

Modified Python code:-

# Crowd-sale contract address of solidity smart contract
crowdsale_contract_address = crowdsale_tx_receipt['contractAddress']
# print(crowdsale_contract_address)

# Crowdsale instance in concise mode
crowd_sale_abi = crowdsale_intrface['abi']
crowdsale_instance = w3.eth.contract(address=crowdsale_contract_address,
                                                abi=crowd_sale_abi, ContractFactoryClass=ConciseContract)
no_of_token = 5
token_price = 1000000000000000
print(w3.eth.estimateGas)
print(crowdsale_instance.buyTokens(no_of_token,transact={'from':w3.eth.accounts[4],'value':token_price , 'gas': 500000}))

My queries is:- When I call buy token function with the help of contract instance. I found error like this:-

 ValueError: {'message': 'VM Exception while processing transaction: revert', 'code': -32000, 'data': {'0xbcb29ffe905c0bce5f7e1c6b09b664399e396c22dce0e608af5472b551439236': {'error': 'revert', 'program_counter': 367, 'return': '0x0'}, 'stack': 'Error\n    at Function.RuntimeError.fromResults (/home/anupam/Desktop/ganache/node_modules/ganache-core/lib/utils/runtimeerror.js:91:13)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/lib/blockchain_double.js:483:30\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/runTx.js:44:5\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:3888:9\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:473:16\n    at replenish (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:1006:25)\n    at iterateeCallback (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:995:17)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:969:16\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:3885:13\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/runTx.js:194:7\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/cache.js:110:7\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:473:16\n    at replenish (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:1006:25)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:1016:9\n    at eachLimit$1 (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:3196:24)\n    at Object.<anonymous> (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:1046:16)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/cache.js:106:11\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:969:16\n    at next (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:5225:18)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/cache.js:103:7\n    at next (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:5223:28)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/cache.js:98:9', 'name': 'RuntimeError'}}

    Process finished with exit code 1ValueError: {'message': 'VM Exception while processing transaction: revert', 'code': -32000, 'data': {'0xbcb29ffe905c0bce5f7e1c6b09b664399e396c22dce0e608af5472b551439236': {'error': 'revert', 'program_counter': 367, 'return': '0x0'}, 'stack': 'Error\n    at Function.RuntimeError.fromResults (/home/anupam/Desktop/ganache/node_modules/ganache-core/lib/utils/runtimeerror.js:91:13)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/lib/blockchain_double.js:483:30\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/runTx.js:44:5\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:3888:9\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:473:16\n    at replenish (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:1006:25)\n    at iterateeCallback (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:995:17)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:969:16\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:3885:13\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/runTx.js:194:7\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/cache.js:110:7\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:473:16\n    at replenish (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:1006:25)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:1016:9\n    at eachLimit$1 (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:3196:24)\n    at Object.<anonymous> (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:1046:16)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/cache.js:106:11\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:969:16\n    at next (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:5225:18)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/cache.js:103:7\n    at next (/home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/async/dist/async.js:5223:28)\n    at /home/anupam/Desktop/ganache/node_modules/ganache-core/node_modules/ethereumjs-vm/dist/cache.js:98:9', 'name': 'RuntimeError'}}

Process finished with exit code 1

Why this error comes. Actual my total token supply is 100000 and I distribute 75000 token in sell. So before selling I am calling transfer function. So problem is that ERC20 solidity transfer function I define two parameter. when I call transfer function with three parameters include token Available parameter it gives function argument errors.

I think it's problem is solve sell and Transfer event call. I listening event in code . No errors comes. But nothing print in terminal.

Query-1:- Where I do wrong in a code??

Query-2:- How can I call event and function in write way??

It might be a silly question but here i want to understand this concept more deeply.

Please help to clear my above query.

Thanks

1

The contract is reverting because you are sending the buyTokens transaction with zero ether. In your code, the msg.value is checked against the equivalent to the price of the number of tokens that you want to buy

in short, you can buy the tokens doing (according to your example):

    no_of_token = 5
    token_price = 1000000000000000
    crowdsale_instance.buyTokens(no_of_token,transact={'value': no_of_toke*token_price})

Hope this helps

  • Hi Jaime, Thanks for answering. I tried to pass no. of token with token price as you answer in your post. But same error came. How I resolve it?? – Anupam Jain Oct 1 '18 at 4:38
  • Hi Jaime I can not find appropriate answer about my query please help me. – Anupam Jain Oct 1 '18 at 11:57
  • I can not replicate the issue because your solidity code has an import that I do not have access to. Also, can you show me how did you modify your code? If your code is deployed, I can try to access it through web3py, just give me the address. – Jaime Oct 1 '18 at 12:05
  • I give you a contract address of ERC20 and tokensale – Anupam Jain Oct 1 '18 at 12:22
  • I also edit my question please check. – Anupam Jain Oct 1 '18 at 12:26

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.