Conflux-Chain / ConfluxWeb

Conflux JavaScript API, under active development
GNU Lesser General Public License v3.0
7 stars 5 forks source link

TypeError: Cannot read property 'Contract' of undefined #4

Closed saarshah closed 4 years ago

saarshah commented 4 years ago

I want to deploy contract through Web3/JavaScript. Here is my code

const ConfluxWeb = require('conflux-web');
const provider = 'http://testnet-jsonrpc.conflux-chain.org:12537';
const confluxWeb = new ConfluxWeb(provider);
const account1 = '0xb7be910d098ae4e5989d50787787b7e7a1245fa8'
const privateKey1 = "0x"+'4a98c06e6b1520bd973c7bc...............'

var compiledContract = require('./build/MyContract.json');

    (async () => {

        const contract = new confluxWeb.cfx.Contract(compiledContract.abi);
        const params = {
            data: '0x' + compiledContract.bytecode,
            arguments: [account1]
        };
        const transaction = contract.deploy(params);
        const options = {
            data: transaction.encodeABI(),
            gas: await transaction.estimateGas({from: account1})
        };
        const signed = await confluxWeb.cfx.accounts.signTransaction(options, privateKey1);
        const receipt = await confluxWeb.cfx.sendSignedTransaction(signed.rawTransaction);
        console.log(`Contract deployed at address: ${receipt.contractAddress}`);
        })();

I can print my compiledContract with its abi and bytecode... means all data i am giving correctly..then why its showing following error. when i run this command node depoy.js

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'Contract' of undefined
    at C:\Users\aa\Desktop\temp\deploy.js:11:41
    at Object.<anonymous> (C:\Users\aa\Desktop\temp\deploy.js:25:7)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
(node:5856) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5856) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
GeekBerry commented 4 years ago

version 0.2 interface are little difference from 0.1. we write a example of deploy a contract for you. Please upgrade conflux-web to 0.2.0-alpha.4 !

GeekBerry commented 4 years ago

version 0.2 interface are little difference from 0.1. we write a example of deploy a contract for you. See document

GeekBerry commented 4 years ago

no confluxWeb no getAccounts, you should manager you account object instance your self.

saarshah commented 4 years ago

Now i can run your code..... but still error with following output...

