### 前言 剩下来的内容并不难,很简单,遇到的坑也是上一个文章有的。 [post cid="522" cover="" size=""/] --- #### Eth代币转账 ``` // 使用教程的测试网 https://goethereumbook.org/zh/transfer-tokens/ client, _ := ethclient.Dial("https://rinkeby.infura.io") // 因为代币,以太坊交易值改为0 https://tokenfactory.surge.sh EthValue := big.NewInt(0) // 使用文章地址,防止出现其他问题 toAddress := common.HexToAddress("0x4592d8f8d7b001e72cb26a73e4fa1806a51ac79d") // 代币合约地址 tokenAddress := common.HexToAddress("0x28b149020d2152179873ec60bed6bf7cd705775d") // 函数方法切片传递 transferFnSignature := []byte("transfer(address,uint256)") // 生成签名hash hash := sha3.New384() hash.Write(transferFnSignature) // 切片 methID := hash.Sum(nil)[:4] // 填充地址 paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32) // 发送代币数目 Number := new(big.Int) Number.SetString("1000000000000000000000", 10) // 代币量填充 var data []byte data = append(data, methID...) data = append(data, paddedAddress...) data = append(data, common.LeftPadBytes(Number.Bytes(), 32)...) // 使用方法估算燃气费 gasLime, _ := client.EstimateGas(context.Background(), ethereum.CallMsg{ To: &toAddress, Data: data, }) logrus.Info("Ethereum GasLime:", gasLime) // 进行交易事务 tx := types.NewTx(&types.LegacyTx{ Nonce: 246, GasPrice: EthValue, Gas: gasLime, To: &tokenAddress, Value: EthValue, Data: make([]byte, 0), }) // 这里的转账和eth的转账一样,就不多说了 logrus.Info(tx) ``` 值得注意的是: 你因为是代币转账,相当于将一个代币转移。 而代币地址可以看作是另外一个钱包,所以与前面的eth转账差别并不大。 #### 监听新区块 ``` // 这个地址原本免费,现在需要注册获取token,请自己更改 client, _ := ethclient.Dial("wss://eth-mainnet.ws.alchemyapi.io/ws/") // 创建通道 heads := make(chan *types.Header) // 订阅返回的对象 sub, _ := client.SubscribeNewHead(context.Background(), heads) // 循环监听 for { select { case err := <-sub.Err(): // 监听区块失败的信息 logrus.Error("SubScribe Block Error:", err) case Heads := <-heads: logrus.Info("Heads Address:", Heads.Hash().Hex()) // 监听区块头信息 block, _ := client.BlockByHash(context.Background(), Heads.Hash()) logrus.Info("Block Address:", block.Hash().String()) } } ``` 要注意:ws监控地址已经更换了,而且需要在ethereum官网申请api才可以使用 [ethereum的Ws订阅操作](https://ethereum.org/zh/developers/tutorials/using-websockets/) --- #### 发送裸交易 ``` // 这个原始url也需要最新的了 client, _ := ethclient.Dial("https://rinkeby.infura.io") // 构建原始交易(裸交易)和之前的eth货币交易差不多,就不管了。 rawTx := "f86d8202b28477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ca05924bde7ef10aa88db9c66dd4f5fb16b46dff2319b9968be983118b57bb50562a001b24b31010004f13d9a26b320845257a6cfc2bf819a3d55e3fc86263c5f0772" RawTX, _ := hex.DecodeString(rawTx) // 构建原始事务 tx := new(types.Transaction) rlp.DecodeBytes(RawTX, &tx) // 广播说一哈 if err := client.SendTransaction(context.Background(), tx); err != nil { logrus.Error("SendTransaction Error:", err) return } logrus.Info(tx.Hash().Hex()) ``` Loading... ### 前言 剩下来的内容并不难,很简单,遇到的坑也是上一个文章有的。 <div class="preview"> <div class="post-inser post box-shadow-wrap-normal"> <a href="https://www.yuhenm.top/archives/522.html" target="_blank" class="post_inser_a no-external-link no-underline-link"> <div class="inner-image bg" style="background-image: url(https://www.yuhenm.top/usr/uploads/2022/10/2043503310.png);background-size: cover;"></div> <div class="inner-content" > <p class="inser-title">eth转账, 查询区块, 查询交易, Golang 开发以太坊 第二章上</p> <div class="inster-summary text-muted"> 前言因为Golang的以太坊协议更新了,所以耽误了些功夫,太草了Github地址:Ethereum_GolangS... </div> </div> </a> <!-- .inner-content #####--> </div> <!-- .post-inser ####--> </div> --- #### Eth代币转账 ``` // 使用教程的测试网 https://goethereumbook.org/zh/transfer-tokens/ client, _ := ethclient.Dial("https://rinkeby.infura.io") // 因为代币,以太坊交易值改为0 https://tokenfactory.surge.sh EthValue := big.NewInt(0) // 使用文章地址,防止出现其他问题 toAddress := common.HexToAddress("0x4592d8f8d7b001e72cb26a73e4fa1806a51ac79d") // 代币合约地址 tokenAddress := common.HexToAddress("0x28b149020d2152179873ec60bed6bf7cd705775d") // 函数方法切片传递 transferFnSignature := []byte("transfer(address,uint256)") // 生成签名hash hash := sha3.New384() hash.Write(transferFnSignature) // 切片 methID := hash.Sum(nil)[:4] // 填充地址 paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32) // 发送代币数目 Number := new(big.Int) Number.SetString("1000000000000000000000", 10) // 代币量填充 var data []byte data = append(data, methID...) data = append(data, paddedAddress...) data = append(data, common.LeftPadBytes(Number.Bytes(), 32)...) // 使用方法估算燃气费 gasLime, _ := client.EstimateGas(context.Background(), ethereum.CallMsg{ To: &toAddress, Data: data, }) logrus.Info("Ethereum GasLime:", gasLime) // 进行交易事务 tx := types.NewTx(&types.LegacyTx{ Nonce: 246, GasPrice: EthValue, Gas: gasLime, To: &tokenAddress, Value: EthValue, Data: make([]byte, 0), }) // 这里的转账和eth的转账一样,就不多说了 logrus.Info(tx) ``` 值得注意的是: 你因为是代币转账,相当于将一个代币转移。 而代币地址可以看作是另外一个钱包,所以与前面的eth转账差别并不大。 #### 监听新区块 ``` // 这个地址原本免费,现在需要注册获取token,请自己更改 client, _ := ethclient.Dial("wss://eth-mainnet.ws.alchemyapi.io/ws/") // 创建通道 heads := make(chan *types.Header) // 订阅返回的对象 sub, _ := client.SubscribeNewHead(context.Background(), heads) // 循环监听 for { select { case err := <-sub.Err(): // 监听区块失败的信息 logrus.Error("SubScribe Block Error:", err) case Heads := <-heads: logrus.Info("Heads Address:", Heads.Hash().Hex()) // 监听区块头信息 block, _ := client.BlockByHash(context.Background(), Heads.Hash()) logrus.Info("Block Address:", block.Hash().String()) } } ``` 要注意:ws监控地址已经更换了,而且需要在ethereum官网申请api才可以使用 [ethereum的Ws订阅操作](https://ethereum.org/zh/developers/tutorials/using-websockets/) --- #### 发送裸交易 ``` // 这个原始url也需要最新的了 client, _ := ethclient.Dial("https://rinkeby.infura.io") // 构建原始交易(裸交易)和之前的eth货币交易差不多,就不管了。 rawTx := "f86d8202b28477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ca05924bde7ef10aa88db9c66dd4f5fb16b46dff2319b9968be983118b57bb50562a001b24b31010004f13d9a26b320845257a6cfc2bf819a3d55e3fc86263c5f0772" RawTX, _ := hex.DecodeString(rawTx) // 构建原始事务 tx := new(types.Transaction) rlp.DecodeBytes(RawTX, &tx) // 广播说一哈 if err := client.SendTransaction(context.Background(), tx); err != nil { logrus.Error("SendTransaction Error:", err) return } logrus.Info(tx.Hash().Hex()) ``` 最后修改:2023 年 03 月 26 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