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

Update pool symbol and name #2739

Merged
merged 5 commits into from
Mar 19, 2024
Merged
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
29 changes: 29 additions & 0 deletions src/dfi/consensus/poolpairs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,38 @@ Res CPoolPairsConsensus::operator()(const CUpdatePoolPairMessage &obj) const {
}
}

const auto &consensus = txCtx.GetConsensus();
const auto height = txCtx.GetHeight();
auto &mnview = blockCtx.GetView();

auto token = mnview.GetToken(obj.poolId);
if (!token) {
return Res::Err("Pool token %d does not exist\n", obj.poolId.v);
}

const auto tokenUpdated = !obj.pairSymbol.empty() || !obj.pairName.empty();
if (tokenUpdated) {
if (height < static_cast<uint32_t>(consensus.DF23Height)) {
return Res::Err("Poolpair symbol cannot be changed below DF23 height");
}
}

if (!obj.pairSymbol.empty()) {
token->symbol = trim_ws(obj.pairSymbol).substr(0, CToken::MAX_TOKEN_POOLPAIR_LENGTH);
}

if (!obj.pairName.empty()) {
token->name = trim_ws(obj.pairName).substr(0, CToken::MAX_TOKEN_NAME_LENGTH);
}

if (tokenUpdated) {
BlockContext dummyContext{std::numeric_limits<uint32_t>::max(), {}, Params().GetConsensus()};
UpdateTokenContext ctx{*token, dummyContext, false, false, true};
if (auto res = mnview.UpdateToken(ctx); !res) {
return res;
}
}

return mnview.UpdatePoolPair(obj.poolId, height, obj.status, obj.commission, obj.ownerAddress, rewards);
}

Expand Down
6 changes: 6 additions & 0 deletions src/dfi/poolpairs.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ struct CUpdatePoolPairMessage {
CAmount commission;
CScript ownerAddress;
CBalances rewards;
std::string pairSymbol;
std::string pairName;

ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
Expand All @@ -134,6 +136,10 @@ struct CUpdatePoolPairMessage {
if (!s.empty()) {
READWRITE(rewards);
}
if (!s.empty()) {
READWRITE(pairSymbol);
READWRITE(pairName);
}
}
};

Expand Down
28 changes: 20 additions & 8 deletions src/dfi/rpc_poolpair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ UniValue updatepoolpair(const JSONRPCRequest &request) {
RPCArg::Type::STR,
RPCArg::Optional::OMITTED,
"Token reward to be paid on each block, multiple can be specified."},
{"name", RPCArg::Type::STR, RPCArg::Optional::NO, "Pool name"},
{"symbol", RPCArg::Type::STR, RPCArg::Optional::NO, "Pool symbol"},
},
}, {
"inputs",
Expand Down Expand Up @@ -790,6 +792,7 @@ UniValue updatepoolpair(const JSONRPCRequest &request) {
CAmount commission = -1;
CScript ownerAddress;
CBalances rewards;
std::string pairSymbol{}, pairName{};
const UniValue &metaObj = request.params[0].get_obj();
const UniValue &txInputs = request.params[1];

Expand Down Expand Up @@ -829,6 +832,12 @@ UniValue updatepoolpair(const JSONRPCRequest &request) {
std::numeric_limits<CAmount>::max()));
}
}
if (!metaObj["symbol"].isNull()) {
pairSymbol = metaObj["symbol"].get_str();
}
if (!metaObj["name"].isNull()) {
pairName = metaObj["name"].get_str();
}
RejectErc55Address(ownerAddress);