100 0xb7be910d098ae4e5989d50787787b7e7a1245fa8 { data: { jsonrpc: '2.0', id: '15744211764826309163', method: 'cfx_estimateGas', params: [ [Object] ] }, result: '0x299fe', duration: 6344 } 170494 { data: { jsonrpc: '2.0', id: '15744211828388148798', method: 'cfx_getTransactionCount', params: [ '0xb7be910d098ae4e5989d50787787b7e7a1245fa8', 'latest_state' ] }, result: '0x3', duration: 5299 } { data: { jsonrpc: '2.0', id: '15744211881514002087', method: 'cfx_sendRawTransaction', params: [ '0xf902140364830299fe8080b901c660806040526000805534801561001457600080fd5b506040516101a63803806101a68339818101604052602081101561003757600080fd5b810190808051906020019092919050505080600081905550506101478061005f6000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806306661abd146100465780637104ddb214610064578063812600df1461006e575b600080fd5b61004e6100b0565b6040518082815260200191505060405180910390f35b61006c6100b6565b005b61009a6004803603602081101561008457600080fd5b81019080803590602001909291905050506100fb565b6040518082815260200191505060405180910390f35b60005481565b3373ffffffffffffffffffffffffffffffffffffffff167fb818399ffd68e821c34de8d5fbc5aeda8456fdb9296fc1b02bf6245ade7ebbd460405160405180910390a2565b60008160008082825401925050819055905091905056fea265627a7a72315820628e7a584d0d60437e4012d16d87ee39db5bea2fd39893f2656063e7fb6ea9f264736f6c634300050b0032000000000000000000000000000000000000000000000000000000000000000a01a09548a00ca943ca5c8769e8d05a81b1155b6f43cd9d74a0a1d355fde0bf787c10a04e20e512999c56cef931c0e01e95a24fb7083cbfdd58f72efab93715f0a7a76c' ] }, result: '0x88b4649f40d365874c992707b1bbed52661543c03844970fc8ba3a2d5b98e0ee', duration: 5616 } { data: { jsonrpc: '2.0', id: '15744211937811885337', method: 'cfx_getTransactionReceipt', params: [ '0x88b4649f40d365874c992707b1bbed52661543c03844970fc8ba3a2d5b98e0ee' ] }, result: null, duration: 5316 } { data: { jsonrpc: '2.0', id: '15744211991042667864', method: 'cfx_getTransactionReceipt', params: [ '0x88b4649f40d365874c992707b1bbed52661543c03844970fc8ba3a2d5b98e0ee' ] }, result: null, duration: 4802 } { data: { jsonrpc: '2.0', id: '15744212039132217077', method: 'cfx_getTransactionReceipt', params: [ '0x88b4649f40d365874c992707b1bbed52661543c03844970fc8ba3a2d5b98e0ee' ] }, result: null, duration: 608 } { data: { jsonrpc: '2.0', id: '15744212049172438453', method: 'cfx_getTransactionReceipt', params: [ '0x88b4649f40d365874c992707b1bbed52661543c03844970fc8ba3a2d5b98e0ee' ] }, result: null, duration: 423 } { data: { jsonrpc: '2.0', id: '15744212059191043032', method: 'cfx_getTransactionReceipt', params: [ '0x88b4649f40d365874c992707b1bbed52661543c03844970fc8ba3a2d5b98e0ee' ] }, result: null, duration: 547 } { data: { jsonrpc: '2.0', id: '15744212069190865967', method: 'cfx_getTransactionReceipt', params: [ '0x88b4649f40d365874c992707b1bbed52661543c03844970fc8ba3a2d5b98e0ee' ] }, result: { blockHash: '0xbd774800312a4f240d9d348abdf521089ae46d0e5a5c9dcc95e71f678b4fc7f4', contractCreated: '0xb955a264e0276ae75c11bea47efa6066940c44d5', epochNumber: 393586, from: '0xb7be910d098ae4e5989d50787787b7e7a1245fa8', gasUsed: '0x299fe', index: 0, logs: [], logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', outcomeStatus: 0, stateRoot: '0x142dc5c89c0f13b06640fe31a9919d74e28f3dbca7d109568546f9773a1d36ab', to: null, transactionHash: '0x88b4649f40d365874c992707b1bbed52661543c03844970fc8ba3a2d5b98e0ee' }, duration: 572 } 0xb955a264e0276ae75c11bea47efa6066940c44d5 { data: { jsonrpc: '2.0', id: '15744212075322118761', method: 'cfx_getCode', params: [ '0xb955a264e0276ae75c11bea47efa6066940c44d5', 'latest_state' ] }, error: { code: -32602, message: 'Account 0xb955a264e0276ae75c11bea47efa6066940c44d5 epoch_number=LatestState does not exist' }, duration: 574 } { Error: Account 0xb955a264e0276ae75c11bea47efa6066940c44d5 epoch_number=LatestState does not exist at HttpProvider.call (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\lib\provider\http.js:44:13) at process._tickCallback (internal/process/next_tick.js:68:7) code: -32602 }

saarshah commented 4 years ago

Is there any provision in conflux-web, if i want to call methods/functions of my contract by giving methods name at runtime like in ethereum . For example myContract.methods(runTimeName)(runTimeArgument). In your documentation lot of examples are missing for some important things, i.e. If i want to change contract state variable. Here you just mentioned Contract.Called.sendTransaction and said See Transaction.callOptions (which is not available at all.)

GeekBerry commented 4 years ago
saarshah commented 4 years ago

The example code is only valid for deployment purpose. Because , after deployment, I run/call its function one one by one (not all at once) , but it show following errors; 1. : When i executed following,

  contract.address = contractAddress;

  const getCode = await cfx.getCode(contract.address);
  console.log(getCode); // same as 'code'

Error: it show following error

{ Error: Account 0x12b00d574920a07c090515a9bd160c00e89c67ac epoch_number=LatestState does not exist
    at HttpProvider.call (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\lib\provider\http.js:44:13)
    at process._tickCallback (internal/process/next_tick.js:68:7) code: -32602 }

2. when i run console.log(await contract.count()); Error: it shows following error

Error: 'to' is required and should match 'Address', got undefined
    at throwError (C:\Users\amirali\Desktop\temp\node_modules\conflux-web-utils\src\transaction.js:5:9)
    at Function.callOptions (C:\Users\amirali\Desktop\temp\node_modules\conflux-web-utils\src\transaction.js:49:44)
    at Conflux.call (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\src\conflux.js:644:55)
    at Called.call (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\src\contract.js:61:42)
    at Called.then (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\src\contract.js:74:33)
    at process._tickCallback (internal/process/next_tick.js:68:7)

3. When i call this code console.log(await contract.inc(1)); Error: it show same above errorError: 'to' is required and should match 'Address', got undefined

4. Finally, when i run following code

const estimateIncGas = await contract.inc(1).estimateGas();
  console.log(estimateIncGas); // it produced estimated gas = 500000000 ...... instead of '26950' (which you mentioned in your example) 
  const receipt = await contract.inc(1).sendTransaction({ from: account, gas: estimateIncGas }).confirmed();
  console.log(receipt);

Error: give following error

{ Error: transaction gas 500000000 exceeds the maximum value 100000000
    at HttpProvider.call (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\lib\provider\http.js:44:13)
    at process._tickCallback (internal/process/next_tick.js:68:7) code: -32602
GeekBerry commented 4 years ago
saarshah commented 4 years ago

Please NOTE that I have now running your example code to avoid any difference between you and me. So considered my code as your code ... means now I have same example code (solidity.sol , abi.json, code.json .. )

i: Yes, i am connected, because my contract has been created with your example code and I can see my contract on confluxscan.io website. 2,3: Now I have set valid address (because i have just followed your code , nothing changed). this is causing following error (both for contract.inc(1) and contract.count()

Error: insufficient data for uint256 type (arg="", coderType="uint256", value="0x", version=4.0.39)
    at Object.throwError (C:\Users\amirali\Desktop\temp\node_modules\ethers\errors.js:76:17)
    at CoderNumber.decode (C:\Users\amirali\Desktop\temp\node_modules\ethers\utils\abi-coder.js:403:20)
    at C:\Users\amirali\Desktop\temp\node_modules\ethers\utils\abi-coder.js:688:32
    at Array.forEach (<anonymous>)
    at unpack (C:\Users\amirali\Desktop\temp\node_modules\ethers\utils\abi-coder.js:680:12)
    at CoderTuple.decode (C:\Users\amirali\Desktop\temp\node_modules\ethers\utils\abi-coder.js:807:22)
    at AbiCoder.decode (C:\Users\amirali\Desktop\temp\node_modules\ethers\utils\abi-coder.js:956:61)
    at Method.decode (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\src\contract.js:113:27)
    at Called.call (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\src\contract.js:69:24)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  reason: 'insufficient data for uint256 type',
  code: 'INVALID_ARGUMENT',
  arg: '',
  coderType: 'uint256',
  value: '0x' }

iv. this is not valid now, because i am now testing your example code and now got following error.

Error: Transaction 0x1902b9df6598b4e9b09eb9afa3a946884e0bc932c493e06fb02bf38249eadd4e is discarded due to a too stale nonce
    at HttpProvider.call (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\lib\provider\http.js:44:13)
    at process._tickCallback (internal/process/next_tick.js:68:7) code: -32602 }

I think , the issue is still not resolved , so should be remained opened.... thanks for your consideration..