Skip to content

Commit

Permalink
Split MetaDEx payload creation (not on transaction level)
Browse files Browse the repository at this point in the history
CreatePayload_MetaDExTrade() is split into:

 - CreatePayload_MetaDExNewTrade()
 - CreatePayload_MetaDExCancelPrice()
 - CreatePayload_MetaDExCancelPair()
 - CreatePayload_MetaDExCancelEcosystem()

This change has no functional effect, and does not change the transaction format, but solely wraps and seperate the seperate actions into different logic units.

Related code is updated, including reference-payload-creation unit tests.
  • Loading branch information
dexX7 committed May 20, 2015
1 parent a8bd651 commit 4761b8f
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 22 deletions.
88 changes: 87 additions & 1 deletion src/omnicore/createpayload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,73 @@ std::vector<unsigned char> CreatePayload_ChangeIssuer(uint32_t propertyId)
return payload;
}

std::vector<unsigned char> CreatePayload_MetaDExTrade(uint32_t propertyIdForSale, uint64_t amountForSale, uint32_t propertyIdDesired, uint64_t amountDesired, uint8_t action)
std::vector<unsigned char> CreatePayload_MetaDExNewTrade(uint32_t propertyIdForSale, uint64_t amountForSale, uint32_t propertyIdDesired, uint64_t amountDesired)
{
std::vector<unsigned char> payload;

uint16_t messageType = 21;
uint16_t messageVer = 0;
uint8_t action = 1; // ADD

mastercore::swapByteOrder16(messageVer);
mastercore::swapByteOrder16(messageType);
mastercore::swapByteOrder32(propertyIdForSale);
mastercore::swapByteOrder64(amountForSale);
mastercore::swapByteOrder32(propertyIdDesired);
mastercore::swapByteOrder64(amountDesired);

PUSH_BACK_BYTES(payload, messageVer);
PUSH_BACK_BYTES(payload, messageType);
PUSH_BACK_BYTES(payload, propertyIdForSale);
PUSH_BACK_BYTES(payload, amountForSale);
PUSH_BACK_BYTES(payload, propertyIdDesired);
PUSH_BACK_BYTES(payload, amountDesired);
PUSH_BACK_BYTES(payload, action);

return payload;
}

std::vector<unsigned char> CreatePayload_MetaDExCancelPrice(uint32_t propertyIdForSale, uint64_t amountForSale, uint32_t propertyIdDesired, uint64_t amountDesired)
{
std::vector<unsigned char> payload;

uint16_t messageType = 21;
uint16_t messageVer = 0;
uint8_t action = 2; // CANCEL_AT_PRICE

mastercore::swapByteOrder16(messageVer);
mastercore::swapByteOrder16(messageType);
mastercore::swapByteOrder32(propertyIdForSale);
mastercore::swapByteOrder64(amountForSale);
mastercore::swapByteOrder32(propertyIdDesired);
mastercore::swapByteOrder64(amountDesired);

PUSH_BACK_BYTES(payload, messageVer);
PUSH_BACK_BYTES(payload, messageType);
PUSH_BACK_BYTES(payload, propertyIdForSale);
PUSH_BACK_BYTES(payload, amountForSale);
PUSH_BACK_BYTES(payload, propertyIdDesired);
PUSH_BACK_BYTES(payload, amountDesired);
PUSH_BACK_BYTES(payload, action);

return payload;
}

