Commit 898137e5 authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

update

parent 92b7a726
......@@ -21,6 +21,7 @@ type ChainConfig struct {
GasSenderPrivateKey string `toml:"gas_sender_private_key"`
ExecutorPrivateKey string `toml:"executor_private_key"`
ModelPrice int `toml:"model_price"`
AppPointContract string `toml:"app_point_contract"`
}
type PGSQLConfig struct {
......
......@@ -65,7 +65,7 @@
"type": "uint256"
},
{
"indexed": true,
"indexed": false,
"internalType": "string",
"name": "userId",
"type": "string"
......@@ -96,7 +96,7 @@
"type": "uint256"
},
{
"indexed": true,
"indexed": false,
"internalType": "string",
"name": "userId",
"type": "string"
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -3,11 +3,14 @@ package dao
import (
"contract_backend/contract/aon_executor"
"contract_backend/contract/aon_user"
"contract_backend/contract/erc20"
"crypto/ecdsa"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
func (d *Dao) Login(privateKey *ecdsa.PrivateKey, userId string, inviter string) (txHash common.Hash, err error) {
......@@ -21,7 +24,7 @@ func (d *Dao) Login(privateKey *ecdsa.PrivateKey, userId string, inviter string)
return common.Hash{}, err
}
auth.GasPrice = d.gasPrice
auth.GasLimit = 800000
auth.GasLimit = 700000
tx, err := instance.Login(auth, userId, inviter)
if err != nil {
return common.Hash{}, err
......@@ -29,8 +32,38 @@ func (d *Dao) Login(privateKey *ecdsa.PrivateKey, userId string, inviter string)
return tx.Hash(), nil
}
func (d *Dao) CheckApprove(privateKey *ecdsa.PrivateKey) (txHash common.Hash, err error) {
instance, err := erc20.NewErc20(common.HexToAddress(d.c.Chain.AppPointContract), d.ethClient)
if err != nil {
return
}
user := crypto.PubkeyToAddress(privateKey.PublicKey)
allowance, err := instance.Allowance(&bind.CallOpts{}, user, common.HexToAddress(d.c.Chain.ExecutorContract))
if err != nil {
return
}
if allowance.Cmp(abi.MaxUint256) == 0 {
return
}
// 执行approve
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, d.chainId)
if err != nil {
return
}
auth.GasPrice = d.gasPrice
auth.GasLimit = 100000
tx, err := instance.Approve(auth, common.HexToAddress(d.c.Chain.ExecutorContract), abi.MaxUint256)
if err != nil {
return
}
return tx.Hash(), nil
}
func (d *Dao) ExecuteTask(privateKey *ecdsa.PrivateKey, executeId, userId, appId string, modelIds []string, price *big.Int) (txHash common.Hash, err error) {
instance, err := aon_executor.NewAonExecutor(common.HexToAddress(d.c.Chain.UserContract), d.ethClient)
instance, err := aon_executor.NewAonExecutor(common.HexToAddress(d.c.Chain.ExecutorContract), d.ethClient)
if err != nil {
return common.Hash{}, err
}
......@@ -40,6 +73,10 @@ func (d *Dao) ExecuteTask(privateKey *ecdsa.PrivateKey, executeId, userId, appId
return common.Hash{}, err
}
auth.GasPrice = d.gasPrice
auth.GasLimit = 1500000
appId = "NYFiNxBEonlwYIrTQ2V8s9ks0I0ZPRoR1UVASanthVMA"
modelIds = []string{"fUIqu5A2igp9nNJaEUMTod4amDFgIXWmHaTZNNbKo58A"}
tx, err := instance.ExecuteByUserId(auth, executeId, userId, appId, modelIds, price)
if err != nil {
return common.Hash{}, err
......
......@@ -3,6 +3,9 @@ package dao
import (
"contract_backend/model"
"encoding/json"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func (d *Dao) UpdateTask(id string, txHash string, status int) (err error) {
......@@ -12,7 +15,8 @@ func (d *Dao) UpdateTask(id string, txHash string, status int) (err error) {
func (d *Dao) GetUnprocessedTasks(limit int) (tasks []*model.ExecTask, err error) {
sql := `SELECT id, user_id, app_key, model_ids, tx_hash, tx_status FROM public.exec_task WHERE tx_hash = '' AND tx_status = 1 LIMIT ?`
err = d.db.Raw(sql, limit).Scan(&tasks).Error
tx := d.db.Session(&gorm.Session{Logger: d.db.Logger.LogMode(logger.Error)})
err = tx.Raw(sql, limit).Scan(&tasks).Error
if err != nil {
return
}
......
......@@ -5,7 +5,6 @@ import (
"math/big"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
......@@ -44,7 +43,7 @@ func (d *Dao) LoopGasPrice() {
// EstimateGasRecharge 估算充值金额
func (d *Dao) EstimateGasRecharge(address common.Address) (val *big.Int, err error) {
_10val := new(big.Int).Mul(d.gasPrice, big.NewInt(10*500000)) // 10次 每次50w
_20val := new(big.Int).Mul(d.gasPrice, big.NewInt(20*500000)) // 20次 每次50w
_20val := new(big.Int).Mul(d.gasPrice, big.NewInt(50*500000)) // 20次 每次50w
currentBalance, err := d.ethClient.BalanceAt(context.Background(), address, nil)
if err != nil {
......@@ -73,14 +72,6 @@ func (d *Dao) TransferETH(hexPrivateKey string, address common.Address, value *b
GasPrice: d.gasPrice,
}
tx := types.NewTx(&innerTx)
from := crypto.PubkeyToAddress(privateKey.PublicKey)
gasLimit, err := d.ethClient.EstimateGas(context.Background(), ethereum.CallMsg{
From: from,
To: &address,
Value: value,
})
innerTx.Gas = gasLimit
innerTx.GasPrice = d.gasPrice
signer := types.NewCancunSigner(d.chainId)
......
package dao
import (
"context"
"contract_backend/contract/aon_user"
"encoding/base64"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/google/uuid"
)
func TestUID(t *testing.T) {
b, _ := base64.StdEncoding.DecodeString("9jS2xPA3kAyM2F7yvSRi3WwAgDU7sXEPgHEIrR6RhckA")
u, _ := uuid.ParseBytes(b)
t.Log(u.String())
}
func TestLogin(t *testing.T) {
cli, _ := ethclient.Dial("https://opbnb-testnet-rpc.bnbchain.org")
instance, _ := aon_user.NewAonUser(common.HexToAddress("0xb50e74f8903bb72b3bff13c37ccc9114336102d1"), cli)
rec, _ := cli.TransactionReceipt(context.Background(), common.HexToHash("0xac2d1b1db3045665770aa63ea1df4672a333cef3c50788ee5df47e27036de6e4"))
lg, _ := instance.ParseRegister(*rec.Logs[2])
t.Log(lg.UserName)
}
......@@ -49,7 +49,6 @@ func (m *Monitor) Start() {
for _, task := range tasks {
m.ProcessOne(task)
}
return
}
}
......@@ -63,7 +62,16 @@ func (m *Monitor) ProcessOne(task *model.ExecTask) {
}).Error("update task error")
return
}
txHash, err := m.d.ExecuteTask(m.privateKey, task.Id, task.UserId, task.AppKey, task.ModelIdsArray, big.NewInt(int64(m.conf.Chain.ModelPrice)))
log.WithFields(log.Fields{
"task_id": task.Id,
"user_id": task.UserId,
"app_key": task.AppKey,
"model_ids": task.ModelIdsArray,
}).Info("execute task")
val10, _ := big.NewInt(10).SetString("10000000000000000000", 10)
// txHash, err := m.d.ExecuteTask(m.privateKey, task.Id, task.UserId, task.AppKey, task.ModelIdsArray, big.NewInt(int64(m.conf.Chain.ModelPrice)))
txHash, err := m.d.ExecuteTask(m.privateKey, task.Id, task.UserId, task.AppKey, task.ModelIdsArray, val10)
if err != nil {
log.WithFields(log.Fields{
"id": task.Id,
......
......@@ -72,8 +72,8 @@ func taskChange(c *gin.Context) {
c.JSON(200, withError(InternalError))
return
}
txHash, err := d.ExecuteTask(key, taskId, userId, appId, modelIds, big.NewInt(10))
val10, _ := big.NewInt(10).SetString("10000000000000000000", 10)
txHash, err := d.ExecuteTask(key, taskId, userId, appId, modelIds, val10)
if err != nil {
log.WithError(err).Error("execute task error")
c.JSON(200, withError(InternalError))
......
......@@ -8,6 +8,7 @@ import (
"io"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
......@@ -116,6 +117,7 @@ func userChange(c *gin.Context) {
"txHash": txHash,
"val": val.String(),
}).Info("user recharge success")
time.Sleep(time.Second)
}
txHash, err := d.Login(key, userId, "")
......@@ -130,5 +132,19 @@ func userChange(c *gin.Context) {
"txHash": txHash,
}).Info("user login")
go func() {
txHash, err = d.CheckApprove(key)
if err != nil {
log.WithError(err).Error("call ca check approve error")
return
}
if txHash.Hex() != (common.Hash{}).Hex() {
log.WithFields(log.Fields{
"userId": userId,
"txHash": txHash,
}).Info("user do approve tx")
}
}()
c.JSON(200, withSuccess(""))
}
......@@ -13,9 +13,10 @@ import (
func TestXpriv(t *testing.T) {
xpri := "xprv9xwX8JDK1LBoHbfUiif8tmaj5dTGEnrYrDat8RUNowmbz2hCD8N1wydNao1TBhgTzvF1gojUwekF4J28BNo4mCVW81BY3XUwDCQhscSMaC3"
uid := "6fc7f308-af15-463f-b15b-fee798d3e296"
uid := "382a2aaf-572f-4a05-92b3-c05cf542bb63"
k, _ := GetPrivateKeyByUserId(xpri, uid)
fmt.Println(crypto.PubkeyToAddress(k.PublicKey))
}
func TestGenHDKey(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment