Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: human focus transfer domain #2679

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 91 additions & 67 deletions src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2117,77 +2117,49 @@ UniValue sendtokenstoaddress(const JSONRPCRequest &request) {

UniValue transferdomain(const JSONRPCRequest &request) {
auto pwallet = GetWallet(request);
// TODO: Add support for non-JSON parameteric input that's human friendly and intuitive

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",
{
{
"",
RPCArg::Type::OBJ,
RPCArg::Optional::OMITTED,
"",
{
{
"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"},
},
},
{
"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"},
},
},
{"nonce",
RPCArg::Type::NUM,
RPCArg::Optional::OMITTED,
"Optional parameter to specify the transaction nonce"},
},
},
},
}, },
HelpRequiringPassphrase(pwallet) + "\n" +
"Arguments:\n"
"1. array | string (json array | string, required) A json array of src and dst json "
"objects\n"
" [\n"
" { (json object)\n"
" \"src\": { (json object) Source arguments\n"
" \"address\": \"str\", (string, required) Source address\n"
" \"amount\": \"str\", (string, required) Amount transfered, the value is amount in "
"amount@token format\n"
" \"domain\": n, (numeric, required) Domain of source: 2 - DVM, 3 - EVM\n"
" },\n"
" \"dst\": { (json object) Destination arguments\n"
" \"address\": \"str\", (string, required) Destination address\n"
" \"amount\": \"str\", (string, required) Amount transfered, the value is amount in "
"amount@token format\n"
" \"domain\": n, (numeric, required) Domain of source: 2 - DVM, 3 - EVM\n"
" },\n"
" \"nonce\": n, (numeric) Optional parameter to specify the transaction nonce\n"
" },\n"
" ...\n"
" ]\n"
" from (string, required) the source address of sender\n"
"2. to (string, required) the destination address of sender\n"
"3. tokenAmount (string, required) in amount@token format\n"
"4. nonce\n",
{},
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);
"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")")},
};

if (pwallet->chain().isInitialBlockDownload()) {
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD,
Expand All @@ -2198,11 +2170,63 @@ UniValue transferdomain(const JSONRPCRequest &request) {

EnsureWalletIsUnlocked(pwallet);

RPCTypeCheck(request.params, {UniValue::VARR}, false);

UniValue srcDstArray(UniValue::VARR);

srcDstArray = request.params[0].get_array();
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) {
return VMDomain::DVM;
} else if (dest.index() == WitV16KeyEthHashType) {
return VMDomain::EVM;
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unsupport domain provided");
}
};

if (request.params[0].isNull()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "\"src\" is required");
}
if (request.params[1].isNull()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "\"dst\" is required");
}
if (request.params[2].isNull()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "\"amount\" is required");
}

std::string src = request.params[0].get_str();
std::string dst = request.params[1].get_str();
std::string amount = request.params[2].get_str();

CTxDestination srcDest = DecodeDestination(src);
CTxDestination dstDest = DecodeDestination(dst);

VMDomain srcDomainType = defineDomain(srcDest);
VMDomain dstDomainType = defineDomain(dstDest);

UniValue srcObj(UniValue::VOBJ);
srcObj.pushKV("address", src);
srcObj.pushKV("amount", amount);
srcObj.pushKV("domain", static_cast<int>(srcDomainType));

UniValue dstObj(UniValue::VOBJ);
dstObj.pushKV("address", dst);
dstObj.pushKV("amount", amount);
dstObj.pushKV("domain", static_cast<int>(dstDomainType));

UniValue elem(UniValue::VOBJ);
elem.pushKV("src", srcObj);
elem.pushKV("dst", dstObj);

if (!request.params[3].isNull()) {
elem.pushKV("nonce", request.params[3].get_int());
}
srcDstArray.push_back(elem);
} else {
RPCTypeCheck(request.params, {UniValue::VARR}, false);
srcDstArray = request.params[0].get_array();
}

CrossBoundaryResult result;
CTransferDomainMessage msg;
Expand Down
4 changes: 4 additions & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "sendtokenstoaddress", 0, "from" },
{ "sendtokenstoaddress", 1, "to" },
{ "transferdomain", 0, "array" },
{ "transferdomain", 0, "from" },
{ "transferdomain", 1, "to" },
{ "transferdomain", 2, "tokenAmount" },
{ "transferdomain", 3, "nonce" },
{ "getanchorteams", 0, "blockHeight" },
{ "getactivemasternodecount", 0, "blockCount" },
{ "appointoracle", 1, "pricefeeds" },
Expand Down
42 changes: 42 additions & 0 deletions test/functional/feature_evm_transferdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,46 @@ def test_contract_methods(self):
assert_equal(contract.functions.totalSupply().call(), 0)
assert_equal(contract.functions.balanceOf(self.address_erc55).call(), 0)

def test_new_transfer_domain(self):
self.rollback_to(self.start_height)

self.nodes[0].utxostoaccount({self.address: "200@DFI"})
transfer_domain(
self.nodes[0], self.address, self.address_erc55, "100@DFI", 2, 3
)
self.nodes[0].generate(1)

dfi_id = "0"
# dvm -> evm
self.nodes[0].transferdomain(
self.address, self.address_erc55, "123.45678901@DFI"
)
balance_dvm_before = float(
self.nodes[0].getaccount(self.address, {}, True)[dfi_id]
)
balance_evm_before = self.nodes[0].w3.eth.get_balance(self.address_erc55)
self.nodes[0].generate(1)
balance_dvm_after = float(
self.nodes[0].getaccount(self.address, {}, True)[dfi_id]
)
balance_evm_after = self.nodes[0].w3.eth.get_balance(self.address_erc55)
assert_equal(balance_dvm_before - 123.45678901000000000, balance_dvm_after)
assert_equal(balance_evm_before + 123456789010000000000, balance_evm_after)

# evm -> dvm
self.nodes[0].transferdomain(self.address_erc55, self.address, "9.87654321@DFI")
balance_dvm_before = float(
self.nodes[0].getaccount(self.address, {}, True)[dfi_id]
)
balance_evm_before = self.nodes[0].w3.eth.get_balance(self.address_erc55)
self.nodes[0].generate(1)
balance_dvm_after = float(
self.nodes[0].getaccount(self.address, {}, True)[dfi_id]
)
balance_evm_after = self.nodes[0].w3.eth.get_balance(self.address_erc55)
assert_equal(balance_dvm_before + 9.87654321000000000, balance_dvm_after)
assert_equal(balance_evm_before - 9876543210000000000, balance_evm_after)

def run_test(self):
self.setup()
self.invalid_before_fork_and_disabled()
Expand Down Expand Up @@ -1292,6 +1332,8 @@ def run_test(self):

self.test_contract_methods()

self.test_new_transfer_domain()


if __name__ == "__main__":
EVMTest().main()
Loading