How To Create Token and Initial Coin Offering Contracts Using Truffle + Zeppelin Solidity

2023-11-03

Token contracts are hot. Token crowd sales aka Initial Coin Offers(ICO) are hotter. There have been a lot going in terms of ICOs lately in the crypto world and if you would like to code one yourself, look no further. In this blog post I am going to go through the steps to create your own and only token as well as the ICO contract for it. We will also use the help of some clever tools.

Caveat: You should not actually release your ICO with the code found here. It is for demo purposes only. But it is a good start.

For this tutorial I am going to be using Truffle and Zeppelin Solidity. Truffle is the de facto framework for creating smart contracts and decentralized applications. Zeppelin Solidity is a library that has extensive and well tested smart contracts that adhere to security best practices. In the smart contract world where a small bug can cost you money, it is good not to reinvent the wheel when there are trusted solutions out there.

We will also use ganache-cli as the blockchain node because it is fast and developer friendly.

Side note: If you want to actually create the contract on the Ethereum testnet or main net you would have to use something like Geth or Parity.

Moving on: provided that you have npm and node installed, from terminal type the commands:

$ npm install -g ganache-cli
$ npm install -g truffle
$ mkdir my-ico && cd my-ico
$ truffle init
$ npm install zeppelin-solidity@1.7.0

A lot of magic happened with those commands above. But in essence it is the setup that will get you up and running with creating and deploying your ICO smart contract.

This adds the zeppelin-solidity folder to node_modules and in it you will find all smart contract templates from the Zeppelin Solidity library.

First step is to create the token contract.

touch contracts/GustavoCoin.sol

And for the code:

How simple is that?!

That is a straightforward token contract. We practically let Zeppelin Solidity smart contract templates do all the heavy duty for us. Here we reference MintableToken . The idea is to have a token where the supply is controlled by an owner who can emit tokens and assign them. For a better look at what this contract does, check it at node_modules/zeppelin-solidity/contracts/token/ERC20/MintableToken.sol .

The following step is to create the Crowdsale contract.

touch contracts/GustavoCoinCrowdsale.sol

We are basically going to inherit the contract at node_modules/zeppelin-solidity/contracts/crowdsale/emission/MintedCrowdsale.sol and node_modules/zeppelin-solidity/contracts/crowdsale/validation/TimedCrowdsale.sol. We end up with only the code below.

Beautiful, huh? We are using the secure contracts provided by Zeppelin Solidity to our advantage. Note that GustavoCoinCrowdsale inherits from TimedCrowdsale and MintedCrowdsale. In order to deploy GustavoCoinCrowdsale , we must give a few parameters to its constructor function as per the Crowdsale and TimeCrowdsale contracts, i.e. openingTimeand closingTime timestamps, the rate of token per ether rate, the tokenaddress itself and the wallet address of the contract owner(s).

Let’s deploy this contract. Open a new terminal tab and run

$ ganache-cli

It will run ganache-cli. We are using it for our development needs.

First, let’s add this to truffle.js file. It is for configuration purposes:

truffle.js:
module.exports = {
    networks: {
        development: {
            host: "localhost",
            port: 8545,
            network_id: "*" // Match any network id
        }
    }
};

Then, go on (you may need to create it) to the file migrations/2_deploy_contracts.js and modify it to this:

Note that we are not deploying GustavoCoin . This is because once GustavoCoinCrowdsale deploys it will create GustavoCoin . Now back to the terminal tab where you installed Truffle, run the commands:

$ truffle compile
$ truffle migrate

If you happen to look at the testrpc tab you will see that the contract was deployed successfully.

Alright, let’s buy some GUS tokens.

Run $ truffle console

This will open truffle console and we are going to use the web3.js API to interact with the deployed contract.

