如何在一个脚本文件中添加两个交易函数?

How to add two transaction function in a script file?

我是超级账本的初学者。我的 model.cto 文件有两个事务处理函数,一个用于将汽车从制造商转移到陈列室,另一个用于将汽车从陈列室转移到车主。 model.cto 文件如下,

namespace org.manufacturer.network

asset Car identified by carID {
  o String carID
  o String name
  o String chasisNumber
  --> Showroom showroom
  --> Owner owner
}

participant Showroom identified by showroomID {
  o String showroomID
  o String name
}

participant Owner identified by ownerID {
  o String ownerID
  o String firstName
  o String lastName
}

transaction Allocate {
  --> Car  car
  --> Showroom newShowroom
}

transaction Purchase {
  --> Showroom showroom
  --> Owner newOwner
}

所以,我想在我的 script.js 文件中添加两个函数,以便我可以执行我的交易。我的 script.js 文件如下

/**
 * New script file
 * @param {org.manufacturer.network.Allocate} allocate - allocating the car from manufacturer to showroom
 * @param {org.manufacturer.network.Purchase} purchase - purchase the car by owner from showroom
 * @transaction
 */

async function transferCar(allocate){
  allocate.car.showroom = allocate.newShowroom;
  let assetRegistry = await getAssetRegistry('org.manufacturer.network.Car');
  await assetRegistry.update(allocate.car);
}

async function purchaseCar(purchase){
  purchase.car.owner = purchase.newOwner;
  let assetRegistry = await getAssetRegistry('org.manufacturer.network.Car');
  await assetRegistry.update(purchase.car);
}

但是脚本文件报错 Transaction processing function transferCar must have 1 function argument of type transaction.

如何在单个 script.js 文件中添加多个事务处理器函数? 这是可能的还是我必须创建两个 script.js 文件来处理交易?

这不是在 script.js 文件中定义两个事务的正确方法。

您的 script.js 文件应该是这样的:

/**
 * New script file
 * @param {org.manufacturer.network.Allocate} allocate - allocating the car from manufacturer to showroom
 * @transaction
 */

async function transferCar(allocate){
  allocate.car.showroom = allocate.newShowroom;
  let assetRegistry = await getAssetRegistry('org.manufacturer.network.Car');
  await assetRegistry.update(allocate.car);
}

/**
 * New script file
 * @param {org.manufacturer.network.Purchase} purchase - purchase the car by owner from showroom
 * @transaction
 */

async function purchaseCar(purchase){
  purchase.car.owner = purchase.newOwner;
  let assetRegistry = await getAssetRegistry('org.manufacturer.network.Car');
  await assetRegistry.update(purchase.car);
}

这是您可以在 script.js 文件中添加多个交易的方法。

希望对你有所帮助