This time, previous I will try using the Scalar DLT emulator using the asset management application explained. The execution environment will continue to work on macOS 10.14.4.
First, get the emulator. Get it with the following command.
$ git clone https://github.com/scalar-labs/scalardl-tools
After cloning, build it.
$ cd scalardl-tools/emulator
$ ./gradlew installDist
Let's run it immediately after building.
$ ./build/install/emulator/bin/emulator
Scalar DL Emulator
Type 'help' for more information
scalar>
If you wait for input, the startup is complete. It will start up quickly!
Next, let's get the asset management application. The location of the source was introduced last time, so let's clone it immediately.
$ git clone https://github.com/indetail-blockchain/getting-started-with-scalardl.git
Since two projects are used, the directory structure is as follows.
git/ ├ scalardl-tools/ └ getting-started-with-scalardl/
In the previous article, I introduced that there are 6 smart contracts available. The source is actually located in the following location.
$ ls -l getting-started-with-scalardl/src/main/java/com/scalar/am/contract
AddAssetContract.java
AddTypeContract.java
AssetHistoryContract.java
ListContract.java
ListTypeContract.java
StatusChangeContract.java
Let's build this.
Build is possible by placing the source under src / main / java
of the emulator.
$ cp -r getting-started-with-scalardl/src/main/java/com scalardl-tools/emulator/src/main/java/
$ rm -rf scalardl-tools/emulator/src/main/java/com/scalar/am/command
$ rm -f scalardl-tools/emulator/src/main/java/com/scalar/am/AssetManager.java
$ cd scalardl-tools/emulator/
$ ./gradlew build
Once it's successfully built, take a look at the build directory.
$ ls -l build/classes/java/main/com/scalar/am/contract
AddAssetContract.class
AddTypeContract.class
AssetHistoryContract.class
ListContract.class
ListTypeContract.class
StatusChangeContract.class
You have a class file.
Now it's time to execute the smart contract. First, let's start the emulator.
$ cd scalardl-tools/emulator
$ ./build/install/emulator/bin/emulator
Scalar DL Emulator
Type 'help' for more information
scalar>
First, register the contract. Register 6 in total.
scalar> register add-type com.scalar.am.contract.AddTypeContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/AddTypeContract.class {"holderId": "Admin"}
Contract 'add-type' successfully registered
scalar> register add-asset com.scalar.am.contract.AddAssetContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/AddAssetContract.class {"holderId": "Admin"}
Contract 'add-asset' successfully registered
scalar> register list-type com.scalar.am.contract.ListTypeContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/ListTypeContract.class {"holderId": "Admin"}
Contract 'list-type' successfully registered
scalar> register list-asset com.scalar.am.contract.ListContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/ListContract.class {"holderId": "Admin"}
Contract 'list-asset' successfully registered
scalar> register status-change com.scalar.am.contract.StatusChangeContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/StatusChangeContract.class {"holderId": "Admin"}
Contract 'status-change' successfully registered
scalar> register asset-history com.scalar.am.contract.AssetHistoryContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/AssetHistoryContract.class {"holderId": "Admin"}
Contract 'asset-history' successfully registered
The specifications of the register
command for registration are as follows.
register [Name to use at runtime] [Java class name] [Class file path] [Initial parameters]
Initial parameters can be passed when creating a smart contract, but this asset management application requires you to specify holderId
.
This holderId
is passed only at the time of registration and has a value that does not change.
By using and checking this in the smart contract, it is possible to ** prevent others from updating the asset information that you have registered **.
The following is a simple image.
Since Scalar DLT does not control the reference / rewrite authority for assets, it is possible to prevent unexpected data updates in this way.
Let's finally do it. Use ʻexecute` for execution.
execute [Registered name of smart contract] [Parameters]
First, register the asset type.
scalar> execute add-type {"name": "Tablet"}
{
"result": "success",
"message": "type Tablet put completed."
}
scalar> execute add-type {"name": "SmartPhone"}
{
"result": "success",
"message": "type SmartPhone put completed."
}
Added types called Tablet and SmartPhone. Next, let's check if it is really registered.
scalar> execute list-type {}
{
"result": "success",
"message": "get list completed.",
"types": [
{
"type": "SmartPhone",
"age": 1
},
{
"type": "Tablet",
"age": 0
}
]
}
It is saved properly.
The caveat here is that if you have no arguments, you pass an empty array {}
.
Please note that if you omit this, an error will occur.
Next, let's register the asset.
scalar>
scalar> execute add-asset {"type": "Tablet", "asset": "iPad", "timestamp": 20190701120000, "id": "1001"}
{
"result": "success",
"message": "asset iPad put completed."
}
You have registered an iPad with an ID of 1001 that is classified as a Tablet. I am registering a UNIX timestamp for timestamp, but I have specified a temporary value for easy understanding. I will also check if the data is registered.
scalar> execute list-asset {"type": "Tablet"}
{
"result": "success",
"message": "get list completed.",
"Tablet": [
{
"id": "1001",
"name": "iPad",
"timestamp": 20190701120000,
"status": "in-stock"
}
]
}
Certainly the iPad is registered. The status is in-stock, so let's borrow it right away.
scalar> execute status-change {"asset_id": "1001", "status": "on-loan", "timestamp": 20190701130000}
{
"result": "success",
"message": "Borrowed"
}
scalar> execute list-asset {"type": "Tablet"}
{
"result": "success",
"message": "get list completed.",
"Tablet": [
{
"id": "1001",
"name": "iPad",
"timestamp": 20190701130000,
"status": "on-loan",
"holderId": "Admin"
}
]
}
I was able to borrow it safely. The timestamp has also been updated and the status has changed to on-loan. Next, let's return it.
scalar> execute status-change {"asset_id": "1001", "status": "in-stock", "timestamp": 20190701140000}
{
"result": "success",
"message": "Returned"
}
scalar> execute list-asset {"type": "Tablet"}
{
"result": "success",
"message": "get list completed.",
"Tablet": [
{
"id": "1001",
"name": "iPad",
"timestamp": 20190701140000,
"status": "in-stock"
}
]
}
This was also successful. Finally, let's check the rental history.
scalar> execute asset-history {"id": "1001"}
{
"result": "success",
"message": "get history complete.",
"history": [
{
"timestamp": 20190701140000,
"status": "in-stock",
"age": 2
},
{
"timestamp": 20190701130000,
"status": "on-loan",
"age": 1,
"holderId": "Admin"
},
{
"timestamp": 20190701120000,
"status": "in-stock",
"age": 0
}
]
}
Registration, lending, returning and history remain.
That's why I tried running an asset management application using an emulator. The emulator starts up quickly and operates quickly, so work progresses. Since I used the emulator this time, the contract and data will be lost if I exit. So next, I would like to follow the actual registration to the Sandbox environment and its call.
Recommended Posts