Skip to content

Commit

Permalink
chore(Communities): Refactor amounts handling for displaying, minting…
Browse files Browse the repository at this point in the history
…, airdropping and burning

Closes: #11491
  • Loading branch information
micieslak committed Aug 10, 2023
1 parent 9cc6bdc commit 8175bb4
Show file tree
Hide file tree
Showing 27 changed files with 383 additions and 159 deletions.
8 changes: 4 additions & 4 deletions src/app/modules/main/communities/tokens/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ method computeAirdropFee*(self: AccessInterface, communityId: string, tokensJson
method selfDestructCollectibles*(self: AccessInterface, communityId: string, collectiblesToBurnJsonString: string, contractUniqueKey: string) {.base.} =
raise newException(ValueError, "No implementation available")

method burnTokens*(self: AccessInterface, communityId: string, contractUniqueKey: string, amount: float64) {.base.} =
method burnTokens*(self: AccessInterface, communityId: string, contractUniqueKey: string, amount: string) {.base.} =
raise newException(ValueError, "No implementation available")

method deployCollectibles*(self: AccessInterface, communityId: string, address: string, name: string, symbol: string, description: string, supply: float64, infiniteSupply: bool, transferable: bool,
method deployCollectibles*(self: AccessInterface, communityId: string, address: string, name: string, symbol: string, description: string, supply: string, infiniteSupply: bool, transferable: bool,
selfDestruct: bool, chainId: int, imageCropInfoJson: string) {.base.} =
raise newException(ValueError, "No implementation available")

method deployAssets*(self: AccessInterface, communityId: string, address: string, name: string, symbol: string, description: string, supply: float64, infiniteSupply: bool, decimals: int,
method deployAssets*(self: AccessInterface, communityId: string, address: string, name: string, symbol: string, description: string, supply: string, infiniteSupply: bool, decimals: int,
chainId: int, imageCropInfoJson: string) {.base.} =
raise newException(ValueError, "No implementation available")

Expand All @@ -44,7 +44,7 @@ method computeDeployFee*(self: AccessInterface, chainId: int, accountAddress: st
method computeSelfDestructFee*(self: AccessInterface, collectiblesToBurnJsonString: string, contractUniqueKey: string) {.base.} =
raise newException(ValueError, "No implementation available")

method computeBurnFee*(self: AccessInterface, contractUniqueKey: string, amount: float64) {.base.} =
method computeBurnFee*(self: AccessInterface, contractUniqueKey: string, amount: string) {.base.} =
raise newException(ValueError, "No implementation available")

method onDeployFeeComputed*(self: AccessInterface, ethCurrency: CurrencyAmount, fiatCurrency: CurrencyAmount, errorCode: ComputeFeeErrorCode) {.base.} =
Expand Down
11 changes: 8 additions & 3 deletions src/app/modules/main/communities/tokens/models/token_model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import NimQml, Tables, strformat, sequtils, stint
import token_item
import token_owners_item
import token_owners_model
import ../../../../../../app_service/service/community/dto/community
import ../../../../../../app_service/service/community_tokens/dto/community_token
import ../../../../../../app_service/common/utils
import ../../../../../../app_service/common/types
Expand Down Expand Up @@ -30,6 +31,7 @@ type
Decimals
BurnState
RemotelyDestructState
MultiplierIndex

QtObject:
type TokenModel* = ref object of QAbstractListModel
Expand Down Expand Up @@ -170,6 +172,7 @@ QtObject:
ModelRole.Decimals.int:"decimals",
ModelRole.BurnState.int:"burnState",
ModelRole.RemotelyDestructState.int:"remotelyDestructState",
ModelRole.MultiplierIndex.int:"multiplierIndex",
}.toTable

method data(self: TokenModel, index: QModelIndex, role: int): QVariant =
Expand All @@ -194,7 +197,7 @@ QtObject:
result = newQVariant(item.tokenDto.description)
of ModelRole.Supply:
# we need to present maxSupply - destructedAmount
result = newQVariant(supplyByType(item.tokenDto.supply - item.destructedAmount, item.tokenDto.tokenType))
result = newQVariant((item.tokenDto.supply - item.destructedAmount).toString(10))
of ModelRole.InfiniteSupply:
result = newQVariant(item.tokenDto.infiniteSupply)
of ModelRole.Transferable:
Expand All @@ -216,17 +219,19 @@ QtObject:
of ModelRole.AccountName:
result = newQVariant(item.accountName)
of ModelRole.RemainingSupply:
result = newQVariant(supplyByType(item.remainingSupply, item.tokenDto.tokenType))
result = newQVariant(item.remainingSupply.toString(10))
of ModelRole.Decimals:
result = newQVariant(item.tokenDto.decimals)
of ModelRole.BurnState:
result = newQVariant(item.burnState.int)
of ModelRole.RemotelyDestructState:
let destructStatus = if len(item.remoteDestructedAddresses) > 0: ContractTransactionStatus.InProgress.int else: ContractTransactionStatus.Completed.int
result = newQVariant(destructStatus)
of ModelRole.MultiplierIndex:
result = newQVariant(if item.tokenDto.tokenType == TokenType.ERC20: 18 else: 0)

proc `$`*(self: TokenModel): string =
for i in 0 ..< self.items.len:
result &= fmt"""TokenModel:
[{i}]:({$self.items[i]})
"""
"""
32 changes: 10 additions & 22 deletions src/app/modules/main/communities/tokens/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,17 @@ proc authenticate(self: Module) =
else:
self.controller.authenticateUser()

# for collectibles conversion is: "1" -> Uint256(1)
# for assets amount is converted to basic units (wei-like): "1.5" -> Uint256(1500000000000000000)
proc convertAmountByTokenType(self: Module, tokenType: TokenType, amount: float64): Uint256 =
const decimals = 18
case tokenType
of TokenType.ERC721:
return stint.parse($amount.int, Uint256)
of TokenType.ERC20:
return conversion.eth2Wei(amount, decimals)
else:
error "Converting amount - unknown token type", tokenType=tokenType

proc getTokenAndAmountList(self: Module, communityId: string, tokensJsonString: string): seq[CommunityTokenAndAmount] =
try:
let tokensJson = tokensJsonString.parseJson
for token in tokensJson:
let contractUniqueKey = token["contractUniqueKey"].getStr
let tokenDto = self.controller.findContractByUniqueId(contractUniqueKey)
let amountStr = token["amount"].getFloat
let amountStr = token["amount"].getStr
if tokenDto.tokenType == TokenType.Unknown:
error "Can't find token for community", contractUniqueKey=contractUniqueKey
return @[]
result.add(CommunityTokenAndAmount(communityToken: tokenDto, amount: self.convertAmountByTokenType(tokenDto.tokenType, amountStr)))
result.add(CommunityTokenAndAmount(communityToken: tokenDto, amount: amountStr.parse(Uint256)))
except Exception as e:
error "Error getTokenAndAmountList", msg = e.msg

Expand Down Expand Up @@ -141,22 +129,22 @@ method selfDestructCollectibles*(self: Module, communityId: string, collectibles
self.tempContractAction = ContractAction.SelfDestruct
self.authenticate()

method burnTokens*(self: Module, communityId: string, contractUniqueKey: string, amount: float64) =
method burnTokens*(self: Module, communityId: string, contractUniqueKey: string, amount: string) =
let tokenDto = self.controller.findContractByUniqueId(contractUniqueKey)
self.tempCommunityId = communityId
self.tempContractUniqueKey = contractUniqueKey
self.tempAmount = self.convertAmountByTokenType(tokenDto.tokenType, amount)
self.tempAmount = amount.parse(Uint256)
self.tempContractAction = ContractAction.Burn
self.authenticate()

method deployCollectibles*(self: Module, communityId: string, fromAddress: string, name: string, symbol: string, description: string,
supply: float64, infiniteSupply: bool, transferable: bool, selfDestruct: bool, chainId: int, imageCropInfoJson: string) =
supply: string, infiniteSupply: bool, transferable: bool, selfDestruct: bool, chainId: int, imageCropInfoJson: string) =
self.tempAddressFrom = fromAddress
self.tempCommunityId = communityId
self.tempChainId = chainId
self.tempDeploymentParams.name = name
self.tempDeploymentParams.symbol = symbol
self.tempDeploymentParams.supply = self.convertAmountByTokenType(TokenType.ERC721, supply)
self.tempDeploymentParams.supply = supply.parse(Uint256)
self.tempDeploymentParams.infiniteSupply = infiniteSupply
self.tempDeploymentParams.transferable = transferable
self.tempDeploymentParams.remoteSelfDestruct = selfDestruct
Expand All @@ -167,14 +155,14 @@ method deployCollectibles*(self: Module, communityId: string, fromAddress: strin
self.tempContractAction = ContractAction.Deploy
self.authenticate()

method deployAssets*(self: Module, communityId: string, fromAddress: string, name: string, symbol: string, description: string, supply: float64, infiniteSupply: bool, decimals: int,
method deployAssets*(self: Module, communityId: string, fromAddress: string, name: string, symbol: string, description: string, supply: string, infiniteSupply: bool, decimals: int,
chainId: int, imageCropInfoJson: string) =
self.tempAddressFrom = fromAddress
self.tempCommunityId = communityId
self.tempChainId = chainId
self.tempDeploymentParams.name = name
self.tempDeploymentParams.symbol = symbol
self.tempDeploymentParams.supply = self.convertAmountByTokenType(TokenType.ERC20, supply)
self.tempDeploymentParams.supply = supply.parse(Uint256)
self.tempDeploymentParams.infiniteSupply = infiniteSupply
self.tempDeploymentParams.decimals = decimals
self.tempDeploymentParams.tokenUri = utl.changeCommunityKeyCompression(communityId) & "/"
Expand Down Expand Up @@ -221,9 +209,9 @@ method computeSelfDestructFee*(self: Module, collectiblesToBurnJsonString: strin
let walletAndAmountList = self.getWalletAndAmountListFromJson(collectiblesToBurnJsonString)
self.controller.computeSelfDestructFee(walletAndAmountList, contractUniqueKey)

method computeBurnFee*(self: Module, contractUniqueKey: string, amount: float64) =
method computeBurnFee*(self: Module, contractUniqueKey: string, amount: string) =
let tokenDto = self.controller.findContractByUniqueId(contractUniqueKey)
self.controller.computeBurnFee(contractUniqueKey, self.convertAmountByTokenType(tokenDto.tokenType, amount))
self.controller.computeBurnFee(contractUniqueKey, amount.parse(Uint256))

proc createUrl(self: Module, chainId: int, transactionHash: string): string =
let network = self.controller.getNetwork(chainId)
Expand Down
8 changes: 4 additions & 4 deletions src/app/modules/main/communities/tokens/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ QtObject:
result.QObject.setup
result.communityTokensModule = communityTokensModule

proc deployCollectible*(self: View, communityId: string, fromAddress: string, name: string, symbol: string, description: string, supply: float, infiniteSupply: bool, transferable: bool, selfDestruct: bool, chainId: int, imageCropInfoJson: string) {.slot.} =
proc deployCollectible*(self: View, communityId: string, fromAddress: string, name: string, symbol: string, description: string, supply: string, infiniteSupply: bool, transferable: bool, selfDestruct: bool, chainId: int, imageCropInfoJson: string) {.slot.} =
self.communityTokensModule.deployCollectibles(communityId, fromAddress, name, symbol, description, supply, infiniteSupply, transferable, selfDestruct, chainId, imageCropInfoJson)

proc deployAssets*(self: View, communityId: string, fromAddress: string, name: string, symbol: string, description: string, supply: float, infiniteSupply: bool, decimals: int, chainId: int, imageCropInfoJson: string) {.slot.} =
proc deployAssets*(self: View, communityId: string, fromAddress: string, name: string, symbol: string, description: string, supply: string, infiniteSupply: bool, decimals: int, chainId: int, imageCropInfoJson: string) {.slot.} =
self.communityTokensModule.deployAssets(communityId, fromAddress, name, symbol, description, supply, infiniteSupply, decimals, chainId, imageCropInfoJson)

proc removeCommunityToken*(self: View, communityId: string, chainId: int, address: string) {.slot.} =
Expand All @@ -39,7 +39,7 @@ QtObject:
proc selfDestructCollectibles*(self: View, communityId: string, collectiblesToBurnJsonString: string, contractUniqueKey: string) {.slot.} =
self.communityTokensModule.selfDestructCollectibles(communityId, collectiblesToBurnJsonString, contractUniqueKey)

proc burnTokens*(self: View, communityId: string, contractUniqueKey: string, amount: float) {.slot.} =
proc burnTokens*(self: View, communityId: string, contractUniqueKey: string, amount: string) {.slot.} =
self.communityTokensModule.burnTokens(communityId, contractUniqueKey, amount)

proc deployFeeUpdated*(self: View, ethCurrency: QVariant, fiatCurrency: QVariant, errorCode: int) {.signal.}
Expand All @@ -53,7 +53,7 @@ QtObject:
proc computeSelfDestructFee*(self: View, collectiblesToBurnJsonString: string, contractUniqueKey: string) {.slot.} =
self.communityTokensModule.computeSelfDestructFee(collectiblesToBurnJsonString, contractUniqueKey)

proc computeBurnFee*(self: View, contractUniqueKey: string, amount: float) {.slot.} =
proc computeBurnFee*(self: View, contractUniqueKey: string, amount: string) {.slot.} =
self.communityTokensModule.computeBurnFee(contractUniqueKey, amount)

proc updateDeployFee*(self: View, ethCurrency: CurrencyAmount, fiatCurrency: CurrencyAmount, errorCode: int) =
Expand Down
1 change: 1 addition & 0 deletions src/app_service/common/conversion.nim
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ proc toUInt256*(flt: float): UInt256 =
proc toUInt64*(flt: float): StUInt[64] =
toStUInt(flt, StUInt[64])

# This method may introduce distortions and should be avoided if possible.
proc eth2Wei*(eth: float, decimals: int = 18): UInt256 =
let weiValue = eth * parseFloat(alignLeft("1", decimals + 1, '0'))
weiValue.toUInt256
Expand Down
11 changes: 0 additions & 11 deletions src/app_service/service/community_tokens/dto/community_token.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,3 @@ proc toCommunityTokenDto*(jsonObj: JsonNode): CommunityTokenDto =
proc parseCommunityTokens*(response: RpcResponse[JsonNode]): seq[CommunityTokenDto] =
result = map(response.result.getElems(),
proc(x: JsonNode): CommunityTokenDto = x.toCommunityTokenDto())

proc supplyByType*(supply: Uint256, tokenType: TokenType): float64 =
try:
var eths: string
if tokenType == TokenType.ERC20:
eths = wei2Eth(supply, 18)
else:
eths = supply.toString(10)
return parseFloat(eths)
except Exception as e:
error "Error parsing supply by type ", msg=e.msg, supply=supply, tokenType=tokenType
13 changes: 12 additions & 1 deletion storybook/pages/AirdropsSettingsPanelPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ SplitView {
assetsModel: AssetsModel {}
collectiblesModel: ListModel {}

accountsModel: ListModel {}

CollectiblesModel {
id: collectiblesModel
}
Expand All @@ -118,6 +120,10 @@ SplitView {
name: "supply"
expression: ((model.index + 1) * 115).toString()
},
ExpressionRole {
name: "multiplierIndex"
expression: 0
},
ExpressionRole {
name: "infiniteSupply"
expression: !(model.index % 4)
Expand Down Expand Up @@ -158,7 +164,12 @@ SplitView {
proxyRoles: [
ExpressionRole {
name: "supply"
expression: ((model.index + 1) * 258).toString()
expression: ((model.index + 1) * 584).toString()
+ "0".repeat(18)
},
ExpressionRole {
name: "multiplierIndex"
expression: 18
},
ExpressionRole {
name: "infiniteSupply"
Expand Down
13 changes: 11 additions & 2 deletions storybook/pages/HoldingsDropdownPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ SplitView {
proxyRoles: [
ExpressionRole {
name: "supply"
expression: (model.index + 1) * 115
expression: ((model.index + 1) * 115).toString()
},
ExpressionRole {
name: "multiplierIndex"
expression: 0
},
ExpressionRole {
name: "infiniteSupply"
Expand Down Expand Up @@ -104,7 +108,12 @@ SplitView {
proxyRoles: [
ExpressionRole {
name: "supply"
expression: (model.index + 1) * 584
expression: ((model.index + 1) * 584).toString()
+ "0".repeat(18)
},
ExpressionRole {
name: "multiplierIndex"
expression: 18
},
ExpressionRole {
name: "infiniteSupply"
Expand Down
20 changes: 17 additions & 3 deletions storybook/pages/InlineNetworksComboBoxPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,30 @@ Item {
{
name: "Optimism",
icon: Style.svg(ModelsData.networks.optimism),
amount: 300,
amount: "300",
multiplierIndex: 0,
infiniteAmount: false
},
{
name: "Arbitrum",
icon: Style.svg(ModelsData.networks.arbitrum),
amount: 400,
amount: "400000",
multiplierIndex: 3,
infiniteAmount: false
},
{
name: "Hermez",
icon: Style.svg(ModelsData.networks.hermez),
amount: 0,
amount: "0",
multiplierIndex: 0,
infiniteAmount: true
},
{
name: "Ethereum",
icon: Style.svg(ModelsData.networks.ethereum),
amount: "12" + "0".repeat(18),
multiplierIndex: 18,
infiniteAmount: false
}
]

Expand Down Expand Up @@ -78,6 +88,10 @@ Item {
Layout.alignment: Qt.AlignHCenter
text: `current amount: ${comboBox.currentAmount}`
}
Label {
Layout.alignment: Qt.AlignHCenter
text: `current multiplier index: ${comboBox.currentMultiplierIndex}`
}
Label {
Layout.alignment: Qt.AlignHCenter
text: `current amount infinite: ${comboBox.currentInfiniteAmount}`
Expand Down
27 changes: 24 additions & 3 deletions storybook/pages/TokenPanelPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

import Qt.labs.settings 1.0

import Models 1.0
import Storybook 1.0
import utils 1.0
Expand All @@ -20,20 +22,30 @@ SplitView {
{
name: "Optimism",
icon: Style.svg(ModelsData.networks.optimism),
amount: 300,
amount: "300",
multiplierIndex: 0,
infiniteAmount: false
},
{
name: "Arbitrum",
icon: Style.svg(ModelsData.networks.arbitrum),
amount: 400,
amount: "400000",
multiplierIndex: 3,
infiniteAmount: false
},
{
name: "Hermez",
icon: Style.svg(ModelsData.networks.hermez),
amount: 500,
amount: "0",
multiplierIndex: 0,
infiniteAmount: true
},
{
name: "Ethereum",
icon: Style.svg(ModelsData.networks.ethereum),
amount: "12" + "0".repeat(18),
multiplierIndex: 18,
infiniteAmount: false
}
]

Expand Down Expand Up @@ -119,8 +131,17 @@ SplitView {
text: ""
}
}

Label {
text: "amount: " + tokenPanel.amount
}
}
}

Settings {
property alias networksModelCheckBoxChecked:
networksModelCheckBox.checked
}
}

// category: Panels
Loading

0 comments on commit 8175bb4

Please sign in to comment.