std::vector<unsigned char> CreatePayload_MetaDExCancelPair(uint32_t propertyIdForSale, uint32_t propertyIdDesired)
{
std::vector<unsigned char> payload;

uint16_t messageType = 21;
uint16_t messageVer = 0;
uint64_t amountForSale = 0;
uint64_t amountDesired = 0;
uint8_t action = 3; // CANCEL_ALL_FOR_PAIR

mastercore::swapByteOrder16(messageVer);
mastercore::swapByteOrder16(messageType);
mastercore::swapByteOrder32(propertyIdForSale);
mastercore::swapByteOrder64(amountForSale);
mastercore::swapByteOrder32(propertyIdDesired);
mastercore::swapByteOrder64(amountDesired);

PUSH_BACK_BYTES(payload, messageVer);
Expand All @@ -300,4 +357,33 @@ std::vector<unsigned char> CreatePayload_MetaDExTrade(uint32_t propertyIdForSale
return payload;
}

std::vector<unsigned char> CreatePayload_MetaDExCancelEcosystem(uint8_t ecosystem)
{
std::vector<unsigned char> payload;

uint16_t messageType = 21;
uint16_t messageVer = 0;
uint32_t propertyIdForSale = ecosystem;
uint64_t amountForSale = 0;
uint32_t propertyIdDesired = ecosystem;
uint64_t amountDesired = 0;
uint8_t action = 4; // CANCEL_EVERYTHING

mastercore::swapByteOrder16(messageVer);
mastercore::swapByteOrder16(messageType);
mastercore::swapByteOrder32(propertyIdForSale);
mastercore::swapByteOrder64(amountForSale);
mastercore::swapByteOrder32(propertyIdDesired);
mastercore::swapByteOrder64(amountDesired);

PUSH_BACK_BYTES(payload, messageVer);
PUSH_BACK_BYTES(payload, messageType);
PUSH_BACK_BYTES(payload, propertyIdForSale);
PUSH_BACK_BYTES(payload, amountForSale);
PUSH_BACK_BYTES(payload, propertyIdDesired);
PUSH_BACK_BYTES(payload, amountDesired);
PUSH_BACK_BYTES(payload, action);

return payload;
}

5 changes: 4 additions & 1 deletion src/omnicore/createpayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ std::vector<unsigned char> CreatePayload_CloseCrowdsale(uint32_t propertyId);
std::vector<unsigned char> CreatePayload_Grant(uint32_t propertyId, uint64_t amount, std::string memo);
std::vector<unsigned char> CreatePayload_Revoke(uint32_t propertyId, uint64_t amount, std::string memo);
std::vector<unsigned char> CreatePayload_ChangeIssuer(uint32_t propertyId);
std::vector<unsigned char> CreatePayload_MetaDExTrade(uint32_t propertyIdForSale, uint64_t amountForSale, uint32_t propertyIdDesired, uint64_t amountDesired, uint8_t action);
std::vector<unsigned char> CreatePayload_MetaDExNewTrade(uint32_t propertyIdForSale, uint64_t amountForSale, uint32_t propertyIdDesired, uint64_t amountDesired);
std::vector<unsigned char> CreatePayload_MetaDExCancelPrice(uint32_t propertyIdForSale, uint64_t amountForSale, uint32_t propertyIdDesired, uint64_t amountDesired);
std::vector<unsigned char> CreatePayload_MetaDExCancelPair(uint32_t propertyIdForSale, uint32_t propertyIdDesired);
std::vector<unsigned char> CreatePayload_MetaDExCancelEcosystem(uint8_t ecosystem);


#endif // OMNICORE_CREATEPAYLOAD_H
28 changes: 14 additions & 14 deletions src/omnicore/rpctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,7 @@ Value sendnewtrade_OMNI(const Array& params, bool fHelp)
if (senderAvailableBalance < amountForSale) throw JSONRPCError(RPC_TYPE_ERROR, "Sender has insufficient balance (due to pending transactions)");

// create a payload for the transaction
uint8_t action = CMPTransaction::ADD; // TODO: move into payload creation
std::vector<unsigned char> payload = CreatePayload_MetaDExTrade(propertyIdForSale, amountForSale, propertyIdDesired, amountDesired, action);
std::vector<unsigned char> payload = CreatePayload_MetaDExNewTrade(propertyIdForSale, amountForSale, propertyIdDesired, amountDesired);

// request the wallet build the transaction (and if needed commit it)
uint256 txid = 0;
Expand All @@ -828,6 +827,7 @@ Value sendnewtrade_OMNI(const Array& params, bool fHelp)
if (!autoCommit) {
return rawHex;
} else {
uint8_t action = CMPTransaction::ADD; // TODO: move into pending creation
PendingAdd(txid, fromAddress, "", MSC_TYPE_METADEX, propertyIdForSale, amountForSale, propertyIdDesired, amountDesired, action);
return txid.GetHex();
}
Expand Down Expand Up @@ -877,8 +877,7 @@ Value sendcanceltradebyprice_OMNI(const Array& params, bool fHelp)
// TODO: check, if there are matching offers to cancel

// create a payload for the transaction
uint8_t action = CMPTransaction::CANCEL_AT_PRICE; // TODO: move into payload creation
std::vector<unsigned char> payload = CreatePayload_MetaDExTrade(propertyIdForSale, amountForSale, propertyIdDesired, amountDesired, action);
std::vector<unsigned char> payload = CreatePayload_MetaDExCancelPrice(propertyIdForSale, amountForSale, propertyIdDesired, amountDesired);

// request the wallet build the transaction (and if needed commit it)
uint256 txid = 0;
Expand All @@ -892,6 +891,7 @@ Value sendcanceltradebyprice_OMNI(const Array& params, bool fHelp)
if (!autoCommit) {
return rawHex;
} else {
uint8_t action = CMPTransaction::CANCEL_AT_PRICE; // TODO: move into pending creation
PendingAdd(txid, fromAddress, "", MSC_TYPE_METADEX, propertyIdForSale, amountForSale, propertyIdDesired, amountDesired, action);
return txid.GetHex();
}
Expand Down Expand Up @@ -930,10 +930,7 @@ Value sendcanceltradebypair_OMNI(const Array& params, bool fHelp)
// TODO: check, if there are matching offers to cancel

// create a payload for the transaction
uint8_t action = CMPTransaction::CANCEL_ALL_FOR_PAIR; // TODO: move into payload creation
int64_t amountForSale = 0;
int64_t amountDesired = 0;
std::vector<unsigned char> payload = CreatePayload_MetaDExTrade(propertyIdForSale, amountForSale, propertyIdDesired, amountDesired, action);
std::vector<unsigned char> payload = CreatePayload_MetaDExCancelPair(propertyIdForSale, propertyIdDesired);

// request the wallet build the transaction (and if needed commit it)
uint256 txid = 0;
Expand All @@ -947,6 +944,9 @@ Value sendcanceltradebypair_OMNI(const Array& params, bool fHelp)
if (!autoCommit) {
return rawHex;
} else {
uint8_t action = CMPTransaction::CANCEL_ALL_FOR_PAIR; // TODO: move into pending creation
int64_t amountForSale = 0;
int64_t amountDesired = 0;
PendingAdd(txid, fromAddress, "", MSC_TYPE_METADEX, propertyIdForSale, amountForSale, propertyIdDesired, amountDesired, action);
return txid.GetHex();
}
Expand Down Expand Up @@ -980,12 +980,7 @@ Value sendcanceltradebyecosystem_OMNI(const Array& params, bool fHelp)
// TODO: check, if there are matching offers to cancel

// create a payload for the transaction
uint8_t action = CMPTransaction::CANCEL_EVERYTHING; // TODO: move into payload creation
int64_t amountForSale = 0;
int64_t amountDesired = 0;
uint32_t propertyIdForSale = ecosystem;
uint32_t propertyIdDesired = ecosystem;
std::vector<unsigned char> payload = CreatePayload_MetaDExTrade(propertyIdForSale, amountForSale, propertyIdDesired, amountDesired, action);
std::vector<unsigned char> payload = CreatePayload_MetaDExCancelEcosystem(ecosystem);

// request the wallet build the transaction (and if needed commit it)
uint256 txid = 0;
Expand All @@ -999,6 +994,11 @@ Value sendcanceltradebyecosystem_OMNI(const Array& params, bool fHelp)
if (!autoCommit) {
return rawHex;
} else {
uint8_t action = CMPTransaction::CANCEL_EVERYTHING; // TODO: move into pending creation
int64_t amountForSale = 0;
int64_t amountDesired = 0;
uint32_t propertyIdForSale = ecosystem;
uint32_t propertyIdDesired = ecosystem;
PendingAdd(txid, fromAddress, "", MSC_TYPE_METADEX, propertyIdForSale, amountForSale, propertyIdDesired, amountDesired, action);
return txid.GetHex();
}
Expand Down
51 changes: 47 additions & 4 deletions src/omnicore/test/create_payload_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,63 @@ BOOST_AUTO_TEST_CASE(payload_dex_offer)
"00010014000000010000000005f5e1000000000001312d000a000000000000271001");
}