const auto txVersion = GetTransactionVersion(targetHeight);
Expand All @@ -839,19 +848,22 @@ UniValue updatepoolpair(const JSONRPCRequest &request) {
rawTx.vin =
GetAuthInputsSmart(pwallet, rawTx.nVersion, auths, true, optAuthTx, txInputs, request.metadata.coinSelectOpts);

CDataStream metadata(DfTxMarker, SER_NETWORK, PROTOCOL_VERSION);
metadata << static_cast<unsigned char>(CustomTxType::UpdatePoolPair)
// serialize poolId as raw integer
<< poolId.v << status << commission << ownerAddress;
CUpdatePoolPairMessage msg;
msg.poolId = poolId;
msg.status = status;
msg.commission = commission;
msg.ownerAddress = ownerAddress;
msg.rewards = rewards;
msg.pairSymbol = pairSymbol;
msg.pairName = pairName;

if (targetHeight >= Params().GetConsensus().DF5ClarkeQuayHeight) {
metadata << rewards;
}
CDataStream metadata(DfTxMarker, SER_NETWORK, PROTOCOL_VERSION);
metadata << static_cast<unsigned char>(CustomTxType::UpdatePoolPair) << msg;

CScript scriptMeta;
scriptMeta << OP_RETURN << ToByteVector(metadata);

rawTx.vout.push_back(CTxOut(0, scriptMeta));
rawTx.vout.emplace_back(0, scriptMeta);

CCoinControl coinControl;

Expand Down
118 changes: 118 additions & 0 deletions test/functional/feature_poolpair_rename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2019 The Bitcoin Core developers
# Copyright (c) DeFi Blockchain Developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
"""Test pool pair rename"""

from test_framework.test_framework import DefiTestFramework

from test_framework.util import assert_equal, assert_raises_rpc_error


class PoolPairRenameTest(DefiTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [
["-txnotokens=0", "-amkheight=1", "-bayfrontheight=1", "-df23height=110"],
]

def run_test(self):
# Set up
self.setup()

# Rename token
self.rename_token()

# Rename pool
self.rename_pool()

def setup(self):
# Generate chain
self.nodes[0].generate(105)
self.sync_blocks()

# Pool owner address
self.address = self.nodes[0].get_genesis_keys().ownerAuthAddress

# Create tokens
self.nodes[0].createtoken(
{
"symbol": "FB",
"name": "dFB",
"isDAT": True,
"collateralAddress": self.address,
}
)
self.nodes[0].createtoken(
{
"symbol": "DUSD",
"name": "Decentralized USD",
"isDAT": True,
"collateralAddress": self.address,
}
)
self.nodes[0].generate(1)

# Create pool pair
self.nodes[0].createpoolpair(
{
"tokenA": "FB",
"tokenB": "DUSD",
"comission": 0.001,
"status": True,
"ownerAddress": self.address,
},
)
self.nodes[0].generate(1)

def rename_token(self):
# Rename FB token
self.nodes[0].updatetoken("FB", {"name": "META", "symbol": "dMETA"})
self.nodes[0].generate(1)

# Check rename
result = self.nodes[0].gettoken("dMETA")["1"]
assert_equal(result["name"], "META")
assert_equal(result["symbol"], "dMETA")

def rename_pool(self):
# Rename pool before height
assert_raises_rpc_error(
-32600,
"Poolpair symbol cannot be changed below DF23 height",
self.nodes[0].updatepoolpair,
{"pool": "FB-DUSD", "name": "dMETA-Decentralized USD"},
)
assert_raises_rpc_error(
-32600,
"Poolpair symbol cannot be changed below DF23 height",
self.nodes[0].updatepoolpair,
{"pool": "FB-DUSD", "symbol": "META-DUSD"},
)

# Move to fork height
self.nodes[0].generate(2)

# Rename pool
self.nodes[0].updatepoolpair(
{
"pool": "FB-DUSD",
"name": "dMETA-Decentralized USD",
"symbol": "META-DUSD",
}
)
self.nodes[0].generate(1)

# Check new names
result = self.nodes[0].gettoken("META-DUSD")["3"]
assert_equal(result["symbol"], "META-DUSD")
assert_equal(result["name"], "dMETA-Decentralized USD")
result = self.nodes[0].getpoolpair("META-DUSD")["3"]
assert_equal(result["symbol"], "META-DUSD")
assert_equal(result["name"], "dMETA-Decentralized USD")


if __name__ == "__main__":
PoolPairRenameTest().main()
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
"rpc_signrawtransaction.py",
"wallet_groups.py",
"p2p_disconnect_ban.py",
"feature_poolpair_rename.py",
"rpc_decodescript.py",
"rpc_blockchain.py",
"rpc_deprecated.py",
Expand Down
Loading