0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

web3.js(v1.2.1)でコントラクトを実行する

0
Posted at

記事の内容

Web3.jsを使ってコントラクトを実行する方法です。
情報としては溢れていますが、バージョンによって動きが違う(1.0.0以前だと結構変わる)ので、私が使っているv1.2.1でやった内容をメモとして残します。

コントラクトのデプロイ

web3を使ったデプロイの方法も調べましたが、うまくいかず断念
truffleを使いました。

1_DepositLock_migration.js
var Migrations = artifacts.require("DepositLock");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};
[root@localhost migrations]# truffle migrate
You can improve web3's peformance when running Node.js versions older than 10.5.0 by installing the (deprecated) scrypt package in your project

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



Starting migrations...
======================
> Network name:    'development'
> Network id:      1111
> Block gas limit: 0x17a70bc9994780


1_DepositLock_migration.js
==========================

   Replacing 'DepositLock'
   -----------------------
   > transaction hash:    0x0c2c99a824fcb9c3accb76f78acb26e35b9b36979c96ae32f311ec52c569c2d4
   > Blocks: 0            Seconds: 4
   > contract address:    0x1AB46E2b7416Cf5ff3299DD0e874Df7832Fe739A
   > block number:        189
   > block timestamp:     1582155929
   > account:             0xCd8A7f55a6E4081A4d5A5882BC093482d257528A
   > balance:             378
   > gas used:            355263
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00710526 ETH

   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00710526 ETH


Summary
=======
> Total deployments:   1
> Final cost:          0.00710526 ETH

web3jを使ったコントラクト実行

サンプルのコードです。
こんな感じでコントラクト実行できました。

exec_DepositLock_deposit.js
var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));

//コントラクトのアドレス
var address = "0x004c24b50e142D7129b5f6389B0E8F1A42148872";
var account = "0x6b34292848fa539e8ff954d66d46eef2e0c55e76";

//ABI
var abi = [{"constant":false,"inputs":[],"name":"withDraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"stage","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"creationTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}];

//コントラクト
var contract = new web3.eth.Contract(abi, address, {
        from: account,
        gasPrice: 1000000});

contract.methods.deposit().estimateGas({from: account},
        function(gasAmount){
                if(typeof(gasAmount) == "object") {
                        console.log("Can't get gasAmount.");
                        gasAmount = 2000000;
                }
                contract.methods.deposit().send({
                        from: account,
                        value: 100000000000,
                        gas: gasAmount,
                        gasPrice: 20000000
                })
                .then(function(receipt) { console.log(receipt) })
                .catch(console.log)
        })
        .catch(function(error) { console.log(error) });
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?