This will be the final step in the LC process, where the buyer receives the goods and the seller receives the payment. Therefore, we define a closing transaction referencing the targeted LC and the reason behind the closure:
transaction Close {
--> LetterOfCredit lc
o String closeReason
}
event CloseEvent {
--> LetterOfCredit lc
o String closeReason
}
The letter status is set to CLOSED, and the step to 10:
/**
* Close the LOC
* @param {org.example.lc.Close} close - the Close transaction
* @transaction
*/
async function close(closeRequest) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'org.example.lc';
let letter = closeRequest.lc;
if (letter.status === 'SELL_RECEIVED_PAYMENT') {
letter.status = 'CLOSED';
letter.closeReason = closeRequest.closeReason;
// update the status of the lc
const assetRegistry = await getAssetRegistry(closeRequest.lc.getFullyQualifiedType());
await assetRegistry.update(letter);
// emit event
const closeEvent = factory.newEvent(namespace, 'CloseEvent');
closeEvent.lc = closeRequest.lc;
closeEvent.closeReason = closeRequest.closeReason;
emit(closeEvent);
} else if (letter.status === 'CLOSED') {
throw new Error('This letter of credit has already been closed');
} else {
throw new Error('Cannot close this letter of credit');
}
}
This was the final part of our Chaincode. Now, let's deploy it in the Fabric network and check whether it works as expected.