conflux-rust icon indicating copy to clipboard operation
conflux-rust copied to clipboard

Could not transfer cfx to my Account (i.e. wallet) through Smart Contract

Open saarshah opened this issue 7 years ago • 6 comments

I have written following contract;

pragma solidity 0.5.1;

contract MyContract {
    uint256 totalSupply; 
    mapping(address => uint256) public balances;
    address payable wallet;

    constructor(address payable _wallet) public {
        totalSupply = 0;
        wallet = _wallet;
    }
    
    function () external payable{
        buyToken();
    }

    function buyToken() public payable {
        balances[msg.sender] += msg.value;
        wallet.transfer(msg.value);
        totalSupply +=msg.value;

    }
    function getTotalSupply()public view returns  (uint256 ){
        return totalSupply;
    }

}

above contract successfully deployed on testnet, running all its functionalities.. except wallet.transfer(msg.value); . This line is trying to send the received value from caller of buyToken() to wallet . I have tested the above contract on ethereum (using web3, metamask's accounts). The account balance at metamask is changing. But the same is not updated on conflux wallet. here is my program in javascript.

const ConfluxTx = require('confluxjs-transaction') ;
const ConfluxWeb = require('conflux-web');
const confluxWeb = new ConfluxWeb('http://testnet-jsonrpc.conflux-chain.org:12537');
const account1 = '0xb7be910d098ae4e5989d50787787b7e7a1245fa8'
const account2 = '0x3d2792f52f314564b58ecc6368f6f47adb727fb3'
const privateKey1 = Buffer.from('4a98c06e6b1520bd973c7bc5d2ee..............','hex')
const privateKey2 = Buffer.from('8f2016c58e898238dd5b4e003985...............', 'hex')
const contractAddress = '0xd022b78af2d8c2ce983da508cd37fab6753ad401'
const contractABI = [{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"buyToken","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"getTotalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_wallet","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"}];

var contract  = new confluxWeb.cfx.Contract(contractABI, contractAddress)

contract.methods.getTotalSupply().call().then(console.log);
contract.methods.balances(account1).call().then(console.log);
contract.methods.balances(account2).call().then(console.log);

// FOR CALLING buyToken() /////////
confluxWeb.cfx.getTransactionCount(account2, (err, txCount) => {
   const data = contract.methods.buyToken().encodeABI();
      const txObject = {
      nonce:    confluxWeb.utils.toHex(txCount),
      gasLimit: confluxWeb.utils.toHex(1000000),
      gasPrice: confluxWeb.utils.toHex(confluxWeb.utils.toDrip('100', 'gdrip')),
      to: contractAddress,
      value: confluxWeb.utils.toHex(confluxWeb.utils.toDrip('7', 'cfx')),
      data:data
     
    }
    // sign the trx
    const tx = new ConfluxTx(txObject)
    // console.log(tx);
//     const tx = new Tx(txObject, {chain:'testrpc'})
// const tx = new Tx(txObject, {chain:'ropsten', hardfork: 'petersburg'})
tx.sign(privateKey2)
// console.log(tx);
const serializedTx = tx.serialize()
// console.log(serializedTx);
const raw = '0x' + serializedTx.toString('hex')
// console.log(raw);
//broadcast tx
confluxWeb.cfx.sendSignedTransaction (raw, (err, txHash)=> {
    console.log('err:', err)
    console.log('txHash', txHash)
})

  })

///// FOR CONTRACT CREATION//////// 
confluxWeb.cfx.getTransactionCount(account1, (err, txCount) => {
  const data='0x608060405234801561001057600080fd5b506040516020806102a28339810180604052602081101561003057600080fd5b81019080805190602001909291905050506000808190555080600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050610209806100996000396000f3fe608060405260043610610051576000357c01000000000000000000000000000000000000000000000000000000009004806327e235e31461005b578063a4821719146100c0578063c4e41b22146100ca575b6100596100f5565b005b34801561006757600080fd5b506100aa6004803603602081101561007e57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506101bc565b6040518082815260200191505060405180910390f35b6100c86100f5565b005b3480156100d657600080fd5b506100df6101d4565b6040518082815260200191505060405180910390f35b34600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc349081150290604051600060405180830381858888f193505050501580156101aa573d6000803e3d6000fd5b50346000808282540192505081905550565b60016020528060005260406000206000915090505481565b6000805490509056fea165627a7a72305820df6b2ff0dfa5f88de9423a5664be62b688c5217b160cd60bc7e3b25f6f9b3eaf0029';
      const txObject = {
      nonce:    confluxWeb.utils.toHex(txCount),
      gasLimit: confluxWeb.utils.toHex(1000000),
      gasPrice: confluxWeb.utils.toHex(confluxWeb.utils.toDrip('10', 'gdrip')),
      data:data
     
    }
    // sign the trx
    const tx = new ConfluxTx(txObject)
    // console.log(tx);
//     const tx = new Tx(txObject, {chain:'testrpc'})
// const tx = new Tx(txObject, {chain:'ropsten', hardfork: 'petersburg'})
tx.sign(privateKey1)
// console.log(tx);
const serializedTx = tx.serialize()
// console.log(serializedTx);
const raw = '0x' + serializedTx.toString('hex')
// console.log(raw);
//broadcast tx
confluxWeb.cfx.sendSignedTransaction (raw, (err, txHash)=> {
    console.log('err:', err)
    console.log('txHash', txHash)
})

  }) 

saarshah avatar Nov 04 '19 09:11 saarshah

Due to deferred execution mechanism, the balance of account will be updated in a short time after the block is mined. You may check your account balance after the state root field in transaction receipt is filled in.

posaggen avatar Nov 05 '19 06:11 posaggen

@posaggen thanks . is it possible we can make three transactions on same time ? Because when I make three transactions . Only first transaction is executed successfully and the remaining two are showing this errors... err: Error: Node error: {"code":-32602,"message":"Failed imported to deferred pool: Tx with same nonce already inserted, try to replace it with a higher gas price"}..Furthermore, if i give nonce manually, then i could make all three transactions successfully, but that transactions (with manually giving nonce) are not updated on my contract /confluxscan at all.

here is my code ;

makeTransaction('10');
makeTransaction('12');
makeTransaction('8');

function makeTransaction(amount){
confluxWeb.cfx.getTransactionCount(account2, (err, txCount) => {
      txObject = {
      nonce:    confluxWeb.utils.toHex(txCount),
      gasLimit: confluxWeb.utils.toHex(1000000),
      gasPrice: confluxWeb.utils.toHex(confluxWeb.utils.toDrip('100', 'gdrip')),
      to: contractAddress,
      value: confluxWeb.utils.toHex(confluxWeb.utils.toDrip(amount, 'cfx')),
      data:txData
    }
    
    tx = new ConfluxTx(txObject)

// sign the trx
tx.sign(privateKey2)

serializedTx = tx.serialize()

raw = '0x' + serializedTx.toString('hex')

  //broadcast tx
  confluxWeb.cfx.sendSignedTransaction (raw, (err, txHash)=> {
    console.log('err:', err)
    console.log('txHash', txHash)
  })

  })

}

saarshah avatar Nov 05 '19 07:11 saarshah

@saarshah Yes it is possible to make three transactions at the same time when nonce are set properly. The nonce of transactions should be consecutive and incremental. The execution order of transactions also depend on nonce. If you send multiple transactions with same nonce, in the end, only one of them will be mined and executed.

posaggen avatar Nov 06 '19 05:11 posaggen

@saarshah Can this issue be closed?

sparkmiw avatar Nov 20 '19 14:11 sparkmiw

still error.. however, i will post it to conflux-web .. if you want to close... you can do it... i have no idea..

saarshah avatar Nov 22 '19 07:11 saarshah

@saarshah I will close this issue, and if you still have any problems of the JavaScript API, please open to conflux-web repo. and the corresponding engineer will help you on time.

boqiu avatar Nov 22 '19 08:11 boqiu