<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by yoisha on Medium]]></title>
        <description><![CDATA[Stories by yoisha on Medium]]></description>
        <link>https://medium.com/@yoisha?source=rss-4f79fd300cc8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*sc1iF3zM8vYVed7bQ4vhxA.png</url>
            <title>Stories by yoisha on Medium</title>
            <link>https://medium.com/@yoisha?source=rss-4f79fd300cc8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 04:36:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yoisha/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[CosmWasm Smart Contract Storage Key Permutation (And Its Application)]]></title>
            <link>https://medium.com/scb10x/cosmwasm-smart-contract-storage-key-permutation-and-its-application-8a2c2593af0d?source=rss-4f79fd300cc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a2c2593af0d</guid>
            <category><![CDATA[cosmwasm]]></category>
            <category><![CDATA[rustlang]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[terra]]></category>
            <dc:creator><![CDATA[yoisha]]></dc:creator>
            <pubDate>Fri, 20 May 2022 05:21:35 GMT</pubDate>
            <atom:updated>2022-05-20T05:25:30.505Z</atom:updated>
            <content:encoded><![CDATA[<p>Normally the key of a map might be a simple address or id, but that doesn’t mean it was limited to those simple thing. By understanding how smart contract’s storage operates, and using key permutation, we can achieve more complex composite key that can be used to index our data in more enhanced way, and make our CosmWasm smart contract experience better.</p><h3>Key-Value Storage</h3><p>First, we need to understand how the underlying IAVL+ key-value storage operates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NiBuPt48bPz8zE4PH3NFrg.png" /><figcaption><a href="https://docs.cosmwasm.com/tutorials/storage/key-value-store">https://docs.cosmwasm.com/tutorials/storage/key-value-store</a></figcaption></figure><p>The storage of CosmWasm smart contract is structured with tree modeling. IAVL+ is a kind of self-balancing binary search tree that is used here and have been modified from original AVL tree to be specially used in smart contract storage.</p><p>Imagine each circle is a key, corresponding with a value that is tied to that key. The iteration process can be done through key prefixes. For example, the key JPL has J JP JPL as its prefixes. So looking back range(JP) will contain JPL .</p><p>The underlying process of these prefixes is each key has an underlying fixed-length bytes representation. If we let one key length equal to 2 bytes, then one key can contain the value from 0000 to FFFF . For JPL , it will be something like 004A,0050,004C (Comma here is for ease in reading, all bytes are also padded to the left), so we can iterate through range(JP) that will go through 0004,A005,0000 to 004A,0050,FFFF , result in getting JP JPL JPLN JPV JPVS JPVSQ JPVSQ JPVSU JPVX .</p><p>As you can see, the iteration is pretty simple (and also limiting). Thus there are many cases where these key iteration is not enough for indexing and do not resonate with state design.</p><h3>Key Permutation And Application</h3><p>Take a look at the struct below. Let&#39;s say each treasure has a unique id and one address can be the owner of multiple treasures.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528255%252C255%252C255%252C0%2529%26t%3Done-dark%26wt%3Dbw%26l%3Drust%26width%3D680%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D22px%26ph%3D84px%26ln%3Dfalse%26fl%3D1%26fm%3DFira%2BCode%26fs%3D14.5px%26lh%3D143%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dpub%252520struct%252520Treasure%252520%25257B%25250A%252520%252520id%25253A%252520u64%25252C%25250A%252520%252520owner%25253A%252520Addr%25252C%25250A%252520%252520ty%25253A%252520TreasureTy%25252C%25250A%25257D%25250A%25250A%252523%25255Brepr%2528u8%2529%25255D%25250Apub%252520enum%252520TreasureTy%252520%25257B%25250A%252520%252520Pearl%25252C%25250A%252520%252520Coin%25252C%25250A%252520%252520BankNotes%25252C%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528255%25252C255%25252C255%25252C0%252529%26t%3Done-dark%26wt%3Dbw%26l%3Drust%26width%3D680%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D22px%26ph%3D84px%26ln%3Dfalse%26fl%3D1%26fm%3DFira%2BCode%26fs%3D14.5px%26lh%3D143%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dpub%25252520struct%25252520Treasure%25252520%2525257B%2525250A%25252520%25252520id%2525253A%25252520u64%2525252C%2525250A%25252520%25252520owner%2525253A%25252520Addr%2525252C%2525250A%25252520%25252520ty%2525253A%25252520TreasureTy%2525252C%2525250A%2525257D%2525250A%2525250A%25252523%2525255Brepr%252528u8%252529%2525255D%2525250Apub%25252520enum%25252520TreasureTy%25252520%2525257B%2525250A%25252520%25252520Pearl%2525252C%2525250A%25252520%25252520Coin%2525252C%2525250A%25252520%25252520BankNotes%2525252C%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d845a83886feba1504e5e686f6d2c3d/href">https://medium.com/media/9d845a83886feba1504e5e686f6d2c3d/href</a></iframe><p>And if you want to index the treasure by owner and type, you would need 2 distinct maps. One for the owner and one for the type, probably like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tf8sffM3dy48_x8xHCuWCg.png" /><figcaption>Mapping</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kJ6R0VCTeIcbEa2b-ECldA.png" /><figcaption>Usage</figcaption></figure><p>You can’t do much about this if you want to iterate through all treasures with owner or treasure type as prefixes, but there is a catch.</p><h3>Composite Key</h3><p>Using the key pattern above, you can add treasure type to the owner’s composite key to make it indexable by treasure type too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ho3lz6OklIfvh9zj8jYJLg.png" /></figure><p>So the key bytes representation of this map here is like below. (Not representing the real pattern used, just for illustration)<strong> The namespace can/will be neglected from now on.</strong></p><pre>Namespace | Address          TreasureTy(u8)   Id(u64)<br>v         | v                v                v<br>t_o       | ################ ################ ################</pre><h3>Namespace</h3><p>The namespace is the internal top key identifier that will separate your multiple composite key map from each other. The namespace specified <strong>can’t</strong> be accessed directly through prefix or range . (Although low-level implementation is possible)</p><h3>Range Iteration</h3><p>There are three options to iterate through the range here.</p><h4>Prefix</h4><p>One is using prefix , prefix will freeze the n — 1 slot of n length composite key and iterate using the last slot as a bound.</p><pre>Total composite key length = 3</pre><pre>Address          TreasureTy(u8)   Id(u64)<br>(&lt;- Prefix                    -&gt;) (&lt;- Bound    -&gt;)<br>v                v                v<br>################ ################ ################</pre><p>prefix in this case, will be (Addr, TreasureTy) and the bound of that prefix is u64 .</p><h4>Sub Prefix</h4><p>Another method is using sub_prefix , sub_prefix will freeze n — 2 slot of n key. But for normal composite keys that have 2 keys or a single key, this will be a unit type () . For example, Map&lt;Addr, Treasure&gt; and Map&lt;(Addr, u64), Treasure)&gt; will both have a unit() as the sub_prefix .</p><pre>Total composite key length = 3</pre><pre>Address          TreasureTy(u8)   Id(u64)<br>(&lt;- Prefix   -&gt;) (&lt;- Bound                     -&gt;)<br>v                v                v<br>################ ################ ################</pre><p>sub_prefix in this case, will be Addr and the bound is (TreasureTy, u64) .</p><h4>Namespace</h4><p>The last option is to create another map accessor to access only the namespace. To put it simply, iterate only through the topnamespace range. Thus return all the values in that namespace.</p><pre>Total composite key length = 0<br>Total key&#39;s bytes representation length = (Addr,u8,u64).joined_key().len()</pre><pre>Namespace | X                X                X<br>v         | v                v                v<br>t_o       | 0000000000000000 0000000000000000 0000000000000000</pre><h4><strong>Custom Prefix And Sub Prefix</strong></h4><p>We can also specify how the struct/enum operates as a storage key too. By implementing PrimaryKey and Prefixes trait, we can fully customize how struct/enum serialize to key bytes representation. And specify how prefix and sub_prefix access the range too.</p><h3>Usage</h3><p>Using sub_prefix method on treasure_owner to set the address bytes to a specific address, here we will supply a_addr .</p><pre>Address          TreasureTy(u8)   Id(u64)<br>v                v                v<br>0000615F61646472 ################ ################</pre><p>Then we can use range method chain on Prefix (Helper struct that is the result of prefix and sub_prefix) on accessing the iterator. Note that the bound we used here must be joined key bytes of TreasureTy and u64 as said before.</p><p>If we supply None to both bound conditions, it will iterate through all treasures of this address. Equivalent to (u8::min_value(), u64::min_value()) to (u8::max_value(), u64::max_value()) .</p><pre>Address          TreasureTy(u8)   Id(u64)<br>v                v                v<br>0000615F61646472 0000000000000000 0000000000000000</pre><pre>Address          TreasureTy(u8)   Id(u64)<br>v                v                v<br>0000615F61646472 00000000000000FF FFFFFFFFFFFFFFFF</pre><p>If we want to use our own bound, it will have to be in the joined key bytes format of (TreasureTy, u64) .</p><p>Suppose that we want to go through this prefix from TreasureTy::Pearl at specific id 3840 to the end of TreasureTy::Coin . A similar representation will be (0,3840) to (1,u64::max_value()) , and the bytes representation will be as below.</p><pre>Address          TreasureTy(u8)   Id(u64)<br>v                v                v<br>0000615F61646472 0000000000000000 0000000000000F00</pre><pre>Address          TreasureTy(u8)   Id(u64)<br>v                v                v<br>0000615F61646472 0000000000000001 FFFFFFFFFFFFFFFF</pre><blockquote><strong>Remarks</strong>: if you use this in query function, as the bound that need to be (TreasureTy,u64) your query start_after pagination parameter might need to look like start_after: (TreasureTy, u64) , or start_after: Treasure , or something like that.</blockquote><p>Using prefix method on treasure_owner to set the address and treasure type to specific bytes, here we will supply (a_addr, TreasureTy::BankNote) .</p><pre>Address          TreasureTy(u8)   Id(u64)<br>v                v                v<br>0000615F61646472 0000000000000002 ################</pre><p>Then we can use range method chain on Prefix like normally we do with a composite key with a two key element. (such as initial treasure_owner )</p><h3>Conclusion</h3><p>By using the more complex composite key, and use prefix and sub_prefix that follows the key bytes representation, we can bring out more useful iterator that can be used to access our data in specific way in addition to original way of indexing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a2c2593af0d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scb10x/cosmwasm-smart-contract-storage-key-permutation-and-its-application-8a2c2593af0d">CosmWasm Smart Contract Storage Key Permutation (And Its Application)</a> was originally published in <a href="https://medium.com/scb10x">SCB 10X OFFICIAL PAGE</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Keeping multiple maps in sync in query and index in CosmWasm smart contracts with `IndexedMap`]]></title>
            <link>https://medium.com/scb10x/keeping-multiple-maps-in-sync-to-query-and-index-in-cosmwasm-smart-contract-with-indexedmap-b258590906d0?source=rss-4f79fd300cc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/b258590906d0</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[terra]]></category>
            <category><![CDATA[cosmwasm]]></category>
            <category><![CDATA[defi]]></category>
            <category><![CDATA[rust]]></category>
            <dc:creator><![CDATA[yoisha]]></dc:creator>
            <pubDate>Thu, 27 Jan 2022 08:05:41 GMT</pubDate>
            <atom:updated>2022-03-31T02:40:53.143Z</atom:updated>
            <content:encoded><![CDATA[<p>If you work with Terra or other CosmWasm blockchain, you are probably familiar with cw-storage-plus package. It contains a lot of useful storage such as Map and Item . But have you encountered these problems?</p><h3>Example 1</h3><p>You create a Treasure. Let’s say your treasure looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/306/1*snT6WdAJFhuoPxZ3etAbVQ.png" /></figure><p>You have Map&lt;&amp;[u8], Treasure&gt; to store your treasure using treasure_id as the key. You now can search for your treasure using treasure_id. BUT you also want to search and index your treasure with owner and treasury, how can you do that?</p><p>Of course, by using IndexedMap!</p><p>IndexedMap will simply keep your multiple maps in-sync. That means, instead of writing these and manually adding/removing each map:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/1*f0Jq-ge3l6YVcfblDk9Ebg.png" /></figure><p>You can simply use IndexedMap and it will do all the work for you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*HjbrDzfekJ0TNHX3jgHLCg.png" /></figure><p>Let’s go through this structure together. There are 2 main sections in this piece of code. Defining Sub Index, and Defining Primary Index.</p><h4>Defining Sub Index</h4><p>First, you start with defining all of your sub-index (the additional index you want). In this case, it will be owner and treasury.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*mUVPhUyWQXmAp4svAnCaAw.png" /></figure><ul><li>Since one address can own multiple treasures, here we will use MultiIndex. An additional Vec&lt;u8&gt; composite key is for automatic MultiIndex indexing by pk_len .</li><li>And since a treasury address can only belong to a single treasure, we will use UniqueIndex.</li></ul><p>Then you need to implement the IndexList trait to iterate through all of your indexes (when save or remove). This is simply putting all index references into heap alloc (Line 3 and 4).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*t1StiNazZqj3nBnWw0rMfw.png" /></figure><h4>Defining Primary Index</h4><p>After that, you can create your function to access the primary index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/1*ZlkNfNAAsiU_U_HqYgvfkQ.png" /></figure><ul><li>First, you define the key of the main map in IndexMap . Here will be U64Key since we will be using treasure_id as the key.</li><li>Then you initialize the namespace of all indexes, and the key(s) to access them.</li><li>For treasury , we will use treasury field in Treasury as the key. And since it is UniqueIndex, it will store them in a separate namespace rather than primary namespace.</li><li>For owner, we will use owner field as the key. And since it is MultiIndex, it needs a primary namespace for the process sub-key deserialization and an index namespace for storing key length for each sub-key.</li><li>Then you provide the primary namespace for the actual primary index.</li></ul><h4>Usage</h4><p>You can access it as normal Map by using idx property on your function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*-ys1J03n3zaA12Yxsmi4ew.png" /></figure><p>This enables you to query all treasures for a specific owner as such.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*FWbBaeCNKkNjLmsgICdUTA.png" /></figure><p>Or query all unique owners of all treasures.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*3HvFsUDAiFg1iXsvWPAytg.png" /></figure><h3>Example 2</h3><p>For a treasure above, you define who can access the treasure with TreasureAccessInfo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*d29CIoi5Eb0UqULOzofUmA.png" /></figure><p>You want to make it so that you can query from treasure_id to know what address has access to the treasure, and you can also query from address to know what treasure_id you have access to.</p><p>So you generally want two composite key map with each one’s key being reverse of the other in order to access both pk ranges.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*FJMSC6AH8vMgoupP4wJIbQ.png" /></figure><p>Another useful time of IndexedMap!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MBeZw-eFAjO6-Fo5EpXsbQ.png" /></figure><p>This time we use the composite key for both primary index key and sub-index key.</p><p>The sad thing is that we can’t use references as keys to the sub-index due to lifetime elision 😭 , Hopes that the next version of storage-plus fix this.</p><h4>Usage</h4><p>Access all of TreasureAccessInfo by address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*jBdpFwxPt6rbu-q3RgFVXQ.png" /></figure><p>Access all of TreasureAccessInfo by treasure_id.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*TspUOuDnbmROGGqHZ815cA.png" /></figure><h3>Conclusion</h3><p>The IndexedMap is a very cool feature that enables you to keep multiple maps in-sync without writing functions to keep that in check by yourself. The code might be hard to understand at first. But the concept is really easy and straightforward.</p><p>I hope that the ability to make a constant constructor will come out soon to reduce gas usage even more. But regardless, it is a very promising smart contract standard.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b258590906d0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scb10x/keeping-multiple-maps-in-sync-to-query-and-index-in-cosmwasm-smart-contract-with-indexedmap-b258590906d0">Keeping multiple maps in sync in query and index in CosmWasm smart contracts with `IndexedMap`</a> was originally published in <a href="https://medium.com/scb10x">SCB 10X OFFICIAL PAGE</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Modern Web Dapp with Flutter]]></title>
            <link>https://medium.com/coinmonks/building-modern-web-dapp-with-flutter-91656c71d8b?source=rss-4f79fd300cc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/91656c71d8b</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[dapps]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[yoisha]]></dc:creator>
            <pubDate>Wed, 16 Jun 2021 14:12:29 GMT</pubDate>
            <atom:updated>2021-07-28T17:51:38.033Z</atom:updated>
            <content:encoded><![CDATA[<h4>No more React! No more JS!(jk) We are now in the Flutter era!</h4><h3>Introduction</h3><p>Today I will show you how to build a modern web Dapp with Flutter!</p><p>We will use the <a href="https://github.com/y-pakorn/flutter_web3">flutter_web3</a> package for interacting with the Ethereum object, connecting with Metamask, Wallet Connect, and other providers.</p><p>This package also includes other nice utilities to connect to providers like Ethers Js and Wallet Connect.</p><p>Talk no more, let&#39;s get started!</p><h3>Setting Up</h3><p>First, create a Flutter project.</p><pre>flutter create coolassname</pre><p>And add some package into pubspec.yaml. This time I’ll be using <a href="https://pub.dev/packages/get">Getx</a> for simple state management, <a href="https://pub.dev/packages/niku">Niku</a> for cool property builder, and our package <a href="https://github.com/y-pakorn/flutter_web3">flutter_web3</a>.</p><pre>dependencies:<br>  flutter:<br>    sdk: flutter</pre><pre>  flutter_web3:<br>  niku:<br>  get:</pre><p>And start Flutter with</p><pre>flutter run -d web-server</pre><p>Because extensions like Metamask can’t be used in Chrome debug browser (sad).</p><blockquote>Keep in mind that the Flutter web server doesn’t have the hot restart feature that automatically refreshes your page, after refreshing in CLI you need to manually refresh your browser again.</blockquote><h3><strong>Building The Website</strong></h3><h4>Planning The Features</h4><p>The topic we will cover today is</p><ul><li>Using the Ethereum object and its method like requestAccount.</li><li>Using Ethers js to interact with smart contracts.</li><li>Using default ContractERC20 to construct the contract that has ERC20 standard.</li></ul><p>Note that these are not all the package features; make sure to check the documentation out for all APIs and features.</p><p>So we’ll try to make Pancake Swap Interactor then, for the sake of simplicity.</p><h4>Connect To Account</h4><p>We first define the basic structure of the website. Then we’ll work on the web3 part after that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*eX-Bz7UbuHGD7rlefIOwYQ.png" /></figure><p>Define some extensions to make life easier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*92skH1gCqqjJgHNHZoKvxg.png" /></figure><p>For the Web3 part, We’ll put it in HomeController .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*adbODraBtizsOfb5gysSIA.png" /></figure><p>For explanation, let’s go line by line.</p><ul><li>Line 2, Check if we can access the Ethereum object getter. From the implementation below, the Etherum object can be null if the browser doesn’t have any provider available. i.e. Metamask, Wallet Connect, Binance Chain Wallet, etc.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0mDdCxP2K2yzGaFyDPM25g.png" /></figure><ul><li>Line 4, Check for the chain that is currently connected to is Binance Smart Chain or not (chain id 56).</li><li>Line 6, Overall check for connection to the website. ethereum.isConnected() is only used for provider availability, so we have to check from the address that is exposed from the node.</li><li>Line 9 and 11 is to store the state variable.</li><li>Line 15, Connect to provider and update currentAddress and currentChain . By using ethereum.requestAccount we can prompt the provider to make the connection with the website. (i.e. Metamask modal) that returns the list of addresses, the first address in that list is the current address that will be used. Be sure to handle errors if the user rejects the modal (In this case I don’t, which I shouldn’t do.) And using ethereum.getChainId to update currentChain</li><li>Line 26, Handle accountChanged and chainChanged events. This will call clear every time the user changed his account or chain and update the variable above.</li></ul><p>Then we can build a super simple UI to show that we’re connected or not. If not, click a connect button to prompt the provider to connect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*cEJ0J0eC6vF4cKNNqG6dMg.png" /></figure><p>So we got the account connection part completed. I will only show crude UI implementation here, you guys can make it your own as good as you want.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/1*6-gO6EPIWsi8-oFXf5QQQg.png" /><figcaption>Connect Button</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P_YvcMGvaEayUdww6yddsQ.png" /><figcaption>Connecting to the provider</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jGoH-K06RxdwXzC8ty5R0w.png" /><figcaption>Connected</figcaption></figure><h4>Connect To Ethers js Provider</h4><p>There are many providers in Ethers js, two of them are Web3Provider and JsonRPCProvider.</p><p>Both can be used to read data from the Blockchain, Web3Provider instantiates by putting Ethereum provider into the constructor. JsonRPCProvider instantiates by putting RPC Url into the constructor.</p><p>For simplicity, We can connect and get Web3 by using the default provider getter. The implementation is below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*rur744Wa3m7ihtib7wJ7DA.png" /></figure><h4>Interacting With Contract</h4><p>We’ll divide this into two sections, interacting with Cake token contract and interacting with the Masterchef contract.</p><p>For the Cake token contract, we’ll use ContractERC20 .</p><p>For the Masterchef contract, we’ll use normal Ethers Contract and specify our own ABI.</p><h4>Cake Token Contract</h4><p>It’s super simple! Just instantiate ContractERC20 class with the token address.</p><p>We’ll also add an end function that burns 1 gwei of your Cake! Spicy!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*iAUC4FYGfmLmmPBn8biy-A.png" /></figure><p>We’ll go line by line here again.</p><ul><li>Line 3, 5 stores our variable about the Cake token and our token balance.</li><li>Line 7, Instantiate cakeToken object if null. And get our Cake balance from balanceOf function.</li><li>Line 15, Burn 1 gwei of Cake from your pocket. First, check if your balance is more than 1 gwei (So you have enough to burn and have some left, haha!) then transfer your cake into addressZero , or 0x0. Then update your cake balance again.</li></ul><p>To build the UI for our contract, we first need to make sure that the Ethereum object is not null and then build it normally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*otc-eRY2kdsiEuzNXeonSA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*PY3pe6nznnUKdW-thoXZnw.png" /><figcaption>Fetch cake and burn cake button</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e77J5UIVtV8ymyBvZhmmsg.png" /><figcaption>After fetching Cake</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MQpf_xmzTDCTrqM9lB6krw.png" /><figcaption>Burning 1 gwei of Cake</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gEAHNA_bbCh-pMdzMHDZDQ.png" /><figcaption>Burned</figcaption></figure><h4><strong>Masterchef</strong> <strong>Contract</strong></h4><p>Since we need to create our own Contract instance, we’ll also need the contract’s abi. We can look this up at block explorer of your choice.</p><p>We can look into the code of the contract using <a href="https://bscscan.com/address/0x73feaa1ee314f8c655e354234017be2193c9e24e#code">BscScan</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*NbL7_HORR61uDA7Qfig8fA.png" /></figure><p>We will select some property, constant function, and write function to be our example.</p><p>First, we need to obtain the abi. For abi specification and type, please refer to <a href="https://docs.ethers.io/v5/api/utils/abi/formats/">Ethers js documentation</a>.</p><p>We’ll use minimal human-readable abi here for simplicity, You can also use JSON abi that is generated from solidity and paste it here.</p><p>So referring to the docs, our abi is.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*eWOvT3k2dUmHqRmuNbGQtw.png" /></figure><p>Then we can create our Contract and call that function. Note that all functions that has uint return type in solidity is actually BigInt in Dart.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*OsoUeDKeF_RP7vLBV4jUfg.png" /></figure><p>For explanation,</p><ul><li>Line 16, Create Masterchef contract if null, and get cakePerBlock and poolLength with annotated type BigInt .</li><li>Line 24, Call emergencyWithdraw with 0 indicating pool number as argument.</li></ul><p>After that, we proceed to build our UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*muejP7qBltnc1Gy-nUnVgA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cu_eH-qoEniSspRxzs2lDw.png" /><figcaption>Fetch information button</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QQ-3yWLM8OSInSM0L3sSdg.png" /><figcaption>After fetching information</figcaption></figure><h3>Conclusion</h3><p>Although Flutter ecosystem for developing Dapp is nowhere to be seen before. But through Dart2js and many Flutter packages that is popping up to strengthen cryptography and web3 utility, this make Flutter a Strong contender for Dapp development in the Future.</p><p>The crude example that I’ve shown already proves that Flutter really has the potential to develop complex Dapp using Dart’s extensive capability.</p><p>You can find the source code for this example <a href="https://gist.github.com/y-pakorn/76e0defd1f3cd7861c64c813760ef5d1">here</a>.</p><blockquote>Join <a href="https://t.me/coincodecap">Coinmonks Telegram Channel</a> and learn about crypto trading and investing</blockquote><h4>Also, Read</h4><ul><li><a href="https://medium.com/coinmonks/youhodler-vs-coinloan-vs-hodlnaut-b1050acde55a">YouHodler vs CoinLoan vs Hodlnaut</a></li><li><a href="https://blog.coincodecap.com/pionex-dca-bot">Dollar-Cost Averaging Trading Bot</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91656c71d8b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/building-modern-web-dapp-with-flutter-91656c71d8b">Building Modern Web Dapp with Flutter</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Algorithmic Stablecoin อนาคตที่สดใส (หรือรุ่งริ่ง) ของ Decentralize Finance]]></title>
            <link>https://yoisha.medium.com/algorithmic-stablecoin-%E0%B8%AD%E0%B8%99%E0%B8%B2%E0%B8%84%E0%B8%95%E0%B8%97%E0%B8%B5%E0%B9%88%E0%B8%AA%E0%B8%94%E0%B9%83%E0%B8%AA-%E0%B8%AB%E0%B8%A3%E0%B8%B7%E0%B8%AD%E0%B8%A3%E0%B8%B8%E0%B9%88%E0%B8%87%E0%B8%A3%E0%B8%B4%E0%B9%88%E0%B8%87-%E0%B8%82%E0%B8%AD%E0%B8%87-decentralize-finance-c14bd0e1aea8?source=rss-4f79fd300cc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/c14bd0e1aea8</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[defi]]></category>
            <category><![CDATA[decentralization]]></category>
            <category><![CDATA[binance-smart-chain]]></category>
            <category><![CDATA[stable-coin]]></category>
            <dc:creator><![CDATA[yoisha]]></dc:creator>
            <pubDate>Sun, 21 Mar 2021 14:53:17 GMT</pubDate>
            <atom:updated>2021-03-21T15:06:57.159Z</atom:updated>
            <content:encoded><![CDATA[<p>ด้วยการที่ Stablecoin มี trends ที่พุ่งสูงขึ้น<strong>อย่างมาก</strong>ในปีที่ผ่านมา ทั้งการที่ USDT มี Growth rate ที่มากกว่า BTC และการที่มีการใช้งาน Stablecoin ในการป้องกัน Inflation ของ fiat currency, นั่นทำให้มี Stablecoin อื่นๆเกิดขึ้นมาเพิ่มเช่นกัน โดยวันนี้เราจะมาวิเคราห์กันถึง รูปแบบของ Algo-stablecoin ที่เปลี่ยนไปจาก Asset-backed stablecoin และการทำงานของมัน รวมถึง <strong>ทำไม</strong> มันถึงไปไม่รอดในบางครั้ง รวมถึงเหตุผลที่ Algo-Stables ควรจะพัฒนาต่อไปจากปัจจุบัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QmKVIBPhebnsci9Cstk6uQ.png" /><figcaption>Algo-Stablecoin Best Bet?</figcaption></figure><h3>What is Algorithmic Stablecoin</h3><p>อะไรคือ Algorithmic Stablecoin — พูดง่ายๆ คือ Stablecoin ที่จะถูกควบคุมราคาด้วย Algorithm และ Smart contract function <strong>ล้วนๆ </strong>ถ้าให้อธิบายเพิ่มเติม คือ</p><p>Algorithmic Stablecoin — หรือ Seignorage supply หรือ Non-Collateralized Stablecoin เป็น Stablecoin ที่จะถูกควบคุมราคาด้วย Oracle ใน Smart contract, หลักการทำงานของมันคล้ายกับ Share-bond หรือตราสารในระดับองค์กร ซึ่งมี Algorithm ในการ Stabilize ราคา Token ผ่านการ Expansion และ Contraction, โดยที่มีองค์ประกอบของ Liquidity pool เพิ่มเข้ามา</p><p>Algo-Stablecoin Provider ที่กำลังมาแรงก็จะมี (ใน BSC)</p><ul><li><a href="https://soups.finance/">https://soups.finance/</a></li><li><a href="https://wantanmee.finance/">https://wantanmee.finance/</a></li><li><a href="https://bdollar.fi/">https://bdollar.fi/</a></li><li><a href="https://monsterslayer.finance/">https://monsterslayer.finance/</a></li><li><a href="https://boltdollar.finance/">https://boltdollar.finance/</a></li><li><a href="https://app.frax.finance/">https://app.frax.finance/</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0RgK199uM23KXiDFQbdTYQ.png" /><figcaption>BDO — Algo-stablecoin Provider</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uOpgtFat0j_NQ22oGJj1fw.png" /><figcaption>BOLTDollar — Algo-stablecoin Provider</figcaption></figure><h3>The “Algorithm” Thing</h3><p>ในที่นี้ขอยกตัวอย่าง Algo-stablecoin provider ชื่อดังอย่าง bDollar ซึ่ง bDollar จะถูก pegged ไว้ที่ 1$ ซึ่งมี 1 Epoch เท่ากับ 6 ชั่วโมง คือ Protocol ของ bDollar จะทำการ stabilize ทุกๆ 6 ชั่วโมง</p><p>ซึ่งการทำงานของ Expansion/Contraction จะเป็นการเพิ่ม/ลด Supply ของ Token โดยที่ <strong>Non Dillutive </strong>—<strong> </strong>แปลว่า Token Holder <strong>จะมี % Supply เท่าเดิม</strong>เมื่อเกิดการเปลี่ยนแปลง</p><h4>Expansion</h4><p>เมื่อราคาของ BDO เพิ่มขึ้นเกิน Equilibrium threshold (5% ของ 1$) เรียกว่าการ Expansion</p><p>สมมติว่าราคาของ BDO คือ 2$ หมายความว่า Oracle จะ Expand supply ของ BDO ให้มากขึ้นเพื่อรองรับกับราคาที่เพิ่มมากขึ้นโดยมี Expansion cap ตาม Total BDO Supply ซึ่ง Oracle จะคำนวณ Seinorage จากราคาเฉลี่ยของ 2 Liquidity Pool (BDO/BNB, BDO/BUSD) ผ่าน TWAP (Time Weight Average Price) ซึ่งป้องกันการ Dump/Pump นาทีสุดท้ายก่อน Epoch end</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wKR0XRc0J0AydymscbbdHQ.png" /><figcaption>BDO Expansion rate</figcaption></figure><p>หลังจากได้ Seinorage ออกมาแล้ว Token ที่ mint ออกมาใหม่ ส่วนหนึ่งจะไปเป็น DAO Fund — หรือ Public Treasury ซึ่งเมื่อเกิด Expansion, Token จะถูก swap เป็น BNB และ BUSD เก็บไว้ใน Treasury เพื่อ Buyback เมื่อเกิด Contraction ต่อไป</p><p>อีกส่วนจะถูกส่งไปที่ Boardroom ซึ่งจะถูกแจกจ่ายให้กับ bDollar Share Holder เพื่อที่จะให้คนเอาไป Swap กลับเป็น BNB/BUSD เพื่อลดราคาของ Token ให้กลับไปเป็น 1$ และมอบ Incentive เพิ่มเติมแก่ผู้ถือ share</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uDh5rTiygolh1tZyKzbsbA.png" /><figcaption>BDO Boardroom</figcaption></figure><p>และส่วนสุดท้ายจะถูกเก็บไว้ให้ Redeem Bond เมื่อเวลามีคนที่ถือ Bond อยู่ และราคาของ Token เกิน Pegged price, 1$ ซึ่งจะอธิบายในส่วนถัดไป</p><h4>Contraction</h4><p>เมื่อราคาของ BDO ลดลงเกิน Equilibrium threshold (5% ของ 1$) เรียกว่าการ Contraction</p><p>สมมติว่าราคาของ BDO คือ 0.8$ หมายความว่า Oracle จะ Contract Supply ของ BDO — ลด Supply ของ BDO ลงเพื่อรองรับกับราคาที่ลดลง</p><p>หลักการลด Supply คือการเปลี่ยน BDO เป็น Bonds ซึ่งไม่ว่าราคา TWAP ของ BDO เป็นเท่าไหร่ 1 BDO จะแลกได้ 1 BDO Bond เสมอ เพื่อเป็น Incentive ให้กับผู้ที่แลก Bond ซึ่ง Bond สามารถเอาไปแลกกลับเป็น BDO ได้เมื่อราคาของ BDO เกิน 1$ ส่วน BDO ที่แลกเป็น Bond ก็จะถูก Burn ทิ้งไป</p><p>ซึ่ง Bond จะแตกต่างกับการ Long BDO ปกติตรงที่ Bond Holder จะได้ Bonus Redemption Rate เพิ่มขึ้นจากปกติ เช่น</p><p>เมื่อ $BDO = 1.15, redeem 1 bBDO จะได้ 1.15 $BDO (ราคา bBDO = 1.32)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wMNhtLsDb100PW2YN136HQ.png" /><figcaption>BDO Bonds</figcaption></figure><p>หลังจาก Oracle คำนวณ Seinorage แล้ว Token ที่ mint ออกมาใหม่ ส่วนหนึ่งจะถูกแบ่งออกเป็น DAO Fund ซึ่งจะใช้ BNB/BUSD ที่ถูกเก็บไว้เมื่อตอน Expansion นำมา Buyback BDO เพื่อให้ราคากลับไปใกล้เคียง 1$ และเก็บ Token ที่เหลือเอาไว้เพื่อนำไปขายตอน Expansion ต่อไป</p><h4>Liquidity Pool is also the key</h4><p>นอกจากการ Stabilize ผ่าน Expansion/Contraction แล้ว นอกจากนี้ยังมีสิ่งที่เรียกว่า Bank อีกด้วย — Bank ในที่นี้หมายถึง Liquidity Pool</p><p>จากที่รู้ว่า Oracle จะใช้ราคา BDO/BNB และ BDO/BUSD ในการคำนวณ Seinorage ดังนั้นก็จะต้องมีการให้ความสำคัญแก่ Liquidity Pool นี้เช่นกัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wEZpWnO4d0scsOVqeGEpXA.png" /><figcaption>BDO Bank</figcaption></figure><p>ซึ่งในแต่ละ Protocol ก็จะมีวิธีการ Stabilize ราคา LP<strong> ที่แตกต่างกัน </strong>แต่หลักสำคัญคือการที่ต้อง Balance ราคาของ BDO และ BDO Share ให้เป็นสัดส่วนเช่นเดียวกับใน Process ของ Expansion/Contraction</p><p>ในส่วนนี้ก็จะมีทั้งการ Mint sBDO (BDO Share) และ BDO ซึ่งไม่ผ่าน Epoch (ได้เป็น Token/Block) ซึ่งมี Max Cap ที่ Mint ได้ ซึ่งทำให้ราคาคงที่เสมอ ไม่ต้องรอจบ Epoch</p><h3>Is Algorithmic Stablecoin Better?</h3><p>คำถามนี้ตอบได้ยาก เพราะแต่ละอย่างมีข้อดีข้อเสียที่แตกต่างกันไป</p><p>ถ้ามองในมุมของความซับซ้อน, Algo-stablecoin มีความซับซ้อนมากกว่า Asset-backed stablecoin อยู่มากโข เป็นการยากที่มือใหม่จะเข้ามาร่วมลงทุน</p><p>แต่ถ้ามองในมุมของ ความ “Decentralized” และ Autonomous แล้ว Algo-stablecoin ถือว่าตอบโจทย์ทุกอย่างในส่วนนี้</p><h3>“Game Theory” Applied Here</h3><p>ถ้ามองจากมุมของ Game Theory เราสามารถแบ่งประเภทของการกระทำได้เป็น 4 ประเภทในแต่ละ Epoch Cycle</p><h4>Contraction</h4><ol><li>Full Cooperation — การร่วมมือกันย่อมเป็นหนทางที่ดีที่สุดที่จะทำให้ราคา Token กลับมาเป็น 1$ และทำให้ Supply ลดลง ทุกคนจะได้ Incentive จากการแลก Bonds ที่มากขึ้น 😆</li><li>Majority Cooperates, Minority Defects — การ Contraction มีอันตรายที่แฝงอยู่ค่อนข้างมาก หากมีคนที่ Defects มากพอ ย่อมทำให้ราคาของ Token ไม่กลับมาเป็น 1$ ถึงราคาจะไม่ตกลงไปมากกว่านี้ ทำให้เกิดความไม่เชื่อมั่นในตัว Token มากขึ้น และทำให้เกิด Defects มากขึ้นในที่สุด แต่ถ้าจำนวน Defects ไม่มากพอ คนที่ Defects จะเสีย loss จากการ Early Sell แต่ก็สามารถทำกำไรจาก Epoch Cycle ได้ 😂</li><li>Full Defection — หากทุกคน Defects ระบบ Epoch จะพังแบบสมบูรณ์ แต่ก็สามารถเกิด Recovery จาก Pump และ Bank ในระยะยาวได้หาก Dev กระตือรือร้นที่จะหา Burn Solution และไม่ Incompetent</li><li>Majority Defects, Minority Cooperates — คำนิยามของ“ลุกช้าจ่ายรอบวง” เช่นเดียวกับกรณี Full Defection, จะสามารถเกิดการ Recovery ได้ แต่ว่าคนที่ Cooperates จะเสียโอกาสมากที่สุด และจะไม่เหลืออะไรเลย</li></ol><h4>Expansion</h4><ol><li>Full Cooperation — จะเกิดขึ้นเมื่อทุกคน“ไม่ขาย” Ex. ทุกคน Compound, ราคา Token จะไม่กลับไปที่ Pegged Price, APR จะเพิ่มสูงขึ้นจากการที่เกิด Expansion มากขึ้น กรณีนี้ดีหากเกิดขึ้นในระยะสั้น แต่จะเกิดเป็น Instability ในระยะยาว ราคาของ Token จะไม่สะท้อนถึงราคาจริง และจะเกิดการ Dump ในที่สุดเมื่อไม่สามารถ Maintain Liquidity Pool Ratio ได้ในราคาหนึ่ง และคนไม่เพิ่ม Liquidity ใน Pool เนื่องจากราคาที่มากเกินไป ทำให้สุดท้ายเกิดการ Breakdown ทั้งราคา Token และราคา Share</li><li>Majority Cooperates, Minority Defects — หากเกิดมวลการ Defect ขึ้นแต่ปริมาณไม่มาก ทำให้ราคาไม่พุ่งสูงจนเกินไป เกิดเป็นการ Stabilize อยู่ที่ค่าใดค่าหนึ่งแทน ขึ้นอยู่กับจำนวน Defects, เช่น 1.2$,1.3$ คนใน Boardroom ยังสามารถทำกำไรจาก Expansion ที่สูง และคนที่ Stake ใน LP Pools ยังสามารถทำกำไรจากราคา Token ที่ไม่มากเกินไปได้ ในกรณีนี้ราคา Share จะพุ่งขึ้นสูง</li><li>Full Defection — แน่นอนหากทุกคน Defects ราคาก็จะกลับไป Sub-one เช่นเดิมในเวลาที่รวดเร็ว จะเกิดขึ้นแน่นอน ขึ้นอยู่กับเวลา (ยังไงก็ต้องมีทั้ง Expansion และ Contraction) และภาพรวมของ Token ต่อไปจะขึ้นอยู่กับตอน Contraction แทน</li><li>Majority Defects, Minority Cooperates — เช่นเดียวกับข้อ 2. แต่จะเกิด Contraction เร็วขึ้น และราคา Share จะไม่สูงเท่า</li></ol><p>จะเห็นได้ว่า Best Scenario ในที่นี้จะเป็นเมื่อ Contraction, ทุกคน Cooperates และเมื่อ Expansion, Majority Cooperates และ Minority Defects</p><p>แต่ในชีวิตจริงแล้ว คนเราทั้งไม่มีเหตุผลในการกระทำและควบคุมไม่ได้ การเพิ่มขึ้นและลดลงของราคา Token ล้วนเกิดขึ้นจาก Demand/Supply จริงของคน หากคนไม่เชื่อมั่นในตัว Token, อะไรก็ไม่สามารถฉุดมันขึ้นมาได้</p><p>ระบบนี้ไม่ได้ต้องการผู้เล่นหน้าใหม่เยอะๆเหมือนกับ Yield farming, แต่ต้องการ Cooperation ระหว่าง Token Holders มากกว่า But who knows?</p><h3>Why (Some) Algo-Stables Doesn’t Work</h3><p>จากการที่เราดูตัวอย่างระบบของ Algo-stablecoin สามารถมีจุดยืนในโลก Defi กันไปแล้ว อยากให้ทุกคนรำลึกไว้ว่า มันก็มี Token ที่ไปไม่รอดเช่นเดียวกัน จึงเลยขอสรุปสาเหตุที่สำคัญไว้คร่าวๆ</p><ul><li>ระบบ Seigniorage พึ่งพาความเชื่อมั่น — ความร่วมมือกันของคนมากเกินไป. Algo-stables ต้องความความไว้ใจ,ความเชื่อมั่น,และความร่วมมือกันของคนทุกคนบน Protocols — การที่จะหาความไว้ในในชีวิตจริงว่ายากแล้ว บนโลก Defi ย่อมยากกว่านับ 100 เท่า ทุกคนสามารถปิดตัวตนได้ ทุกคนมาเพื่อหาผลกำไรให้ตัวเองเหมือนกัน ระบบดังกล่าวที่เล่นกับความเชื่อใจความไว้ใจในตัวคนย่อมมีสิทธิที่จะพังลงทุกเมื่อ</li><li>การทำงาน Algo-Stablecoins ยากเกินไปที่จะเข้าใจสำหรับคนทั่วไป รวมถึงยากเกินไปที่จะอ่าน Smart Contract สำหรับ Solidity Newbies — Algo-Stablecoins ไม่ใช่แค่ระบบการเงิน Fintech ง่ายๆ แต่เป็นระบบที่ต้องใช้ความรู้ทางด้าน Financial Engineering และ Advance Mathematic Models รวมถึงการทำงานเบื้องลึกของ Price Oracle ทำให้นักลงทุนที่ไม่มีความรู้ด้าน Financial และ Crypto มากพออาจตกเป็นเหยื่อการ Pump and Dump จาก APR ที่ดูสูง รวมถึง Smart Contract ที่ซับซ้อนกว่า Yield Farming หรือ AMM ต่างๆ ทำให้เกิดความเข้าใจการทำงานได้ยาก และที่สำคัญอาจมี Malicious code เหลืออยู่หากไม่ผ่าน Audit หรือ Proofread จากคนที่มีความรู้ใน Smart Contract มากพอ</li><li>การมี Epoch และ Timelock (a.k.a Deposit/Withdrawal delay) ทำให้ราคาไม่ Fluid — ระบบ Seigniorage ควรจะเป็นระบบที่ Fluid ซึ่งไม่ควรทำให้เกิด Price Spike มากเกินไป เมื่อไหร่ที่นักลงทุนได้ผลตอบแทนจาก DAO ก็ควรจะเกิด Process ของ Stabilization ขึ้นเลย — ไม่ควรเอาเงินของนักลงทุนที่ไม่อยากอยู่มากองไว้ ก็จะไม่เกิด Big Dump และทำให้ไม่เกิด Panic Sell และถ้าหาก Fluid มากพอก็ไม่จำเป็นต้องมี Epoch</li><li>ระบบ Bond เล่นกับความไว้ใจมากเกินไปและจัดว่าเป็น Weak Force ในการดันราคา Token ขึ้น — Developer ควรจะพัฒนาระบบที่ใช้เป็น Strong Force เมื่อเกิด Contraction, การใช้ Bond อย่างเดียวมีความเสี่ยงที่จะไม่เกิดการ Cooperation ระหว่างผู้ลงทุนทำให้ระบบอาจพังในที่สุด</li><li>Developer ไม่ฟังเสียง Community — สิ่งนี้เป็นสิ่งที่สำคัญที่สุดของการ Gain Trust, หากไม่ฟังเสียงคนใช้ ก็ไม่มีวันที่จะประสบความสำเร็จได้</li></ul><h3>Conclusion</h3><p>สุดท้ายนี้ Algo-stables มีจุดสำคัญอยู่อย่างหนึ่ง คือ “มันไม่ขึ้นกับ Economic Principles” จากการที่ราคาของสิ่งของใดต้องขึ้นกับ Demand/Supply ของคนซื้อคนขาย แต่กลับเปลี่ยนตาม Stabilization Algorithm แทน</p><p>ปัญหาจะเกิดขึ้นเมื่อมีคนไม่ต้องการที่จะร่วมมือกับคนหมู่มากเมื่อเกิด Contraction Epoch, จะไม่มีปัญหาหากทุกคนทำสิ่งที่ถูกต้อง — นั่นคือการซื้อ Bonds, แต่ถ้าเมื่อไหร่มีบางคนคิดจะแตกแยก เมื่อนั้นจะเกิดหายนะกับทั้งระบบ</p><p>That’s all for now. Remember to <strong>ape in responsibly</strong> and your funds will be <strong>SAFU</strong>, stay safe my fellow degens.</p><p>Feedback is always appreciated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c14bd0e1aea8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Defi Scams —Yield Farming: วิธีการเอา Liquidity Pool Token ออกจากเว็บที่บินหรือโดน Rug Pull…]]></title>
            <link>https://yoisha.medium.com/defi-scams-yield-farming-%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%80%E0%B8%AD%E0%B8%B2-liquidity-pool-token-%E0%B8%AD%E0%B8%AD%E0%B8%81%E0%B8%88%E0%B8%B2%E0%B8%81%E0%B9%80%E0%B8%A7%E0%B9%87%E0%B8%9A%E0%B8%97%E0%B8%B5%E0%B9%88%E0%B8%9A%E0%B8%B4%E0%B8%99%E0%B8%AB%E0%B8%A3%E0%B8%B7%E0%B8%AD%E0%B9%82%E0%B8%94%E0%B8%99-rug-pull-1898a75a9ecd?source=rss-4f79fd300cc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/1898a75a9ecd</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[yield-farming]]></category>
            <category><![CDATA[thai]]></category>
            <category><![CDATA[unstake]]></category>
            <category><![CDATA[defi]]></category>
            <dc:creator><![CDATA[yoisha]]></dc:creator>
            <pubDate>Sun, 28 Feb 2021 19:42:27 GMT</pubDate>
            <atom:updated>2021-03-01T04:28:14.835Z</atom:updated>
            <content:encoded><![CDATA[<h3>Defi Scams —Yield Farming: วิธีการเอา LP Token ออก พร้อมทั้ง Remove Liquidity จากเว็บที่บินหรือโดน Rug Pull (2/2021)</h3><p><em>Disclaimer: This article only aims to provide basic knowledge to the smart contract fraudulent schemes and prevent scams to grow and hurt the Defi community. This is </em><strong><em>NOT </em></strong><em>financial advice.</em></p><p><em>This guide wrote especially for Specific yield farming in Binance smart chain, </em><strong><em>Other protocols may differ.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wHxULnmDkvY2s4kluijYFg.jpeg" /><figcaption>How to Unstake &amp; Remove Liquidity</figcaption></figure><h3>Introduction</h3><p>ในปี 2021 เรียกว่าเป็นช่วงเวลาที่ฮอทฮิตที่สุดของ Decentralize Finance หรือโดยย่อ “Defi” มี Dapp (Decentralize Application) ผุดขึ้นมาเป็นดอกเห็ด คอยให้บริการเหล่านักซิ่งไม่ว่าทั้งเร็วหรือช้า ไม่ว่าจะเป็น DEX (Decentralize Exchange), Yield Farming, Liquidity Mining, Collateral Loans, Stable coins, และเทคโนโลยีที่ถูกสร้างขึ้นมาโดย Blockchain Smart Contract อื่นๆอีกมากมาย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*042ZkZXf0izcUXueuf70rQ.png" /><figcaption>PancakeSwap — Yield farming</figcaption></figure><p>แต่ใช่ว่าทุกผู้ให้บริการ Smart Contract จะหวังดีต่อผู้ใช้เสมอไป บางเจ้ามีประสงค์ร้ายและคอยกัดกินผู้ใช้งาน Defi ทั้งทางตรงและทางอ้อม คอยสรรหาวิธีการ Scam ผู้ใช้โดยวิธีต่างๆ ไม่ว่าจะเป็นช่องโหว่ของระบบ Smart Contract, หรือแม้แต่ User Error.</p><p>ทั้งนี้ ถึงแม้ว่า Smart Contract จะสามารถตรวจสอบได้ 100% แต่ไม่ได้หมายความว่าทุกคนจะมีความสามารถในการมองเห็นช่องโหว่ที่ถูกสร้าง(หรือไม่ได้จงใจสร้าง) ในนั้น ผู้ใช้งานสมควรจะรู้ถึงหลักการทำงานต่างๆและอ่าน Source code ของ Smart contract ในเบื้องต้นได้ เพื่อป้องกันตัวเองจากภัยอันตรายของโลก Defi ที่พร้อมจะกลืนกินผู้ใช้หน้าใหม่ที่ไม่ระวังตัวและตกเป็นเหยื่อในที่สุด</p><h3>Rug Pull Execution</h3><p>ตามรายงานและข้อมูลที่เก็บได้แล้ว แต่ละเดือนมี % ของการ Rug pull เพิ่มขึ้นมากเรื่อยๆ และเพิ่มขึ้นสูงมากกว่าช่วงเวลาก่อนๆเป็นพิเศษในเดือนนี้(Feb 2021) ด้วยเหตุผลของจำนวนผู้ใช้งานที่เพิ่มมากขึ้น และโอกาสในการทำกำไรของ Scammer เอง ด้วยเหตุนี้ผู้ใช้ถึงต้องระวังตัวเป็นพิเศษ</p><p>จะไม่ขอลงรายละเอียดวิธีการและประเภทของ Rug pull ในส่วนนี้ แต่พูดง่ายๆ, Rug pull, ก็คือเว็บบิน — โดนโกงเงินนั่นแหละ เราจะไม่สามารถถอน LP Token โดยผ่านทางเว็บได้อีกต่อไป (เพราะเว็บมันปิด) จึงต้องทำการถอนด้วยตัวเองผ่าน Smart Contract Function Execution ผ่านหน้าเว็บ Bscscan</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MkOhIFs_E-4JTZil3BZWUQ.png" /><figcaption>Bscscan.com write contract</figcaption></figure><h3>Verify Your Token</h3><p>เมื่อเรา Stake LP Token ของเราลงไปในเว็บ นั่นหมายความว่า LP Token ที่อยู่ในกระเป๋าเงินของเรา ถูก”ส่ง”ไปให้กระเป๋าเงินของ Smart Contract — แปลว่าเรา ”ไม่มี Token จริงๆอยู่ในกระเป๋าเงินของเราอีกต่อไป” แต่มี Allowance หรือ <strong>การยินยอมให้ใช้จ่าย Token ใน Smart Contract </strong>ตามจำนวน Token ที่เราส่ง (Stake)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yAAGILWg-0-JQ_NgJmM8uA.jpeg" /><figcaption>การทำงานของ Staking</figcaption></figure><p>ดังนั้นตัว Contract ก็จะสามารถแยกแยะได้ว่า Token ที่อยู่ใน Contract นั้น<strong>เป็นของใครบ้าง จำนวนเท่าไหร่บ้าง</strong></p><p>ก่อนที่เราจะเอา LP Token ออกได้นั้น เราต้องมาดูก่อนว่า Token ของเรา “อยู่ใน Pool ไหน” — เช่น BNB/BUSD, BNB/EGG, BUSD/EGG เป็นต้น</p><h3>วิธีเช็คเบื้องต้น</h3><p>เริ่มต้น เข้าไปที่ Contract ของ <strong>MasterChef </strong>หรือ Main Staking Contract หลักที่เป็นตัวจัดการ Token, Mint เหรียญ, ปล่อย Farm, จัดการ Assets ต่างๆตามหลักแล้วจะใช้ชื่อ Masterxxx, xxxChef, ฯลฯ และกดที Tab <strong>Contract</strong> และเลือก <strong>Read Contract</strong></p><p>วิธีหา <strong>MasterChef Contract</strong> ง่ายๆคือ เข้าไปที่ Transaction ที่เรา Stake LP Token ของเรา จะเห็นได้ชัดว่าเรา Interact กับ Staking Contractให้กดเข้าไปใน Contract นั้น</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IbpAJP5CbsmxUt3S4XAXnQ.png" /><figcaption>Staking Transaction</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c1fo5iweuQLq-DgvzXuEKA.png" /><figcaption>Staking Transaction Details</figcaption></figure><p>จากนั้นหา Function ที่ชื่อว่า poolInfo , Function นี้จะเป็นตัวบอกตำแหน่งของ Pool ของเรา เอาไว้ใช้เป็นข้อมูลในการทำอย่างอื่นต่อไป จากนั้นใส่ตัวเลขไปเริ่มจาก 0 (0 เป็นอันแรก) แล้วคลิกใน lpToken address ดูว่า pool นี้เป็นของ LP อะไร ถ้าคลิกเข้าไปแล้วจะเจอหน้า LP Token Contract ให้สุ่มเลขไปเรื่อยๆจนกว่าจะกดเข้าไปแล้วเจอหน้า LP Token ของเรา แล้ว<strong>จำเลข</strong> pool แล้วไปใช้ใน Function ถัดไป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*sEtHF9UZMFM9NMTG9PLKiA.png" /><figcaption>poolInfo</figcaption></figure><p>จากนั้นหา Function ที่ชื่อว่า userInfo (ชื่ออาจแตกต่างกันไปในแต่ละ Contract) ช่องแรกให้ใส่ poolIdที่เราได้จาก Function ด้านบน ช่องที่สองให้ใส่ addressของกระเป๋าเรา, Function นี้จะทำให้เราสามารถดูได้ว่าเรา Stake ใน pool นั้นและมี LP อยู่ใน pool เท่าไหร่ในช่อง amount และดู Token ที่เราสามารถ Harvest ได้ที่ rewardDebt</p><p>ซึ่งในตัวเลขพวกนี้จะ<strong>มีหน่วยเป็น Wei</strong> หรือ 10¹⁸, เช่นมันขึ้นเป็นเลข 46304767801968835341 เราก็เอามาหาร 18 จะได้ 46.3 กว่าๆ นั่นหมายความว่าเรา Stake LP Token ของเราใน pool 0 อยู่ 46.3 LP Token นั่นเอง</p><p>ต่อไปให้<strong>จำเลข</strong> amount นี้ไว้ แล้วกลับไปที่ poolInfo และกดเข้า address ของ lpToken มันจะเด้งไปหน้า LP Token Contract</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*sfMJ3NPGH4wDyBWuw-al3w.png" /><figcaption>userInfo</figcaption></figure><p>พอเข้าหน้า LP Token Contract มาแล้วเราสามารถดูธุรกรรมได้ว่าใครทำอะไรกับ LP Token นี้บ้าง รวมถึงสามารถกดดูจำนวนและมูลค่าของ Token ต้น (เช่น BNB หรือ USD) ที่อยู่ใน Contract นี้ได้ด้วย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S-K8KFlBq0KCipXFbiYMNA.png" /><figcaption>LP Token Contract</figcaption></figure><p>จากนั้นให้มองตรงฝั่งขวา จะมีที่เขียนว่า Token Tracker ให้กดเข้าไป จะเจอหน้าของ Token นั้นๆ แล้วให้กดตรง Holder, Tab ที่ 2 นับจาก Transfer</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c1l7iqwJnodJcuEKIZR41w.png" /><figcaption>LP Token page</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4ELddQqS9fl7kQ7C7P2-Nw.png" /><figcaption>Lp Token page, Holders</figcaption></figure><p>ให้กดตรงแว่นขยายขวาบน จะเป็นการ Search Token Holder จาก address ซึ่งตรงนี้ให้คุณใส่ address ของ <strong>MasterChef </strong>เข้าไป เพื่อเป็นการ Recheck ว่า <strong>MasterChef</strong> มี LP Token ของคุณอยู่ <strong>จ ริ ง ๆ</strong> หรือเปล่า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dqF5f5BDAliIu2GT5jSUhg.png" /><figcaption>Lp Token page, Token holder balances</figcaption></figure><p>ถ้าเกิดว่า Balance ของ Contract มีมากกว่า amount ที่คุณจดมาตอนแรก <strong>ยินดีด้วย 🎆 เงินของคุณยังอยู่ดี </strong>(แม้มูลค่าของ LP จะลดลงเนื่องจาก Impermanent Loss จากการโดน Dump แต่ก็ยังได้เหรียญต้นกลับมา)</p><p>แต่ถ้า Balance มีน้อยกว่า amount หรือ Balance เป็น 0</p><h4>ยินดีด้วย คุณโดน รั บ น้ อ ง แบบ ส ม บู ร ณ์ แ บ บ</h4><p>คือถ้า Balance เป็น 0 นั่นหมายความว่ามีคนอื่นหรือเจ้าของ Contract สามารถเข้าถึง LP Token ของคุณได้ บอกได้สองอย่าง 1. คุณโดน Backdoor ใน Contract Code 2. คุณไม่ควรเชื่อใจ Smart Contract ใดๆบนโลกอีก</p><p>หากยังเหลือ Balance แต่ว่า น้อยกว่า amount ของคุณ ให้จดค่า Balance นั้นไว้ อย่าลืมนับจุด Decimal ให้ครบ 18 ตัว ขั้นตอนต่อไปเราจะไป <strong>Unstake จาก Contract แล้วเอา LP Token กลับมากัน</strong></p><h3>Unstake</h3><p>ก่อนอื่นให้กลับไปที่ <strong>MasterChef </strong>Contract และเลือก <strong>Write Contract</strong> แทน</p><p>ถ้า Balance เหลือ<strong>มากกว่าหรือเท่ากับ</strong> amount ให้มองหา Function ที่ชื่อว่า emergencyWithdraw</p><p>ก่อนที่จะกดใช้ เรามาดู Code ก่อนว่ามันทำงานยังไง, emergencyWithdraw รับค่า _pid (Pool Id) และเรียกดู amount ของเราใน poolId จากนั้นใช้ safeTransfer เข้าสู่ address ของเรา, และ set rewardDebt หรือ Token ที่ยังไม่ถูก Harvest เป็น 0, แต่ใครจะสนใจกันล่ะตอนนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*tDKiJmvMCwlH-2lbmPQ7fA.png" /><figcaption>emergencyWithdraw Source Code</figcaption></figure><p>safeTransfer เป็น Standard ของ OpenZeplin ที่หลักการทำงานของมันคือ ถ้า amount มากกว่า balance ในตัว, Transaction จะถูก <strong>revert </strong>นั่นแปลว่าถ้าเรามี amount ที่เรา Stake ไปเกิน balance ของ Contract <strong>เราจะไม่สามารถถอนได้</strong></p><p>ถ้าไม่มีอะไรแล้วก็ใส่ _pid เป็น Pool Id ของ LP เราแล้วกด Write หากไม่มีอะไรผิดพลาด LP Token จะกลับมาใน Wallet ของเรา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*JBN-eTDiNOPMiZphNWCasg.png" /><figcaption>emergencyWithdraw</figcaption></figure><p>ถ้า Balance เหลือ<strong>น้อยกว่า</strong> amount ให้มองหา Function ที่ชื่อว่า withdraw</p><p>Source code ของ withdraw ก็จะทำงานคล้ายๆ emergencyWithdraw แต่จะโอน Token ที่ยังไม่ถูก Harvest เข้ากระเป๋าของเราด้วย ทำให้ใช้ Gas มากกว่า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*DHhnu8lbeTsmbKuBAQwTig.png" /><figcaption>withdraw Source Code</figcaption></figure><p>ถ้าพร้อมแล้วก็ใส่ _pid เป็น Pool Id ของ LP เรากับ amount ที่เราจดมาจาก BSC Scan ( amount ต้องมากกว่า 0 ) ให้ลบจุด Decimal ออกด้วย เช่น 44.5128 Token ให้ x 10¹⁸ เข้าไป ก็จะกลายเป็น 44512800000000000000 เสร็จแล้วกด Write</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*Ce1_INeyqnoj2dBk1GuBLg.png" /><figcaption>withdraw</figcaption></figure><p>หากได้ LP Token ออกมาแล้วให้กลับไป <strong>Remove Liquidity</strong> ที่ <strong>PancakeSwap</strong> ถ้าเป็น LP/ใช้ AMM ของ Pancake แต่ถ้าเป็นของเจ้าอื่นหรือเป็นของเว็บที่ปิดไปแล้ว <strong>ขั้นตอนถัดไปจะมาสอน Remove Liquidity โดยใช้ BSCScan กัน</strong></p><h3>Remove Liquidity</h3><p>ก่อนอื่นเข้าไปที่หน้า LP Token Contract ของเราก่อน แล้วเข้าไป Recheck ว่ามี Token ต้นอยู่ใน LP Contract ไหม วิธีการเช็คมีอยู่ 2 วิธีให้ลองทำทั้ง 2 วิธี</p><ul><li>กดดูตรง Token Dropdown</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*V7ZBzcUhBgbKrH7yWvbhmw.png" /><figcaption>Initial Token on LP Token pages</figcaption></figure><ul><li>กด Read Contract แล้วหา Function ที่ชื่อว่า getReserves</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*QnI-Ne2-H9OAqRTBHij0jw.png" /><figcaption>getReserves function</figcaption></figure><p>ถ้าเกิดว่าอันใดอันหนึ่งมีค่าเป็น 0 ขึ้นมา นั่นยังไม่ได้แปลว่าเงินคุณหาย เช่นเดียวกับการที่คุณยังเห็นตัวเลขในนั้น ไม่ได้แปลว่าเงินคุณยังอยู่ เพราะว่าค่าของ getReserves <strong>อาจจะ</strong>ยังไม่ถูกอัพเดตโดย sync</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*LJr8YsQabJY0YAliuR2nGA.png" /><figcaption>sync function</figcaption></figure><p>อย่างไรก็ถาม ให้ลอง Remove Liquidity/Unwrap LP เลย</p><p>หากจะ Remove Liquidity ต้องไปทำที่ <strong>Router Contract</strong>, ซึ่ง remove,add liquidity เป็น periphery ของ swap อยู่ใน Contract เดียวกัน</p><p>วิธีหา <strong>Router Contract</strong> ง่ายๆคือ เข้าไปที่ Transaction ที่เราแลก Token เป็น LP Token จะเห็นได้ชัดว่าเรา swap หรือ add liquidity บน Router ให้กดเข้าไปใน Contract</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fjA1i15sc31bSHibzR55cg.png" /><figcaption>Transaction on Router Contract</figcaption></figure><p>ให้เข้าไปที่ Contract แล้วกด Write Contract จากนี้ไปจะซับซ้อนเล็กน้อย ให้หา Function ที่ชื่อว่า removeLiquidity</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*V2vwn9SXR_kUxHHezcUU4A.png" /><figcaption>removeLiquidity Function</figcaption></figure><p>จะเห็นได้ว่ามีช่องให้กรอกเยอะแยะเต็มไปหมด เราจะไปกันอย่างช้าๆในตรงนี้</p><p>ช่อง tokenA/ tokenB ให้ใส่ address Token ต้นทั้งสองอันของ LP Token เข้าไป เช่นถ้ามี LP ของ BNB/BUSD ช่องแรก Token A ให้ใส่ Contract Address ของ BNB (ซึ่งปกติแล้วถ้าใช้ BNB ในการ Stake จะถูกแปลงเป็น WBNB โดยอัตโนมัติ) คือ <a href="https://bscscan.com/token/0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c">0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c</a> และ Token B ให้ใส่ Contract Address ของ BUSD คือ <a href="https://bscscan.com/token/0xe9e7cea3dedca5984780bafc599bd69add087d56">0xe9e7cea3dedca5984780bafc599bd69add087d56</a></p><p>ช่อง liquidity ให้ใส่จำนวน LP Token ที่เรามีในหน่วย Wei (x10¹⁸)</p><p>ช่อง amountAMin/amountBMin เป็นช่องที่กำหนดว่าเราจะได้รับ Token ต้นของเราคืนมาขั้นต่ำเท่าไหร่ (ถ้าเว็บยังใช้ได้มันจะถูกเรียกว่า Slippage) ในส่วนนี้ให้ใส่ไปที่ 0 ทั้งสองช่องเพราะเราจะเอา Token เราออกมาไม่ว่าจะได้เท่าไหร่</p><p>ช่อง to ให้ใส่ Address ของกระเป๋าเรา</p><p>ช่อง deadline จะเป็นช่องที่กำหนดเวลา Transaction มากสุด ในหน่วย Unix Epoch Time หากเกินเวลานี้ Transaction จะถูก Revert, ให้ไปดูเวลาที่ <a href="https://www.epochconverter.com/">https://www.epochconverter.com/</a> แล้วปรับเวลาให้มากขึ้นสัก 1 ชม เสร็จแล้วก็ก็อป Unix Timestamp กลับมาแปะที่ช่องนี้</p><p>เสร็จแล้วให้กด Write ตามหลักการแล้วถ้ายังมี Token อยู่ใน Reserve แม้แต่ 0.0000001 มันก็จะถูกโอนมาให้เรา แต่ถ้า Transaction ถูก Revert แปลว่า</p><h3>RIP 💤</h3><p>ถ้าอยากจะ Recheck อีกรอบ ให้กด Write ที่ Function syncเพื่อให้ LP Token Contract Re-calculate getReserves ใหม่ หรือไปเช็คจาก Contract ของ Token ต้นโดยใส่ Address ของ LP Token เข้าไป</p><p>ถ้าทำทั้งสองทางแล้วยังเจอ Balance ที่เป็น 0 อยู่ก็</p><p>That’s all for now. Remember to <strong>ape in responsibly</strong> and your funds will be <strong>SAFU</strong>, stay safe my fellow degens.</p><p>Feedback is always appreciated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1898a75a9ecd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Defi Scams — Rug pull Analysis: Wallstreetswap.finance]]></title>
            <link>https://yoisha.medium.com/defi-scams-rug-pull-analysis-wallstreetswap-finance-ca657280b85a?source=rss-4f79fd300cc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca657280b85a</guid>
            <category><![CDATA[rugpull]]></category>
            <category><![CDATA[defi]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[staking]]></category>
            <category><![CDATA[scam]]></category>
            <dc:creator><![CDATA[yoisha]]></dc:creator>
            <pubDate>Fri, 26 Feb 2021 11:12:34 GMT</pubDate>
            <atom:updated>2021-02-27T08:49:15.515Z</atom:updated>
            <content:encoded><![CDATA[<h3>Defi Scams — Rug pull Analysis: Wallstreetswap.finance</h3><p><em>Disclaimer: This article only aims to provide basic knowledge to the smart contract fraudulent schemes and prevent scams to grow and hurt the Defi community. This is </em><strong><em>NOT </em></strong><em>financial advice.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hTkp4Cy46OfORJ5eRDAn5w.jpeg" /><figcaption>Wallst Rugged</figcaption></figure><h3>Addresses</h3><ul><li>Owner <a href="https://bscscan.com/address/0xa7524ec326d81dd26765f1bb17ad976d04778304">Address</a></li><li><a href="https://bscscan.com/address/0xd395ce2bbc62eeb612bd4c41d51ef4b6bf611d6c#code">WallStreetSwapFactory</a></li><li><a href="https://bscscan.com/address/0x2dc38868a9215a1347c6fd8480d4a127da4812eb#code">WALLSTToken</a></li><li><a href="https://bscscan.com/address/0x3d696c7ae704fccf33828b83c8c7e6b2f577796d#code">TheFarmerofWallStreet</a></li><li><a href="https://bscscan.com/address/0x5535c172d2c4ce6d4a0675f2cc55c4f010a65c1a#code">WallSteetSwapRouter</a></li><li><a href="https://bscscan.com/address/0x3c8d3677032ae2ca07fdf355fc0ec6fa7f1a4185#code">WallStreetSwapPair</a> (BNB/BUSD)</li><li><a href="https://bscscan.com/address/0x96812e7300b7ec45fe8e77df20d11aecd923fe6d#code">Timelock</a></li></ul><h3>Introduction</h3><p>Wallstreetswap.finance is another typical Defi protocol that allows you to swap, stake, and farm tokens. At first glance, nothing too out of the ordinary and looks very trustable because it has been <a href="https://github.com/TechRate/Smart-Contract-Audits/blob/6bd0802fc8f33d2a104305b8a9cc3e737860d8f0/Wall%20Street%20Swap%20Smart%20Contract%20Audit.pdf">audited</a> (but <a href="https://github.com/TechRate/Smart-Contract-Audits/commit/91f3269a3e6d3cd190e915703cecd10283798cc7#diff-d3cd0297ac4043a87b3da657beadaf2313b0a95301da5006a7c12018cb4ec579">deleted</a> after rugged), has convincing and well-written documentation, and has the most crucial thing, the timelock contract.</p><h4>The Audit problem</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gIymY59TG1_BPhfFmSKiZQ.png" /><figcaption>Some content of the audit.</figcaption></figure><p>In the audit pdf file, it states that TechRate performs an audit of “smart contracts” but only shows one file — the “Pool” contract.</p><p>But in reality, Since the main Github repo is already gone and the pdf file contains nothing about the contract information, We really can’t determine whether the auditor audited the actual backdoored contact or not.</p><h3>The Rug Pull</h3><p>At around 11:30 AM UTC, February 15, 2021, Wallstreetswap.finance suddenly close its website and began the rug pull.</p><p>When the website has been closed, I thought that it will be another developer dump. Then we’ll have to manually unstake and remove the liquidity, and get our tokens back eventually. But that&#39;s not the case for this time.</p><p>In the <a href="https://bscscan.com/address/0x3c8d3677032ae2ca07fdf355fc0ec6fa7f1a4185#code"><strong>LP token contract</strong></a> (for now on I’ll use BNB/BUSD for example), you can clearly see that the reserve, the <strong>actual</strong> balance of BNB and BUSD in the contract, is 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/992/1*ohk4i3ghtXhRRIvjg-mWfA.png" /><figcaption>getReserves function in LP contract</figcaption></figure><p>We can recheck this by going to the <a href="https://bscscan.com/address/0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"><strong>BNB contract</strong></a> and use the balanceOf function to query back the LP contract’s balance. And it resulted in 0.</p><p>** Some of the pools reserve fund is not updated because the sync function hasn&#39;t been called, So it has to be manually checked **</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*8iKwteGRM0E4S7s7DPEKkA.png" /><figcaption>Wallst LP contract address in balanceOf function of BNB contract.</figcaption></figure><p>Then in the <a href="https://bscscan.com/address/0xa7524ec326d81dd26765f1bb17ad976d04778304"><strong>Contract owner wallet</strong></a>, we’ll call him Culprit “C” from now on, we can see various transactions being executed by him, transferring the LPs token to his wallet and swapping all of it to BNB on PCS, then transfer all of the 220370 BNB (Approx 5m$) to an unknown wallet.</p><p>After that, the funds are split up and vanish into Binance hub, where we can’t track it anymore.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0x6bzirvSAjj6-se76SmXQ.png" /><figcaption>Contract owner transaction history</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vKPW7p2bwfsyOvAGqkw1Nw.png" /><figcaption>Unknown contract transferred the funds to BSC Hub</figcaption></figure><p>Now we know that our funds are actually stolen this time. It&#39;s time to trace back to the root cause.</p><h3>Tracing Back To The Root</h3><h4>Transfering token</h4><p>We’ll start our progress by answering the question “How the duck can C withdraw our token!?”.</p><p>Looking into <a href="https://bscscan.com/tx/0xb8fb142aa94092de2ccf4749a70400f6a91f8365e9ee53ed4d38512e2ec0a5a3">this</a> transaction emitted log, C called some function that emits Transfer, moving the token from src address to dst address with wad value. In this case, from 0x3c8 address, our BNB/BUSD contract, to 0xa75, C address, with the value of 1834.95 Ether. (The wad is in Wei unit)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ju1nAetAXCKsDSR5eJmSGg.png" /><figcaption>Transaction Log</figcaption></figure><p>Then we seek the function that emits Transfer in the <a href="https://bscscan.com/address/0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"><strong>BNB contract</strong></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*buN5K6N6McY95bAvfC6Nrw.png" /><figcaption>BNB contract code</figcaption></figure><p>The only function that emits TransferistransferFrom. Which is the line we can figure out what’s going on. The complicated thing is, how can he executed this function without being reverted? According to the if-statement in line 58, he <strong>NEEDS</strong> to have the allowance to transfer the token. So we know for sure he somehow got his hand on the LP contract allowance.</p><h4>Liquidity Pool Contract (LP Contract)</h4><p>Let me explain how adding liquidity normally works</p><ul><li>Execute the add liquidity function with the desired token and let the contract transfer your tokens to the contract balance</li><li>The contract then mints you that LP token pairs that can be used in staking</li><li>You lose both <strong>actual</strong> <strong>tokens</strong>, then you gain the<strong> corresponding LP token</strong> back</li></ul><p>This means both of the tokens that you use to create LP token are not yours anymore and is now owned by the contract instead. Most of the time it’ll be fine, but not for this contract.</p><p>From the clue we have, we’ll skim the LP contract for abnormality about Approval first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lPa2xVwBmc_HzF06jy-6Iw.png" /><figcaption>A part of the LP token contract</figcaption></figure><p>We found something interesting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7UkgkGJSJ-LSBm-yWBm4Gw.png" /><figcaption>constructor and initialize function</figcaption></figure><p>What the duck is that sufficient check? Why would you approve _factorywith an unlimited amount? (uint(-1) is maximum unsigned integer)</p><p>Something sketchy is going on.</p><p>The comment says //Called once by the factory at time of deployment so we look for this function that ran when you created the LP contract. The contract that deploys this is <a href="https://bscscan.com/address/0xd395ce2bbc62eeb612bd4c41d51ef4b6bf611d6c#code"><strong>WallStreetSwapFactory</strong></a>.</p><h4><strong>WallStreetSwapFactory, The Culprit</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*750R6HLHN1waoykKh4IuSQ.png" /><figcaption>WallStreetSwapFactory contract</figcaption></figure><p>In WallStreetSwapFactory contract, line 396, createPair is the function we are looking for. Now let’s take a closer look at it..</p><p>The thing looks fine until we reached line 406. The pair initialize with _factory address of feeToSetter . It means feeToSetter address will have unlimited token approval in that pair. Then what’s the feeToSetter ? We’ll check it by querying feeToSetter()</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*QriiJ6PfKWKC0S_6-DMrWg.png" /><figcaption>feeToSetter address</figcaption></figure><p>Look familiar right? That’s the C address!</p><p>This explains why he can use transferFrom function without it being reverted.</p><p>C, The contract owner, actually has access to everyone’s LP token the entire time!</p><h3>Conclusion</h3><p>The contract owner puts a backdoor code in the LP creation contract so he can have unlimited token approval to every LP pairs he created. Then he rugged, transferring every token from the LP pair contract to his own address, then bridged through Binance. This resulted in 5m$ TVL fraud.</p><p>That’s all for now. Remember to <strong>ape in responsibly</strong> and your funds will be <strong>SAFU</strong>, stay safe my fellow degens.</p><p>Feedback is always appreciated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca657280b85a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[วิธีการใช้งาน Vim ให้เป็น Flutter IDE แบบง่ายๆ]]></title>
            <link>https://yoisha.medium.com/%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%83%E0%B8%8A%E0%B9%89%E0%B8%87%E0%B8%B2%E0%B8%99-vim-%E0%B9%83%E0%B8%AB%E0%B9%89%E0%B9%80%E0%B8%9B%E0%B9%87%E0%B8%99-flutter-ide-%E0%B9%81%E0%B8%9A%E0%B8%9A%E0%B8%87%E0%B9%88%E0%B8%B2%E0%B8%A2%E0%B9%86-d85b58191f9f?source=rss-4f79fd300cc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/d85b58191f9f</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[thai]]></category>
            <category><![CDATA[vim]]></category>
            <dc:creator><![CDATA[yoisha]]></dc:creator>
            <pubDate>Wed, 21 Oct 2020 03:35:18 GMT</pubDate>
            <atom:updated>2021-02-20T09:47:28.317Z</atom:updated>
            <content:encoded><![CDATA[<p>ถ้าท่านเป็นคนที่ใช้งาน Vim เป็นและ อ ย า ก ลองใช้ Vim เป็น IDE เพื่อพัฒนา Flutter application ล่ะก็ คุณนั้นมาถูกทางแล้ว เพื่อความมันส์ในการ code และเพื่อความ lightweight ที่จะทำให้เราเปิด emulator ได้หลายจอมากขึ้น เราไปดูกันเลยว่าต้องทำอะไรบ้าง</p><h3>1. ลง Dart SDK</h3><p>ก่อนอื่นจะต้องติดตั้ง Dart ก่อน (หรือไปแก้ path ให้ชี้ไปที่ Dart ของ Flutter) เพื่อใช้งาน Syntax highlighting, Support .dart filetype และอื่นๆ</p><pre>$ brew tap dart-lang/dart // ใช้ Homebrew<br>$ brew install dart</pre><pre>---------------------------</pre><pre>$ sudo apt-get update // ใช้ apt-get<br>$ sudo apt-get install apt-transport-https</pre><pre>$ sudo sh -c &#39;wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -&#39;</pre><pre>$ sudo sh -c &#39;wget -qO- https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list &gt; /etc/apt/sources.list.d/dart_stable.list&#39;</pre><pre>$ sudo apt-get update<br>$ sudo apt-get install dart</pre><p>หลังจากนั้นให้เพิ่ม Dart ลง PATH</p><h3>2. ลง Vim Plugin</h3><h4>2.1 Dart lang support</h4><p>เพื่อจะให้ vim นั้นอ่านไฟล์ .dart ได้, มี syntax highlighting และ indentation ที่ถูกต้อง เราจะต้องลง <a href="https://github.com/dart-lang/dart-vim-plugin"><em>dart-vim-plugin</em></a> ใน vim เสียก่อน</p><p>หากเราใช้งาน Vundle หรือ Vim-Plug อยู่แล้ว วิธีการก็แค่เพิ่มลงไปใน <em>.vimrc</em> เท่านั้นเอง</p><pre>Plugin &#39;dart-lang/dart-vim-plugin&#39; //Vundle<br>Plug &#39;dart-lang/dart-vim-plugin&#39; //Vim-Plug</pre><p>หลังจากเพิ่ม <em>dart-vim-plugin </em>ลงใน vim แล้ว vim ของเราก็จะมีสีสันขึ้นมาทันตาเห็นครับผม</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*FA_ubGQz0PyeOIOcK1vsww.png" /><figcaption>Vim Syntax Highlighting</figcaption></figure><p>นอกจากนี้เรายังสามารถใช้คำสั่ง :DartFmt เพื่อ format code ของเราได้ด้วย และ <a href="https://github.com/dart-lang/dart-vim-plugin#configuration">config</a> อย่างอื่นได้จาก</p><pre>let g:dart_style_guide = 2 //Indent space<br>let g:dart_format_on_save = 1 //Format on save<br>...</pre><h4>2.2 Auto Complete, Lint, Code Actions</h4><h4>Coc and Coc-Flutter</h4><p>ในส่วนของ Intellisense engine ที่แนะนำก็จะเป็น <a href="https://github.com/neoclide/coc.nvim">Coc</a> และ <a href="https://github.com/iamcco/coc-flutter">Coc-Flutter</a> เนื่องจากสามารถลง Plugin เสริมได้เยอะและค่อนข้าง Configurable</p><p>ก่อนอื่นก็ติดตั้ง Coc ใน vim</p><pre>Plugin &#39;neoclide/coc.nvim&#39; //Vundle<br>Plug &#39;neoclide/coc.nvim&#39;, {&#39;branch&#39;: &#39;release&#39;} //Vim-Plug</pre><p>หลังจากลง Coc แล้ว เราก็ต้องติดตั้งส่วนเสริม Coc-Flutter โดยใช้คำสั่ง</p><pre>:CocInstall coc-flutter</pre><p>เท่านี้เราก็ได้ฟังก์ชั่นต่างๆที่ใช้ได้ใน VSCode อาธิเช่น Lint when type, snippets, auto complete, etc. มาใช้ใน vim แล้ว 🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7d9RILRYxC6x7hnQRhq5xg.png" /><figcaption>Coc-Flutter Auto Complete and Auto Lint</figcaption></figure><ul><li><em>แต่ว่า snippets ที่มีไม่ใช้อาจจะมีไม่ครบเท่าใน VSCode ในส่วนนี้เราต้องใช้ </em><a href="https://github.com/neoclide/coc-snippets"><em>Coc-Snippets</em></a><em> และเพิ่ม snippets ที่ขาดหายไปเอง (เช่น statefull/less builder)</em></li></ul><p>และหากต้องการใช้ Code Actions/ Refractor ก็สามารถใช้ Code Action ของ Coc ได้เลย</p><p>ก่อนอื่นต้องไป map code action key ใน config ของเราก่อน</p><pre>nmap &lt;silent&gt; &lt;leader&gt;aa &lt;Plug&gt;(coc-codeaction-selected)</pre><p>หลังจากนั้นก็กด key ที่เรา map ไว้แล้วกด enter</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*EjOf3IBGUCBGXpMHQm_62w.png" /><figcaption>Coc-Flutter Code Actions</figcaption></figure><p>เราก็จะได้ Code Actions มาใช้สมใจอยากเป็นทีเรียบร้อย</p><p>และยังมีความสามารถอื่นของ Coc อย่างเช่น code navigation ได้จาก <a href="https://github.com/neoclide/coc.nvim/blob/master/README.md"><em>อ้างอิง</em></a></p><pre>&quot; GoTo code navigation.<br>nmap &lt;silent&gt; gd &lt;Plug&gt;(coc-definition)<br>nmap &lt;silent&gt; gy &lt;Plug&gt;(coc-type-definition)<br>nmap &lt;silent&gt; gi &lt;Plug&gt;(coc-implementation)<br>nmap &lt;silent&gt; gr &lt;Plug&gt;(coc-references)</pre><p>อีกทั้ง Coc-Flutter ยัง support ในส่วนของ integrated flutter simulator เพื่อให้เราสามารถควบคุม simulator ภายใน vim ได้อย่างรวดเร็ว และรวมถึงความสามารถอื่นๆอีกมากมายของ Coc-Flutter รวมถึงความสามารถในการ configurable อย่างหลากหลาย <em>อ้างอิง </em><a href="https://github.com/iamcco/coc-flutter"><em>Coc-Flutter</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xEpVTfDropG7scWjU-wSRA.gif" /><figcaption>Coc-Flutter Hot Reload</figcaption></figure><p>นอกจากนี้ยังมี Plugin ของ vim ที่สามารถใช้งานร่วมกับ Flutter ได้อีกอย่างเช่น <a href="https://github.com/natebosch/vim-lsc-dart">vim-lsc-dart </a>และ <a href="https://github.com/thosakwe/vim-flutter">vim-flutter</a></p><h3>Conclusion</h3><p>อย่างที่ทุกท่านเห็น การติดตั้งและ config vim ให้เป็น IDE นั้นไม่ยากอย่างที่คิด อีกทั้งยังมีความยืดหยุ่นสูง และน่าจะตอบโจทย์การใช้งานสำหรับทุกคน</p><p>สุดท้ายนี้ ขอให้ทุกท่านมี ค ว า ม สุ ข กับการใช้ vim เพื่อเป็น Flutter IDE และหวังเป็นอย่างยิ่งว่าทุกคนจะหันมาใช้ vim กันมากขึ้น แล้วไว้เจอกัน Blog หน้านะครับ</p><p>Any feedback is appreciated :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d85b58191f9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>