Skip to content

Commit 1fa1929

Browse files
committed
added createHTLC function in SDK
Signed-off-by: Krishnasuri Narayanam <[email protected]>
1 parent 3fa8af4 commit 1fa1929

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

sdks/corda/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ repositories {
4242
if (file("$projectDir/./github.properties").exists()) {
4343
file("$projectDir/github.properties").withInputStream { constants.load(it) }
4444
maven {
45-
url 'https://maven.pkg.github.com/sanvenDev/weaver-dlt-interoperability'
45+
url constants.url
4646
credentials {
4747
username constants.username
4848
password constants.password

sdks/corda/src/main/kotlin/com/weaver/corda/sdk/AssetManager.kt

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,41 @@ class AssetManager {
2727
companion object {
2828
private val logger = LoggerFactory.getLogger(AssetManager::class.java)
2929

30+
@JvmStatic
31+
fun createHTLC(
32+
proxy: CordaRPCOps,
33+
assetType: String,
34+
assetId: String,
35+
recipientParty: String,
36+
hashBase64: String,
37+
expiryTimeSecs: Long,
38+
getAssetStateAndRefFlow: String,
39+
deleteAssetStateCommand: CommandData
40+
): Either<Error, String> {
41+
return try {
42+
AssetManager.logger.debug("Sending asset-lock request to Corda as part of asset-exchange.\n")
43+
val contractId = runCatching {
44+
45+
val assetAgreement = createAssetExchangeAgreement(assetType, assetId, recipientParty, "")
46+
val lockInfo = createAssetLockInfo(hashBase64, expiryTimeSecs)
47+
48+
proxy.startFlow(::LockAsset, lockInfo, assetAgreement, getAssetStateAndRefFlow, deleteAssetStateCommand)
49+
.returnValue.get()
50+
}.fold({
51+
it.map { linearId ->
52+
AssetManager.logger.debug("Locking asset was successful and the state was stored with linearId $linearId.\n")
53+
linearId.toString()
54+
}
55+
}, {
56+
Left(Error("Corda Network Error: Error running LockAsset flow: ${it.message}\n"))
57+
})
58+
contractId
59+
} catch (e: Exception) {
60+
AssetManager.logger.error("Error locking asset in Corda network: ${e.message}\n")
61+
Left(Error("Error locking asset in Corda network: ${e.message}"))
62+
}
63+
}
64+
3065
@JvmStatic
3166
fun createFungibleHTLC(
3267
proxy: CordaRPCOps,
@@ -39,7 +74,7 @@ class AssetManager {
3974
deleteAssetStateCommand: CommandData
4075
): Either<Error, String> {
4176
return try {
42-
AssetManager.logger.debug("Sending asset-lock request to Corda as part of asset-exchange.\n")
77+
AssetManager.logger.debug("Sending fungible asset-lock request to Corda as part of asset-exchange.\n")
4378
val contractId = runCatching {
4479

4580
val assetAgreement = createFungibleAssetExchangeAgreement(tokenType, numUnits, recipientParty, "")
@@ -49,16 +84,16 @@ class AssetManager {
4984
.returnValue.get()
5085
}.fold({
5186
it.map { linearId ->
52-
AssetManager.logger.debug("Locking asset was successful and the state was stored with linearId $linearId.\n")
87+
AssetManager.logger.debug("Locking fungible asset was successful and the state was stored with linearId $linearId.\n")
5388
linearId.toString()
5489
}
5590
}, {
5691
Left(Error("Corda Network Error: Error running LockFungibleAsset flow: ${it.message}\n"))
5792
})
5893
contractId
5994
} catch (e: Exception) {
60-
AssetManager.logger.error("Error locking asset in Corda network: ${e.message}\n")
61-
Left(Error("Error locking asset in Corda network: ${e.message}"))
95+
AssetManager.logger.error("Error locking fungible asset in Corda network: ${e.message}\n")
96+
Left(Error("Error locking fungible asset in Corda network: ${e.message}"))
6297
}
6398
}
6499

@@ -164,6 +199,22 @@ class AssetManager {
164199
}
165200
}
166201

202+
fun createAssetExchangeAgreement(
203+
assetType: String,
204+
assetId: String,
205+
recipient: String,
206+
locker: String): AssetLocks.AssetExchangeAgreement {
207+
208+
val assetAgreement = AssetLocks.AssetExchangeAgreement.newBuilder()
209+
.setType(assetType)
210+
.setId(assetId)
211+
.setLocker(locker)
212+
.setRecipient(recipient)
213+
.build()
214+
215+
return assetAgreement
216+
}
217+
167218
fun createFungibleAssetExchangeAgreement(
168219
tokenType: String,
169220
numUnits: Long,

0 commit comments

Comments
 (0)