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)
})
})