Skip to content
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
124 changes: 124 additions & 0 deletions include/xrpl/ledger/helpers/MPToken.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#pragma once

#include <xrpl/ledger/helpers/DirectoryHelpers.h>
#include <xrpl/ledger/helpers/MPTokenHelpers.h>
#include <xrpl/ledger/helpers/TokenHolderBase.h>

namespace xrpl {

class MPToken : public virtual TokenHolderBase
{
public:
MPToken(MPTokenIssuance const& issuance, AccountID const& holder)
: ReadOnlySLE(
issuance.readView().read(keylet::mptoken(issuance.getMptID(), holder)),
issuance.readView())
, TokenHolderBase(
issuance.readView(),
issuance.readView().read(keylet::mptoken(issuance.getMptID(), holder)),
issuance,
holder)
, issuance_(issuance)
{
}

MPTokenIssuance const&
getIssuance() const
{
return issuance_;
}

protected:
MPTokenIssuance const& issuance_;
};

class WritableMPToken : public virtual WritableTokenHolderBase, public virtual MPToken
{
public:
WritableMPToken(WritableMPTokenIssuance& issuance, AccountID const& holder)
: ReadOnlySLE(
issuance.applyView().peek(keylet::mptoken(issuance.getMptID(), holder)),
issuance.applyView())
, TokenHolderBase(
issuance.applyView(),
issuance.applyView().peek(keylet::mptoken(issuance.getMptID(), holder)),
issuance,
holder)
, WritableSLE(
issuance.applyView().peek(keylet::mptoken(issuance.getMptID(), holder)),
issuance.applyView())
, WritableTokenHolderBase(
issuance.applyView(),
issuance.applyView().peek(keylet::mptoken(issuance.getMptID(), holder)),
issuance,
holder)
, MPToken(issuance, holder)
, writableIssuance_(issuance)
{
}

// Resolve ambiguity: use writable operator-> for non-const, read-only for const
using WritableSLE::operator->;
using MPToken::operator->;
using WritableSLE::operator*;
using MPToken::operator*;

WritableMPTokenIssuance&
getWritableIssuance()
{
return writableIssuance_;
}

static TER
createMPToken(
WritableMPTokenIssuance& issuance,
AccountID const& account,
std::uint32_t const flags)
{
WritableMPToken mptoken = makeNew(issuance, account);

auto const ownerNode = mptoken.applyView().dirInsert(
keylet::ownerDir(account), mptoken->key(), describeOwnerDir(account));

if (!ownerNode)
return tecDIR_FULL; // LCOV_EXCL_LINE

(*mptoken)[sfAccount] = account;
(*mptoken)[sfMPTokenIssuanceID] = issuance.getMptID();
(*mptoken)[sfFlags] = flags;
(*mptoken)[sfOwnerNode] = *ownerNode;

return tesSUCCESS;
}

/** Create a WritableMPToken backed by a brand-new SLE
* (not yet inserted into the view).
*/
[[nodiscard]] static WritableMPToken
makeNew(WritableMPTokenIssuance& issuance, AccountID const& holder)
{
return WritableMPToken(
issuance, holder, std::make_shared<SLE>(keylet::mptoken(issuance.getMptID(), holder)));
}

private:
// This is a private constructor only used by `makeNew`
WritableMPToken(
WritableMPTokenIssuance& issuance,
AccountID const& holder,
std::shared_ptr<SLE> sle)
: ReadOnlySLE(sle, issuance.applyView())
, TokenHolderBase(issuance.applyView(), sle, issuance, holder)
, WritableSLE(sle, issuance.applyView())
, WritableTokenHolderBase(issuance.applyView(), sle, issuance, holder)
, MPToken(issuance, holder)
, writableIssuance_(issuance)
{
insert();
}

protected:
WritableMPTokenIssuance& writableIssuance_;
};

} // namespace xrpl
6 changes: 6 additions & 0 deletions include/xrpl/ledger/helpers/MPTokenHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ class MPTokenIssuance : public virtual TokenBase
[[nodiscard]] bool
requiresAuth() const override;

[[nodiscard]] bool
hasHolder(AccountID const& holder) const override
{
return readView_.exists(keylet::mptoken(mptID_, holder));
}

