Skip to content

Commit

Permalink
feat(@desktop/communities): Adjust owner and master tokens deployment…
Browse files Browse the repository at this point in the history
… flow to new API

Issue #11954
  • Loading branch information
endulab committed Sep 29, 2023
1 parent 18a50c6 commit beeaff5
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/app/modules/main/communities/tokens/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ proc getCommunityTokens*(self: Controller, communityId: string): seq[CommunityTo
proc computeDeployFee*(self: Controller, chainId: int, accountAddress: string, tokenType: TokenType, requestId: string) =
self.communityTokensService.computeDeployFee(chainId, accountAddress, tokenType, requestId)

proc computeDeployOwnerContractsFee*(self: Controller, chainId: int, accountAddress: string, requestId: string) =
self.communityTokensService.computeDeployOwnerContractsFee(chainId, accountAddress, requestId)
proc computeDeployOwnerContractsFee*(self: Controller, chainId: int, accountAddress: string, communityId: string, ownerDeploymentParams: DeploymentParameters, masterDeploymentParams: DeploymentParameters, requestId: string) =
self.communityTokensService.computeDeployOwnerContractsFee(chainId, accountAddress, communityId, ownerDeploymentParams, masterDeploymentParams, requestId)

proc computeSelfDestructFee*(self: Controller, walletAndAmountList: seq[WalletAndAmount], contractUniqueKey: string, addressFrom: string, requestId: string) =
self.communityTokensService.computeSelfDestructFee(walletAndAmountList, contractUniqueKey, addressFrom, requestId)
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/main/communities/tokens/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ method onUserAuthenticated*(self: AccessInterface, password: string) {.base.} =
method resetTempValues*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")

method computeDeployFee*(self: AccessInterface, chainId: int, accountAddress: string, tokenType: TokenType, isOwnerDeployment: bool, requestId: string) {.base.} =
method computeDeployFee*(self: AccessInterface, communityId: string, chainId: int, accountAddress: string, tokenType: TokenType, isOwnerDeployment: bool, requestId: string) {.base.} =
raise newException(ValueError, "No implementation available")

method computeSelfDestructFee*(self: AccessInterface, collectiblesToBurnJsonString: string, contractUniqueKey: string, addressFrom: string, requestId: string) {.base.} =
Expand Down
18 changes: 11 additions & 7 deletions src/app/modules/main/communities/tokens/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ method deployCollectibles*(self: Module, communityId: string, fromAddress: strin
self.tempContractAction = ContractAction.Deploy
self.authenticate()

proc createOwnerAndMasterDeploymentParams(self: Module, communityId: string): (DeploymentParameters, DeploymentParameters) =
let communityDto = self.controller.getCommunityById(communityId)
let commName = communityDto.name
let commNameShort = try: commName[0 .. 2].toUpper except: commName.toUpper
return (DeploymentParameters(name: "Owner-" & commName, symbol: "OWN" & commNameShort, supply: stint.u256("1"), infiniteSupply: false, transferable: true, remoteSelfDestruct: false, tokenUri: utl.changeCommunityKeyCompression(communityId) & "/"),
DeploymentParameters(name: "TMaster-" & commName, symbol: "TM" & commNameShort, infiniteSupply: true, transferable: false, remoteSelfDestruct: true, tokenUri: utl.changeCommunityKeyCompression(communityId) & "/"))

method deployOwnerToken*(self: Module, communityId: string, fromAddress: string, ownerName: string, ownerSymbol: string, ownerDescription: string,
masterName: string, masterSymbol: string, masterDescription: string, chainId: int, imageCropInfoJson: string) =
let ownerToken = self.controller.getOwnerToken(communityId)
Expand All @@ -185,11 +192,7 @@ method deployOwnerToken*(self: Module, communityId: string, fromAddress: string,
self.tempAddressFrom = fromAddress
self.tempCommunityId = communityId
self.tempChainId = chainId
let communityDto = self.controller.getCommunityById(communityId)
let commName = communityDto.name
let commNameShort = try: commName[0 .. 2].toUpper except: commName.toUpper
self.tempOwnerDeploymentParams = DeploymentParameters(name: "Owner-" & commName, symbol: "OWN" & commNameShort, supply: stint.u256("1"), infiniteSupply: false, transferable: true, remoteSelfDestruct: false, tokenUri: utl.changeCommunityKeyCompression(communityId) & "/")
self.tempMasterDeploymentParams = DeploymentParameters(name: "TMaster-" & commName, symbol: "TM" & commNameShort, infiniteSupply: true, transferable: false, remoteSelfDestruct: true, tokenUri: utl.changeCommunityKeyCompression(communityId) & "/")
(self.tempOwnerDeploymentParams, self.tempMasterDeploymentParams) = self.createOwnerAndMasterDeploymentParams(communityId)
self.tempOwnerTokenMetadata.description = ownerDescription
self.tempOwnerTokenMetadata.tokenType = TokenType.ERC721
self.tempMasterTokenMetadata.description = masterDescription
Expand Down Expand Up @@ -250,9 +253,10 @@ method onAirdropFeesComputed*(self: Module, args: AirdropFeesArgs) =
method onBurnFeeComputed*(self: Module, ethCurrency: CurrencyAmount, fiatCurrency: CurrencyAmount, errorCode: ComputeFeeErrorCode, responseId: string) =
self.view.updateBurnFee(ethCurrency, fiatCurrency, errorCode.int, responseId)

method computeDeployFee*(self: Module, chainId: int, accountAddress: string, tokenType: TokenType, isOwnerDeployment: bool, requestId: string) =
method computeDeployFee*(self: Module, communityId: string, chainId: int, accountAddress: string, tokenType: TokenType, isOwnerDeployment: bool, requestId: string) =
if isOwnerDeployment:
self.controller.computeDeployOwnerContractsFee(chainId, accountAddress, requestId)
let (ownerDeploymentParams, masterDeploymentParams) = self.createOwnerAndMasterDeploymentParams(communityId)
self.controller.computeDeployOwnerContractsFee(chainId, accountAddress, communityId, ownerDeploymentParams, masterDeploymentParams, requestId)
else:
self.controller.computeDeployFee(chainId, accountAddress, tokenType, requestId)

Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/main/communities/tokens/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ QtObject:
proc airdropFeesUpdated*(self: View, json: string) {.signal.}
proc burnFeeUpdated*(self: View, ethCurrency: QVariant, fiatCurrency: QVariant, errorCode: int, responseId: string) {.signal.}

proc computeDeployFee*(self: View, chainId: int, accountAddress: string, tokenType: int, isOwnerDeployment: bool, requestId: string) {.slot.} =
self.communityTokensModule.computeDeployFee(chainId, accountAddress, intToEnum(tokenType, TokenType.Unknown), isOwnerDeployment, requestId)
proc computeDeployFee*(self: View, communityId: string, chainId: int, accountAddress: string, tokenType: int, isOwnerDeployment: bool, requestId: string) {.slot.} =
self.communityTokensModule.computeDeployFee(communityId, chainId, accountAddress, intToEnum(tokenType, TokenType.Unknown), isOwnerDeployment, requestId)

proc computeSelfDestructFee*(self: View, collectiblesToBurnJsonString: string, contractUniqueKey: string, addressFrom: string, requestId: string) {.slot.} =
self.communityTokensModule.computeSelfDestructFee(collectiblesToBurnJsonString, contractUniqueKey, addressFrom, requestId)
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/main/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ proc init*(self: Controller) =

self.events.on(SIGNAL_OWNER_TOKEN_DEPLOY_STATUS) do(e: Args):
let args = OwnerTokenDeployedStatusArgs(e)
self.delegate.onOwnerTokenDeployStateChanged(args.communityId, args.chainId, args.ownerContractAddress, args.masterContractAddress, args.deployState)
self.delegate.onOwnerTokenDeployStateChanged(args.communityId, args.chainId, args.ownerContractAddress, args.masterContractAddress, args.deployState, args.transactionHash)

self.events.on(SIGNAL_COMMUNITY_TOKEN_REMOVED) do(e: Args):
let args = CommunityTokenRemovedArgs(e)
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/main/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ method onCommunityTokenOwnersFetched*(self: AccessInterface, communityId: string
method onCommunityTokenDeployStateChanged*(self: AccessInterface, communityId: string, chainId: int, contractAddress: string, deployState: DeployState) {.base.} =
raise newException(ValueError, "No implementation available")

method onOwnerTokenDeployStateChanged*(self: AccessInterface, communityId: string, chainId: int, ownerContractAddress: string, masterContractAddress: string, deployState: DeployState) {.base.} =
method onOwnerTokenDeployStateChanged*(self: AccessInterface, communityId: string, chainId: int, ownerContractAddress: string, masterContractAddress: string, deployState: DeployState, transactionHash: string) {.base.} =
raise newException(ValueError, "No implementation available")

method onCommunityTokenSupplyChanged*(self: AccessInterface, communityId: string, chainId: int, contractAddress: string, supply: Uint256, remainingSupply: Uint256, destructedAmount: Uint256) {.base.} =
Expand Down
6 changes: 4 additions & 2 deletions src/app/modules/main/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1078,11 +1078,13 @@ method onCommunityTokenDeployStateChanged*[T](self: Module[T], communityId: stri
if item.id != "":
item.updateCommunityTokenDeployState(chainId, contractAddress, deployState)

method onOwnerTokenDeployStateChanged*[T](self: Module[T], communityId: string, chainId: int, ownerContractAddress: string, masterContractAddress: string, deployState: DeployState) =
method onOwnerTokenDeployStateChanged*[T](self: Module[T], communityId: string, chainId: int, ownerContractAddress: string, masterContractAddress: string, deployState: DeployState, transactionHash: string) =
let item = self.view.model().getItemById(communityId)
if item.id != "":
# update temporary master contract address first
item.updateCommunityTokenAddress(chainId, temporaryMasterContractAddress(ownerContractAddress), masterContractAddress)
if transactionHash != "":
item.updateCommunityTokenAddress(chainId, temporaryMasterContractAddress(transactionHash), masterContractAddress)
item.updateCommunityTokenAddress(chainId, temporaryOwnerContractAddress(transactionHash), ownerContractAddress)
# then update states
item.updateCommunityTokenDeployState(chainId, ownerContractAddress, deployState)
item.updateCommunityTokenDeployState(chainId, masterContractAddress, deployState)
Expand Down
11 changes: 10 additions & 1 deletion src/app_service/service/community_tokens/async_tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type
chainId: int
addressFrom: string
requestId: string
ownerParams: JsonNode
masterParams: JsonNode
communityId: string
signerPubKey: string

const asyncGetDeployOwnerContractsFeesTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[AsyncDeployOwnerContractsFeesArg](argEncoded)
Expand All @@ -30,7 +34,12 @@ const asyncGetDeployOwnerContractsFeesTask: Task = proc(argEncoded: string) {.gc
var feeTable: Table[int, SuggestedFeesDto] # fees for chain
let response = eth.suggestedFees(arg.chainId).result
feeTable[arg.chainId] = response.toSuggestedFeesDto()
let deployGas = community_tokens.deployOwnerTokenEstimate().result.getInt

# get deployment signature
let signatureResponse = community_tokens.createCommunityTokenDeploymentSignature(arg.chainId, arg.addressFrom, arg.communityId)
let signature = signatureResponse.result.getStr()

let deployGas = community_tokens.deployOwnerTokenEstimate(arg.chainId, arg.addressFrom, arg.ownerParams, arg.masterParams, signature, arg.communityId, arg.signerPubKey).result.getInt
gasTable[(arg.chainId, "")] = deployGas
arg.finish(%* {
"feeTable": tableToJsonArray(feeTable),
Expand Down
50 changes: 37 additions & 13 deletions src/app_service/service/community_tokens/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -311,22 +311,32 @@ QtObject:
if not receivedData.success:
warn "Owner contracts not deployed", chainId=ownerTransactionDetails.ownerToken.chainId, address=ownerTransactionDetails.ownerToken.address
var masterContractAddress = ownerTransactionDetails.masterToken.address
var ownerContractAddress = ownerTransactionDetails.ownerToken.address

try:
# get master token address from transaction logs
if receivedData.success:
let response = tokens_backend.getMasterTokenContractAddressFromHash(ownerTransactionDetails.masterToken.chainId, receivedData.transactionHash)
var response = tokens_backend.getOwnerTokenContractAddressFromHash(ownerTransactionDetails.ownerToken.chainId, receivedData.transactionHash)
ownerContractAddress = response.result.getStr()
if ownerContractAddress == "":
raise newException(RpcException, "owner contract address is empty")
response = tokens_backend.getMasterTokenContractAddressFromHash(ownerTransactionDetails.masterToken.chainId, receivedData.transactionHash)
masterContractAddress = response.result.getStr()
if masterContractAddress == "":
raise newException(RpcException, "master contract address is empty")

debug "Minted owner token contract address:", ownerContractAddress
debug "Minted master token contract address:", masterContractAddress

# update master token address
discard updateCommunityTokenAddress(ownerTransactionDetails.masterToken.chainId, ownerTransactionDetails.masterToken.address, masterContractAddress)
# update owner token address
discard updateCommunityTokenAddress(ownerTransactionDetails.ownerToken.chainId, ownerTransactionDetails.ownerToken.address, ownerContractAddress)
#update db state for owner and master token
discard updateCommunityTokenState(ownerTransactionDetails.ownerToken.chainId, ownerTransactionDetails.ownerToken.address, deployState)
discard updateCommunityTokenState(ownerTransactionDetails.ownerToken.chainId, ownerContractAddress, deployState)
discard updateCommunityTokenState(ownerTransactionDetails.masterToken.chainId, masterContractAddress, deployState)
# now add owner token to community and publish update
var response = tokens_backend.addCommunityToken(ownerTransactionDetails.communityId, ownerTransactionDetails.ownerToken.chainId, ownerTransactionDetails.ownerToken.address)
var response = tokens_backend.addCommunityToken(ownerTransactionDetails.communityId, ownerTransactionDetails.ownerToken.chainId, ownerContractAddress)
if response.error != nil:
let error = Json.decode($response.error, RpcError)
raise newException(RpcException, "error adding owner token: " & error.message)
Expand All @@ -340,7 +350,7 @@ QtObject:
error "Error updating owner contracts state", message = getCurrentExceptionMsg()

let data = OwnerTokenDeployedStatusArgs(communityId: ownerTransactionDetails.communityId, chainId: ownerTransactionDetails.ownerToken.chainId,
ownerContractAddress: ownerTransactionDetails.ownerToken.address,
ownerContractAddress: ownerContractAddress,
masterContractAddress: masterContractAddress,
deployState: deployState,
transactionHash: receivedData.transactionHash)
Expand Down Expand Up @@ -446,8 +456,11 @@ QtObject:
addedCommunityToken.chainId,
)

proc temporaryMasterContractAddress*(ownerContractAddress: string): string =
return ownerContractAddress & "-master"
proc temporaryMasterContractAddress*(ownerContractTransactionHash: string): string =
return ownerContractTransactionHash & "-master"

proc temporaryOwnerContractAddress*(ownerContractTransactionHash: string): string =
return ownerContractTransactionHash & "-owner"

proc deployOwnerContracts*(self: Service, communityId: string, addressFrom: string, password: string,
ownerDeploymentParams: DeploymentParameters, ownerTokenMetadata: CommunityTokensMetadataDto,
Expand All @@ -458,14 +471,21 @@ QtObject:
if txData.source == parseAddress(ZERO_ADDRESS):
return

let response = tokens_backend.deployOwnerToken(chainId, %ownerDeploymentParams, %masterDeploymentParams, %txData, password)
let ownerContractAddress = response.result["contractAddress"].getStr()
# set my pub key as signer
let signerPubKey = singletonInstance.userProfile.getPubKey()

# get deployment signature
let signatureResponse = tokens_backend.createCommunityTokenDeploymentSignature(chainId, addressFrom, communityId)
let signature = signatureResponse.result.getStr()

# deploy contract
let response = tokens_backend.deployOwnerToken(chainId, %ownerDeploymentParams, %masterDeploymentParams,
signature, communityId, signerPubKey, %txData, password)
let transactionHash = response.result["transactionHash"].getStr()
debug "Deployed owner contract address ", ownerContractAddress=ownerContractAddress
debug "Deployment transaction hash ", transactionHash=transactionHash

var ownerToken = self.createCommunityToken(ownerDeploymentParams, ownerTokenMetadata, chainId, ownerContractAddress, communityId, addressFrom, PrivilegesLevel.Owner)
var masterToken = self.createCommunityToken(masterDeploymentParams, masterTokenMetadata, chainId, temporaryMasterContractAddress(ownerContractAddress), communityId, addressFrom, PrivilegesLevel.TokenMaster)
var ownerToken = self.createCommunityToken(ownerDeploymentParams, ownerTokenMetadata, chainId, temporaryOwnerContractAddress(transactionHash), communityId, addressFrom, PrivilegesLevel.Owner)
var masterToken = self.createCommunityToken(masterDeploymentParams, masterTokenMetadata, chainId, temporaryMasterContractAddress(transactionHash), communityId, addressFrom, PrivilegesLevel.TokenMaster)

var croppedImage = croppedImageJson.parseJson
ownerToken.image = croppedImage{"imagePath"}.getStr
Expand Down Expand Up @@ -687,15 +707,19 @@ QtObject:
#TODO: handle error - emit error signal
error "Error loading fees", msg = e.msg

proc computeDeployOwnerContractsFee*(self: Service, chainId: int, accountAddress: string, requestId: string) =
proc computeDeployOwnerContractsFee*(self: Service, chainId: int, accountAddress: string, communityId: string, ownerDeploymentParams: DeploymentParameters, masterDeploymentParams: DeploymentParameters, requestId: string) =
try:
let arg = AsyncDeployOwnerContractsFeesArg(
tptr: cast[ByteAddress](asyncGetDeployOwnerContractsFeesTask),
vptr: cast[ByteAddress](self.vptr),
slot: "onDeployOwnerContractsFees",
chainId: chainId,
addressFrom: accountAddress,
requestId: requestId
requestId: requestId,
signerPubKey: singletonInstance.userProfile.getPubKey(),
communityId: communityId,
ownerParams: %ownerDeploymentParams,
masterParams: %masterDeploymentParams
)
self.threadpool.start(arg)
except Exception as e:
Expand Down
Loading

0 comments on commit beeaff5

Please sign in to comment.