Skip to content

Commit

Permalink
Merge pull request #3 from Simple-Software-Solutions/patch-1
Browse files Browse the repository at this point in the history
[RPC] Add Burn
  • Loading branch information
Liquid369 authored Jul 14, 2021
2 parents a4f7833 + 9992ba5 commit 816e58b
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3704,6 +3704,84 @@ UniValue settxfee(const JSONRPCRequest& request)
return true;
}

void BurnMoney(const CScript scriptPubKeyIn, CAmount nValue, CTransactionRef& tx, bool fUseIX = false)
{
// Check amount
if (nValue <= 0)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid amount");

if (nValue > pwalletMain->GetAvailableBalance())
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds");

std::string strError;
if (pwalletMain->IsLocked()) {
strError = "Error: Wallet locked, unable to create transaction!";
LogPrintf("BurnMoney() : %s", strError);
throw JSONRPCError(RPC_WALLET_ERROR, strError);
}

// Get scriptPubKey
CScript scriptPubKey = scriptPubKeyIn;

// Create and send the transaction
CReserveKey reservekey(pwalletMain);
CAmount nFeeRequired;
if (!pwalletMain->CreateTransaction(scriptPubKey, nValue, tx, reservekey, nFeeRequired, strError, nullptr, ALL_COINS, (CAmount)0)) {
if (nValue + nFeeRequired > pwalletMain->GetAvailableBalance())
strError = strprintf("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!", FormatMoney(nFeeRequired));
LogPrintf("BurnMoney() : %s\n", strError);
throw JSONRPCError(RPC_WALLET_ERROR, strError);
}

const CWallet::CommitResult&& res = pwalletMain->CommitTransaction(tx, reservekey, g_connman.get());
if (res.status != CWallet::CommitStatus::OK)
throw JSONRPCError(RPC_WALLET_ERROR, res.ToString());
}

UniValue burn(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2)
throw std::runtime_error(
"burn <amount> [\"optional string\"]\n"
"This command is used to burn RBX and optionally write custom data into the burn transaction, \n"
"<amount> is real and is rounded to the nearest zen (ex: 0.00000001).\n"
"You may use 0 as the <amount> to skip a specific burn amount, for only writing data into the chain."
+ HelpRequiringPassphrase());

CScript scriptPubKey;

if (params.size() > 1) {
std::vector<unsigned char> data;
if (params[1].get_str().size() > 0) {
// Parse plain-text string into HEX, then HEX to HEX-Vector
data = ParseHexV(HexStr(params[1].get_str()), "data");
// Ensure the data is under the maximum OP_RETURN relay (Minus overhead)
if (data.size() > MAX_OP_RETURN_RELAY - 3)
throw std::runtime_error("Your custom data (worth " + std::to_string(data.size()) + " bytes) exceeds the maximum relay of " + std::to_string(MAX_OP_RETURN_RELAY - 3) + " bytes!");
} else {
// Empty data is valid, but cannot have a zero-value burn
if (params[0].get_real() == 0)
throw std::runtime_error("You cannot create a zero-value burn transaction without custom data!");
}
scriptPubKey = CScript() << OP_RETURN << data;
} else {
if (params[0].get_real() == 0)
throw std::runtime_error("You cannot create a zero-value burn transaction without custom data!");
scriptPubKey = CScript() << OP_RETURN;
}

// Amount (Use <amount> parameter if it's larger than 0, else, use a single zen)
int64_t nAmount = AmountFromValue(params[0].get_real() > 0 ? params[0] : 0.00000001);
CTxDestination address1;
CTransactionRef tx;
BurnMoney(scriptPubKey, nAmount, tx, false);

EnsureWalletIsUnlocked();
CWalletTx& wtx = pwalletMain->mapWallet.at(tx->GetHash());
return wtx.GetHash().GetHex();
}


UniValue getwalletinfo(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 0)
Expand Down Expand Up @@ -4196,6 +4274,7 @@ extern UniValue dumpwallet(const JSONRPCRequest& request);
extern UniValue importwallet(const JSONRPCRequest& request);
extern UniValue bip38encrypt(const JSONRPCRequest& request);
extern UniValue bip38decrypt(const JSONRPCRequest& request);
extern UniValue burn(const UniValue& params, bool fHelp);

extern UniValue exportsaplingkey(const JSONRPCRequest& request);
extern UniValue importsaplingkey(const JSONRPCRequest& request);
Expand Down

0 comments on commit 816e58b

Please sign in to comment.