<?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 Vladimir Kuzmenkov on Medium]]></title>
        <description><![CDATA[Stories by Vladimir Kuzmenkov on Medium]]></description>
        <link>https://medium.com/@vovakuzmenkov?source=rss-db2a0e9d48ba------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0naM1M-8Wjg74QqVydTCnQ.png</url>
            <title>Stories by Vladimir Kuzmenkov on Medium</title>
            <link>https://medium.com/@vovakuzmenkov?source=rss-db2a0e9d48ba------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 13:05:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vovakuzmenkov/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[Codex Vibecoding Guide— build a complex Telegram Bot]]></title>
            <link>https://medium.com/@vovakuzmenkov/codex-vibecoding-guide-build-a-complex-telegram-bot-3e7638f413c1?source=rss-db2a0e9d48ba------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e7638f413c1</guid>
            <category><![CDATA[telegram-bot]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[vibe-coding]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[telegram]]></category>
            <dc:creator><![CDATA[Vladimir Kuzmenkov]]></dc:creator>
            <pubDate>Tue, 15 Sep 2026 13:37:44 GMT</pubDate>
            <atom:updated>2026-09-15T13:37:44.288Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A0Q2CuTZZA3Vclxkj3a7lQ.png" /><figcaption>Vibecoding feels like magic</figcaption></figure><p>A few days ago <a href="https://www.linkedin.com/in/mikhail-kuzmenkov/">my elder brother</a>, who is working in regenerative farming with agrorobotics and has lots of communication going on in his life, approached me and asked to build a non-trivial telegram bot assistant for him. I said “Sure!”.</p><p>I will share all the steps to build this assistant with you. This guide is written in a way, that even a person, who never programmed in their life will be able to reach the result.<br>You will see how easy it is to build a sophisticated telegram bot using Codex. Also I give not only the way to program the bot, but all the steps necessary to run it in cloud —and this part takes most effort.</p><p><a href="#eb2a">Prerequisites</a><br><a href="#4ade">Vibecoding</a><br><a href="#d039">Deployment</a><br><a href="#730d">Improving further</a><br><a href="#78b5">Thank you</a></p><h3>Prerequisites</h3><p>Alright, let’s start.<br>I assume you have Codex <a href="https://openai.com/codex/">downloaded</a> onto your machine. Run it.<br>Ensure Codex mode is chosen in the left-top corner of the application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*uZpVTraghgZI6ieZNwV5sQ.png" /><figcaption>Codex chosen</figcaption></figure><p>Choose an empty folder locally for the project by clicking a plus sign near the Projects section. Name the folder something like “<em>telegram-bot</em>”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*JF5MhLbSMlxleOfO93hcGQ.png" /><figcaption>Create new project</figcaption></figure><p>Now, choose the model complexity, presuming you have OpenAI paid subscription, a variety of models should be available. GPT-6 Astra would be an overkill, so let’s choose <strong>GPT-5.6 Sol</strong>. High effort is optimal — it is very capable and does not waste tokens too much.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*3Sq9sSMU2e6JWaOgPumj-Q.png" /><figcaption>GPT-5.6 High</figcaption></figure><p>Here is a great tip for vibecoding — <strong>Plan mode</strong>. Turn it on. Before starting the execution Codex will ask a dozen of very smart questions that did not cross your mind when you started vibecoding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*wZ-CV9y5atmuiAVIqy9lAQ.png" /><figcaption>Turn on Plan mode</figcaption></figure><h3>Vibecoding — creation of the bot</h3><p>Everything is set. We are ready to create our bot.<br>What should it be able to do?<br>Here is the prompt we will use, just stick it into codex prompt chat:</p><pre>Create a Telegram bot.<br>It should be able to:<br>- Understand both my voice and text requests.<br>- Operate using ChatGPT via the OpenAI API key.<br>- Integrate with my Gmail. When a new email arrives, the bot notifies me. I can ask to see the email, and the bot responds with a summary of its content.<br>- Allow me to dictate a reply via voice; the bot displays a polished version of the text (refined by ChatGPT) and, upon my confirmation, sends the reply via Gmail.<br>- Be added to Telegram groups. At the end of the day, the bot sends me a summary of the day&#39;s conversations from all the groups it has joined. Alternatively, upon request, it can provide a daily summary for a specific group I name.</pre><p>Bam!<br>Our <strong>vibecoding started</strong>.<br>Remember, Codex is in Plan mode, so it will first bombard us with questions, here are a few examples:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/1*PUd-rW-Nht-4aoz9-HnL_g.png" /><figcaption>Periodicity</figcaption></figure><p>As you can see, Codex provides options to choose from, which is very helpful. Here I choose the recommended option — let it poll every 1 minute.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*_MZg7ysZM_2Ivf-8YSg6Ig.png" /><figcaption>Third option to be chosen</figcaption></figure><p>Here on the contrary, I do not choose recommended option and decide that Primary emails are the only ones that I want to pay attention to.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UmaWgxann968PVFYjUPItg.png" /><figcaption>Python</figcaption></figure><p>In this example, the recommended option is the best one — let’s stick with Python.</p><p>All in all it asked me 15 good questions and <strong>built a plan</strong> by itself — this step took over <strong>20 minutes</strong>.</p><p>The last question is “<em>Do you want me to implement this plan?</em>” — You can familiarize yourself with it and then press “<em>Yes, implement the plan</em>”.</p><p>Codex starts to work, writing thousands of lines of code. Overall this step will take for about <strong>1 hour</strong>. It might sometimes prompt you for an action, such as “<em>Allow codex to run this command</em>” — so be aware of it, your presense could be required.</p><p>The result would look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/951/1*iB7E1OKqdcGHrLSIr6ma1Q.png" /><figcaption>Wow, 3800 lines of code!</figcaption></figure><p><strong>Wow, that is truly magical!</strong> It does feel very powerful.</p><p>Moreover, given nowadays’ level of AI development, in most of the cases you can rest assured — all this code is fully ready. From the first shot. Completed.</p><p><a href="https://chatgpt.com/s/cx_6aa7bbd685048191b667a5cf82b1e267">Here</a> is my history chat with codex for that project.<br>You may notice, that I had some error appeared during deployment, so I shoved that error to codex — and it fixed it in no time, providing all the instructions to implement.</p><h3>Deployment</h3><p>Upon completion Codex opens README.md file with all the deployment steps explained. For example, <a href="https://github.com/vladimirk-dev/telegram-assistant-eng/blob/main/README.md">here is readme file</a> that was created during my session.</p><p>However these steps is the <strong>hardest part</strong>, and AI can not take it upon itself yet. If you are a total novice — no worries, I’ll help you, follow along.</p><p>There is a file <em>.env.example</em> in your project folder — make a copy, and rename it into <em>.env — </em>open it via notepad and have a look.<em><br></em>We need to fill in this file with proper keys.</p><ul><li>In Telegram create a bot with @BotFather and save it’s token into .env file as TELEGRAM_BOT_TOKEN</li><li>Obtain your numeric Telegram user ID (for example via @userinfobot). This becomes TELEGRAM_OWNER_ID.</li><li>Run /setprivacy in @BotFather, select this bot, and disable privacy mode.</li><li>Ensure /setjoingroups is enabled.</li></ul><p>Add credits to your <a href="https://platform.openai.com/home">https://platform.openai.com/home</a> account and create OpenAI key — this becomes OPEN_API_KEY in .env file.</p><p>Next we need keys for our <strong>gmail</strong>. Go to <a href="https://console.cloud.google.com/">https://console.cloud.google.com/</a> and create a new project there, then in APIs &amp; Services enable Gmail API. Now configure OAuth screen — on the credentials screen ensure to download the .json file. Place this json file into your project folder, rename it and put the name into .env file — that will be our GMAIL_CLIENT_SECRET_FILE. Choose Branding tab and fill it with necessary info, next in Audience tab make the app External.<br>You might also want to fill in the value for GMAIL_OWN_ADDRESSES .env file with your gmail address.</p><p>According to <a href="https://github.com/vladimirk-dev/telegram-assistant-eng/blob/main/README.md">README.md</a> file the next step is to generate fernet cryprography key and create gmail-token.enc file. The exact instructions will be given to you in your README.md file, follow it accurately.</p><p>Alright when you’ve filled in the .env file and also placed gmail key files in proper places time to get the cloud machine to run it on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/0*szggYghQnILsi4h5.jpg" /><figcaption>Alibaba is great and Jack Ma is smiling</figcaption></figure><p><a href="https://www.alibabacloud.com/">Alibaba cloud</a> boasts impressive 3 month free trial with 4Gb RAM instance. Go get it (or rent a cheap machine from any other platform). Even 0.5 Gb instance is enough.</p><p>Overall Alibaba Cloud is very decent and the UI is visually similar to AWS.</p><p>AlibabaCloud: when creating instance, choose Ubuntu 24.04 LSTM, and ensure Assign Public IPv4 checked. Start the instance.</p><p>Set the password or a key pair of your running instance — in Console/Instances:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/0*Z1eEJZCpg8mANG1L.png" /><figcaption>Setting alibaba Ubuntu VPS password</figcaption></figure><p>Next log onto your Ubuntu VPS machine via <a href="https://winscp.net/eng/download.php">WinSCP</a>. Copy your project to it (ensure <strong>not to copy</strong> <strong>.venv</strong> folder if you created it locally). When using WinSCP it is useful to make hidden files (such as .env, .venv) visible.<br><strong><em>In WinSCP: </em></strong><em>Via Settings: Go to Options &gt; Preferences from the top menu, select Panels on the left, and check the box for Show hidden files.</em></p><p>Now we can launch the code. Use <a href="https://www.putty.org/">Putty </a>to connect to alibaba ubuntu machine. Simply paste your public ip into putty, click Open and use specified credentials (user — <em>root </em>and the password you’ve set).</p><p>Ensure to navigate to your project folder on the machine via terminal, could be something like:</p><pre>cd telegram-bot</pre><p>Then run the commands provided to you by Codex in README.md.<br><strong>Hooray! Your bot should work now!</strong> Try to talk to it.</p><p>I tried to keep my instructions as concise as possible, so whenever you’re stumbled you can always ask a plain question to your Codex chat.<br>For example, as I mentioned earlier, I had <a href="https://chatgpt.com/s/cx_6aa7bbd685048191b667a5cf82b1e267">some errors during bot launch</a>, so I gave those errors to the Codex, and it was fixed gracefully.</p><h3>Improving further</h3><p>We have a base version of our bot, now upon testing it you can change and improve it the way you want.<br>For example I’ve added transcribtion feature for long audio. Telegram has limit — only 20 Mb audio files can be transfered to it’s servers. I talked to Codex, and was advised, that one of the solutions would be to launch it’s own telegram bot API server — which can be done on the same 4GB RAM ubuntu machine. Let me share the prompt with you:</p><pre>Currently, large audio messages don&#39;t work because Telegram limits file transfers to 20 MB.<br>Fully automated processing requires a local Telegram Bot API Server.<br>If you deploy a local Telegram Bot API Server—which has no download limits—the bot can download the source file, split it into chunks of up to 50 MB using FFmpeg, transcribe each part, and merge the resulting text.<br>Let&#39;s set up such a Telegram Bot API Server.<br><br>Add a `/record` command to the bot: once triggered, the user can record an audio message, which the bot then fully transcribes into a Word document. Additionally, the bot generates a summary—a concise overview of the key points—and displays it in the chat.</pre><p>How easy it was! Codex was working for another 1.5 hour and provided clear instructions how to run the server and additionally connect the bot to it.</p><p>Now my elder brother can put the phone on the table during offline meetings, press record in telegram and get full transcription afterwards with summarization.</p><p>Additionally we decided to implement deep memory for group chats. Bot now stores all messages from the groups it was added to and my brother can ask the bot any questions regarding past conversations. The bot uses RAG mechanism for search.</p><h3>Thank you!</h3><p>That’s it! Hope the article was helpful.<br>Chime in with any comments, questions, suggestions!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e7638f413c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fullstack RAG Solution with Private LLM: A Step-by-Step Guide]]></title>
            <link>https://medium.com/@vovakuzmenkov/building-a-fullstack-rag-solution-with-private-llm-a-step-by-step-guide-48a0a4467efc?source=rss-db2a0e9d48ba------2</link>
            <guid isPermaLink="false">https://medium.com/p/48a0a4467efc</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[scraping]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[telegram-bot]]></category>
            <dc:creator><![CDATA[Vladimir Kuzmenkov]]></dc:creator>
            <pubDate>Wed, 06 Mar 2024 00:07:08 GMT</pubDate>
            <atom:updated>2026-02-14T02:44:04.114Z</atom:updated>
            <content:encoded><![CDATA[<p>End to End approach on how to build a a virtual assistant for any website. Flask webapp, Telegram bot integration and deployment into cloud. Both local LLM and a solution via API to ChatGPT.</p><p>In this article I pick random website (<a href="https://www.hsbc.co.uk/">HSBC UK Bank</a>) and build an Assistant Chatbot for them, while sharing every step with explanations and code along — so that anyone even with little background can do the same for their business.</p><p>Before start, please bear in mind, I would like to make it “quick and dirty”, yet yelding impressive result. So, the code and architecture is simplified as much as possible for vividness.</p><p>You’ll find here no water, only practical juice.</p><p><strong>Contents</strong><br>- <a href="#7376">Fire up local standalone LLM</a><br>- <a href="#0a34">Webapp with local LLM</a><br>- <a href="#3fe7">Telegram bot integration</a><br>- <a href="#daef">Virtual assistant for HSBC UK Bank</a><br>- <a href="#3d3c">Gather and prepare data</a><br>- <a href="#d858">RAG (Retrieval augmented generation)</a><br>- <a href="#9532">Host the model into cloud</a><br>- — <a href="#f1aa">Reassess approach (API)</a><br>- — <a href="#55b8">Back to deployment</a><br>- <a href="#4658">Multilingual queries</a><br>- <a href="#41ef">Langchain and Vectordb solution</a><br>- <a href="#3be4">Thanks</a></p><h3>Fire up local standalone LLM</h3><p>Large Language Models such as ChatGPT 4 are huge — trillion parameters - and require immense compute resources to run. Luckily there are plenty small solutions to download. Let’s pick a 7-billion model. <a href="https://huggingface.co/TheBloke/StableBeluga-7B-GGUF">Quantized Beluga </a>looks very good. Medium version takes only 4GB space. Yet, you would need at least 8Gb GPU in order to run it on your PC.</p><p>Alternatively, you can run solution in Google Colab, which I <a href="https://colab.research.google.com/drive/1ucMZ6qm1zkjS_WdjrcyXLyQOtk3F6O4d?usp=sharing">prepared for you</a>.</p><p>Another alternative is to run on CPU using cpu version of <a href="https://github.com/marella/ctransformers">ctransformers </a>or <a href="https://gpt4all.io/index.html">gpt4all</a> — but my experiments proved they are way too slow.</p><p>For the very first step I suggest to run jupyter notebook (best option is to install <a href="https://www.anaconda.com/">anaconda</a>, it has jupyter). Make sure to create new environment in anaconda before proceed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eef35a6c17bb9889d8f835c17e41d089/href">https://medium.com/media/eef35a6c17bb9889d8f835c17e41d089/href</a></iframe><p>What’s next? Jumping to RAG? — No!</p><p>First let’s create simple webapp with backend and frontend — a webpage with a chat window. And then also integrate with Telegram bot.</p><h3>Webapp with local LLM</h3><p>We need Flask backend, and html, css, js front end. A nice page with chat window. Damn. Creating it would be such a tedious task! — is it not?</p><p>Hop onto <a href="https://chat.openai.com/">https://chat.openai.com/</a> and ask chatgpt 3.5 to do all the work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*c0mU2in5S8hxSgObkjcw4A.png" /><figcaption>Start of the conversation with ChatGPT 3.5</figcaption></figure><p>Follow the steps gpt3.5 gives you, copy-paste and refine the code with a few more prompts, such as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/1*D2qDqqaLotwrwD4h72fDNg.png" /></figure><p>And bam! What a no-brainer. We have a decent webapp in several minutes. Here is <a href="https://chat.openai.com/share/b54a7ba0-90bc-4326-b9cb-b81ccc2fe55a">my full conversation</a>.</p><p>Follow ChatGPT instructions, run webapp chat and see it responding with ‘I recieved your message’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*rN8Dgh0k7INfvCuqsuHY9A.png" /></figure><p>Now just stick our llm model call into send_message function in app.py file to return intelligible response.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79464f551983a3587b3412b68cd20d32/href">https://medium.com/media/79464f551983a3587b3412b68cd20d32/href</a></iframe><p>What Chat gpt3.5 did not mention, is that it is important to run webapp in a separate virtual environment. So, in the folder with app.py run the following commands:</p><pre>python -m venv .venv<br>.venv/scripts/activate</pre><p>Again install flask and ctransformers pip install flask ctransformers[cuda]</p><p>Run the webapp python app.py and ask something.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*6LbFh7e0Hz0lwmXW5sZnog.png" /><figcaption>But… does this puny little llm suggests military expansion is good though?</figcaption></figure><p>Cool, we now have a working full stack webapp :D</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*l0NgtlUFCNdVXyRuacayBA.jpeg" /></figure><h3>Telegram bot integration</h3><p>Same as with flask webapp, in separate folder activate new virtual env and run installations.</p><pre>python -m venv .venv<br>.\.venv\Scripts\activate<br>pip install aiogram requests</pre><p>Put following code into file named main_telegram.py</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c6449d789b7eb8d975d8323fe0a2ac8/href">https://medium.com/media/8c6449d789b7eb8d975d8323fe0a2ac8/href</a></iframe><p>Notice url variable — it is an address to our active flask-llm webapp.</p><p>Also token variable. You would need to obtain it from telegram <a href="https://t.me/BotFather">@BotFather</a>, it creates a bot with the name of your choice and gives a token. The process is very simple. After token variable assigned, run the telegram python app: python main_telegram.py then open telegram and engage in conversation with the botname that was provided to you by BotFather:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*lG87_cf9seGt3zaWOvuBSg.png" /></figure><p>Pretty powerful. Have you already noticed, that llm understands not only the question, but also instructions such as “Write shortly/briefly”, otherwise the response is very thorough.</p><p>The next step would be moving towards RAG implementation. So, we need to choose some business website and make our llm answer questions about business based on the data from their website.</p><h3>Virtual assistant for HSBC UK Bank</h3><p>Ok, let’s choose <a href="https://www.hsbc.co.uk/">HSBC UK Bank</a> as a victim — top 1 bank in UK. They have some kind of chatbot assistant on the website, but maaan, it is awful, he does not answer anything! Here is their asnwer to a simple question: “how do i get student loan”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/1*b1qhilJcFuwrI0ZB4-smpA.png" /></figure><p>Let’s see if we can cook a better virtual assistant for them.</p><h3>Gather and prepare data</h3><p>Now, the DataScience Law dictates that ‘data’ part is the most responsible one. It consumes more than 60% of all effort, and the result massively depends on your dataset. Absolutely agree, this is the area to target in order to improve your product.</p><p>But… for vividness, let’s spend as little effort as possible on this task.</p><p>Go to <a href="https://apify.com/">https://apify.com/</a> console, they give free 5$ credits on webscraping — more than enough.</p><p>HSBC UK bank have information about their services scattered all over website. So i quickly hovered through all their menus, distinguished main sections and copied them all into the website content crawler — 10-15 urls.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/1*6BipF3nHPhtDFbknmEAC9w.png" /><figcaption>apify website content crawler</figcaption></figure><p>Also tweak some knobs here and there for better results. The idea is that the crawler will go in depth through specified urls and download all the content. Click Start and go get some tea/coffee to unwind.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*M3xEgxU1vZSinGcrw0GlJA.png" /></figure><p>50 minutes later and 1.4$ free credits, 671 pages are ready for export.</p><p>Now let’s make some basic cleaning of extracted hsbc uk texts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e77c7ca1fab5d6280961a5ec73400fc5/href">https://medium.com/media/e77c7ca1fab5d6280961a5ec73400fc5/href</a></iframe><p>Basically I superficially peek into the contents, check the distribution of article sizes, get rid of unnecessary small articles, which don’t contain any info, and also truncate overly huge ones (yes, for serious projects, the data needs to be treated in a much more careful manner). Anyways, we now know everything about the bank!</p><p>You can get resulting hsbc bank dataset at my <a href="https://colab.research.google.com/corgiredirector?site=https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F1gFVEzyTBgDmMP48SjySA3skq8nwRxZZw%2Fview%3Fusp%3Dsharing">google drive</a>.</p><h3>RAG (Retrieval augmented generation)</h3><p>What is this RAG thing? Simply saying, <em>given some question, we find several most related articles from our dataset</em>, glue them together, and shove it down to the language model, saying: Answer question: how do i get student loan, based on the given texts — &lt;texts come here&gt;. That would be a large prompt sent to the model, so she finds meaning in it, summarizes and returns a nice specific answer.</p><p>Now, as you might guess, the step of finding most related articles for the user question is absolutely crucial here! The quality is majorly depends on it.</p><p>How to do that? — Use embeddings: 1) encode question into numbers, 2) encode texts into numbers, 3) then compute similarity between those and get most similar. Embeddings assign certain numbers to “words” based on their meaning.</p><p>I tested a dozen of open source and api embeddings and will present here the choices which give amazing results.</p><p>Test retrieval quality in jupyter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f1786fc7b5f26ccef5d99c999c11b45/href">https://medium.com/media/1f1786fc7b5f26ccef5d99c999c11b45/href</a></iframe><p>Looks good, the title of the highest similarity article is ‘Borrowing money as a student’.</p><p>Additionaly polish retrieved results with reranker. It will check similarity of retrieved articles with different algorithm and sort them accordingly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8e0a36a57d47ced9815808b800358de/href">https://medium.com/media/c8e0a36a57d47ced9815808b800358de/href</a></iframe><p>Splendid. Maybe for the question “how do I get student loan” reranked difference is not obvious, but my personal experiments proved the order to become better more often than not.</p><p>Also, this whole RAG code runs strikingly fast ~1 second.</p><p>Before gluing everything together into large prompt, define a template with initial instructions:</p><pre>prompt_template = &quot;&quot;&quot;You are a professional consultant at HSBC UK Bank.<br>Use the context to answer the question at the end.<br>Give a very detailed answer.<br>Never mention about context and where information comes from.<br><br>Context:<br>{context}<br><br>Question: {question}<br><br>Answer:&quot;&quot;&quot;</pre><p>You can see the initial instructions are quite dry, because in fact small local LLM is not very good at grasping and following them all. This is the best she can understand.</p><p>Crafting full query:</p><pre>context_chpunks_as_str = &#39;\n###\n&#39;.join([str(elem) for elem in context_chunks])<br>llm_full_query = prompt_template.format(context=context_chunks_as_str, question=query)</pre><p>Before putting this query into our llm, it is important to check the size of it. Beluga has input limit of only 4096 tokens (roughly 3 words = 4 tokens).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbf3e16781d5975beaef74a11a461735/href">https://medium.com/media/cbf3e16781d5975beaef74a11a461735/href</a></iframe><p>8673 tokens — not acceptable. You need to implement logic, that ensures to craft the query within defined limits. I have it covered in my example colab notebook shared at the beginning of this article.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a17d56631b07212da24cd1d87f9ce2c/href">https://medium.com/media/8a17d56631b07212da24cd1d87f9ce2c/href</a></iframe><p>The answer is very decent for a 7 billion parameters model, is it not? Takes 3 minutes to answer though, because of such large input.</p><p>Additionally it would be great if the answer had a link added at the end for more details. Which is easily doable, up to you! (Okay, okay, I’ve included this function for you in aforementioned colab)</p><p>You would need now carefully gather all this RAG logic and stick it into our app.py send_message function.</p><p>Let’s ask our HSBC virtual assistant something else, now in webapp.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*ibP_3yVfbxAXEQKINBQWXw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*dDkWh6DVwvWzMKhZXZ814g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*O0bqBrk20O3CvXPY5qH8mA.gif" /><figcaption>She is perfection…</figcaption></figure><h3>Host the model into cloud</h3><p>It runs locally, now we need to make it available on the internet.</p><p>So, what were the requirements… around 8 Gb GPU minimum, ideally 16. Ok Amazon AWS is our first choice… <a href="https://aws.amazon.com/ec2/instance-types/g4/">g4 instances</a> should be ideal. Great, they have g4dn.xlarge instance with 16 Gb memory for only… 360 usd/month.</p><p>Let’s buy it?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*pwS0AQc_uZzLAOBEK5KeiA.gif" /><figcaption>Pay 360$?</figcaption></figure><p>Right… scouring the internet shows similar price offers. We need a different approach.</p><h3>Reassess approach</h3><p>How about replacing local llm and embeddings with calls to apis?</p><p>The benefits are: webapp becomes super lightweight, answers return within seconds, much cheaper, and the LLM is much more powerful!</p><p>In our code, change model initialization to chatgpt3.5 api call.</p><p>As for embeddings model, I found that <a href="https://cohere.com/">Cohere</a> is an excellent choice. OpenAI embeddings is a good option as well.</p><p><a href="https://github.com/quicksilver0/llm_webapp_api">Github repo</a> with the code running api calls.</p><p>Since we use gpt3.5, we can improve prompt template:</p><pre>You are a professional, polite consultant at HSBC UK Bank. But never mention your role.<br>Use below Context to answer the Question at the end. Try to use all context blocks to answer. Context blocks separated with ###<br>Try to consider all contexts if they answer the question.<br>Answer in detail.<br>Don&#39;t say where the information came from. Don&#39;t mention Context.<br>If you don&#39;t know the answer, just answer that you don&#39;t know, don&#39;t make anything up.<br>Communicate only within Context; if there is no answer to a question within the context, say that you only respond to inquiries about HSBC UK Bank and politely ask them to ask another question and make it more specific. In this situation don&#39;t mention context.<br>Answer in the same language as the Question.<br>Format your answer nicely.<br>At the end, add a link of each context used for the answer to get more details<br>Remember, always answer in the same language as the given question!<br><br>Context:<br>{context}<br><br>Question: {question}<br>Answer:</pre><p>Expand instructions and be very specific, GPT understands it mostly. Notice, some of them seem repetitive, that is sometimes useful in order to get good answers. Tune prompt instructions as much as needed.</p><p>Let’s test locally python webapp_hsbc.py</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*11HKwsQwA5AqkSssw1F46A.png" /></figure><p>Bombard with irrelevant question:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*B6dTihrKei_wYlfJHNEdgw.png" /></figure><h3>Back to deployment</h3><p>A tiny cheap machine is required, even 1Gb RAM would be enough. Cheap? How about free!</p><p><a href="https://www.alibabacloud.com/">Alibaba cloud</a> boasts impressive 3 month free trial with 4Gb RAM instance. Go get it (or rent a cheap machine from AWS, Azure, even DigitalOcean or any other hosting).</p><p>Overall Alibaba Cloud is very decent and the UI is visually similar to AWS.</p><p>AlibabaCloud: when creating instance, choose Frankfurt, London or US location, Ubuntu 22.04 LSTM, and ensure Assign Public IPv4 checked. Start the instance.</p><p>Set the password or a key pair of your running instance — in Console/Instances:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*HOzsJf8x81k-QIC1jH0Ntg.png" /></figure><p>Use <a href="https://www.putty.org/">Putty </a>to connect to alibaba ubuntu machine. Simply paste your public ip into putty, click Open and use specified credentials.</p><p>Get the code via command:</p><pre>git clone https://github.com/quicksilver0/llm_webapp_api.git</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/649/1*NY1KwjEQO1xW0yfH2v5Oqw.png" /><figcaption>github repo downloaded on ubuntu machine</figcaption></figure><p>Alternative way to copy files onto remote machine is to use <a href="https://winscp.net/eng/download.php">WinSCP</a> or some other FTP client — it has nice interface and allows you drag-and-drop your files where you need.</p><p>Run following commands:</p><pre>cd llm_webapp_api<br>sudo apt update<br>sudo apt upgrade<br>sudo apt-get install python3-pip<br>sudo pip3 install virtualenv<br><br>virtualenv venv<br>source venv/bin/activate<br><br>pip3 install flask tiktoken cohere langchain-openai numpy</pre><p>This enters llm_webapp_folder, installs essential utilities and packages, create and activate virtual environment, and install packages specific for this virtual env.</p><p>Remember, at this point you have to provide OPENAI_API_KEY and COHERE_KEY. Edit the file using command vi webapp_hsbc.py to assign these variables. <a href="https://www.dummies.com/article/technology/programming-web-design/web-hosting/how-to-edit-files-with-vi-204443/">How to use unix vi editor</a>.</p><p>Now run the app python3 webapp_hsbc.py — and it is live, accessible on the internet. Just put instance public ip into browser.</p><p>The browser will try to open it as https, while the instance is accessible only via htpp:// — you would need manually type http:// prefix</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/121/1*AHkQHMA3G8iuzAy6SmtiQg.png" /></figure><p>Ask some questions to virtual assistant to prove it is working. Then we need to run the process in background, since the session (terminal window) dies in a few minutes if idle, and webapp halts.</p><p>Stop the program in terminal window by pressing Ctrl+c and run the webapp as following: nohup python3 webapp_hsbc.py &amp; — the process will executes in background and detaches.</p><p>Next, activate <strong>Telegram</strong> bot on the same machine. Logon to instance via new terminal window (another putty connection). Execute following:</p><pre>git clone https://github.com/quicksilver0/telegram_bot.git<br>cd telegram_bot<br><br>virtualenv venv<br>source venv/bin/activate<br><br>pip3 install aiogram requests</pre><p>Again the above commands fetch the code, create virtual env and install required packages.</p><p>Ensure to edit the file vi telegram_bot.py to define variables url and token with instance public ip and telegram token respectively.</p><p>Start the process python3 telegram_bot.py and test how it answers in telegram. If everything ok — terminate and run in background detached nohup python3 telegram_bot.py &amp;</p><p>Nice thing about nohup is that it writes output log into nohup.out in the same folder.</p><p>Incase you need to stop/restart your processes: find the process as following: ps aux | grep webapp_hsbc — note the PID of the process and kill it: kill -9 pid_number</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*lpvDP_cOc3oZvybJakh67g.png" /><figcaption>Example, how to kill the process</figcaption></figure><h3>Multilingual queries</h3><p>As a last blow, how about to check multilingual capabilities?</p><p>It should be possible due to the choice of multilingual cohere embeddings embed-multilingual-v3.0and also because ChatGPT is multilingual as well.</p><p>Question: I would like to dispute transaction</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*tnX9ml2o6l9daYCIB8ousg.png" /><figcaption>Disputing transaction in Chinese</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*7A8IZGabMbT0IqFzxrwYWQ.png" /><figcaption>Disputing transaction in Russian</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*UDHmHSJsEiVLxJBd_Iopsw.png" /><figcaption>Disputing transaction in Spanish</figcaption></figure><p>Done! We have a chatbot that handles HSBC bank queries with impressive efficiency, even though this solution is bare bones! (Therefore it can be improved significantly).</p><h3>Langchain and Vectordb solution</h3><p>Where is langchain and vector db implementation?</p><p><strong>Answer</strong>: I explored <strong>a lot</strong> with langchain and vector db. They are not mandatory at all! In certain cases they may be beneficial, yet langchain tampers control, while vector dbs — Chromadb or Faiss to <em>my surprise</em> proved to retrieve subpar results! Yes, probably some parameter tuning required.</p><h3>Advanced RAG development</h3><p>How to implment solutions such as:</p><ul><li>vector databases with metadata</li><li>red teaming (protection from promt hijacking)</li><li>quality and safety of RAG application</li><li>automated testing of RAG solution</li><li>efficient serving of LLM</li><li>efficient data preprocessing for LLM</li><li>prompt engineering</li><li>agentic RAG</li></ul><p>I highly recommend short free courses by <a href="https://www.deeplearning.ai/short-courses/">deeplearning.ai</a></p><h3>Thanks!</h3><p>Now you have a decent <strong>virtual assistant solution suitable for any business</strong>. Again, the code and practices presented here are intentionally oversimplified — consider it as a foundation, a template, to build upon and improve further.</p><p>To <strong>HSBC UK Bank</strong>: my apologies for calling your chatbot names.</p><p>Thanks and chime in with comments, questions, suggestions!</p><p>I bow out.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48a0a4467efc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get a website with GitHub Pages]]></title>
            <link>https://medium.com/@vovakuzmenkov/get-a-website-with-github-pages-9151ed636446?source=rss-db2a0e9d48ba------2</link>
            <guid isPermaLink="false">https://medium.com/p/9151ed636446</guid>
            <category><![CDATA[blogging]]></category>
            <category><![CDATA[website-development]]></category>
            <category><![CDATA[github]]></category>
            <dc:creator><![CDATA[Vladimir Kuzmenkov]]></dc:creator>
            <pubDate>Tue, 17 Apr 2018 01:14:32 GMT</pubDate>
            <atom:updated>2026-02-14T03:50:43.058Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bhciO7GPVDVuSeK_sbGPnw.png" /><figcaption>The front page. Looks good isn’t it?</figcaption></figure><p>Having a website is no surprise nowadays, be it a corporate, a personal blog, portfolio page or even an online shop! The services out there aplenty, usually not free of charge though.</p><p>Here I would share a really cool way I particularly like: via <a href="https://pages.github.com/">Pages Github</a>. GitHub initially is a software development and version control system, where developers collaborate on mostly open-source projects. That would not be an exaggeration to say, github is the largest creative community in the world (hey the world is digital nowadays isn’t it?)!</p><p>So, this place has the ability to store code, which developers took advantage of, and, eventually, extended into a full fledged service, featuring repositories bustling with open-source themes and templates to choose from. You’d be surprised how good it is! Follow along.</p><h3>Website in under 2 minutes</h3><p>Really? Yes, even faster!</p><p>Get a <a href="https://github.com/">github.com</a> account beforehand, if you don’t have it yet. Ensure to verify it via a email.</p><p>Now choose a design. These are the directories showing off custom templates of any kind:</p><ul><li><a href="http://themes.jekyllrc.org/">themes.jekyllrc.org</a></li><li><a href="http://jekyllthemes.org/">jekyllthemes.org</a></li><li><a href="http://drjekyllthemes.github.io/">drjekyllthemes.github.io</a></li></ul><p>As an example, let’s set up a professional looking website using <a href="http://themes.jekyllrc.org/airspace/">Airspace Jekyll template</a> by Andrew Lee.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*fMn0RLa56cJlXbIoArtszw@2x.png" /></figure><p>The rest is as easy as 1–2–3. Here are the steps:</p><p>1. Fork the repository where template code of our choice is stored: press “Homepage” to get to the repo, then click “Fork” button on the top right-hand corner:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/1*WyS7o8NZvrcPMUBxPF_ozg.png" /><figcaption>Fork a repo</figcaption></figure><p>In a few seconds our github would have a full “copy” of the designated website.</p><p>2. The next thing is renaming. Go to the Settings tab and give the repository a name <em>username.github.io</em>, it is important that <em>username</em> part is identical to the account name:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*iRqU9I29Yi7lk7-DARfJ9w.png" /></figure><p>3. Following step ensures the template is configured exactly for our github.</p><p>Go to <em>_config.yml</em>. Each theme offers various depth of customization, yet some fields are mandatory to be filled so that the website kicks off. Airspace’s such mandatory variables are <em>url</em> and <em>baseurl</em>. Edit the file as per example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0aa2b593cfa6f9ec221bd4ff6b48d472/href">https://medium.com/media/0aa2b593cfa6f9ec221bd4ff6b48d472/href</a></iframe><p>Notice how <em>url</em> is set to <a href="https://username.github.io/"><em>https://username.github.io</em></a> — that would be the actual website address! Also keep it <em>https </em>— not only it is a secure extension of http protocol, but most websites can be hosted only that way nowadays.</p><p>Scroll the page and “Commit changes”.</p><p>Vous voilà <a href="https://justanother-timetraveller.github.io/">https://justanother-timetraveller.github.io/</a> The site is ready!</p><h3>The content</h3><p>Even though Airspace features most content as html pages, the blog section can be filled through <em>markdown</em>.</p><p>See how the <em>_posts</em> folder contains *.md files such as <em>2016–05–20-my-example-post.md</em> which stand for markdown. Basically markdown standard allows writing as if in a notepad (or on Medium!) with addition of special characters to transform words into <em>bold/italics/etc</em>. Here’s an amazing 5 minute <a href="http://markdowntutorial.com/">tutorial on markdown</a> I’d highly recommend.</p><p>So, to publish a blogpost, create a file in <em>_posts</em> such as <em>yyyy-mm-dd-postname.md</em> (ensure the date prefix is there) and type yourself away! Also for an ease of use consider <a href="https://prose.io/">prose.io</a> service, which integrates seamlessly with github repository and has a nice markdown editorial interface.</p><p>About YAML front matter. It is important to specify parameters of the page at it’s very top before publishing. Here is an example:</p><pre>---<br>title: Contact me<br>subtitle: Here you&#39;ll find all the ways to get in touch with me<br>layout: post<br>---</pre><p>There’s plenty of capabilities that jekyll-websites offer through parameters, such as tags, image preview, social icons — you can learn which from the examples and readme.md of your theme.</p><p>Ok, upon commit Jekyll would automatically transform the <em>.md</em> into nice html webpage and publish it!</p><h3>Advanced stuff</h3><p>You might want to add Disqus comments for the blogs, which is a breeze to install, or maybe analytic tools to track user views, or set personal domain and any other features? Usually it is about customizing <em>_config.yml</em>, depends on a template. Anyway I suggest to consult the <em>readme.md</em> of your theme or address an excellent <a href="https://github.com/daattali/beautiful-jekyll">readme for beautiful-jekyll</a>.</p><p>If the theme of your choice does not allow the above through <em>_config.yml</em> for some reason, check out my tutorial on <a href="https://silversurfer0.github.io/tutorial/2017/09/13/Jekyll-with-github-pages.html">Jekyll tutorial with Github pages</a> with indepth walkthrough on how to add relevant code snippets by yourself.</p><h3>Thanks</h3><p>Yes, thanks for reading!</p><p>My credits go to <a href="http://deanattali.com/">Dean Attali</a>, as I was largely inspired by his <a href="https://github.com/daattali/beautiful-jekyll">template and description</a>.</p><p>All in all, modern themes both are easy to install and maintain with a few lines of a config file. While posting through <a href="https://prose.io/">prose.io</a> or a local editor becomes a streamline with just a few clicks!</p><p>Ok, I bow out:) Have fun and great time \o/</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9151ed636446" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Poincare’s Creativity]]></title>
            <link>https://medium.com/@vovakuzmenkov/poincares-creativity-8e31d5031529?source=rss-db2a0e9d48ba------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e31d5031529</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[science-education]]></category>
            <category><![CDATA[philosophy]]></category>
            <category><![CDATA[science]]></category>
            <dc:creator><![CDATA[Vladimir Kuzmenkov]]></dc:creator>
            <pubDate>Tue, 03 Apr 2018 12:05:01 GMT</pubDate>
            <atom:updated>2026-02-13T20:08:31.270Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>The scientist does not study nature because it is useful; he studies it because he delights in it, and he delights in it because it is beautiful.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*gGW9ZneSowwnNdbpPQhk5A.jpeg" /><figcaption>Poincare in his younger age.</figcaption></figure><p>Henri Poincare, a French mathematician, theoretical physicist, engineer and the philosopher of science, often described as “The Last Universalist” in math - a remarkable title, since he excelled in all fields of this discipline as it existed.</p><p><em>With this post I’m not going to highlight the biography of this genius, it can be found in abundance from other sources.</em></p><p><em>I‘d rather explore something really fascinating: the finest philosophical principle discovered by this man, or it is better to say a principle he embodied.</em></p><h3>Is it about Rigour?</h3><p>Probably not. See for yourself.</p><p>So, Poincaré’s work habits have been compared to a bee flying from flower to flower. Poincaré himself was interested in the way his mind worked; he studied his habits and gave a talk about his observations in 1908 at the Institute of General Psychology in Paris. He linked his way of thinking to how he made several discoveries.</p><p>The mathematician Darboux claimed he was <em>un intuitif</em> (intuitive), arguing that this is demonstrated by the fact that he worked so often by visual representation. <strong><em>He did not care about being rigorous and disliked logic</em></strong><em>.</em></p><p>Despite this opinion, <a href="https://en.wikipedia.org/wiki/Jacques_Hadamard">Jacques Hadamard</a> wrote that Poincaré’s research demonstrated marvelous clarity and <strong><em>Poincaré himself wrote that he believed that logic was not a way to invent but a way to structure ideas and that logic limits ideas</em></strong>.</p><p>Édouard Toulouse wrote a book entitled <em>Henri Poincaré</em> (1910); Here are some shortcomings to mention:</p><ul><li>He was physically clumsy and artistically inept.</li><li>He was always in a rush and disliked going back for changes or corrections.</li><li>He never spent a long time on a problem since he believed that the subconscious would continue working on the problem while he consciously worked on another problem.</li></ul><p>In addition, Toulouse stated that most mathematicians worked from principles already established while Poincaré started from basic principles each time (O’Connor et al., 2002).</p><h3>A trip towards truth with Poincare</h3><p>Most of us perceive a math as pure logic, where any statement is a ground truth, and a mere continuation of a logic construct. Is it so?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*A8-xOKYD2Yr_SOtC6AU4aQ.jpeg" /></figure><p>True inventors and enthusiasts on the verge of the cutting edge in their field, know there is more to it, and the reality is so much different from what people learn at school.</p><p>Here I publish an excerpt from a bestseller “<em>Zen and the Art of Motorcycle Maintenance</em>” by Robert M. Pirsig. Yes, I kept the excerpt quite long, and it might be a bit hard at first to grasp it, but it allows consistency and reveals this very principle, fascinating in a way that it possibly the closest description of reality layed into words.</p><p>…</p><p>“To solve the problem of what is mathematical truth, Poincare said, we should first ask ourselves what is the nature of geometric axioms. Are they synthetic a priori judgments, as Kant said? That is, do they exist as a fixed part of man’s consciousness, independently of experience and uncreated by experience? Poincare thought not. They would then impose themselves upon us with such force that we couldn’t conceive the contrary proposition, or build upon it a theoretic edifice. There would be no non-Euclidian geometry. (And we would be robots rather than humans.)</p><p>Should we therefore conclude that the axioms of geometry are experimental verities? Poincare didn’t think that was so either. If they were, they would be subject to continual change and revision as new laboratory data came in. This seemed to be contrary to the whole nature of geometry itself. Poincare concluded that the axioms of geometry are conventions, our choice among all possible conventions is guided by experimental facts, but it remains free and is limited only by the necessity of avoiding all contradiction. … Then he turned to question, is Euclidian geometry true or is Riemann geometry true? He answered, The question has no meaning. Geometry is not true, it is advantageous.</p><p>Our concepts of space and time are also definitions, selected on the basis of their convenience in handling the facts. … But now the burden of sustaining the order of the universe rests on “facts”. What are facts?<br>Poincare proceeded to examine these critically. Which facts are you going to observe? — he asked. There is an infinity of them. There is no more chance that an unselective observation of facts will produce science than there is that a monkey at a typewriter will produce the Lord’s Prayer. … If the scientist had at his disposal infinite time, Poincare said, it would only be necessary to say to him, “Look and notice well”; but as there isn’t time to see everything, and as it’s better not to see than to see wrongly, it’s necessary for him to make a choice.</p><blockquote><em>Science is built up of facts, as a house is built of stones; but an accumulation of facts is no more a science than a heap of stones is a house.</em></blockquote><p>Poincare laid down some rules: There is a hierarchy of facts.<br>The more general a fact, the more precious it is. Those which serve many times are better than those which have little chance of coming up again. … How to recognize them? Choose those that seem simple.</p><p>Where is the simple fact? Scientists have been seeking it in the two extremes, in the infinitely great and in the infinitely small. Biologists, for example, have been instinctively led to regard the cell as more interesting than the whole animal; and, since Poincare’s time, the protein molecule as more interesting than the cell. The outcome has shown the wisdom of this, since cells and molecules belonging to different organisms have been found to be more alike than the organisms themselves. … It’s proper to begin with the regular facts, but after a rule is established beyond all doubt, the facts in conformity with it become dull because they no longer teach us anything new. Then it’s the exception that becomes important. We seek not resemblance but differences because they’re the most striking and also the most instructive. … cases in which this rule has the greatest chance of failing; by going very far away in space or very far away in time, we may find our usual rules entirely overtuned, and these grand overturning enable us the better to see the little changes that may happen nearer to us. Particular rules seem at first discordant, but looking more closely we see in general that they resemble each other; When we look at them with this bias we shall see them enlarge and tend to embrace everything.</p><p>No, Poincare concluded, a scientist does not choose at random the facts he observes. He seeks to condense much experience and much thought into a slender volume; and that’s why a little book on physics contains so many past experiences and a thousand times as many possible experiences whose result is known beforehand.</p><p>Then Poincare illustrated how a fact is discovered. He had described generally how scientists arrive at facts and theories but now he penetrated narrowly into his own personal experience with the mathematical functions that established his early fame.</p><p>When working on his math functions he tried a great number of combinations and reach no results. Then one evening, contrary to his custom, he drank a black coffee and couldn’t sleep. Ideas arose in crowds. He felt them collide until pairs interlocked, so to speak, making a stable combination. The next morning he had only to write out the results. A wave of crystallization had taken place. A later discovery occurred while he was walking by a seaside bluff. It came to him with just the same characteristics of brevity, suddenness and immediate certainty. Another major discovery occurred while he was walking down a street. Others eulogized this process as the mysterious workings of genius, but Poincare was not content with such a shallow explanation. He tried to fathom more deeply what had happened.</p><p>The true work of the inventor consists in choosing among a large number of combinations so as to eliminate the useless ones, or rather, to avoid the trouble of making them, and the rules that must guide the choice are extremely fine and delicate. It’s almost impossible to state them precisely; they must be felt rather than formulated. Poincare hypothesized that his selection is made by what he called the “subliminal” self. For instance Math solutions are selected by the subliminal self on the basis of “mathematical beauty”, of the harmony of numbers and forms, of geometric elegance. “This is a true aesthetic feeling which all mathematicians know”, Poincare said, “but of which the profane are so ignorant as often to be tempted to smile”. But it is this harmony, this beauty, that is the center of it all.</p><p>Poincare made it clear that he was not speaking of romantic beauty, the beauty of appearances which strikes the senses. He meant classic beauty, which comes from the harmonious order of the parts… It is not the facts but the relation of things that results in the universal harmony that is the sole objective reality.</p><p>What guarantees the objectivity of the world in which we live is that this world is common to us with other thinking beings. Through the communications that we have with other men we receive from them ready-made harmonious reasonings. We know that these reasonings do not come from us and at the same time we recognize in them, because of their harmony, the wok of reasonable beings like ourselves. And as these reasonings appear to fit the world of our sensations, we think we may infer that these reasonable beings have seen the same thing as we; thus it is that we know we haven’t been dreaming. It is this harmony, this quality if you will, that is the sole basis for the only reality we can ever know.</p><p>Poincare’s contemporaries refused to acknowledge that facts are preselected because they thought that to do so would destroy the validity of scientific method. They presumed that “preselected” facts meant that truth is “whatever you like” and called his ideas conventionalism.</p><p>What Poincare neglected to say was that the selection of facts before you “observe” them is “whatever you like” only in a dualistic, subject-object metaphysical system! When Quality enters the picture as a third metaphysical entity, the preselection of facts is no longer arbitrary. The preselection of facts is not based on subjective, capricious “whatever you like” but on Quality, which is reality itself.</p><p>But we know from Phaedrus’ metaphysics that the harmony Poincare talked about is not subjective. It is the source of subjects and objects and exists in an anterior relationship to them. It is not capricious, it is the force that opposes capriciousness; the ordering principle of all scientific and mathematical thought which destroys capriciousness, and without which no scientific thought can proceed. To produce a complete structure of thought capable of uniting the separate languages of Science and Art into one.”</p><p>…</p><p>“Zen and the Art of Motorcycle Maintenance” by Robert M. Pirsig.</p><p>How does it tunes with you?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e31d5031529" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Full-Stack Crowdfunding Smart Contract development]]></title>
            <link>https://medium.com/@vovakuzmenkov/full-stack-crowdfunding-smart-contract-development-15ed139cde06?source=rss-db2a0e9d48ba------2</link>
            <guid isPermaLink="false">https://medium.com/p/15ed139cde06</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[openzeppelin]]></category>
            <category><![CDATA[blockchain-development]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Vladimir Kuzmenkov]]></dc:creator>
            <pubDate>Fri, 29 Dec 2017 23:07:25 GMT</pubDate>
            <atom:updated>2026-02-13T17:30:17.239Z</atom:updated>
            <content:encoded><![CDATA[<p>There is so much buzz about Ethereum smart contracts out there! Articles describe this technology to be secure, reliable, world-changing and overall predict our near-future to be governed through micro-transactions between individuals. In conversations I’ve heard statements as “This world would be trustworthy only when we have smart-contracts in all areas of our life”. This is all certainly true, yet, to a certain extent. Follow along for a deep dive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ardJWR21kRS5DoLJRAunug.jpeg" /><figcaption>Our fully connected blue marble.</figcaption></figure><p>You are reading a comprehensive step-by-step tutorial that guides through building a dApp (decentralized app) using Ethereum Smart Contracts. I’ve created it so, that even a complete novice, with little to no knowledge of the subject can fully understand what Smart Contracts are, how can they be used and most important, would build Crowdfunding application with own hands! More advanced enthusiasts would find this tutorial absolutely useful as well.</p><p>So, by the end of this tutorial you’ll have not only a solid grasp, but also a <em>ERC20 compliant, secure, industry level</em> Smart Contract deployed on Ethereum blockchain — all this built by yourself! Sounds cool? … Let’s get started.</p><h3>What is Ethereum Smart Contract</h3><p>First let me share 2 great posts, which I find genius in a way authors explain the basics:</p><ul><li><a href="https://medium.freecodecamp.org/how-does-blockchain-really-work-i-built-an-app-to-show-you-6b70cd4caf7d">How blockchain really works?</a> — best explanation on blockchain ever.</li><li><a href="https://medium.com/@jgm.orinoco/understanding-erc-20-token-contracts-a809a7310aa5">Understanding ERC-20 contracts</a> — best introduction.</li></ul><p>So, any contract is a set of rules, that describes what happens in certain conditions. A smart contract is a set of rules executed automatically if those conditions occur! So, it is a piece of code written onto the blockchain, meaning it is transparent (visible to anybody) and reliable as the blockchain itself. And this is what actually makes it so powerful if you think about it. Since it’s so transparent and runs on a blockchain, it eliminates the need for trust to the third parties. Individuals and small companies can interact without even knowing each other and feel safe for their funds, because it is the contract that governs outcome.</p><p>To make it even more clear: there is no need for third parties, that would create paper contracts, verify personalities, make decisions in conflict situations, yada-yada-ya… the contract is right there and it is automatic. Yes, nowadays technology allows that! With certain limitations, though. Yet it is a great decentralization already!</p><h3>Crowdfunding Campaign</h3><p>Suppose some creative folks are about to create an awesome product, so they need funding to make it happen at a large scale. How do they get a perfect sponsor?- Run a crowdfunding campaign! First they determine how much money is needed, secondly advertise the idea to the world and showcase the prototype, and, most importantly, make a promise that anyone investing <em>now</em> would get the product <em>later</em> proportional to the investment made.</p><p>We can leverage this thanks to the smart contracts technology!</p><h3>Crowdfunding contract business design</h3><p>Let’s think, what our contract would actually do.</p><p>It would encompass following scenarios:</p><ol><li>The contract would accept funds and issue tokens to the investors. <em>Say, 100 tokens per 1 ETH, or 1 token per 0.01 ETH.</em></li><li>Tokens can be used to buy products from us, or even traded among users (yes, people might decide to sell it to someone else for different price).</li><li>Funds would be delivered to the campaign starter only if the goal reached. Suppose, the goal would be 100 ETH, which is pretty decent amount.</li><li>If the campaign have not raised 100ETH until certain date, all funds would be claimed back by the investors.</li></ol><p>Cool! All that smart contract would provide for us. From technical perspective, you might be excited to know, that the blockchain would serve as a server, running all the logic and storing transactional data. Meaning the campaign can be run on a static web page! Well, there are <a href="https://blog.zeppelin.solutions/designing-the-architecture-for-your-ethereum-application-9cec086f8317">different architecture approaches to choose from</a> and sometimes a web server is a must, yet at the very essence we can dispense with it.</p><h3>The Contract Code itself</h3><p>Actually a full-fledged crowdfunding smart contract is quite heavy, if you think about it, as it should have vault to store funds, allow pretty complex interactions, and all that should be absolutely secure. Please remember, we deal with real money here, with no banking systems as intermediary!</p><p>A great thing about this world is that there exist frameworks already!</p><p>Meet <a href="https://openzeppelin.org/">OpenZeppelin</a> is an open framework of reusable and secure smart contracts in the Solidity language, built with the best practices and industry standard security patterns!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*IGourfMFRc77Qm5eqFl0AQ.png" /></figure><p>Guys at Zeppelin Solutions provide audit, development, and also have a Slack channel which is a great place to solve questions, discuss ideas or collaborate.</p><p>So, the smart contract is written in <strong>Solidity</strong>. This is a statically typed language to write Ethereum smart contracts. From <a href="http://solidity.readthedocs.io/en/develop/index.html">the documentation</a>:</p><blockquote>Solidity is a contract-oriented, high-level language whose syntax is similar to that of JavaScript and it is designed to target the Ethereum Virtual Machine (EVM).</blockquote><p>OpenZeppelin provide <a href="https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/examples/SampleCrowdsale.sol">SampleCrowdsale contract</a> which is a fully-fledged solution, created with modular approach, consisting of a number of subcontracts. Here is how it looks like:</p><pre>pragma solidity ^0.4.18;</pre><pre>import &quot;../crowdsale/CappedCrowdsale.sol&quot;;<br>import &quot;../crowdsale/RefundableCrowdsale.sol&quot;;<br>import &quot;../token/MintableToken.sol&quot;;</pre><pre>contract SampleCrowdsaleToken is MintableToken {</pre><pre>string public constant name = &quot;Sample Crowdsale Token&quot;;<br>  string public constant symbol = &quot;SCT&quot;;<br>  uint8 public constant decimals = 18;<br>}</pre><pre>contract SampleCrowdsale is CappedCrowdsale, RefundableCrowdsale {</pre><pre>function SampleCrowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, uint256 _goal, uint256 _cap, address _wallet) public<br>    CappedCrowdsale(_cap)<br>    FinalizableCrowdsale()<br>    RefundableCrowdsale(_goal)<br>    Crowdsale(_startTime, _endTime, _rate, _wallet)<br>  {<br>    require(_goal &lt;= _cap);<br>  }</pre><pre>function createTokenContract() internal returns (MintableToken) {<br>    return new SampleCrowdsaleToken();<br>  }<br>}</pre><p>First we see line pragma solidity ^0.4.18; which defines version of solidity compiler to be used. Note a caret sign (^) to ensure the version range of earlier compilers and up to 0.4.18.</p><p>Imports such as import “../token/MintableToken.sol”; importing other modules to extend our contract with features. Those modules are actually contracts themselves, and serve as subcontracts.</p><p>contract SampleCrowdsaleToken is MintableToken {…} — here the contract with a name SampleCrowdsaleToken is defined and it inherits all the functions from MintableToken contract.</p><p>Further on we define the name for our token with string public constant name = “Sample Crowdsale Token”;, also symbol and decimals. Standard decimals are 18, which is a token base per 1 ETH and equals to the amount of wei transfered per 1 ETH (1 ETH contains 10¹⁸ wei).</p><p>Afterwards we have our main contract contract SampleCrowdsale is CappedCrowdsale, RefundableCrowdsale {…} which fully inherits from Capped and Refundable contracts. Function function SampleCrowdsale(…) takes all the necessary arguments for contract creation, such as startTime, endTime, goal, etc. which we will define and pass during contract deployment onto the blockchain.</p><p>There is also require(_goal &lt;= _cap); which ensures that the goal is less or equal than the campaign cap.</p><h3>Install and Test locally</h3><p><em>I’ll describe all steps assuming you run on Windows, yet implementation on other platforms should be similar.</em></p><h4>Installations and preparations</h4><p><em>Having </em><strong><em>bash installed</em></strong><em> on your system would ease the process significantly. </em><a href="http://win-bash.sourceforge.net/"><em>Get it here</em></a><em>.</em></p><p>Ensure you have <strong>package manager</strong> (npm), which is available as soon as you <a href="https://nodejs.org/en/">install Nodejs</a>.</p><p>We’ll use <strong>Truffle</strong>, so <a href="http://truffleframework.com/">please install it</a>. Truffle is an awesome and smooth framework for Ethereum dApp development with some great off-the-shelf necessities.</p><p>Let’s build our smart contract upon an existing truffle box <strong>tutorialtoken</strong>. There is an excellent tutorial on <a href="http://truffleframework.com/tutorials/robust-smart-contracts-with-openzeppelin#creating-the-quot-tutorialtoken-quot-smart-contract">token creation available on truffle</a> website (which I also highly recommend to walk-through).</p><p>Ok, so run your bash terminal as adminitrator. Create a folder named <strong>crowdsale</strong>, go into it and run truffle unbox tutorialtoken. Sweet! Truffle created initial structure for us.</p><p>Next, we’ll install OpenZeppelin, the most recent version can be found as an npm package. Run npm install zeppelin-solidity</p><p>Notice, there is <strong>contracts</strong> folder been created — this is where actual contract should reside. Let’s get <strong>SampleCrowdsale.sol</strong> from node_modules/zeppelin-solidity/contracts/examples and copy it to our <strong>contracts</strong> folder.</p><p>Edit the contents of SampleCrowdsale.sol to reflect correct inheritance paths such as: import “../crowdsale/CappedCrowdsale.sol”; change to import “zeppelin-solidity/contracts/crowdsale/CappedCrowdsale.sol”;</p><p>Go to <strong>migrations</strong> folder. Migration files here govern the way the contract would be deployed onto the blockchain. There already exist a file 1_initial_migration.js. This handles deploying the Migrations.solcontract to observe subsequent smart contract migrations, and ensures we don&#39;t double-migrate unchanged contracts in the future.</p><ol><li>Create a new file named 2_deploy_contracts.js in the migrations/directory.</li><li>Add the following content to the 2_deploy_contracts.js file:</li></ol><pre>var SampleCrowdsale = artifacts.require(&quot;SampleCrowdsale&quot;);</pre><pre>module.exports = function(deployer, network, accounts) {</pre><pre>    const startTime = Date.now()/1000|0 + 120;<br>    const endTime = startTime + (3600 * 1 * 1); // *1 hour *1 days<br>    const ethRate = new web3.BigNumber(100);<br>    const wallet = accounts[0];</pre><pre>deployer.deploy(SampleCrowdsale, startTime, endTime, ethRate, 100000000000000000000, 200000000000000000000, wallet);<br>};</pre><p>See how startTime is defined as current epoch time +120 sec. These 2 minutes is a reserve time for our contract to start functioning right after deployment. Actually you might want to set it even higher when deploying on MainNet. Here, according to endTime our campaign will last only 1 hour (well, it’s for test purposes only!), real campaigns could last months. The ethRate variable defines that 100 tokens are provided per 1 Eth. Also notice 100*10¹⁸ and 200*10¹⁸ input numbers. These ensure campaign goal of 100 Eth and a campaign cap of 200 respectively.</p><p>Cool!</p><p><strong>And, hey, our Crowdsale Smart Contract is ready!</strong></p><p>Now we can deploy and interact with it.</p><h4>Deploy and interact</h4><p>While in <strong>crowdsale </strong>folder run truffle develop — it would run local testnet, establish an rpc connection to it, and also provide 10 test accounts with their mnemonic passphrase, each having 100 ethereum balance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*Lp2y4BjvaCk-QHWasDBS_Q.png" /></figure><p>To check balance of one of your accounts run: web3.eth.getBalance(‘your acc address starting with 0x’)/1000000000000000000 Since the balance is returned not in ETH, but in Wei as BigNumber we divide it by 10¹⁸.</p><p>It’s time to <strong>install MetaMask</strong> — is a lightweight wallet that “Brings Ethereum to your browser”. Get chrome extension.</p><ol><li><a href="http://metamask.io/">Install MetaMask</a> for your browser.</li><li>Once installed, you’ll see the MetaMask fox icon next to your address bar. Click it, and after pressing “Next” there would be an initial Metamask screen. Click <strong>Import Existing DEN</strong>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*O6aMo_kR8d_61rYMJShYGw.png" /></figure><p>Import the mnemonic phrase provided by truffle, which is: candy maple cake sugar pudding cream honey rich smooth crumble sweet treat and type any password. That would allow to generate those 10 test accounts in metamask.</p><p>Now we need to connect MetaMask to our local blockchain. Click the menu that shows “Main Network” and select <strong>Custom RPC</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/358/1*82y-e1744iJL6gFBebSjWA.png" /><figcaption>Adding Custom RPC URL</figcaption></figure><p>In the box titled “New RPC URL” enter <a href="http://localhost:9545/">http://localhost:9545/</a> and click <strong>Save</strong>.</p><p>Great, you should see one of your test accounts filled with 100 Eth.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*ab8w_Qhd-_Iiy-cvN8YRQQ.png" /><figcaption>Test account</figcaption></figure><p>We’ve configured Metamask! Let’s get back to contract deployment.</p><p><strong>Complile the contract</strong> by running: compile It would gather all the contracts and subcontracts that inherit from each other and compile schema artifacts, saving it to <strong>build/</strong> folder.</p><p>Deploy the contract onto the blockchain: migrate --reset Whoosh. Done.</p><p>Now our contract resides on a blockchain and has an address SampleCrowdsale.address</p><p>We can interact with it! Let’s buy some tokens! First assign it’s instance to a variable: var crowdsale = SampleCrowdsale.at(SampleCrowdsale.address). Run crowdsale to display all functions available.</p><p>We would want to add <strong>token address</strong> to our Metamask wallet in order to display token balance. What does it actually mean token address? It is a subcontract address which stores the balances. Get this address by running in the console: crowdsale.token(). Then, in Metamask choose Token tab, press Add button and plug in the address.</p><p>Ok, let’s buy some tokens. How to do that? Simply throw ETH to the contract address, which as we saw earlier can be obtained with the command SampleCrowdsale.address. In Metamask send 5 ETH to the SampleCrowdsale address. Remember, we defined ethRate as 100, so 5 ETH should yield us 500 tokens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*4bUe1EkuEKQ4SYn1czlhTw.png" /><figcaption>Setting transaction parameters</figcaption></figure><p>Metamask would ask to set transaction parameters. You can set Gas Limit to 1000000, in real network such transaction would require about 20k Gas, all the rest would be returned.</p><p>If all done right, we would see our token balance of 500!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*iyI5jzA_SFh73_SYmSCfCg.png" /></figure><p>Our first interaction with the contract is successful!</p><p>Here are a few function examples available at hand:</p><ul><li>Check how much the campaign raised: crowdsale.weiRaised()</li><li>Is goal reached? crowdsale.goalReached()</li><li>When campaign ends? crowdsale.endTime()</li><li>What deposit was made by an investor?</li></ul><pre>crowdsale.vault()<br>&gt;&#39;0xaa8f61728cb614f37a2fdb8b420c3c33134c7f69&#39;<br>var vault = RefundVault.at(&#39;0xaa8f61728cb614f37a2fdb8b420c3c33134c7f69&#39;)<br>vault.deposited(address of the investor)</pre><p>Ok nice. It all works in terminal. But…</p><h4>Browser Application</h4><p>… in fact we want it all work on a beautiful webpage, not in some console. The functions should be presented as buttons, and all the info displayed in a nice fashion!</p><p>Let’s think about the design:</p><ol><li>So, we want a page that shows the progress of the campaign: when it starts, when it ends. How much funds have been collected and what is the overall goal. Also it should have nice buttons, such as “Buy 100 tokens”, which allow to invest with just a click.</li><li>We also want an admin page, that allows to control the campaign, namely run finalization.</li><li>Also there should be a way to easily perform full refund for investors if the campaign failed.</li></ol><p>Cool, having a clear picture of what we want, it’s much easier to build it! And I already have these pages with the javascript completed for you! <a href="https://github.com/SilverSurfer0/Ethereum/blob/master/src.zip">Get it on the github.</a></p><p>It contains an src folder with 3 html pages and app.js file. Copy this src folder to your project directory, overwrite if necessary.</p><p>Now we want to serve our website. In a <em>new terminal window</em> cd to your project and run npm run dev. That should fire up a lite-server:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*Ts2pNOGG-MR0A49W5og-Kw.png" /><figcaption>Crowdfunding Campaign page!</figcaption></figure><p>Voila! Play around, <strong>you can buy tokens</strong> with different test accounts!</p><p><em>Note: make sure that campaign end time not reached (remember, we’ve defined it to last only for 1 hour).</em></p><p><strong>Test how Refund works</strong>: do not reach campaign goal, wait till end time. Then when logged with the owner account via Metamask (<em>an owner account is an account under which the contract was deployed</em>), press Finalize.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*ixCoZ1m9RqLzxj2zn6W5qw.png" /></figure><p>After campaign finalized, any account would be able to get full Refunds on the Refund page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*u2vGO_9_NIzbIdGXfCPP2A.png" /></figure><h4>dApp walkthrough</h4><p>How is this magic happens? How on Earth our webpage communicates with the blockchain? For that, let’s have a walk-through the contents of <strong>app.js</strong></p><pre>App = {<br>  web3Provider: null,<br>  contracts: {},</pre><pre>init: function() {<br>    return App.initWeb3();<br>  },<br>initWeb3: function() {<br>    // Initialize web3 and set the provider to the testRPC.<br>    if (typeof web3 !== &#39;undefined&#39;) {<br>      App.web3Provider = web3.currentProvider;<br>      web3 = new Web3(web3.currentProvider);<br>    } else {<br>      // set the provider you want from Web3.providers<br>      App.web3Provider = new Web3.providers.HttpProvider(&#39;<a href="http://127.0.0.1:9545&#39;">http://127.0.0.1:9545&#39;</a>);<br>      web3 = new Web3(App.web3Provider);<br>    }<br>...</pre><p>Note that there is a global App object to manage our application, also there is a call function initWeb3(). The <a href="https://github.com/ethereum/web3.js/">web3 JavaScript library</a> — this is what allows to interact with the Ethereum blockchain. It can retrieve user accounts, send transactions, interact with smart contracts, and more.</p><p>Notice: first, the code checks if there’s a web3 instance already active. (Ethereum browsers like <a href="https://github.com/ethereum/mist">Mist</a> or Chrome with the <a href="https://metamask.io/">MetaMask</a> extension will inject their own web3 instances.) If an injected web3 instance is present, we get its provider and use it to create our web3 object.</p><p>Now that we can interact with Ethereum via web3, we need to instantiate our smart contract so web3 knows where to find it and how it works. Truffle has a library to help with this called truffle-contract. It keeps information about the contract in sync with migrations, so you don&#39;t need to change the contract&#39;s deployed address manually. Heres is an example of how it looks like:</p><pre>initContract: function() {<br>    $.getJSON(&#39;SampleCrowdsale.json&#39;, function(data) {<br>      // Get the necessary contract artifact file and instantiate it with truffle-contract.<br>      var SampleCrowdsaleArtifact = data;<br>      App.contracts.SampleCrowdsale = TruffleContract(SampleCrowdsaleArtifact);</pre><pre>// Set the provider for our contract.<br>      App.contracts.SampleCrowdsale.setProvider(App.web3Provider);<br>      return App.getRaisedFunds(), App.getGoalFunds(), App.getEndTime(), App.isFinalized(), App.getTokenPrice1(), App.getTokenPrice10(), App.getTokenPrice100(), App.isGoalReached(), App.getEthRefundValue();<br>    });<br>}</pre><p>Also notice how I also instantiate RefundVault.json and MintableToken.json, as those contracts contain some useful functions we would call.</p><p><strong>Buy token</strong> function looks as following:</p><pre>handleBuy1Tokens: function send() {<br>    App.contracts.SampleCrowdsale.deployed().then(function(instance) {<br>      crowdsale = instance;<br>      var crowdsaleContractAddress = crowdsale.address;<br>      crowdsale.rate().then(function(rate){<br>        var rate = rate;</pre><pre>web3.eth.sendTransaction({<br>          from: web3.eth.coinbase,<br>          to: crowdsaleContractAddress,<br>          value: web3.toWei(1/rate, &#39;ether&#39;)</pre><pre>},function(error, result) {<br>          if (!error) {<br>            document.getElementById(&#39;response1&#39;).innerHTML = &#39;Success: &lt;a href=&quot;<a href="https://ropsten.etherscan.io/tx/&#39;">https://ropsten.etherscan.io/tx/&#39;</a> + result + &#39;&quot;&gt; View Transaction &lt;/a&gt;&#39;<br>          } else {<br>            document.getElementById(&#39;response1&#39;).innerHTML = &#39;&lt;pre&gt;&#39; + error + &#39;&lt;/pre&gt;&#39;<br>          }<br>          })<br>      })<br>    })<br>  },</pre><p>First it gets instance object of the SampleCrowdsale contract and assigns it to a variable crowdsale. Also it binds address to a variable crowdsaleContractAddress. Next it obtains rate value via crowdsale.rate() call. Please note here, that function calls performed via web3 on a blockchain are asynchronous, so we have to retrieve an object that is returned with .then(function(result){}) Therefore we use following syntax:</p><pre>crowdsale.rate().then(function(rate){<br>        var rate = rate;<br>...</pre><p>Then we can perform a transaction with web3.eth.sendTransaction(). web3.eth.coinbase argument is current ethereum address (as logged in with the wallet). web3.toWei(1/rate, ‘ether’) — essentially we buy 1 token here, divided by rate, all this converted toWei(), because transactions are sent in Weis.</p><p>And the last part of our buy1Token function is populating element of id response1 with “Successful transaction link”.</p><p><em>These are the underlying principles to encode transactions and obtain any info from the blockchain with javascript!</em></p><blockquote>So, again, all the magic comes from an Ethereum client like Metamask or Mist connected to a blockchain and injecting web3 instance into our dApp, which is used to create web3 object to interact with.</blockquote><h3>Getting Live onto a Real Blockchain!</h3><p>Now we can get live to a real blockchain. First we’ll deploy on <strong>Ropsten</strong> — it is a large and most popular Ethereum testnet, which best resembles mainnet in it’s distribution and also allows free ETH.</p><h4>Preparations, additional installations…</h4><p>So. It is a good practice to establish own node whether you want to connect to testnet or mainnet. However we’ll use gateway to a node provided by awesome guys from <strong>Infura</strong>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*FdIx_CBAkSm_yDGYzuqIRQ.png" /><figcaption>Infura :)</figcaption></figure><p>Go to <a href="http://infura.io/">Infura.io</a> and register to obtain endpoint addresses. It would look something like this:</p><p>https://ropsten.infura.io/&lt;unique_string&gt;</p><p>Also we would need a real account further on. <a href="https://theethereum.wiki/w/index.php/Accounts,_Addresses,_Public_And_Private_Keys,_And_Tokens#How_To_Create_A_New_Account_In_MyEtherWallet">How to create Ethereum account</a>. Essentially we need a private key JSON and associated password both as a separate files.</p><p>Next fill your account with free Ethereum. Either via <a href="https://faucet.metamask.io/">MetaMask’s Ether Faucet</a> or <a href="https://gitter.im/ethereum/ropsten">Ropsten Gitter</a>.</p><p>Install additional wallet-provider packages. From your project folder run npm install ethereumjs-wallet and npm install truffle-wallet-provider.</p><p>Now, edit <strong>truffle.js</strong>. Add following contents:</p><pre>var WalletProvider = require(&quot;truffle-wallet-provider&quot;);<br> <br>var keystore = require(&#39;fs&#39;).readFileSync(&#39;./test/keystore/key.json&#39;).toString();<br>var pass = require(&#39;fs&#39;).readFileSync(&#39;./test/keystore/pass&#39;).toString();<br>var wallet = require(&#39;ethereumjs-wallet&#39;).fromV3(keystore, pass);</pre><pre>//var pkey_str = require(&#39;fs&#39;).readFileSync(&#39;./keystore/pkey&#39;).toString();<br>//var prkey_buff = new Buffer(pkey_str, &#39;hex&#39;)<br>//var wallet = require(&#39;ethereumjs-wallet&#39;).fromPrivateKey(prkey_buff)<br> <br>module.exports = {<br>  networks: {<br>    development: {<br>      host: &quot;localhost&quot;,<br>      port: 9545,<br>      network_id: &quot;*&quot; // Match any network id <br>    },<br>    ropsten: {<br>      provider: new WalletProvider(wallet, &quot;<a href="https://ropsten.infura.io/cpokRXa96X1xQ48pv841">https://ropsten.infura.io/cpokRXa96X1xQ48pv841</a>&quot;),<br>      network_id: 3,<br>      gas: 4700000<br>    }<br>  },<br>};</pre><p>See how keystore and pass variables are assigned using your private key json and a password file. This account would be an owner and used to deploy the contract. Alternative method is to login using raw private key — fromPrivateKey()</p><p>Note, how we define two networks. Ropsten network uses WalletProvider with wallet variable and infura.io endpoint as inputs. Also gas: 4700000 is a gas limit usage for deployment transaction.</p><p>All set.</p><h4><strong>We are good to go live on a network!</strong></h4><p>Fire up truffle as truffle console --network ropsten. Run migrate --network ropsten --reset.</p><p>Since we deploy on live network, interweaving our contrat into the fabric of the blockchain would take some time…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*9xAZthaC20cifsTgquzhyA.png" /><figcaption>Check your gas amount.</figcaption></figure><p>Oops! Something went wrong. Error message suggests to check gas amount. However we set our gas limit to 4.7M which is maximum on ropsten… Let’s lookup details at <a href="https://ropsten.etherscan.io/">ropsten etherscan</a>. <em>Copying transaction hash and pasting it in the etherscan displays</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*T2qnlVBSx5yI7keArky4Pg.png" /><figcaption>Out of gas during deployment.</figcaption></figure><p>Too bad! Our contract is way too large and consumes gas beyond limit. What are the solutions then? Theoretically we could make it simplier and diminish in size, but we would lose security and necessary functionality. Another solution would be to split contracts and deploy separately, which would take additional effort to link contracts… hmm.</p><p>There is a better solution —<strong> enable solc optimizer</strong>, which essentially would ‘squeeze’ our contract in size. Add the following to the <strong>truffle.js, </strong>just before the last curly bracket:</p><pre>solc: {<br>   optimizer: {<br>     enabled: true,<br>     runs: 200<br>   }<br>  }</pre><p>Now back to another migration attempt! First you might want to delete build directory from your project and compile again: compile, followed by migration:migrate --network ropsten --reset</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/895/1*dgRckVxaGCW2r-Pdmlb9rw.png" /><figcaption>Successful Migration on testnet.</figcaption></figure><p>Success!</p><p>Our contract is live! It costs roughly 2.9M gas, which is good. Notice that Gas price on Ropsten is set to 100 Gwei by default, on mainnet it makes sense to define it 10 or even 5 Gwei (<a href="https://ethgasstation.info/">check with gas station prices</a>). So, preliminary estimation is that real network deployment of our contract would cost about 0.03 Eth.</p><p>Again, run lite server locally to interact with the contract on Ropsten blockchain: npm run dev. Just make sure that your client (be it Metamask, Mist or Cryptonite) is connected to Ropsten.</p><p><strong>And we did it! Congratulations!</strong></p><p>Now you have solid understanding how Smart Contracts work and have a good basis to develop dApp by yourself!</p><p>Chime in with comments, questions and suggestions! I’d be happy to answer.</p><p>Cheers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15ed139cde06" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>