6.ERC20合约调用

本文介绍了如何实现一个简单的ERC20代币水龙头合约,用于在以太坊的Rinkeby测试网上分配测试Token。首先,定义了ERC20合约的基本接口,包括转移、批准和转账等功能。接着,创建了ERC20Faucet合约,该合约设置了一个固定的代币地址,并提供了getTokens函数来调用ERC20合约进行转账。示例中给出了部署合约和交易的链接,以及Bancor协议的相关资料。
摘要由CSDN通过智能技术生成

写于 2019

合约的调用

在这里插入图片描述

// Log represents a contract log event. These events are generated by the LOG opcode and
// stored/indexed by the node.
type Log struct {
	// Consensus fields:
	// address of the contract that generated the event
	Address common.Address `json:"address" gencodec:"required"`
	// list of topics provided by the contract.
	Topics []common.Hash `json:"topics" gencodec:"required"`
	// supplied by the contract, usually ABI-encoded
	Data []byte `json:"data" gencodec:"required"`

	// Derived fields. These fields are filled in by the node
	// but not secured by consensus.
	// block in which the transaction was included
	BlockNumber uint64 `json:"blockNumber"`
	// hash of the transaction
	TxHash common.Hash `json:"transactionHash" gencodec:"required"`
	// index of the transaction in the block
	TxIndex uint `json:"transactionIndex" gencodec:"required"`
	// hash of the block in which the transaction was included
	BlockHash common.Hash `json:"blockHash"`
	// index of the log in the block
	Index uint `json:"logIndex" gencodec:"required"`

	// The Removed field is true if this log was reverted due to a chain reorganisation.
	// You must pay attention to this field if you receive logs through a filter query.
	Removed bool `json:"removed"`
}

实现一个简单的 ERC20 代币水龙头

/*
author: yqq
data: 2019-09-06 12:33
desc: call erc20 contranct to transfer
*/

contract ERC20 {
    function totalSupply() public constant returns (uint);
    function balanceOf(address tokenOwner) public constant returns (uint balance);
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);
    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}



contract ERC20Faucet{
  address token_addr = 0xe30d2B6F144e6eAD802414C09a3F519B72Ff12F3; 

  function ERC20Faucet() public{
  }

  function getTokens(address to, uint256 value ) public {
    ERC20(token_addr).transfer(to, value );
  }
}

部署水龙头合约成功:

https://rinkeby.etherscan.io/tx/0x1fd57e172662f0981f601a1c1dd7527ae3ee307b8b36db0f97b4d4b7d8985133

向合约地址转一些测试Token:

https://rinkeby.etherscan.io/tx/0xb44d99315004431f1a15d89383d2a565a58029e30b382999650884976873581c

从水龙头获取测试Token:

https://rinkeby.etherscan.io/tx/0x57004ad1812036e6802ace93e71c02c1f071cee99d4a408c6ff45712a35a8151

Bancor 协议

https://www.jianshu.com/p/7a535407d732
https://www.jianshu.com/p/c96100c60f9a
https://etherscan.io/address/0x1f573d6fb3f13d689ff844b4ce37794d79a7ff1c
https://mp.weixin.qq.com/s?__biz=MzUyNjcwMjY3NQ==&mid=2247484542&idx=1&sn=3981d497f33c0511f1b5d04df790fdc7&scene=21#wechat_redirect

https://etherscan.io/tx/0xf5983f143afe39c50c57b774fb41e527ec63ab5777afe0455b8d7aea635983a9

【技术背景】区块链,是一个分布式的共享账本和数据库,具有去中心化、不可篡改、可追溯、公开透明等特点。区块链技术作为科技创新的代表和未来技术的发展方向,已经上升至国家战略高度。它将为解决信息不对称问题、创造信任与合作机制等提供丰富的应用空间,也会是未来我们技术自主创新、引领产业变革的重要突破口。比特币被认为是区块链技术1.0版的应用,主要实现的是电子现金的分布式记账转账功能。而随着技术的不断发展更新,越来越多的人希望突破“账本”的限制,从而可以把这项未来技术应用在更广阔的领域。以太坊(Ethereum)为代表的第二代区块链公链项目,就是其中的佼佼者。与比特币不同,以太坊的定位是一个“世界计算机”。以区块链作为底层存储技术,我们不仅可以记账转账,而且可以构建“智能合约”(smart contract)定义程序化的处理流程,进而实现区块链上运行的“去中心化应用”(DApp)。以太坊项目自提出后就受到了广泛关注,快速的发展和壮大,而且由于其“分布式应用平台”而非“分布式账本”的定位,越来越多的开发人员开始以以太坊为基础设施,在上面开发DApp。随着更多开发人员的参与,和项目的逐步落地,以太坊已成为从事区块链学习和开发不可或缺的一个环节;既了解区块链底层原理、又熟悉以太坊架构、还能基于以太坊开发DApp的专业人才,也成为了各大公司发力区块链技术储备的重点对象。【课程简介】本套以太坊课程,对以太坊基础理论知识和架构做了系统的梳理和深入的阐述,并对solidity和DApp的开发做了系统讲解,另外还对以太坊白皮书、黄皮书做了介绍;为有志于学习区块链技术、了解以太坊底层架构和DApp开发原理的工程师提供学习平台和帮助。本教程内容主要分为五大部分:以太坊基础、以太坊原理和架构、以太坊编程及应用、合约工作流以及原理深入分析。通过学习本套课程,可以使学习者对以太坊有充分的认识,对整个区块链技术有更深刻的理解,对区块链应用开发有更加整体的领悟。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值