ethers.js/docs/v4/cookbook-contracts.html

334 lines
14 KiB
HTML
Raw Normal View History

2020-06-11 23:29:05 +03:00
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contracts &mdash; ethers.js 4.0.0 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/css/ethers.css" type="text/css" />
<link rel="stylesheet" href="_static/css/ethers.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Providers" href="cookbook-providers.html" />
<link rel="prev" title="Accounts" href="cookbook-accounts.html" />
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> ethers.js
</a>
<div class="version">
4.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Developer Documentation</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="getting-started.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">Application Programming Interface (API)</a></li>
<li class="toctree-l1"><a class="reference internal" href="api-advanced.html">Low-Level API</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="cookbook.html">Cookbook</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="cookbook-accounts.html">Accounts</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Contracts</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#return-a-value-from-a-state-changing-method">Return a Value from a State-Changing Method</a></li>
<li class="toctree-l3"><a class="reference internal" href="#economic-incentives-and-economic-value">Economic Incentives and Economic Value</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="cookbook-providers.html">Providers</a></li>
<li class="toctree-l2"><a class="reference internal" href="cookbook-signing.html">Signing Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="cookbook-testing.html">Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="cookbook-react.html">React Native</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="migration.html">Migration Guides</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes.html">Notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="testing.html">Testing</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">ethers.js</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> &raquo;</li>
<li><a href="cookbook.html">Cookbook</a> &raquo;</li>
<li>Contracts</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/cookbook-contracts.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="contracts">
<h1>Contracts<a class="headerlink" href="#contracts" title="Permalink to this headline"></a></h1>
<p>Some quick examples for techniques with Contracts.</p>
<hr class="docutils" />
<div class="section" id="return-a-value-from-a-state-changing-method">
<h2>Return a Value from a State-Changing Method<a class="headerlink" href="#return-a-value-from-a-state-changing-method" title="Permalink to this headline"></a></h2>
<p>Since a state-changing (non-constant) function can take a non-trivial
amount of time to mine, the response is a transaction, and cannot
return values directly.</p>
<p>Using events, we can simulate a return value from a non-constant function.</p>
<div class="literal-block-wrapper docutils container" id="id1">
<div class="code-block-caption"><span class="caption-text"><em>Solidity</em></span><a class="headerlink" href="#id1" title="Permalink to this code"></a></div>
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="nx">contract</span> <span class="nx">Example</span> <span class="p">{</span>
<span class="nx">event</span> <span class="nx">Return</span><span class="p">(</span><span class="nx">uint256</span><span class="p">);</span>
<span class="nx">uint256</span> <span class="nx">_accum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kd">function</span> <span class="nx">increment</span><span class="p">()</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">uint256</span> <span class="nx">sum</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">_accum</span><span class="o">++</span><span class="p">;</span>
<span class="nx">Returns</span><span class="p">(</span><span class="nx">_accum</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="literal-block-wrapper docutils container" id="id2">
<div class="code-block-caption"><span class="caption-text"><em>JavaScript</em></span><a class="headerlink" href="#id2" title="Permalink to this code"></a></div>
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="kr">const</span> <span class="nx">assert</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;assert&#39;</span><span class="p">)</span>
<span class="kr">const</span> <span class="p">{</span>
<span class="nx">Contract</span><span class="p">,</span>
<span class="nx">Wallet</span><span class="p">,</span>
<span class="nx">getDefaultProvider</span>
<span class="p">}</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;ethers&#39;</span><span class="p">)</span>
<span class="kr">const</span> <span class="nx">provider</span> <span class="o">=</span> <span class="nx">getDefaultProvider</span><span class="p">(</span><span class="s1">&#39;ropsten&#39;</span><span class="p">)</span>
<span class="kr">const</span> <span class="nx">wallet</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Wallet</span><span class="p">(</span><span class="nx">privateKey</span><span class="p">,</span> <span class="nx">provider</span><span class="p">)</span>
<span class="kr">const</span> <span class="nx">abi</span> <span class="o">=</span> <span class="p">[</span>
<span class="s2">&quot;event Return(uint256)&quot;</span><span class="p">,</span>
<span class="s2">&quot;function increment() returns (uint256 sum)&quot;</span>
<span class="p">]</span>
<span class="kr">const</span> <span class="nx">contractAddress</span> <span class="o">=</span> <span class="s2">&quot;0x...&quot;</span>
<span class="kr">const</span> <span class="nx">contract</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Contract</span><span class="p">(</span><span class="nx">contractAddress</span><span class="p">,</span> <span class="nx">abi</span><span class="p">)</span>
<span class="nx">async</span> <span class="kd">function</span> <span class="nx">increment</span><span class="p">()</span> <span class="p">{</span>
<span class="c1">// Call the contract, getting back the transaction</span>
<span class="kd">let</span> <span class="nx">tx</span> <span class="o">=</span> <span class="nx">await</span> <span class="nx">contract</span><span class="p">.</span><span class="nx">increment</span><span class="p">()</span>
<span class="c1">// Wait for the transaction to have 2 confirmations.</span>
<span class="c1">// See the note below on &quot;Economic Value&quot; for considerations</span>
<span class="c1">// regarding the number of suggested confirmations</span>
<span class="kd">let</span> <span class="nx">receipt</span> <span class="o">=</span> <span class="nx">await</span> <span class="nx">tx</span><span class="p">.</span><span class="nx">wait</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="c1">// The receipt will have an &quot;events&quot; Array, which will have</span>
<span class="c1">// the emitted event from the Contract. The &quot;Return(uint256)&quot;</span>
<span class="c1">// call is the last event.</span>
<span class="kd">let</span> <span class="nx">sumEvent</span> <span class="o">=</span> <span class="nx">receipt</span><span class="p">.</span><span class="nx">events</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span>
<span class="c1">// Not necessary; these are just for the purpose of this</span>
<span class="c1">// example</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">sumEvent</span><span class="p">.</span><span class="nx">event</span><span class="p">,</span> <span class="s1">&#39;Return&#39;</span><span class="p">)</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">sumEvent</span><span class="p">.</span><span class="nx">eventSignature</span><span class="p">,</span> <span class="s1">&#39;Return(uint256)&#39;</span><span class="p">)</span>
<span class="c1">// The sum is the first (and in this case only) parameter</span>
<span class="c1">// in the &quot;Return(uint256 sum)&quot; event</span>
<span class="kd">let</span> <span class="nx">sum</span> <span class="o">=</span> <span class="nx">sumEvent</span><span class="p">.</span><span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">return</span> <span class="nx">sum</span>
<span class="p">}</span>
<span class="nx">increment</span><span class="p">.</span><span class="nx">then</span><span class="p">((</span><span class="nx">value</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
<span class="p">});</span>
</pre></div>
</div>
</div>
</div>
<hr class="docutils" />
<div class="section" id="economic-incentives-and-economic-value">
<h2>Economic Incentives and Economic Value<a class="headerlink" href="#economic-incentives-and-economic-value" title="Permalink to this headline"></a></h2>
<p>A very important aspect of Smart Contracts is consideration of the
Economic Value being protected; even a completely logically correct
Smart Contract can fall victim to misaligned economic incentives.</p>
<p>Imagine a city with an average parking ticket cost of $60, but the
parking enforcement is not effective, allowing a person to park anytime
and anywhere with only an average of 3 tickets per year. If
the average cost to pay for parking is $10, and you park 100 times per year,
the $180 in fines compared to the $1,000 in paying for parking is actually
incentivizing illegal parking and disincentivizing paying legitimate parking.</p>
<p>This is a fairly involved topic, which we will write an article for
and then link to from here, along with a related topic, “Miner
Front-Running”.</p>
<hr class="docutils" />
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="cookbook-providers.html" class="btn btn-neutral float-right" title="Providers" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="cookbook-accounts.html" class="btn btn-neutral" title="Accounts" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2016, Richard Moore &lt;me@ricmoo.com&gt;
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/language_data.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>