fabric-samples/full-stack-asset-transfer-guide/docs/SmartContractDev/02-Exercise-Adding-tx-function.md
jkneubuh a299e18e26
Moves the Full Stack Asset Transfer Development Guide to fabric-samples (#852)
* Import Full Stack Asset Transfer Guide at commit fb554befdbbeff9e69159b54fce0b811603f29c7

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Update the workshop with a new WORKSHOP_PATH under fabric-samples

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Update the workshop with a new WORKSHOP_PATH under fabric-samples

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* missed a .git ignored directory on add

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Updates to run the workshop on the Apple M1

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Workaround for https://github.com/eslint/eslint/issues/15299 in the contract tslinter

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Build an arch-specific CC images on M1

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* empty commit - force a build

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* revert an accidental commit that was building the top-level asset-transfer as arm64

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>
2022-11-10 10:40:27 -05:00

52 lines
2.3 KiB
Markdown

## Adding a Transaction Function
[PREVIOUS - Getting Started](./01-Exercise-Getting-Started.md) <==> [NEXT - Test And Debug](./03-Test-And-Debug-Reference.md)
In this exercise, we're going to add a transaction function to check the appraised value. We're going to write this function as part of the iterative development cycle with external chaincode-as-a-service, so there is no requirement to stop fabric or worry about versions of deployed chaincode. We will simply be updating the chaincode source code, restarting the chaincode service, and testing out the new function.
The function will:
- be a 'read-only' function
- take a given asset id, and an upper and lower value
- return a true/false indication if the appraised value is within the upper/lower values
## Steps
Firstly ensure that you've run the Smart Contract and been able to issue transactions against it. It's also worth making sure that you can stop and restart the code after making some minor changes.
- In the `assetTransfer.ts` file create a new function `ValidateValue` . The `ReadAsset` function is a good one to use as a basis. This is a read-only function and already gets the asset from the ledger.
- Add an upper and lower value to the parameters of the function
- `ReadAsset` returns the asset directly, look at the `UpdateAsset` function for how to process the data
- Check the value and return true/false depending on if the value is in the bounds.
- If you wish also set an event.
Remember to stop the running code, rebuild it and start again. Remember you can attach the debugger to help track down issues
## Testing
You can invoke this then with similar commands as in Getting Started.
For example to check if the value is between 1000 and 4200, issue something like
```
peer chaincode query -C mychannel -n asset-transfer -c '{"Args":["ValidateValue","001","1000","4200"]}'
```
## Example implementation
A possible implementation would be
```
@Transaction(false)
async ValidateValue(ctx: Context, id: string, lower:number, upper:number): Promise<boolean> {
const existingAssetBytes = await this.#readAsset(ctx, id);
const existingAsset = newAsset(unmarshal(existingAssetBytes));
if (existingAsset.AppraisedValue > lower && existingAsset.AppraisedValue < upper){
return true;
} else {
return false;
}
}
```