Skip to content

Commit

Permalink
refine RPCHelpMan
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Nov 7, 2023
1 parent 5d66756 commit 9e23c23
Showing 1 changed file with 83 additions and 87 deletions.
170 changes: 83 additions & 87 deletions src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2115,102 +2115,96 @@ UniValue sendtokenstoaddress(const JSONRPCRequest &request) {
return signsend(rawTx, pwallet, optAuthTx)->GetHash().GetHex();
}

UniValue transferdomain(const JSONRPCRequest &request) {
auto pwallet = GetWallet(request);

if (!request.params[0].isArray()) {
RPCHelpMan{
"transferdomain",
"Creates (and submits to local node and network) a tx to transfer assets across domains. DVM to EVM/EVM to "
"DVM, etc.\n" +
HelpRequiringPassphrase(pwallet) + "\n",
static std::vector<RPCArg> OutputTransferDomainArgs(UniValue param) {
if (param.isArray()) {
return {
{
{"from", RPCArg::Type::STR, RPCArg::Optional::NO, "the source address of sender"},
{"to", RPCArg::Type::STR, RPCArg::Optional::NO, "the destination address of sender"},
{"tokenAmount", RPCArg::Type::STR, RPCArg::Optional::NO, "in amount@token format"},
{"domain",
RPCArg::Type::NUM,
RPCArg::Optional::OMITTED_NAMED_ARG,
"the destination of fund, 0 - auto, 1 - UTXO, 2 - DVM, 3 - EVM"},
{"nonce", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "specified nonce if needed"},
},
RPCResult{"\"hash\" (string) The hex-encoded hash of broadcasted transaction\n"},
RPCExamples{HelpExampleCli("transferdomain", R"("from" "to" "100@DFI")") +
HelpExampleRpc("transferdomain", R"("from", "to", "100@BTC" 2 3)")},
}
.Check(request);
} else {
RPCHelpMan{
"transferdomain",
"Creates (and submits to local node and network) a tx to transfer assets across domains. DVM to EVM/EVM to "
"DVM, etc.\n" +
HelpRequiringPassphrase(pwallet) + "\n",
{
{
"array",
RPCArg::Type::ARR,
RPCArg::Optional::NO,
"A json array of src and dst json objects",
"array", RPCArg::Type::ARR,
RPCArg::Optional::NO,
"A json array of src and dst json objects", {
{
"",
RPCArg::Type::OBJ,
RPCArg::Optional::OMITTED,
"",
{
"",
RPCArg::Type::OBJ,
RPCArg::Optional::OMITTED,
"",
{
"src",
RPCArg::Type::OBJ,
RPCArg::Optional::OMITTED,
"Source arguments",
{
"src",
RPCArg::Type::OBJ,
RPCArg::Optional::OMITTED,
"Source arguments",
{
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "Source address"},
{"amount",
RPCArg::Type::STR,
RPCArg::Optional::NO,
"Amount transfered, the value is amount in amount@token format"},
{"domain",
RPCArg::Type::NUM,
RPCArg::Optional::NO,
"Domain of source: 2 - DVM, 3 - EVM"},
// {"data", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Optional data"},
},
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "Source address"},
{"amount",
RPCArg::Type::STR,
RPCArg::Optional::NO,
"Amount transfered, the value is amount in amount@token format"},
{"domain",
RPCArg::Type::NUM,
RPCArg::Optional::NO,
"Domain of source: 2 - DVM, 3 - EVM"},
},
},
{
"dst",
RPCArg::Type::OBJ,
RPCArg::Optional::OMITTED,
"Destination arguments",
{
"dst",
RPCArg::Type::OBJ,
RPCArg::Optional::OMITTED,
"Destination arguments",
{
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "Destination address"},
{"amount",
RPCArg::Type::STR,
RPCArg::Optional::NO,
"Amount transfered, the value is amount in amount@token format"},
{"domain",
RPCArg::Type::NUM,
RPCArg::Optional::NO,
"Domain of source: 2 - DVM, 3 - EVM"},
// {"data", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Optional data"},
},
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "Destination address"},
{"amount",
RPCArg::Type::STR,
RPCArg::Optional::NO,
"Amount transfered, the value is amount in amount@token format"},
{"domain",
RPCArg::Type::NUM,
RPCArg::Optional::NO,
"Domain of source: 2 - DVM, 3 - EVM"},
},
{"nonce",
RPCArg::Type::NUM,
RPCArg::Optional::OMITTED,
"Optional parameter to specify the transaction nonce"},
},
{"nonce",
RPCArg::Type::NUM,
RPCArg::Optional::OMITTED,
"Optional parameter to specify the transaction nonce"},
},
},
}, },
RPCResult{"\"hash\" (string) The hex-encoded hash of broadcasted transaction\n"},
RPCExamples{
HelpExampleCli(
"transferdomain", R"('[{"src":{"address":"<DFI_address>", "amount":"1.0@DFI", "domain": 2}, "dst":{"address":"<ETH_address>", "amount":"1.0@DFI", "domain": 3}}]')") +
HelpExampleCli(
"transferdomain", R"('[{"src":{"address":"<ETH_address>", "amount":"1.0@DFI", "domain": 3}, "dst":{"address":"<DFI_address>", "amount":"1.0@DFI", "domain": 2}}]')")},
}
.Check(request);
}, }
};
} else {
return {
{"from", RPCArg::Type::STR, RPCArg::Optional::NO, "the source address of sender" },
{"to", RPCArg::Type::STR, RPCArg::Optional::NO, "the destination address of sender"},
{"tokenAmount", RPCArg::Type::STR, RPCArg::Optional::NO, "in amount@token format" },
{"domain",
RPCArg::Type::NUM,
RPCArg::Optional::OMITTED_NAMED_ARG,
"the destination of fund, 0 - auto, 1 - UTXO, 2 - DVM, 3 - EVM" },
{"nonce", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "specified nonce if needed" },
};
}
}