BOOST_AUTO_TEST_CASE(payload_meta_dex_offer)
BOOST_AUTO_TEST_CASE(payload_meta_dex_new_trade)
{
// Trade tokens for tokens [type 21, version 0]
std::vector<unsigned char> vch = CreatePayload_MetaDExTrade(
std::vector<unsigned char> vch = CreatePayload_MetaDExNewTrade(
static_cast<uint32_t>(1), // property: MSC
static_cast<int64_t>(250000000), // amount for sale: 2.5 MSC
static_cast<uint32_t>(31), // property desired: TetherUS
static_cast<int64_t>(5000000000), // amount desired: 50.0 TetherUS
static_cast<uint8_t>(1)); // sub-action: new offer
static_cast<int64_t>(5000000000)); // amount desired: 50.0 TetherUS
// sub-action: (1) new-offer

BOOST_CHECK_EQUAL(HexStr(vch),
"0000001500000001000000000ee6b2800000001f000000012a05f20001");
}

BOOST_AUTO_TEST_CASE(payload_meta_dex_cancel_at_price)
{
// Trade tokens for tokens [type 21, version 0]
std::vector<unsigned char> vch = CreatePayload_MetaDExCancelPrice(
static_cast<uint32_t>(1), // property: MSC
static_cast<int64_t>(250000000), // amount for sale: 2.5 MSC
static_cast<uint32_t>(31), // property desired: TetherUS
static_cast<int64_t>(5000000000)); // amount desired: 50.0 TetherUS
// sub-action: (2) cancel-at-price

BOOST_CHECK_EQUAL(HexStr(vch),
"0000001500000001000000000ee6b2800000001f000000012a05f20002");
}

