Skip to content

Commit

Permalink
Move error strings into DeFiErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamc1 committed Jun 9, 2023
1 parent a92ccc2 commit d5186dc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
48 changes: 48 additions & 0 deletions src/masternodes/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,54 @@ class DeFiErrors {
static Res AccountsFuturesErase() {
return Res::Err("Failed to erase futures");
}

static Res TransferDomainNotEnoughBalance(const std::string address) {
return Res::Err("Not enough balance in %s to cover \"EVM\" domain transfer", address);
}

static Res InvalidAuth() {
return Res::Err("tx must have at least one input from account owner");
}

static Res TransferDomainEVMNotEnabled() {
return Res::Err("Cannot create tx, EVM is not enabled");
}

static Res TransferDomainSameDomain() {
return Res::Err("Cannot transfer inside same domain");
}

static Res TransferDomainUnequalAmount() {
return Res::Err("Source amount must be equal to destination amount");
}

static Res TransferDomainIncorrectToken() {
return Res::Err("For transferdomain, only DFI token is currently supported");
}

static Res TransferDomainETHSourceAddress() {
return Res::Err("Src address must not be an ETH address in case of \"DVM\" domain");
}

static Res TransferDomainDFISourceAddress() {
return Res::Err("Src address must be an ETH address in case of \"EVM\" domain");
}

static Res TransferDomainInvalidSourceDomain() {
return Res::Err("Invalid domain set for \"src\" argument");
}

static Res TransferDomainETHDestinationAddress() {
return Res::Err("Dst address must not be an ETH address in case of \"DVM\" domain");
}

static Res TransferDomainDVMDestinationAddress() {
return Res::Err("Dst address must be an ETH address in case of \"EVM\" domain");
}

static Res TransferDomainInvalidDestinationDomain() {
return Res::Err("Invalid domain set for \"dst\" argument");
}
};

#endif // DEFI_MASTERNODES_ERRORS_H
Expand Down
24 changes: 12 additions & 12 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3823,7 +3823,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor {
arith_uint256 balanceIn = src.amount.nValue;
balanceIn *= CAMOUNT_TO_GWEI * WEI_IN_GWEI;
if (!evm_sub_balance(evmContext, HexStr(fromAddress.begin(), fromAddress.end()), ArithToUint256(balanceIn).ToArrayReversed(), tx.GetHash().ToArrayReversed())) {
return Res::Err("Not enough balance in %s to cover \"EVM\" domain transfer", EncodeDestination(dest));
return DeFiErrors::TransferDomainNotEnoughBalance(EncodeDestination(dest));
}
}
if (dst.domain == CTransferDomain::DVM) {
Expand Down Expand Up @@ -3889,7 +3889,7 @@ Res HasAuth(const CTransaction &tx, const CCoinsViewCache &coins, const CScript
}
}
}
return Res::Err("tx must have at least one input from account owner");
return DeFiErrors::InvalidAuth();
}

Res ValidateTransferDomain(const CTransaction &tx,
Expand All @@ -3903,22 +3903,22 @@ Res ValidateTransferDomain(const CTransaction &tx,

// Check if EVM feature is active
if (!IsEVMEnabled(height, mnview)) {
return Res::Err("Cannot create tx, EVM is not enabled");
return DeFiErrors::TransferDomainEVMNotEnabled();
}

// Iterate over array of transfers
for (const auto &[src, dst] : obj.transfers) {
// Reject if transfer is within same domain
if (src.domain == dst.domain)
return Res::Err("Cannot transfer inside same domain");
return DeFiErrors::TransferDomainSameDomain();

// Check for amounts out equals amounts in
if (src.amount.nValue != dst.amount.nValue)
return Res::Err("Source amount must be equal to destination amount");
return DeFiErrors::TransferDomainUnequalAmount();

// Restrict only for use with DFI token for now
if (src.amount.nTokenId != DCT_ID{0} || dst.amount.nTokenId != DCT_ID{0})
return Res::Err("For transferdomain, only DFI token is currently supported");
return DeFiErrors::TransferDomainIncorrectToken();

CTxDestination dest;

Expand All @@ -3928,7 +3928,7 @@ Res ValidateTransferDomain(const CTransaction &tx,
// Reject if source address is ETH address
if (ExtractDestination(src.address, dest)) {
if (dest.index() == WitV16KeyEthHashType) {
return Res::Err("Src address must not be an ETH address in case of \"DVM\" domain");
return DeFiErrors::TransferDomainETHSourceAddress();
}
}
// Check for authorization on source address
Expand All @@ -3939,34 +3939,34 @@ Res ValidateTransferDomain(const CTransaction &tx,
// Reject if source address is DFI address
if (ExtractDestination(src.address, dest)) {
if (dest.index() != WitV16KeyEthHashType) {
return Res::Err("Src address must be an ETH address in case of \"EVM\" domain");
return DeFiErrors::TransferDomainDFISourceAddress();
}
}
// Check for authorization on source address
res = HasAuth(tx, coins, src.address, AuthStrategy::EthKeyMatch);
if (!res)
return res;
} else
return Res::Err("Invalid domain set for \"src\" argument");
return DeFiErrors::TransferDomainInvalidSourceDomain();

// Destination validation
// Check domain type
if (dst.domain == CTransferDomain::DVM) {
// Reject if source address is ETH address
if (ExtractDestination(dst.address, dest)) {
if (dest.index() == WitV16KeyEthHashType) {
return Res::Err("Dst address must not be an ETH address in case of \"DVM\" domain");
return DeFiErrors::TransferDomainETHDestinationAddress();
}
}
} else if (dst.domain == CTransferDomain::EVM) {
// Reject if source address is DFI address
if (ExtractDestination(dst.address, dest)) {
if (dest.index() != WitV16KeyEthHashType) {
return Res::Err("Dst address must be an ETH address in case of \"EVM\" domain");
return DeFiErrors::TransferDomainDVMDestinationAddress();
}
}
} else
return Res::Err("Invalid domain set for \"dst\" argument");
return DeFiErrors::TransferDomainInvalidDestinationDomain();
}

return res;
Expand Down

0 comments on commit d5186dc

Please sign in to comment.