// The account that will buy GUS tokens.
> purchaser = web3.eth.accounts[2]
'0xddac5d057c79facd674bc95dfd9104076fd34d6b'
// The address of the GUS token instance that was created when the crowdsale contract was deployed
// assign the result of GustavoCoinCrowdsale.deployed() to the variable crowdsale
> GustavoCoinCrowdsale.deployed().then(inst => { crowdsale = inst })
> undefined
> crowdsale.token().then(addr => { tokenAddress = addr } )
> tokenAddress
'0x87a784686ef69304ac0cb1fcb845e03c82f4ce16'
> gustavoCoinInstance = GustavoCoin.at(tokenAddress)
// change token ownership to crowdsale so it is able to mint tokens during crowdsale
> gustavoCoinInstance.transferOwnership(crowdsale.address)
Now check the number of GUS tokens purchaser has. It should have 0
gustavoCoinInstance.balanceOf(purchaser).then(balance => balance.toString(10))
'0'
// Buying GUS tokens
> GustavoCoinCrowdsale.deployed().then(inst => inst.sendTransaction({ from: purchaser, value: web3.toWei(5, "ether")}))
{ tx: '0x68aa48e1f0d0248835378caa1e5b2051be35a5ff1ded82878683e6072c0a0cfc',
  receipt:
   { transactionHash: '0x68aa48e1f0d0248835378caa1e5b2051be35a5ff1ded82878683e6072c0a0cfc',
     transactionIndex: 0,
     blockHash: '0xb48ceed99cf6ddd4f081a99474113c4c16ecf61f76625a6559f1686698ee7d57',
     blockNumber: 5,
     gasUsed: 68738,
     cumulativeGasUsed: 68738,
     contractAddress: null,
     logs: [] },
  logs: [] }
undefined
// Check the amount of GUS tokens for purchaser again. It should have some now.
> gustavoCoinInstance.balanceOf(purchaser).then(balance => purchaserGusTokenBalance = balance.toString(10))
'5000000000000000000000'
// When we created our token we made it with 18 decimals, which the same as what ether has. That's a lot of zeros, let's display without the decimals:
> web3.fromWei(purchaserGusTokenBalance, "ether")
'5000'

Yay! It worked.

Where to go after here you ask?

You could test this code on the Ethereum testnet, or you could create a web app that would allow users to interact with the CrowdsaleToken contract, you could add more functionality to the contract itself such as deadline, token cap, etc. Unleash your inner token creator.

The Ethereum community grows by the day and though there are not many tools out there yet, the ones such as Truffle and Zeppelin Solidity do a great job at improving developer’s experience.

I am curious to know what you will be coding from here.

By the way, here is the repo containing the code for this blog post.

I want to leave my gratitude for Francisco Giordano for reviewing the code used in this blog post.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

