<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: M. Cetin</title>
    <description>The latest articles on DEV Community by M. Cetin (@thisiscetin).</description>
    <link>https://dev.to/thisiscetin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F557629%2F610e6ea7-5999-4dd8-a825-ac33d6ac958d.jpeg</url>
      <title>DEV Community: M. Cetin</title>
      <link>https://dev.to/thisiscetin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thisiscetin"/>
    <language>en</language>
    <item>
      <title>Interacting with a smart contract - .send or .call?</title>
      <dc:creator>M. Cetin</dc:creator>
      <pubDate>Tue, 10 May 2022 18:39:35 +0000</pubDate>
      <link>https://dev.to/thisiscetin/interacting-with-a-smart-contract-send-or-call-3l3n</link>
      <guid>https://dev.to/thisiscetin/interacting-with-a-smart-contract-send-or-call-3l3n</guid>
      <description>&lt;p&gt;I like learning things when building. So, tutorials, blog posts and then books are my go-to tools when picking up a new paradigm or tool. With the same mindset, after some tinkering with DApps, I realized an important detail that might be helpful to any beginner. &lt;/p&gt;

&lt;p&gt;When building your first web3 application by following up on tutorials, you may not notice the difference between &lt;code&gt;send&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; methods immediately.&lt;/p&gt;

&lt;p&gt;Let me try to explain the difference below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Call&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Contract methods that do not modify the state of the contract and have no logic other than returning a value can be invoked with &lt;code&gt;call&lt;/code&gt; methods. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;they don't cost any gas or transaction fees - it is free to call them&lt;/li&gt;
&lt;li&gt;used for view and pure functions (read-only), so it's faster to execute&lt;/li&gt;
&lt;li&gt;no need to sign them with MetaMask or with another provider&lt;/li&gt;
&lt;li&gt;safer to use them because they don't forward gas and publish anything&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;an example &lt;a href="https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#getbalance"&gt;call to contract&lt;/a&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1")
.then(console.log);
&amp;gt; "1000000000000"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Send&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Contract methods that modify the state of the contract uses &lt;code&gt;send&lt;/code&gt; method to add this modification to the blockchain.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;they cost gas and transaction fees - it is not free to invoke those functions&lt;/li&gt;
&lt;li&gt;it needs a &lt;code&gt;from&lt;/code&gt; as the sender address&lt;/li&gt;
&lt;li&gt;it needs to be signed through MetaMask or with another provider&lt;/li&gt;
&lt;li&gt;it is published in the form of transaction and can publicly be seen on the blockchain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;an example &lt;a href="https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#sendtransaction"&gt;send to contract&lt;/a&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web3.eth.sendTransaction({
    from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
    to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
    value: '1000000000000000'
})
.on('transactionHash', function(hash){
    ...
})
.on('receipt', function(receipt){
    ...
})
.on('confirmation', function(confirmationNumber, receipt){ ... })
.on('error', console.error);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice, &lt;code&gt;getBalance&lt;/code&gt; will return a &lt;strong&gt;value&lt;/strong&gt; while &lt;code&gt;sendTransaction&lt;/code&gt; method returns a &lt;strong&gt;callback&lt;/strong&gt; which is expected to include a 32 bytes transaction hash. &lt;/p&gt;

&lt;p&gt;This is an important detail to keep in mind when designing our interactions with the contract and user.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>web3</category>
      <category>solidity</category>
      <category>dapp</category>
      <category>blockchain</category>
    </item>
  </channel>
</rss>