STAmount
accountHolds(
AccountID const& account,
Expand Down
158 changes: 158 additions & 0 deletions include/xrpl/ledger/helpers/RippleState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#pragma once

#include <xrpl/basics/Expected.h>
#include <xrpl/ledger/helpers/RippleStateHelpers.h>
#include <xrpl/ledger/helpers/TokenHolderBase.h>

namespace xrpl {

class RippleState : public virtual TokenHolderBase
{
public:
RippleState(IOUToken const& token, AccountID const& holder)
: ReadOnlySLE(
token.readView().read(keylet::line(holder, token.getIssuer(), token.getCurrency())),
token.readView())
, TokenHolderBase(
token.readView(),
token.readView().read(keylet::line(holder, token.getIssuer(), token.getCurrency())),
token,
holder)
, iouToken_(token)
{
}

/** Constructor with explicit SLE (for when SLE is already available) */
RippleState(
ReadView const& view,
std::shared_ptr<SLE const> sle,
IOUToken const& token,
AccountID const& holder)
: ReadOnlySLE(sle, view), TokenHolderBase(view, sle, token, holder), iouToken_(token)
{
}

IOUToken const&
getIOUToken() const
{
return iouToken_;
}

protected:
IOUToken const& iouToken_;
};

class WritableRippleState : public virtual WritableTokenHolderBase, public virtual RippleState
{
public:
WritableRippleState(ApplyView& view, WritableIOUToken& token, AccountID const& holder)
: ReadOnlySLE(view.peek(keylet::line(holder, token.getIssuer(), token.getCurrency())), view)
, TokenHolderBase(
view,
view.peek(keylet::line(holder, token.getIssuer(), token.getCurrency())),
token,
holder)
, WritableSLE(view.peek(keylet::line(holder, token.getIssuer(), token.getCurrency())), view)
, WritableTokenHolderBase(
view,
view.peek(keylet::line(holder, token.getIssuer(), token.getCurrency())),
token,
holder)
, RippleState(token, holder)
, writableIOUToken_(token)
{
}

/** Constructor with explicit SLE (for creation or when SLE is already available) */
WritableRippleState(
ApplyView& view,
std::shared_ptr<SLE> sle,
WritableIOUToken& token,
AccountID const& holder)
: ReadOnlySLE(sle, view)
, TokenHolderBase(view, sle, token, holder)
, WritableSLE(sle, view)
, WritableTokenHolderBase(view, sle, token, holder)
, RippleState(view, sle, token, holder)
, writableIOUToken_(token)
{
}

// Resolve ambiguity: use writable operator-> for non-const, read-only for const
using WritableSLE::operator->;
using RippleState::operator->;
using WritableSLE::operator*;
using RippleState::operator*;

WritableIOUToken&
getWritableIOUToken()
{
return writableIOUToken_;
}

//--------------------------------------------------------------------------
//
// Trust line operations
//
//--------------------------------------------------------------------------

/** Create a trust line

This can set an initial balance.
*/
[[nodiscard]] static TER
trustCreate(
ApplyView& view,
bool const bSrcHigh,
AccountID const& uSrcAccountID,
AccountID const& uDstAccountID,
uint256 const& uIndex, // ripple state entry
WritableAccountRoot& wrappedAcct, // the account being set.
bool const bAuth, // authorize account.
bool const bNoRipple, // others cannot ripple through
bool const bFreeze, // funds cannot leave
bool bDeepFreeze, // can neither receive nor send funds
STAmount const& saBalance, // balance of account being set.
// Issuer should be noAccount()
STAmount const& saLimit, // limit for account being set.
// Issuer should be the account being set.
std::uint32_t uQualityIn,
std::uint32_t uQualityOut,
beast::Journal j);

[[nodiscard]] static TER
trustDelete(
ApplyView& view,
std::shared_ptr<SLE> const& sleRippleState,
AccountID const& uLowAccountID,
AccountID const& uHighAccountID,
beast::Journal j);

/** Create a WritableRippleState backed by a brand-new SLE
* (not yet inserted into the view).
*/
[[nodiscard]] static WritableRippleState
makeNew(WritableIOUToken& token, AccountID const& holder)
{
return WritableRippleState(
token, holder, std::make_shared<SLE>(keylet::line(holder, token.getIssue())));
}

private:
// This is a private constructor only used by `makeNew`
WritableRippleState(WritableIOUToken& token, AccountID const& holder, std::shared_ptr<SLE> sle)
: ReadOnlySLE(sle, token.applyView())
, TokenHolderBase(token.applyView(), sle, token, holder)
, WritableSLE(sle, token.applyView())
, WritableTokenHolderBase(token.applyView(), sle, token, holder)
, RippleState(token, holder)
, writableIOUToken_(token)
{
insert();
}

protected:
WritableIOUToken& writableIOUToken_;
};

} // namespace xrpl
50 changes: 12 additions & 38 deletions include/xrpl/ledger/helpers/RippleStateHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class IOUToken : public virtual TokenBase
return currency_;
}

[[nodiscard]] Issue const&
getIssue() const
{
return issue_;
}

[[nodiscard]] bool
isGlobalFrozen() const override
{
Expand Down Expand Up @@ -132,6 +138,12 @@ class IOUToken : public virtual TokenBase
[[nodiscard]] bool
requiresAuth() const override;

[[nodiscard]] bool
hasHolder(AccountID const& holder) const override
{
return readView_.exists(keylet::line(issuer_, holder, currency_));
}

protected:
Issue const issue_;
AccountID const issuer_;
Expand Down Expand Up @@ -244,44 +256,6 @@ creditBalance(
Currency const& currency);
/** @} */

//------------------------------------------------------------------------------
//
// Trust line operations
//
//------------------------------------------------------------------------------

/** Create a trust line

This can set an initial balance.
*/
[[nodiscard]] TER
trustCreate(
ApplyView& view,
bool const bSrcHigh,
AccountID const& uSrcAccountID,
AccountID const& uDstAccountID,
uint256 const& uIndex, // ripple state entry
WritableAccountRoot& wrappedAcct, // the account being set.
bool const bAuth, // authorize account.
bool const bNoRipple, // others cannot ripple through
bool const bFreeze, // funds cannot leave
bool bDeepFreeze, // can neither receive nor send funds
STAmount const& saBalance, // balance of account being set.
// Issuer should be noAccount()
STAmount const& saLimit, // limit for account being set.
// Issuer should be the account being set.
std::uint32_t uQualityIn,
std::uint32_t uQualityOut,
beast::Journal j);

[[nodiscard]] TER
trustDelete(
ApplyView& view,
std::shared_ptr<SLE> const& sleRippleState,
AccountID const& uLowAccountID,
AccountID const& uHighAccountID,
beast::Journal j);

//------------------------------------------------------------------------------
//
// IOU issuance/redemption
Expand Down
3 changes: 3 additions & 0 deletions include/xrpl/ledger/helpers/TokenHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ class TokenBase : public virtual ReadOnlySLE
[[nodiscard]] virtual bool
requiresAuth() const = 0;

[[nodiscard]] virtual bool
hasHolder(AccountID const& holder) const = 0;

protected:
TokenBase(ReadView const& view, std::shared_ptr<SLE const> sle) : ReadOnlySLE(sle, view)
{
Expand Down
Loading
Loading