我正在尝试在更新资产之前使用 if 条件检查条件。但我不能这样做。我需要检查房产是否存在,以及用户的账户余额是否超过房产的市场价格。
我已尝试使用我所知道的所有类型的选项。但我是新手,因此无法解决问题。如果我只尝试检查该属性是否存在,则它可以正常工作。但是当我尝试检查平衡条件时,它会出现问题。
模型.cto
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* This namespace helps in idetifying the entities for the network. */
namespace org.example.property
/* Asset Property identified by a striing PID
This is used to maintain the properties which are registered in the system.
*/
asset Property identified by PID {
o String PID
o String owner
o Integer mktprice
o String RegistrationDate
o String PropertyType
o String Location
o String Status default = "Registered"
o Boolean Public
o Boolean Private
o Boolean IntentOfSale
}
/* Asset PropertyListing identified by a striing PLID
This is used to maintain the properties which are listed for sale in the system.
*/
asset PropertyListing identified by PLID {
o String PLID
o String owner
o Integer mktprice
o String RegistrationDate
o String PropertyType
o String Location
o String Status default = "Intent Of Sale"
}
/* Participant Buyer identified by a striing Bname
This is used to maintain the buyers who are part of the system.
*/
participant Buyer identified by Bname {
o String Bname
o String Bemail
o Integer IdentityNo //Passport, SSN, Aadhar etc.
o String Bnkname
o String Bnkaddress
o Integer AccNo
o String IFSC
o Integer Balance
}
/* Participant Seller identified by a striing Sname
This is used to maintain the sellers who are part of the system.
*/
participant Seller identified by Sname {
o String Sname
o String Semail
o Integer IdentityNo
o String Bnkname
o String Bnkaddress
o Integer AccNo
o String IFSC
o Integer Balance
o String SaleDeedDocs
}
/* Participant Registrar identified by a striing Rname
This is used to maintain the registrar who are part of the system.
*/
participant Registrar identified by Rname {
o String Rname
o String Remail
}
/* Transaction Created
This is used to add new properties in the system.
*/
transaction Created {
o String PID
--> Property cproperty
}
transaction Registered {
o String PID
--> PropertyListing rpropertylisting
--> Buyer rbuyer
}
transaction IntentForSale {
--> Property iproperty
--> PropertyListing ipropertylisting
--> Seller iseller
}
脚本.js
/**
* Transaction Created to add the new property in the system
* @param {org.example.property.Registered} tx3
* @transaction
*/
async function Registered(tx3) {
const propertynamespace = 'org.example.property';
const factory = getFactory();
var updateOwner;
var buyerBalance = tx3.rbuyer.Balance;
var marketPrice = tx3.rpropertylisting.mktprice;
return getAssetRegistry(propertynamespace + '.Property')
.then(function(assetRegistry){
return assetRegistry.exists(tx3.PID);
})
return getParticipantRegistry(propertynamespace + '.Buyer')
.then(function (participantRegistry) {
// Get the specific driver from the driver participant registry.
return participantRegistry.get(tx3.rbuyer.Bname);
})
.then(function(exists){
if(exists && buyerBalance > marketPrice){
return getAssetRegistry(propertynamespace + '.Property')
.then(function(assetRegistry2){
return assetRegistry2.get(tx3.PID);
})
.then(function(updateProperty){
updateOwner = updateProperty
updateOwner.owner = tx3.rbuyer.Bname;
updateOwner.Status = "Registered";
return getAssetRegistry(propertynamespace + '.Property')
})
.then(function(assetRegistry3){
return assetRegistry3.update(updateOwner);
})
}
else{
console.log('Property doesnot exists')
}
})
}
它应该更新财产的所有者和状态。
看起来问题出在exists函数上。由于传递了错误的参数。
脚本.js
async function Registered(tx3) {
//Getting the namespace and factory
const factory = getFactory();
const propertynamespace = 'org.example.property';
//Checking if property exists
return getAssetRegistry(propertynamespace + '.Property')
.then(function(assetRegistry){
return assetRegistry.exists(tx3.PID);
})
.then(function(exists){
//Creating the condition if property exists
if(exists && tx3.rbuyer.Balance > tx3.rpropertylisting.mktprice){
//updating property
const property = factory.newResource(propertynamespace, 'Property', tx3.PID);
property.owner = tx3.rbuyer.Bname;
property.mktprice = tx3.rpropertylisting.mktprice;
property.RegistrationDate = tx3.rpropertylisting.RegistrationDate;
property.PropertyType = tx3.rpropertylisting.PropertyType;
property.Location = tx3.rpropertylisting.Location;
property.Status = "Registered";
//Setting the flags
property.Public = true;
property.Private = false;
property.IntentOfSale = false;
//Getting asset registry
return getAssetRegistry(propertynamespace + '.Property')
//Saving property
.then(function(assetRegistry3){
return assetRegistry3.update(property);
})
}
//Creating condition if property dosent exist
else{
console.log('Property does not exist');
}
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句