UniValue transferdomain(const JSONRPCRequest &request) {
auto pwallet = GetWallet(request);

RPCHelpMan{
"transferdomain",
"Creates (and submits to local node and network) a tx to transfer assets across domains. DVM to EVM/EVM to "
"DVM, etc.\n" +
HelpRequiringPassphrase(pwallet) + "\n",
OutputTransferDomainArgs(request.params[0]),
RPCResult{"\"hash\" (string) The hex-encoded hash of broadcasted transaction\n"},
RPCExamples{
HelpExampleCli(
"transferdomain",
R"('[{"src":{"address":"<DFI_address>", "amount":"1.0@DFI", "domain": 2}, "dst":{"address":"<ETH_address>", "amount":"1.0@DFI", "domain": 3}}]')") +
HelpExampleCli(
"transferdomain",
R"('[{"src":{"address":"<ETH_address>", "amount":"1.0@DFI", "domain": 3}, "dst":{"address":"<DFI_address>", "amount":"1.0@DFI", "domain": 2}}]')") +
HelpExampleCli("transferdomain", R"("from" "to" "100@DFI")") +
HelpExampleCli("transferdomain", R"("from", "to", "100@BTC" 2 3)")},
}
.Check(request);

if (pwallet->chain().isInitialBlockDownload()) {
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD,
Expand All @@ -2224,6 +2218,8 @@ UniValue transferdomain(const JSONRPCRequest &request) {
UniValue srcDstArray(UniValue::VARR);

if (!request.params[0].isArray()) {
RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VSTR, UniValue::VSTR}, false);

auto defineDomain = [](CTxDestination &dest) {
if (dest.index() == WitV0KeyHashType || dest.index() == PKHashType) {
// by default for DVM in this pipeline
Expand Down Expand Up @@ -2319,7 +2315,8 @@ UniValue transferdomain(const JSONRPCRequest &request) {
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameters, src argument \"domain\" must not be null");
}
bool isEVMIn = src.domain == static_cast<uint8_t>(VMDomain::DVM) || src.domain == static_cast<uint8_t>(VMDomain::UTXO);
bool isEVMIn =
src.domain == static_cast<uint8_t>(VMDomain::DVM) || src.domain == static_cast<uint8_t>(VMDomain::UTXO);

auto srcKey = AddrToPubKey(pwallet, ScriptToString(src.address));
if (isEVMIn) {
Expand Down Expand Up @@ -2407,8 +2404,7 @@ UniValue transferdomain(const JSONRPCRequest &request) {
useNonce,
nonce});
if (!result.ok) {
throw JSONRPCError(RPC_MISC_ERROR,
strprintf("Failed to create and sign TX: %s", result.reason.c_str()));
throw JSONRPCError(RPC_MISC_ERROR, strprintf("Failed to create and sign TX: %s", result.reason.c_str()));
}

std::vector<uint8_t> evmTx(createResult.tx.size());
Expand Down

0 comments on commit 9e23c23

Please sign in to comment.