How To Create Token and Initial Coin Offering Contracts Using Truffle + Zeppelin Solidity 的相关文章

  • 区块链职业培训任重道远,四个方向可入行

    作者 赛联区块链教育 张群 区块链已经来到世界14年了 中国成为重大战略也三年了 这两年区块链产业发展十分迅猛 以北京 杭州 上海 深圳 重庆 成都为代表的的区块链产业迅速崛起 成为中国区块链发展的领军者 由于在教育圈的原因 最近几年和区块
  • Solidity transfer,call和send 的区别

    address transfer throws on failure forwards 2 300 gas stipend not adjustable safe against reentrancy should be used in m
  • 使用web3 部署智能合约

    CentOS 7 环境 web3安装 及 对象的创建 m0 47233175的博客 CSDN博客https blog csdn net m0 47233175 article details 121960931还未安装web3环境 请参照以
  • 元宇宙不是Web3

    个人对元宇宙的定义为 大规模 可互操作的网络 能够实时渲湘3D虚拟世界 借助大量连使性数据 如身份 历史 权利 对象 通信和支付等 可以让无限数量的用户体脸实时同步和持续有效的在场感 现在 你应该能理解我为何会给出这样的定义了 许多人可能会
  • 深入理解Solidity——创建合约

    Solidity的合约类似于面向对象语言中的类 它们包含存放持久化数据的状态变量和可修改这些变量的函数 调用不同的合约实例上的函数将执行EVM函数调用 从而切换上下文 使得状态变量不可访问 创建合约 Creating Contracts 合
  • Web3 用例全解析:传统品牌加速进入 Web3 的原因?

    Web3 有能力彻底改变品牌和客户相互联系的方式 许多品牌已经在尝试使用 NFT 和元宇宙来提高品牌知名度和消费者忠诚度 这是传统社交媒体和电子商务渠道根本无法做到的 NIKE Panini 和 Vodafone nbsp 是最早认识到 N
  • 区块链智能合约开发学习

    最近正在肝区块链知识学习 入手学习智能合约的开发 由于网上资料实在是太少了 好不容易东拼西凑完成了智能合约的开发 编译 部署 web3js调用 网页页面 和web3j调用 java调用 赶紧趁热把重点提炼出来 先上图 是我最近学习知识点的一
  • Solidity 合约安全,常见漏洞 (下篇)

    Solidity 合约安全 常见漏洞 下篇 Solidity 合约安全 常见漏洞 上篇 不安全的随机数 目前不可能用区块链上的单一交易安全地产生随机数 区块链需要是完全确定的 否则分布式节点将无法达成关于状态的共识 因为它们是完全确定的 所
  • 智能合约之短地址攻击

    在了解以太坊智能合约短地址攻击之前 先要简单了解一下以太坊代币ERC 20 TOKEN 的一些基础知识 ERC EthereumRequest for Comment 即以太坊通用征求意见协议 开发者可以通过提交EIP Ethereum I
  • 以太坊开发者工具的最新清单

    以太坊开发者工具的最新终极清单 用于在以太坊上开发应用程序的可用工具 组件 框架和平台的指南 对于任何开发者 无论你是一个睁大眼睛的Web3新手还是一个头发灰白的OG加密无政府主义技术霸主 Github都是你的朋友 特别是ConsenSys
  • 014.Solidity入门——01数据类型

    数据类型是编写智能合约的基础 Solidity支持多种数据类型 包括基本数据类型 数组 结构体 枚举 映射等 基本数据类型包括 bool 布尔型 true或false int uint 整型 可以表示正负整数 int 或非负整数 uint
  • 以太坊Python智能合约开发指南

    在以太坊上获得一个基本的智能合约是一个很简单的事 只需google查询 ERC20代币教程 你会发现有关如何做到这一点的大量信息 以编程方式与合约交互完全是另一回事 如果你是一个Python程序员 那么教程就很少 所以写这个Python中的
  • stop容器

    docker ps 查看所有正在运行容器 docker stop containerId containerId 是容器的ID docker ps a 查看所有容器 docker ps a q 查看所有容器ID docker stop do
  • 从 sCrypt 智能合约中访问区块链数据(5)

    在本系列前几部分奠定的基础上 在本文中将演示如何在 BSV 中实现相对锁定时间 而无需新的操作码 OP CheckSequenceVerify 时间锁 时间锁会限制某些 BSV 的支出 直到指定的未来时间或区块高度 有两种类型的时间锁 绝对
  • 搭建第一个Dapp应用(4)——搭建SmartDev-Scaffold——2021.5.3

    搭建第一个Dapp应用 4 搭建SmartDev Scaffold 一丶环境配置 Java gt JDK 1 8 Solidity 0 4 25 Git 下载安装包需要使用Git Gradle 大于6 小于7 使用gradle7会报错 二丶
  • 【元宇宙】智能手机万岁

    凭借出色的新设备 我们很快就能进人元字宙 想象这样的情景是很趣的 但是 至少到21世纪20年代 元宇宙时代的大多数设备很可能是我们已经在使用的设备 AR 和 VR 设备不仅面临重大的技术 财务和体验障碍 而且它们在上市后同样会面临币场反响冷
  • 区块链之java调用智能合约(二)部署智能合约

    前言 上一节 已经说过 如何的创建一个合约 如何编译合约 然后在java中调用 但是呢 这些还远远不够 那么还差哪些呢 现在就是如何将创建的智能合约部署的对应的共链 私链 测试链中了 需要部署后 才能真正的使用 现在就讲讲如何部署智能合约
  • 网络化,元宇宙的门槛

    如果森林中的一棵树倒下 但周围没有人听到 那它是否会发出声音 这一思想实验可以追溯到数百年前 这个实验之所以经久不衰 部分原因是它很有趣 而它之所以很有趣 是因为它耐人寻味并且融人了哲思 人们通常认为 上面这个问题最初是由主观唯心主义哲学家
  • Go语言实现区块链与加密货币-Part3(交易优化,单机模拟多节点通信)

    交易 二 在这个系列文章的一开始 我们就提到了 区块链是一个分布式数据库 不过在之前的文章中 我们选择性地跳过了 分布式 这个部分 而是将注意力都放到了 数据库 部分 到目前为止 我们几乎已经实现了一个区块链数据库的所有元素 今天 我们将会
  • 探索元宇宙链游戏:一场数字世界的奇妙融合

    随着互联网的飞速发展 以及人们不断对互动娱乐体验的要求提高 元宇宙渐渐成为人们追求的目标 而区块链技术的出现给元宇宙链游开发带来了新的机遇和挑战 一 元宇宙链游定义 元宇宙链游全称为基于区块链技术的元宇宙游戏 是一种新型的网络互动娱乐形式

