Skip to content

Commit

Permalink
Fix NFT Amounts (#451)
Browse files Browse the repository at this point in the history
* make some NFT amounts CurrencyAmount instead of XrpCurrencyAmount
* Improve unit tests

---------

Co-authored-by: David Fuelling <[email protected]>
  • Loading branch information
nkramer44 and sappenin authored Jul 14, 2023
1 parent 1da875a commit 7823236
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.immutables.value.Value;
import org.xrpl.xrpl4j.model.flags.NfTokenOfferFlags;
import org.xrpl.xrpl4j.model.transactions.Address;
import org.xrpl.xrpl4j.model.transactions.CurrencyAmount;
import org.xrpl.xrpl4j.model.transactions.Hash256;
import org.xrpl.xrpl4j.model.transactions.XrpCurrencyAmount;

Expand All @@ -48,12 +49,12 @@ static ImmutableBuyOffer.Builder builder() {
}

/**
* The amount offered to buy the NFT for {@link XrpCurrencyAmount}.
* The amount offered to buy the NFT.
*
* @return The {@link XrpCurrencyAmount}.
* @return The {@link CurrencyAmount}.
*/
@JsonProperty("Amount")
XrpCurrencyAmount amount();
CurrencyAmount amount();

/**
* A set of boolean {@link NfTokenOfferFlags} containing options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -26,12 +26,11 @@
import org.immutables.value.Value;
import org.xrpl.xrpl4j.model.flags.NfTokenOfferFlags;
import org.xrpl.xrpl4j.model.transactions.Address;
import org.xrpl.xrpl4j.model.transactions.CurrencyAmount;
import org.xrpl.xrpl4j.model.transactions.Hash256;
import org.xrpl.xrpl4j.model.transactions.XrpCurrencyAmount;

/**
* An Offer returned in an {@link SellOffer} list.
* This offer is related to NfTokens.
* An Offer returned in an {@link SellOffer} list. This offer is related to NfTokens.
*/
@Value.Immutable
@JsonSerialize(as = ImmutableSellOffer.class)
Expand All @@ -48,16 +47,15 @@ static ImmutableSellOffer.Builder builder() {
}

/**
* The amount offered to buy the NFT for {@link XrpCurrencyAmount}.
* The amount offered to sell the NFT.
*
* @return The {@link XrpCurrencyAmount}.
* @return The {@link CurrencyAmount}.
*/
@JsonProperty("Amount")
XrpCurrencyAmount amount();
CurrencyAmount amount();

/**
* A set of boolean {@link NfTokenOfferFlags} containing options
* enabled for this object.
* A set of boolean {@link NfTokenOfferFlags} containing options enabled for this object.
*
* @return The {@link NfTokenOfferFlags} for this object.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.immutables.value.Value;
import org.xrpl.xrpl4j.model.flags.NfTokenOfferFlags;
import org.xrpl.xrpl4j.model.transactions.Address;
import org.xrpl.xrpl4j.model.transactions.CurrencyAmount;
import org.xrpl.xrpl4j.model.transactions.Hash256;
import org.xrpl.xrpl4j.model.transactions.NfTokenId;
import org.xrpl.xrpl4j.model.transactions.XrpCurrencyAmount;
Expand Down Expand Up @@ -63,12 +64,12 @@ default LedgerEntryType ledgerEntryType() {
}

/**
* The amount of XRP, in drops, expected or offered for the token.
* The amount expected or offered for the token.
*
* @return The {@link XrpCurrencyAmount}.
* @return The {@link CurrencyAmount}.
*/
@JsonProperty("Amount")
XrpCurrencyAmount amount();
CurrencyAmount amount();

/**
* {@link Address} of the source account that created and owns the offer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.xrpl.xrpl4j.model.ledger.ImmutableNfTokenOfferObject;
import org.xrpl.xrpl4j.model.ledger.LedgerObject;
import org.xrpl.xrpl4j.model.transactions.Address;
import org.xrpl.xrpl4j.model.transactions.CurrencyAmount;
import org.xrpl.xrpl4j.model.transactions.Hash256;
import org.xrpl.xrpl4j.model.transactions.NfTokenId;
import org.xrpl.xrpl4j.model.transactions.XrpCurrencyAmount;
Expand All @@ -46,12 +47,12 @@
public interface MetaNfTokenOfferObject extends MetaLedgerObject {

/**
* The amount of XRP, in drops, expected or offered for the token.
* The amount expected or offered for the token.
*
* @return The {@link XrpCurrencyAmount}.
* @return The {@link CurrencyAmount}.
*/
@JsonProperty("Amount")
Optional<XrpCurrencyAmount> amount();
Optional<CurrencyAmount> amount();

/**
* {@link Address} of the source account that created and owns the offer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -29,14 +29,14 @@
import org.xrpl.xrpl4j.model.ledger.NfTokenOfferObject;
import org.xrpl.xrpl4j.model.transactions.Address;
import org.xrpl.xrpl4j.model.transactions.Hash256;
import org.xrpl.xrpl4j.model.transactions.IssuedCurrencyAmount;
import org.xrpl.xrpl4j.model.transactions.NfTokenId;
import org.xrpl.xrpl4j.model.transactions.XrpCurrencyAmount;

public class NfTokenOfferObjectJsonTests extends AbstractJsonTest {
public class NfTokenOfferObjectJsonTests extends AbstractJsonTest {

@Test
public void testJson() throws JsonProcessingException, JSONException {

public void testJsonWithXrpAmount() throws JsonProcessingException, JSONException {
NfTokenOfferObject object = NfTokenOfferObject.builder()
.amount(XrpCurrencyAmount.ofDrops(10000))
.destination(Address.of("rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn"))
Expand All @@ -60,4 +60,40 @@ public void testJson() throws JsonProcessingException, JSONException {

assertCanSerializeAndDeserialize(object, json);
}

@Test
public void testJsonWithIssuedCurrencyAmount() throws JsonProcessingException, JSONException {
NfTokenOfferObject object = NfTokenOfferObject.builder()
.amount(
IssuedCurrencyAmount.builder()
.currency("USD")
.issuer(Address.of("rsjYGpMWQeNBXbUTkVz4ZKzHefgZSr6rys"))
.value("10")
.build()
)
.destination(Address.of("rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn"))
.owner(Address.of("rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW"))
.previousTransactionId(Hash256.of("E3FE6EA3D48F0C2B639448020EA4F03D4F4F8FFDB243A852A0F59177921B4879"))
.previousTransactionLedgerSequence(UnsignedInteger.valueOf(14090896))
.nfTokenId(NfTokenId.of("000B013A95F14B0044F78A264E41713C64B5F89242540EE208C3098E00000D65"))
.flags(NfTokenOfferFlags.BUY_TOKEN)
.build();

String json = "{\n" +
" \"Flags\": 1,\n" +
" \"Amount\": {" +
" \"currency\": \"USD\",\n" +
" \"issuer\": \"rsjYGpMWQeNBXbUTkVz4ZKzHefgZSr6rys\"," +
" \"value\": \"10\"" +
" },\n" +
" \"Owner\": \"rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW\",\n" +
" \"NFTokenID\": \"000B013A95F14B0044F78A264E41713C64B5F89242540EE208C3098E00000D65\",\n" +
" \"Destination\": \"rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn\",\n" +
" \"PreviousTxnID\": \"E3FE6EA3D48F0C2B639448020EA4F03D4F4F8FFDB243A852A0F59177921B4879\",\n" +
" \"PreviousTxnLgrSeq\": 14090896,\n" +
" \"LedgerEntryType\": \"NFTokenOffer\"\n" +
"}";

assertCanSerializeAndDeserialize(object, json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -27,6 +27,7 @@
import org.xrpl.xrpl4j.model.flags.NfTokenOfferFlags;
import org.xrpl.xrpl4j.model.transactions.Address;
import org.xrpl.xrpl4j.model.transactions.Hash256;
import org.xrpl.xrpl4j.model.transactions.IssuedCurrencyAmount;
import org.xrpl.xrpl4j.model.transactions.NfTokenId;
import org.xrpl.xrpl4j.model.transactions.XrpCurrencyAmount;

Expand All @@ -36,8 +37,7 @@
public class NfTokenSellOffersResultTest extends AbstractJsonTest {

@Test
public void test() throws JsonProcessingException, JSONException {

public void testWithXrpAmount() throws JsonProcessingException, JSONException {
SellOffer sellOffer = SellOffer.builder()
.amount(XrpCurrencyAmount.ofDrops(1000))
.owner(Address.of("rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW"))
Expand Down Expand Up @@ -67,4 +67,45 @@ public void test() throws JsonProcessingException, JSONException {

assertCanSerializeAndDeserialize(params, json);
}

@Test
public void testWithIssuedCurrencyAmount() throws JsonProcessingException, JSONException {
SellOffer sellOffer = SellOffer.builder()
.amount(IssuedCurrencyAmount.builder()
.issuer(Address.of("rsjYGpMWQeNBXbUTkVz4ZKzHefgZSr6rys"))
.currency("USD")
.value("100")
.build()
)
.owner(Address.of("rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW"))
.flags(NfTokenOfferFlags.AUTHORIZED)
.nftOfferIndex(Hash256.of("000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007"))
.build();

List<SellOffer> list = new ArrayList<>();
list.add(sellOffer);

NftSellOffersResult params = NftSellOffersResult.builder()
.nfTokenId(NfTokenId.of("000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007"))
.offers(list)
.build();

String offer = "{\n" +
" \"Flags\": 2,\n" +
" \"Amount\": {\n" +
" \"issuer\": \"rsjYGpMWQeNBXbUTkVz4ZKzHefgZSr6rys\",\n" +
" \"currency\": \"USD\",\n" +
" \"value\": \"100\"\n" +
" },\n" +
" \"owner\": \"rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW\",\n" +
" \"nft_offer_index\": \"000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007\"\n" +
"}";

String json = "{\n" +
" \"nft_id\": \"000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007\",\n" +
" \"offers\": [" + offer + "]\n" +
"}";

assertCanSerializeAndDeserialize(params, json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -21,15 +21,17 @@
*/

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.primitives.UnsignedInteger;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.xrpl.xrpl4j.model.AbstractJsonTest;
import org.xrpl.xrpl4j.model.transactions.Marker;
import org.xrpl.xrpl4j.model.transactions.NfTokenId;

public class NftBuyOffersRequestParamsTest extends AbstractJsonTest {

@Test
public void test() throws JsonProcessingException, JSONException {
public void testWithRequiredValue() throws JsonProcessingException, JSONException {
NftBuyOffersRequestParams params = NftBuyOffersRequestParams.builder()
.nfTokenId(NfTokenId.of("000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007"))
.build();
Expand All @@ -40,4 +42,21 @@ public void test() throws JsonProcessingException, JSONException {

assertCanSerializeAndDeserialize(params, json);
}

@Test
public void testWithAllValues() throws JsonProcessingException, JSONException {
NftBuyOffersRequestParams params = NftBuyOffersRequestParams.builder()
.nfTokenId(NfTokenId.of("000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007"))
.limit(UnsignedInteger.valueOf(10L))
.marker(Marker.of("123"))
.build();

String json = "{\n" +
" \"nft_id\": \"000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007\",\n" +
" \"limit\": 10,\n" +
" \"marker\": \"123\"\n" +
" }";

assertCanSerializeAndDeserialize(params, json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -27,6 +27,7 @@
import org.xrpl.xrpl4j.model.flags.NfTokenOfferFlags;
import org.xrpl.xrpl4j.model.transactions.Address;
import org.xrpl.xrpl4j.model.transactions.Hash256;
import org.xrpl.xrpl4j.model.transactions.IssuedCurrencyAmount;
import org.xrpl.xrpl4j.model.transactions.NfTokenId;
import org.xrpl.xrpl4j.model.transactions.XrpCurrencyAmount;

Expand All @@ -36,7 +37,7 @@
public class NftBuyOffersResultTest extends AbstractJsonTest {

@Test
public void test() throws JsonProcessingException, JSONException {
public void testWithXrpCurrencyAmount() throws JsonProcessingException, JSONException {

BuyOffer buyOffer = BuyOffer.builder()
.amount(XrpCurrencyAmount.ofDrops(1000))
Expand Down Expand Up @@ -67,4 +68,46 @@ public void test() throws JsonProcessingException, JSONException {

assertCanSerializeAndDeserialize(params, json);
}

@Test
public void testWithIssuedCurrencyAmount() throws JsonProcessingException, JSONException {

BuyOffer buyOffer = BuyOffer.builder()
.amount(IssuedCurrencyAmount.builder()
.issuer(Address.of("rsjYGpMWQeNBXbUTkVz4ZKzHefgZSr6rys"))
.currency("USD")
.value("100")
.build()
)
.owner(Address.of("rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW"))
.flags(NfTokenOfferFlags.BUY_TOKEN)
.nftOfferIndex(Hash256.of("000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007"))
.build();

List<BuyOffer> list = new ArrayList<>();
list.add(buyOffer);

NftBuyOffersResult params = NftBuyOffersResult.builder()
.nfTokenId(NfTokenId.of("000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007"))
.offers(list)
.build();

String offer = "{\n" +
" \"Flags\": 1,\n" +
" \"Amount\": {\n" +
" \"issuer\": \"rsjYGpMWQeNBXbUTkVz4ZKzHefgZSr6rys\",\n" +
" \"currency\": \"USD\",\n" +
" \"value\": \"100\"\n" +
" },\n" +
" \"owner\": \"rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW\",\n" +
" \"nft_offer_index\": \"000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007\"\n" +
"}";

String json = "{\n" +
" \"nft_id\": \"000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007\",\n" +
" \"offers\": [" + offer + "]\n" +
"}";

assertCanSerializeAndDeserialize(params, json);
}
}
Loading

0 comments on commit 7823236

Please sign in to comment.