go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Smart Contract --III: Interact with the deployed contract
 
Subject: Smart Contract --III: Interact with the deployed contract
Author: WebSpider
Posted on: 06/08/2021 07:52:05 PM

You can interact with an on-chain contract (yours or others) as long as you have the contract's ABI and ADDRESS.

	const ABI = CONTRACT.ABI;
	const ADDRESS = "0x01bf16cfCA22a5CeDa1291DD9512a1BF2A1A44A9";
		
        console.log("Contract's address: " + ADDRESS);
        
        let contract = new web3_conn.eth.Contract(ABI, ADDRESS);  
    
        // read access -- call()
        let balance = await contract.methods.getBalance().call();
        console.log("Balance before deposit: " + balance);

	let deposit_estimateGas = await contract.methods.deposite(
				99
			).estimateGas(
				{gas: 500000}, 
				function(error, gasAmount){
					console.log('Callback -- Estimate gasLimit: ', gasAmount);
					if(gasAmount == 500000)
						console.log('Method ran out of gas');
				}
	);
	console.log('Estimate gasLimit: ', deposit_estimateGas);

	// write access -- send()
	let receipt = await contract.methods.deposite(99).send({
            from: accounts[0],
            gas: 500000,             // the limit to cap how much gas to be burned, but may fail due to out-of-gas
            gasPrice: '20000000000'  // how fast you want it go through
        });
        console.log("Receipt's transactionHash: ",  receipt.transactionHash);

        balance = await contract.methods.getBalance().call();
        console.log("Balance after deposit: " + balance);


Output
Contract's address: 0x01bf16cfCA22a5CeDa1291DD9512a1BF2A1A44A9
Balance before deposit: 99
Callback -- Estimate gasLimit:  26853
Estimate gasLimit:  26853
Receipt's transactionHash:  0xa30d194d3308c250d4e5d87a3bdf755da0c89614bae86ae6e9db84ea40fa61ee
Balance after deposit: 198


Further verify onhttps://ropsten.etherscan.io/
From:                    0x7b6ee9541a2741a9f2631fbed9ffc532746be2de 
To:                      Contract 0x01bf16cfca22a5ceda1291dd9512a1bf2a1a44a9 
Value:                   0 Ether ($0.00)
Transaction Fee:         0.00087906 Ether ($0.00)
Gas Price:               0.00000002 Ether (20 Gwei)
Gas Limit:               500,000
Gas Used by Transaction: 26,853 (5.37%)
Nonce Position:          2
Input Data:              0x3104562b0000000000000000000000000000000000000000000000000000000000000063


It should be noted that the estimated value 26853 is the exact number actually used by the blockchain for this case.




References:

 


 
Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.