随机推荐

  • TensorRT基于caffe模型加速MobileNet SSD

    TensorRT加速MobileNet SSD分解为三个问题 1 图像的预处理 2 结果的处理 3 depthwise convolution层的实现 针对1 图像预处理我就不多言了 针对2 结果处理 由于tensorRT中有SSD的det
  • mysql建表语句

    工作的时候总会写一些建表语句提交给db 有的时候就会忘记主键自增写法 以及一些类型的标注 下面是一个比较全的建表语句 包括各种类型 create table minisite lock site id int not null auto i
  • 使用OpenGL实现计算着色器

    使用OpenGL实现计算着色器 在OpenGL中 我们可以通过计算着色器来实现各种计算任务 计算着色器不同于传统的图形着色器 它只关注数据的计算 而无需输出任何图形 下面我们将以一个简单的示例为例 展示如何使用OpenGL实现计算着色器 首
  • 计算机图形学:光线追踪原理(ray tracing)

    现实中的光学模型 在现实中 当关闭屏幕光亮的时候 会看到屏幕反射到的一张帅气或者漂亮的脸 那么这个看到的物体就是从屏幕中反射出来 其实不光镜子 任何物体都存在反射 发光源发出光线 通过物体反射到人的眼睛中 这里要提出另一个概念 就是图形光栅
  • 「Python|音视频处理|场景案例」如何获取音视频中声音片段的起止时间?

    本文主要介绍如何使用python的第三方库moviepy找出音视频中声音开始出现的位置以及声音结束的位置 文章目录 场景描述 准备工作 解决方案 源代码 场景描述 假设我们有一段音频 音频开始有一段无声片段 音频结束也有一段无声片段 我们需
  • 问题解决:The project cannot be built until build path errors are resolved

    http blog csdn net marty zhu article details 2566299 1 看看project Build Automatically有没有勾上 如果没有 勾上以后 clean一下 重启eclipse 2
  • 无法连接到Windows服务——Windows无法连接到SENS服务

    前言 小编的电脑不知道发生了什么事情 突然变成了这个样子 开机本来十几秒 现在突然变成一分钟了 还有点不适应了 这也就罢了 他竟然还给我断了网 问题描述为 无法连接到Windows服务 因为没有联网 所以小编拍了张照片记录如下 不是很清晰
  • springcloud maven 打jar包 build配置

    父工程pom配置
  • 强化学习学习笔记(二):入门例子

    内容来自莫烦Python的教程 很适合入门理解 首先附上例子的代码 coding utf 8 import numpy as np import pandas as pd import time np random seed 2 repro
  • Jmeter 几个常见元素说明

    配置元件 脚本中格式 数据进行设置 统一管理 例如 http 请求默认值 定时器 控制线程如何执行测试 操作和操作之间等待时间 比如同步定时器 用来实现真正的 并发 前置处理器 用于实际请求发出之前对即将发生的请求进行特殊处理 后置处理器
  • python中实现分页

    分页使用 比如说在Python的django框架中使用 目录 分页使用比如说在Python的django框架中使用 1 先导入PageNumberPagination变量 2 自己定义的视图引用该变量 3 写默认每页信息条数和每页显示的最大
  • iwebsec靶场 文件包含漏洞通关笔记10-data伪协议利用

    目录 前言 1 data伪协议 2 使用条件 第10关 data 伪协议利用 1 打开靶场 2 源码分析 3 渗透 1 明文渗透 2 base64编码渗透 前言 1 data伪协议 data协议和input协议差不多 指定data是get方
  • chatgpt赋能python:如何放大Python界面?

    如何放大Python界面 Python的用户界面可能有时候会显得太小或太难看 因此 许多用户想要放大Python界面 这篇文章介绍了一些方法来放大Python界面 方法一 使用Tkinter库 Tkinter是Python中用来创建GUI应
  • IPv6地址简介

    今天继续给大家介绍IPv6 本文主要介绍IPv6的地址 一 IPv6地址格式 IPv6地址有128bit 在这128bit中 前64bit是网络前缀 后64bit是接口标识 在前64bit中 前48bit又是全球可汇总地址 在给一个公司分配
  • hal层修改屏幕亮度

    平台 rk3568 系统 Android11 我的板子有两块屏幕 而且背光用的不是同一个PWM来控制 所以要修改上层添加对另外一个屏幕亮度的调节 好了话不多少先找到在hardware rockchip light aidl Lights c
  • 导入keras包时,彻底解决Using TensorFlow backend问题(2022最新解决方案)

    说明 19 20 21年份搜到的方法都试了 都没用 改导包语句 降版本 重安装 直接跳过 下载vs插件 等等 结果依然如下图 解决方案 1 担心在原环境中装包卸载包容易搞混原环境 可以重新单独建立个tensorflow环境 2 安装最新版的
  • caffe中batch_norm层代码详细注解

    caffe中batch norm层代码注解 一 BN的解释 在训练深层神经网络的过程中 由于输入层的参数在不停的变化 因此 导致了当前层的分布在不停的变化 这就导致了在训练的过程中 要求 learning rate 要设置的非常小 另外 对
  • 目标检测——概述

    学习视觉与深度学习有一年了 想把以前看到的文章和资料整理一下 不知道从哪些开始 这个暑假 趁着刚刚开完题 稍微闲一些 把目标检测相关的经典论文逐一记录一下 有时间跑一下作者的原码 水平有限 如涉及的内容有误 恳请大家指出 前言 计算机视觉领
  • 软件测试知识点总结(五)——动态测试

    一 白盒测试 白盒 测试又称为结构测试或逻辑驱动测试是一种按照程序内部逻辑结构和编码结构设计测试数据并完成测试的一种测试方法 一般分为静态测试和动态测试 测试方法 l 语句覆盖 要求每一条语句至少执行一次 l 判定覆盖 要求每一条分支都要至
  • How To Create Token and Initial Coin Offering Contracts Using Truffle + Zeppelin Solidity

    Token contracts are hot Token crowd sales aka Initial Coin Offers ICO are hotter There have been a lot going in terms of