diff --git a/src/asset.cpp b/src/asset.cpp index e747897..3d9abbf 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -28,6 +28,11 @@ Asset *Asset::createNonNativeAsset(QString code, const KeyPair &issuer) } } +Asset *Asset::createNonNativeAsset(QString code, QString issuer) +{ + return Asset::createNonNativeAsset(code, KeyPair::fromAccountId(issuer)); +} + Asset *Asset::create(QString type, QString code, QString issuer) { if (type == "native") { return new AssetTypeNative(); diff --git a/src/asset.h b/src/asset.h index fcda58a..10a8384 100644 --- a/src/asset.h +++ b/src/asset.h @@ -16,6 +16,8 @@ class Asset { public: virtual ~Asset(){} + + /** * Creates one of AssetTypeCreditAlphaNum4 or AssetTypeCreditAlphaNum12 object based on a code length * @param code Asset code @@ -23,6 +25,7 @@ class Asset */ static Asset* createNonNativeAsset(QString code, KeyPair* issuer); static Asset* createNonNativeAsset(QString code, const KeyPair& issuer);//makes a copy of issuer + static Asset* createNonNativeAsset(QString code, QString issuer); static Asset* create(QString type, QString code, QString issuer); /** * Generates Asset object from a given XDR object @@ -45,6 +48,8 @@ class Asset * Generates XDR object from a given Asset object */ virtual stellar::Asset toXdr() = 0; + + virtual QString toString() const = 0; }; Asset* checkNotNull(Asset* asset, const char *error); diff --git a/src/assettypecreditalphanum.cpp b/src/assettypecreditalphanum.cpp index 5b30fb3..8c8d0f2 100644 --- a/src/assettypecreditalphanum.cpp +++ b/src/assettypecreditalphanum.cpp @@ -18,11 +18,15 @@ AssetTypeCreditAlphaNum::~AssetTypeCreditAlphaNum() delete m_issuer; } -const KeyPair &AssetTypeCreditAlphaNum::getIssuer() { +QString AssetTypeCreditAlphaNum::getCode() const{ + return m_code; +} + +const KeyPair &AssetTypeCreditAlphaNum::getIssuer() const{ return *m_issuer; } -uint AssetTypeCreditAlphaNum::hashCode() { +uint AssetTypeCreditAlphaNum::hashCode() const{ return qHash(m_issuer->getAccountId() + this->getCode()); } @@ -33,3 +37,8 @@ bool AssetTypeCreditAlphaNum::equals(Asset *object) { } return false; } + +QString AssetTypeCreditAlphaNum::toString() const +{ + return getCode()+":"+this->getIssuer().getAccountId(); +} diff --git a/src/assettypecreditalphanum.h b/src/assettypecreditalphanum.h index c0ef13d..d72708d 100644 --- a/src/assettypecreditalphanum.h +++ b/src/assettypecreditalphanum.h @@ -30,18 +30,18 @@ class AssetTypeCreditAlphaNum : public Asset /** * Returns asset code */ - QString getCode() { - return m_code; - } + QString getCode() const; /** * Returns asset issuer */ - const KeyPair &getIssuer(); + const KeyPair &getIssuer() const; - uint hashCode(); + uint hashCode() const; bool equals(Asset* object); + + virtual QString toString() const; }; #endif // ASSETTYPECREDITALPHANUM_H diff --git a/src/assettypenative.cpp b/src/assettypenative.cpp index 625187f..4e5e1d3 100644 --- a/src/assettypenative.cpp +++ b/src/assettypenative.cpp @@ -14,7 +14,7 @@ bool AssetTypeNative::equals(Asset* object) { return dynamic_cast(object); } -int AssetTypeNative::hashCode() { +uint AssetTypeNative::hashCode() { return 0; } @@ -23,3 +23,8 @@ stellar::Asset AssetTypeNative::toXdr() { xdr.type = stellar::AssetType::ASSET_TYPE_NATIVE; return xdr; } + +QString AssetTypeNative::toString() const +{ + return QString("native"); +} diff --git a/src/assettypenative.h b/src/assettypenative.h index ee053d0..8d30980 100644 --- a/src/assettypenative.h +++ b/src/assettypenative.h @@ -18,9 +18,11 @@ class AssetTypeNative : public Asset bool equals(Asset *object); - int hashCode(); + uint hashCode(); stellar::Asset toXdr(); + + QString toString() const; }; #endif // ASSETTYPENATIVE_H diff --git a/src/requests/accountsrequestbuilder.cpp b/src/requests/accountsrequestbuilder.cpp index f7999a2..61942cd 100644 --- a/src/requests/accountsrequestbuilder.cpp +++ b/src/requests/accountsrequestbuilder.cpp @@ -16,6 +16,18 @@ AccountResponse* AccountsRequestBuilder::account(KeyPair *account) { return this->account(this->buildUri()); } +AccountsRequestBuilder &AccountsRequestBuilder::signer(KeyPair *accountID) +{ + addParameter("signer", accountID->getAccountId()); + return *this; +} + +AccountsRequestBuilder &AccountsRequestBuilder::asset(Asset *asset) +{ + RequestBuilder::setAssetParameter("asset",asset); + return *this; +} + AccountsRequestBuilder &AccountsRequestBuilder::cursor(QString token) { RequestBuilder::cursor(token); return *this; diff --git a/src/requests/accountsrequestbuilder.h b/src/requests/accountsrequestbuilder.h index b14ccfc..53b0a0c 100644 --- a/src/requests/accountsrequestbuilder.h +++ b/src/requests/accountsrequestbuilder.h @@ -30,6 +30,23 @@ class AccountsRequestBuilder : public RequestBuilder */ AccountResponse *account(KeyPair* account); + /** + * @brief signer filters accounts who have a given signer account id + * @see Accounts + * @param token Account ID + * @return AccountsRequestBuilder + */ + AccountsRequestBuilder& signer(KeyPair* accountID); + + + /** + * @brief asset filters accounts who have a given trustline + * @see Accounts + * @param asset issued asset to filter with + * @return AccountsRequestBuilder + */ + AccountsRequestBuilder& asset(Asset* asset); + AccountsRequestBuilder& cursor(QString token); diff --git a/src/requests/requestbuilder.cpp b/src/requests/requestbuilder.cpp index e7fccd0..d540f96 100644 --- a/src/requests/requestbuilder.cpp +++ b/src/requests/requestbuilder.cpp @@ -99,6 +99,18 @@ RequestBuilder &RequestBuilder::setAssetsParameter(QString name, QList return *this; } +RequestBuilder &RequestBuilder::setAssetParameter(QString name, Asset* asset) { + + if (dynamic_cast(asset)) { + this->addParameter(name, "native"); + } else if (AssetTypeCreditAlphaNum * creditAsset = dynamic_cast(asset)) { + this->addParameter(name, creditAsset->getCode()+"%3A"+creditAsset->getIssuer().getAccountId());//%3A is ":" we already encode it to dont reencode everything + + } else { + throw std::runtime_error("unsupported asset"); + } + return *this; +} QUrl RequestBuilder::buildUri() { QString base = this->m_uri.toString(); diff --git a/src/requests/requestbuilder.h b/src/requests/requestbuilder.h index d6254c8..7eb4168 100644 --- a/src/requests/requestbuilder.h +++ b/src/requests/requestbuilder.h @@ -68,12 +68,7 @@ class RequestBuilder RequestBuilder& asc(); RequestBuilder& desc(); - /** - * Sets a parameter consisting of a comma separated list of assets on the request. - * @param name the name of the query parameter - * @param assets the list of assets to be serialized into the query parameter value - */ - RequestBuilder& setAssetsParameter(QString name, QList assets); + QUrl buildUri(); void setRequestUri(QUrl &uri);//useful for the links @@ -82,6 +77,15 @@ class RequestBuilder QString toString(Order order); Order orderFromString(QString s); + +protected: + /** + * Sets a parameter consisting of a comma separated list of assets on the request. + * @param name the name of the query parameter + * @param assets the list of assets to be serialized into the query parameter value + */ + RequestBuilder& setAssetsParameter(QString name, QList assets); + RequestBuilder& setAssetParameter(QString name, Asset *asset); }; diff --git a/test/assettest.h b/test/assettest.h index e9347c7..aace672 100644 --- a/test/assettest.h +++ b/test/assettest.h @@ -31,6 +31,7 @@ private slots: auto xdr = asset.toXdr(); Asset* parsedAsset = Asset::fromXdr(xdr); QVERIFY(dynamic_cast(parsedAsset)); + QCOMPARE(asset.toString(),QString("native")); } void testAssetTypeCreditAlphaNum4() { @@ -41,6 +42,7 @@ private slots: AssetTypeCreditAlphaNum4* parsedAsset = (AssetTypeCreditAlphaNum4*) Asset::fromXdr(xdr); QCOMPARE(code, asset.getCode()); QCOMPARE(issuer->getAccountId(), parsedAsset->getIssuer().getAccountId()); + QCOMPARE(asset.toString(),QString("USDA:%1").arg(issuer->getAccountId())); } void testAssetTypeCreditAlphaNum12() { @@ -51,6 +53,7 @@ private slots: AssetTypeCreditAlphaNum12* parsedAsset = (AssetTypeCreditAlphaNum12*) Asset::fromXdr(xdr); QCOMPARE(code, asset.getCode()); QCOMPARE(issuer->getAccountId(), parsedAsset->getIssuer().getAccountId()); + QCOMPARE(asset.toString(),QString("TESTTEST:%1").arg(issuer->getAccountId())); } @@ -92,6 +95,7 @@ private slots: QVERIFY(!AssetTypeCreditAlphaNum12("ABCDE", *issuer1).equals(new AssetTypeCreditAlphaNum12("EDCBA", *issuer1))); QVERIFY(!AssetTypeCreditAlphaNum12("ABCDE", *issuer1).equals(new AssetTypeCreditAlphaNum12("ABCDE", *issuer2))); } + }; ADD_TEST(AssetTest) diff --git a/test/requests/accountsrequestbuildertest.h b/test/requests/accountsrequestbuildertest.h index 7bd64de..1c8816e 100644 --- a/test/requests/accountsrequestbuildertest.h +++ b/test/requests/accountsrequestbuildertest.h @@ -5,7 +5,7 @@ #include #include "../testcollector.h" #include "../../src/server.h" - +#include "../../src/keypair.h" class AccountRequestBuilderTest: public QObject { Q_OBJECT @@ -35,6 +35,32 @@ private slots: } + void testAccountsSigner() { + Server* server = new Server("https://horizon-testnet.stellar.org"); + KeyPair* signer = new KeyPair("GD42RQNXTRIW6YR3E2HXV5T2AI27LBRHOERV2JIYNFMXOBA234SWLQQB"); + QUrl uri = server->accounts() + .limit(200) + .signer(signer) + .order(RequestBuilder::Order::ASC) + .buildUri(); + + QString expected("https://horizon-testnet.stellar.org/accounts?limit=200&signer=GD42RQNXTRIW6YR3E2HXV5T2AI27LBRHOERV2JIYNFMXOBA234SWLQQB&order=asc"); + QVERIFY(uri.toString() == expected); + } + + void testAccountsAsset() { + Server* server = new Server("https://horizon-testnet.stellar.org"); + KeyPair* issuer = new KeyPair("GD42RQNXTRIW6YR3E2HXV5T2AI27LBRHOERV2JIYNFMXOBA234SWLQQB"); + Asset* issuedAsset = Asset::createNonNativeAsset("USD",issuer); + QUrl uri = server->accounts() + .limit(200) + .asset(issuedAsset) + .order(RequestBuilder::Order::ASC) + .buildUri(); + + QString expected("https://horizon-testnet.stellar.org/accounts?limit=200&asset=USD%3AGD42RQNXTRIW6YR3E2HXV5T2AI27LBRHOERV2JIYNFMXOBA234SWLQQB&order=asc"); + QVERIFY(uri.toString() == expected); + } }; ADD_TEST(AccountRequestBuilderTest)