Skip to content

Commit 0dba905

Browse files
committed
chore: Refactor asset name handling and update Value methods
1 parent 2d0ef44 commit 0dba905

File tree

4 files changed

+142
-149
lines changed

4 files changed

+142
-149
lines changed

transaction-spec/src/main/java/com/bloxbean/cardano/client/transaction/spec/Asset.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,7 @@ public class Asset {
2121

2222
@JsonIgnore
2323
public byte[] getNameAsBytes() {
24-
byte[] assetNameBytes = null;
25-
if (name != null && !name.isEmpty()) {
26-
//Check if caller has provided a hex string as asset name
27-
if (name.startsWith("0x")) {
28-
try {
29-
assetNameBytes = HexUtil.decodeHexString(name.substring(2));
30-
} catch (IllegalArgumentException e) {
31-
// name is not actually a hex string
32-
assetNameBytes = name.getBytes(StandardCharsets.UTF_8);
33-
}
34-
} else {
35-
assetNameBytes = name.getBytes(StandardCharsets.UTF_8);
36-
}
37-
} else {
38-
assetNameBytes = new byte[0];
39-
}
40-
return assetNameBytes;
24+
return nameToBytes(name);
4125
}
4226

4327
/**
@@ -100,6 +84,14 @@ public Asset minus(Asset that) {
10084
return this.subtract(that);
10185
}
10286

87+
public boolean hasName(String assetName) {
88+
byte[] assetNameBytes = nameToBytes(assetName);
89+
byte[] existingAssetNameBytes = nameToBytes(name);
90+
91+
//check if both byte array are same
92+
return Arrays.equals(assetNameBytes, existingAssetNameBytes);
93+
}
94+
10395
@Override
10496
public boolean equals(Object o) {
10597
if (this == o) return true;
@@ -112,4 +104,24 @@ public boolean equals(Object o) {
112104
public int hashCode() {
113105
return Objects.hash(Arrays.hashCode(getNameAsBytes()), value);
114106
}
107+
108+
private static byte[] nameToBytes(String assetName) {
109+
byte[] assetNameBytes = null;
110+
if (assetName != null && !assetName.isEmpty()) {
111+
//Check if caller has provided a hex string as asset name
112+
if (assetName.startsWith("0x")) {
113+
try {
114+
assetNameBytes = HexUtil.decodeHexString(assetName.substring(2));
115+
} catch (IllegalArgumentException e) {
116+
// name is not actually a hex string
117+
assetNameBytes = assetName.getBytes(StandardCharsets.UTF_8);
118+
}
119+
} else {
120+
assetNameBytes = assetName.getBytes(StandardCharsets.UTF_8);
121+
}
122+
} else {
123+
assetNameBytes = new byte[0];
124+
}
125+
return assetNameBytes;
126+
}
115127
}

transaction-spec/src/main/java/com/bloxbean/cardano/client/transaction/spec/Value.java

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package com.bloxbean.cardano.client.transaction.spec;
22

3+
import co.nstant.in.cbor.model.Map;
34
import co.nstant.in.cbor.model.Number;
45
import co.nstant.in.cbor.model.*;
56
import com.bloxbean.cardano.client.common.cbor.CborSerializationUtil;
67
import com.bloxbean.cardano.client.common.cbor.custom.SortedMap;
7-
import com.bloxbean.cardano.client.util.Tuple;
88
import lombok.AllArgsConstructor;
99
import lombok.Builder;
1010
import lombok.Data;
1111
import lombok.NoArgsConstructor;
1212

1313
import java.math.BigInteger;
14-
import java.util.ArrayList;
15-
import java.util.Collections;
16-
import java.util.HashMap;
17-
import java.util.List;
14+
import java.util.*;
1815
import java.util.stream.Collectors;
1916
import java.util.stream.Stream;
2017

@@ -172,27 +169,42 @@ public Value add(String policyId, String assetName, BigInteger amount) {
172169
return this.add(from(policyId, assetName, amount));
173170
}
174171

175-
public Value add(String unit, BigInteger amount) {
176-
Tuple<String, String> policyAndAssetName = getPolicyAndAssetName(unit);
177-
return this.add(from(policyAndAssetName._1, policyAndAssetName._2, amount));
172+
/**
173+
* Adds the specified coin(lovelace) amount to the current {@code Value} instance.
174+
*
175+
* @param amount The amount in lovelace to be added
176+
* @return A new {@code Value} instance with the added coin amount.
177+
*/
178+
public Value addCoin(BigInteger amount) {
179+
return this.add(fromCoin(amount));
178180
}
179181

180-
182+
/**
183+
* Creates a new Value instance from provided policy ID, asset name, and amount.
184+
*
185+
* @param policyId The policy ID associated with the asset.
186+
* @param assetName The name of the asset.
187+
* @param amount The amount of the asset.
188+
* @return A new Value instance containing the provided asset information.
189+
*/
181190
public static Value from(String policyId, String assetName, BigInteger amount) {
182-
if ((policyId != null && policyId.equals("lovelace")) || (assetName != null && assetName.equals("lovelace"))) {
183-
return fromLovelace(amount);
184-
} else {
185-
return Value.builder()
186-
.multiAssets(List.of(MultiAsset.builder()
187-
.policyId(policyId)
188-
.assets(List.of(Asset.builder().name(assetName).value(amount).build()))
189-
.build()))
190-
.build();
191-
}
191+
Objects.requireNonNull(policyId);
192+
return Value.builder()
193+
.multiAssets(List.of(MultiAsset.builder()
194+
.policyId(policyId)
195+
.assets(List.of(Asset.builder().name(assetName).value(amount).build()))
196+
.build()))
197+
.build();
192198
}
193199

194-
public static Value fromLovelace(BigInteger lovelaces) {
195-
return Value.builder().coin(lovelaces).build();
200+
/**
201+
* Creates a {@link Value} instance from the given amount of lovelaces.
202+
*
203+
* @param coin The amount of lovelaces to be converted into a {@link Value} instance.
204+
* @return A new {@link Value} instance containing the specified amount of lovelaces.
205+
*/
206+
public static Value fromCoin(BigInteger coin) {
207+
return Value.builder().coin(coin).build();
196208
}
197209

198210
/**
@@ -237,44 +249,53 @@ public Value minus(Value that) {
237249
}
238250

239251

240-
public Value subtractLovelace(BigInteger amount) {
241-
return this.subtract(fromLovelace(amount));
252+
/**
253+
* Subtracts the specified coin (lovelace) amount from the current {@code Value} instance.
254+
*
255+
* @param amount The amount in lovelace to be subtracted.
256+
* @return A new {@code Value} instance with the subtracted coin amount.
257+
*/
258+
public Value substractCoin(BigInteger amount) {
259+
return this.subtract(fromCoin(amount));
242260
}
243261

262+
/**
263+
* Subtracts a specified amount of an asset from the current {@code Value} instance.
264+
*
265+
* @param policyId The policy ID associated with the asset.
266+
* @param assetName The name of the asset.
267+
* @param amount The amount of the asset to be subtracted.
268+
* @return A new {@code Value} instance with the subtracted asset amount.
269+
*/
244270
public Value subtract(String policyId, String assetName, BigInteger amount) {
245271
return this.subtract(from(policyId, assetName, amount));
246272
}
247273

248-
public Value subtract(String unit, BigInteger amount) {
249-
Tuple<String, String> policyAndAssetName = getPolicyAndAssetName(unit);
250-
return this.subtract(from(policyAndAssetName._1, policyAndAssetName._2, amount));
251-
}
252-
253-
274+
/**
275+
* Returns the amount of a specific asset identified by the given policy ID and asset name.
276+
* If the asset is not found, the method returns BigInteger.ZERO.
277+
*
278+
* @param policyId The policy ID corresponding to the asset.
279+
* @param assetName The name of the asset.
280+
* @return The amount of the specified asset as a BigInteger, or BigInteger.ZERO if the asset is not found.
281+
*/
254282
public BigInteger amountOf(String policyId, String assetName) {
255283
return getMultiAssets()
256284
.stream()
257285
.filter(multiAsset -> multiAsset.getPolicyId().equals(policyId))
258286
.findAny()
259287
.stream()
260-
.flatMap(multiAsset -> multiAsset.getAssets().stream().filter(asset -> asset.getName().equals(assetName)))
288+
.flatMap(multiAsset -> multiAsset.getAssets().stream().filter(asset -> asset.hasName(assetName)))
261289
.map(Asset::getValue)
262290
.findAny()
263291
.orElse(BigInteger.ZERO);
264292
}
265293

266-
public BigInteger amountOf(String unit) {
267-
Tuple<String, String> policyAndAssetName = getPolicyAndAssetName(unit);
268-
return amountOf(policyAndAssetName._1, policyAndAssetName._2);
269-
}
270-
271-
private Tuple<String, String> getPolicyAndAssetName(String unit) {
272-
String sanitisedUnit = unit.replace(".", "");
273-
String policyId = sanitisedUnit.substring(0, 56);
274-
String assetName = "0x" + sanitisedUnit.substring(56);
275-
return new Tuple<>(policyId, assetName);
276-
}
277-
294+
/**
295+
* Determines if the value represented by this instance is zero.
296+
*
297+
* @return true if both `multiAssets` is null or empty and `coin` equals to zero, otherwise false.
298+
*/
278299
public boolean isZero() {
279300
return (multiAssets == null || multiAssets.isEmpty()) && BigInteger.ZERO.equals(coin);
280301
}

transaction-spec/src/test/java/com/bloxbean/cardano/client/transaction/spec/AssetSpecTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import java.nio.charset.StandardCharsets;
77

88
import static org.assertj.core.api.Assertions.assertThat;
9-
import static org.junit.jupiter.api.Assertions.assertEquals;
10-
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
import static org.junit.jupiter.api.Assertions.*;
1110

1211
class AssetSpecTest {
1312

@@ -84,4 +83,28 @@ void invalidHexName() {
8483
byte[] expectedBytes = "0xtest".getBytes(StandardCharsets.UTF_8);
8584
assertThat(asset.getNameAsBytes()).isEqualTo(expectedBytes);
8685
}
86+
87+
@Test
88+
void hasName() {
89+
Asset asset = Asset.builder().name("asset1").value(BigInteger.valueOf(700L)).build();
90+
91+
var same = asset.hasName("asset1");
92+
assertTrue(same);
93+
}
94+
95+
@Test
96+
void hasNameReturnsFalse() {
97+
Asset asset = Asset.builder().name("asset1").value(BigInteger.valueOf(700L)).build();
98+
99+
var same = asset.hasName("asset2");
100+
assertFalse(same);
101+
}
102+
103+
@Test
104+
void hasNameWhenHex() {
105+
Asset asset = Asset.builder().name("asset1").value(BigInteger.valueOf(700L)).build();
106+
107+
var same = asset.hasName("0x617373657431");
108+
assertTrue(same);
109+
}
87110
}

0 commit comments

Comments
 (0)