本文已参与「新人创作礼」活动,一起敞开掘金创作之路

接上文/post/710134…

完善交互

最主要的功用后端也算是完成了,完善一下功用
1、显现水龙头剩余余额
2、显现地址、时刻、获得多少币

1、交互

//获取剩余的ETH
func GetBlance(client *ethclient.Client)(*big.Int,error){
   ins,err := HaveFacet(client)
   blance_val,err:=ins.TranContract(nil)
   if err != nil {
      log.Fatal(err)
   }
   return  blance_val, nil
}

2、衔接mysql

1、引包

package db
import (
   "database/sql"
   "fmt"
   "math/big"
   "time"
   _ "github.com/go-sql-driver/mysql"  //引进mysql,运用_代替途径
)

2、初始化

var db *sql.DB
func Init() (err error) {
   // 翻开mysql数据库
   fmt.Println("翻开数据库")
   //root:mysql用户名   password:数据库密码
   db, err = sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/faucet?charset=utf8&parseTime=true")
   if err != nil {
      panic(err)
   }
   err = db.Ping() //衔接数据库
   if err != nil {
      panic(err)
      fmt.Print("数据库衔接失利")
   }
   fmt.Println("已经衔接MYSQL")
//创建表格
   var sqlStr = `CREATE TABLE IF NOT EXISTS faucet(
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      account VARCHAR(100) NULL,
        timeNow datetime,
      created DATETIME DEFAULT CURRENT_TIMESTAMP
   );`
   _, err = db.Exec(sqlStr)
   if err != nil {
      return err
   }
   return
   }

sql文件如下:(含部分数据)

/*
 Navicat Premium Data Transfer
 Source Server         : faucet
 Source Server Type    : MySQL
 Source Server Version : 80024
 Source Host           : localhost:3306
 Source Schema         : faucet
 Target Server Type    : MySQL
 Target Server Version : 80024
 File Encoding         : 65001
 Date: 29/12/2021 15:18:59
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for faucet
-- ----------------------------
DROP TABLE IF EXISTS `faucet`;
CREATE TABLE `faucet`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `account` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `timeNow` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of faucet
-- ----------------------------
INSERT INTO `faucet` VALUES (7, '0x888224e5653fCBDB34e442929ED789556d078bD0', '2021-12-29 03:36:37');
INSERT INTO `faucet` VALUES (8, '0x443e3040387477f1B51e508cC1F1EFf438122bD7', '2021-12-29 06:57:18');
-- ----------------------------
-- Table structure for statistics
-- ----------------------------
DROP TABLE IF EXISTS `statistics`;
CREATE TABLE `statistics`  (
  `id` int(10) UNSIGNED ZEROFILL NOT NULL,
  `ETHAmount` int(0) NULL DEFAULT NULL,
  `limitTimes` int(0) NULL DEFAULT NULL,
  `
residue` int(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of statistics
-- ----------------------------
INSERT INTO `statistics` VALUES (0000000001, 4, NULL, NULL);
SET FOREIGN_KEY_CHECKS = 1;

3、结构体模板

package db  //db文件夹下
import "time"
type FAUCET_yuanshuo struct {
   ID      int       `sql:"id" json:"id" form:"id"`                // 唯一标识
   Account string    `sql:"account" json:"account" form:"account"` // 请求账户
   Timenow time.Time `sql:"timeNow" json:"timeNow" form:"timeNow"` // 请求的时刻
}
type Statistics_yuanshuo struct {
   id        int    `sql:"id" json:"id" form:"id"`                         // 唯一标识
   ethamount int    `sql:"ETHAmount" json:"ETHAmount" form:"ETHAmount"`    // 发送ETH总额
   limitimes int    `sql:"limitTimes" json:"limitTimes" form:"limitTimes"` // 请求次数
   res       string `sql:"res json:"res" from:"res"`                       //水龙头余额
}