Skip to content

Commit

Permalink
new accounts filters
Browse files Browse the repository at this point in the history
add Asset:toString() to generate ASSET_NAME:ASSET_ISSUER string, or "native"
Move setAssetsParameter and setAssetParameter to protected
  • Loading branch information
bnogalm committed Jan 26, 2020
1 parent 08e10ec commit 308a6b9
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ class Asset
{
public:
virtual ~Asset(){}


/**
* Creates one of AssetTypeCreditAlphaNum4 or AssetTypeCreditAlphaNum12 object based on a <code>code</code> length
* @param code Asset code
* @param issuer Asset issuer
*/
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
Expand All @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/assettypecreditalphanum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -33,3 +37,8 @@ bool AssetTypeCreditAlphaNum::equals(Asset *object) {
}
return false;
}

QString AssetTypeCreditAlphaNum::toString() const
{
return getCode()+":"+this->getIssuer().getAccountId();
}
10 changes: 5 additions & 5 deletions src/assettypecreditalphanum.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion src/assettypenative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bool AssetTypeNative::equals(Asset* object) {
return dynamic_cast<AssetTypeNative*>(object);
}

int AssetTypeNative::hashCode() {
uint AssetTypeNative::hashCode() {
return 0;
}

Expand All @@ -23,3 +23,8 @@ stellar::Asset AssetTypeNative::toXdr() {
xdr.type = stellar::AssetType::ASSET_TYPE_NATIVE;
return xdr;
}

QString AssetTypeNative::toString() const
{
return QString("native");
}
4 changes: 3 additions & 1 deletion src/assettypenative.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions src/requests/accountsrequestbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/requests/accountsrequestbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ class AccountsRequestBuilder : public RequestBuilder
*/
AccountResponse *account(KeyPair* account);

/**
* @brief signer filters accounts who have a given signer account id
* @see <a href="https://www.stellar.org/developers/horizon/reference/endpoints/accounts.html">Accounts</a>
* @param token Account ID
* @return AccountsRequestBuilder
*/
AccountsRequestBuilder& signer(KeyPair* accountID);


/**
* @brief asset filters accounts who have a given trustline
* @see <a href="https://www.stellar.org/developers/horizon/reference/endpoints/accounts.html">Accounts</a>
* @param asset issued asset to filter with
* @return AccountsRequestBuilder
*/
AccountsRequestBuilder& asset(Asset* asset);


AccountsRequestBuilder& cursor(QString token);

Expand Down
12 changes: 12 additions & 0 deletions src/requests/requestbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ RequestBuilder &RequestBuilder::setAssetsParameter(QString name, QList<Asset *>
return *this;
}

RequestBuilder &RequestBuilder::setAssetParameter(QString name, Asset* asset) {

if (dynamic_cast<AssetTypeNative*>(asset)) {
this->addParameter(name, "native");
} else if (AssetTypeCreditAlphaNum * creditAsset = dynamic_cast<AssetTypeCreditAlphaNum*>(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();
Expand Down
16 changes: 10 additions & 6 deletions src/requests/requestbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Asset*> assets);


QUrl buildUri();
void setRequestUri(QUrl &uri);//useful for the links
Expand All @@ -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<Asset*> assets);
RequestBuilder& setAssetParameter(QString name, Asset *asset);
};


Expand Down
4 changes: 4 additions & 0 deletions test/assettest.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private slots:
auto xdr = asset.toXdr();
Asset* parsedAsset = Asset::fromXdr(xdr);
QVERIFY(dynamic_cast<AssetTypeNative*>(parsedAsset));
QCOMPARE(asset.toString(),QString("native"));
}

void testAssetTypeCreditAlphaNum4() {
Expand All @@ -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() {
Expand All @@ -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()));
}


Expand Down Expand Up @@ -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)
Expand Down
28 changes: 27 additions & 1 deletion test/requests/accountsrequestbuildertest.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <QtTest>
#include "../testcollector.h"
#include "../../src/server.h"

#include "../../src/keypair.h"
class AccountRequestBuilderTest: public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 308a6b9

Please sign in to comment.