BOOST_AUTO_TEST_CASE(payload_meta_dex_cancel_pair)
{
// Trade tokens for tokens [type 21, version 0]
std::vector<unsigned char> vch = CreatePayload_MetaDExCancelPair(
static_cast<uint32_t>(1), // property: MSC
// amount for sale: 0.0 MSC
static_cast<uint32_t>(31)); // property desired: TetherUS
// amount desired: 0.0 TetherUS
// sub-action: (3) cancel-pair

BOOST_CHECK_EQUAL(HexStr(vch),
"000000150000000100000000000000000000001f000000000000000003");
}

BOOST_AUTO_TEST_CASE(payload_meta_dex_cancel_ecosystem)
{
// Trade tokens for tokens [type 21, version 0]
std::vector<unsigned char> vch = CreatePayload_MetaDExCancelEcosystem(
static_cast<uint8_t>(1)); // ecosystem: Main
// property: MSC
// amount for sale: 0.0 MSC
// property desired: MSC
// amount for sale: 0.0 MSC
// sub-action: (4) cancel-ecosystem

BOOST_CHECK_EQUAL(HexStr(vch),
"0000001500000001000000000000000000000001000000000000000004");
}

BOOST_AUTO_TEST_CASE(payload_accept_dex_offer)
{
// Purchase tokens with bitcoins [type 22, version 0]
Expand Down
15 changes: 14 additions & 1 deletion src/qt/metadexcanceldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,21 @@ void MetaDExCancelDialog::SendCancelTransaction()
return;
}

// TODO: restructure and seperate
// create a payload for the transaction
std::vector<unsigned char> payload = CreatePayload_MetaDExTrade(propertyIdForSale, amountForSale, propertyIdDesired, amountDesired, action);
std::vector<unsigned char> payload;
if (action == 2) { // CANCEL_AT_PRICE
payload = CreatePayload_MetaDExCancelPrice(propertyIdForSale, amountForSale, propertyIdDesired, amountDesired);
}
if (action == 3) { // CANCEL_ALL_FOR_PAIR
payload = CreatePayload_MetaDExCancelPair(propertyIdForSale, propertyIdDesired);
}
if (action == 4) { // CANCEL_ALL_FOR_PAIR
uint8_t ecosystem = 0;
if (isMainEcosystemProperty(propertyIdForSale) && isMainEcosystemProperty(propertyIdDesired)) ecosystem = OMNI_PROPERTY_MSC;
if (isTestEcosystemProperty(propertyIdForSale) && isTestEcosystemProperty(propertyIdDesired)) ecosystem = OMNI_PROPERTY_TMSC;
payload = CreatePayload_MetaDExCancelEcosystem(ecosystem);
}

// request the wallet build the transaction (and if needed commit it)
uint256 txid = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/qt/metadexdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ void MetaDExDialog::sendTrade(bool sell)
}

// create a payload for the transaction
std::vector<unsigned char> payload = CreatePayload_MetaDExTrade(propertyIdSell, amountSell, propertyIdDes, amountDes, 1);
std::vector<unsigned char> payload = CreatePayload_MetaDExNewTrade(propertyIdSell, amountSell, propertyIdDes, amountDes);

// request the wallet build the transaction (and if needed commit it)
uint256 txid = 0;
Expand Down

0 comments on commit 4761b8f

Please sign in to comment.