首页 > 其他分享 >ton 合约map批量同步和go调用

ton 合约map批量同步和go调用

时间:2024-10-19 17:20:42浏览次数:1  
标签:map BuyInfo chainservice tlb uint16 ton go orders

tact代码:

import "@stdlib/deploy";
import "@stdlib/ownable";
struct RoundInfo {
  orders: map<Int as uint32, BuyInfo>; 
  sum: Int as uint16;                
  arrLength: Int as uint16;           
}
struct BuyInfo {
  startLuckyNum: Int as uint16;    
  nifiNum: Int as uint16;            

message BatchSyncOrderMsg {
  round: Int as uint32;
  orders: map<Int as uint32, BuyInfo>;
}

contract Contract {
  rounds: map<Int as uint32, RoundInfo>;
  
  receive(msg: BatchSyncOrderMsg) {
    self.requireOwner();  
    let roundInfo: RoundInfo = self.rounds.get(msg.round)!!; 

    let totalNifiNum: Int = 0;
    foreach (orderNumber, buyInfo in msg.orders) {
      roundInfo.orders.set(orderNumber, buyInfo);
      totalNifiNum += buyInfo.nifiNum;
      roundInfo.sequence += 1;
    }
  }

go调用代码:

type BatchSyncOrderMsg struct {
	RoundId uint32
	Orders  tlb.HashmapE[tlb.Uint32, tlb.Ref[chainservice.BuyInfo]] 
}
func (t TonApiServiceImpl) BatchSyncOrder(ctx context.Context, roundId uint32, orders map[uint32]chainservice.BuyInfo) (string, error) {
	msg := BatchSyncOrderMsg3{
		RoundId: roundId,
	}

	var keys []tlb.Uint32
	var values []tlb.Ref[chainservice.BuyInfo]
	for key, value := range orders {
		keys = append(keys, tlb.Uint32(key))
		values = append(values, tlb.Ref[chainservice.BuyInfo]{Value: value})
	}
	msg.Orders = tlb.NewHashmapE(keys, values)
	return t.InvokeContract(ctx, roundContractConfig.ContractAddress, BatchSyncOrderMethodId, msg, 51_000_000)
}

func TestTonApiServiceImpl_BatchSyncOrder(t *testing.T) {
	var boughtRoundNum uint16 = 0
	var startLuckNum uint16 = 1
	orders := make(map[uint32]chainservice.BuyInfo)
	for i := 1; i <= int(ordersMax); i++ {
		buyNum := maxRoundNum / uint16(ordersMax)
		if boughtRoundNum+buyNum > maxRoundNum {
			buyNum = maxRoundNum - boughtRoundNum
		}
		if buyNum == 0 {
			break
		}
		orderID := StartOrderID + uint32(i)

		orders[orderID] = chainservice.BuyInfo{
			StartLuckNum: startLuckNum,
			Amount:       buyNum,
		}
		startLuckNum += buyNum
		boughtRoundNum += buyNum
	}

	txHash, err := tonService.BatchSyncOrder(context.Background(), RoundId, orders, nil)
	if err != nil {
		t.Error(err)
	}

	fmt.Println("txHash:", txHash)

}

这边截取部分代码,

标签:map,BuyInfo,chainservice,tlb,uint16,ton,go,orders
From: https://www.cnblogs.com/zhanchenjin/p/18476195

相关文章

  • GoFly框架可以快速且更容易的完成信息及软件开发相关专业同学的毕业论文设计
    前言随着gofly开始开发框架的不断宣传,这段时间有很多软件开发相关专业同学问我们框架是否可以拿来做毕业论文设计技术框架。借助本文给正在选择毕业设计技术或者为将来毕业设计准备的同学介绍一下GoFly框架如何用于毕业设计。介绍之前可以肯定的回答,GoFly框架是完全可以用于毕......
  • GoFly快速开发框架集成ZincSearch全文搜索引擎-ZincSearch是ElasticSearch轻量级替代
    前言我们在项目开发中会遇到如下业务场景:1. 电子商务:实现商品搜索与推荐、价格监控。2. 日志分析:进行系统日志分析和网络流量监控。3. 社交媒体:内容搜索与发现以及用户行为分析。4. 企业知识管理:进行知识搜索与共享和文档版本管理。5. 新闻媒体:实现新闻搜索与推荐以......
  • tonkeeper的toogo库的Hashmap序列化有bug
    packagetonapiserviceimport( "fmt" "testing" "github.com/tonkeeper/tongo/boc" "github.com/tonkeeper/tongo/tlb")funcTestHashmapE(t*testing.T){ //Hashmap的序列化有bug,数据一样的情况下,有时候会提示notenouthbits. c:=......
  • 操作系统(6) (Named /Unnamed Semaphore信号量详解)
    目录1:信号量的基本概念2:命名信号量的示例代码3.无名信号量(UnnamedSemaphore)背景(Background)示例代码讲解初始化无名信号量线程函数创建线程并等待完成销毁信号量总结4.对比1:信号量的基本概念背景介绍:信号量是一种并发编程中的同步原语,它用于协调多......
  • ton合约中的变量需要在运行结束之后才能存储到合约中
    receive(msg:BatchSyncOrderMsg){self.requireOwner();//EnsurethecalleristhecontractownerletroundInfo:RoundInfo=self.rounds.get(msg.round)!!;//Fetchtheinformationforthespecifiedroundrequire(!(roundInfo.finish),"......
  • (环境篇日志-CVPR2024 ) Physical 3D Adversarial Attacks against Monocular Depth E
    题目:Physical3DAdversarialAttacksagainstMonocularDepthEstimationinAutonomousDriving作者:JunhaoZheng,ChenhaoLin*,JiahaoSun,ZhengyuZhao,QianLi,ChaoShen*单位:Xi’anJiaotongUniversity收录:CVPR2024论文:[Physical3DAdversarialAttacks......
  • Django drf jwt token认证前后端使用流程
    在DjangoRestFramework(DRF)中使用JWT(JSONWebToken)进行认证时,前后端需要配合工作。下面是DRF使用JWT认证的一个基本流程。后端部分安装必要的库:需要安装djangorestframework和djangorestframework-simplejwt两个库。后者是处理JWT的工具。pipin......
  • 【Python技术之Django精品教学】第11课--Python Django 迁移
    PythonDjango迁移没有这样的表?-product/models.py中定义的类仅仅是我们的数据库的概念,但它并没有在数据库中创建任何表。我们可以认为类Phone是概念性的模式。在创建任何表之前,如果我们试图访问创建前的表,它将抛出这样的错误。OperationalErrorat/admin/product/phone/......
  • 【Python技术之Django精品教学】第13课--Django模型集–2
    Django模型集–2模型字段–模型字段定义了将被存储在所提供的变量中的数据类型。要存储价格,整数类型更适合。要存储高度、长度等,浮动/十进制类型更合适。要存储标题或标题,最好是提供字符限制。对于写一篇文章,最好有一个没有字符限制的文本框。示例:Price=models.Integer......
  • 安装ArgoCD
    安装ArgoCD参考:https://argo-cd.readthedocs.io/en/stable/getting_started/创建命名空间kubectlcreatenamespaceargocd下载install.ymlcurl-Lhttps://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml>argocd.yaml修改镜像地址先查看......