diff --git a/src/app_service/service/community_tokens/airdrop_details.nim b/src/app_service/service/community_tokens/airdrop_details.nim new file mode 100644 index 0000000000..72ca6967da --- /dev/null +++ b/src/app_service/service/community_tokens/airdrop_details.nim @@ -0,0 +1,29 @@ +import json +include ../../common/json_utils + +type + AirdropDetails* = object + chainId*: int + contractAddress*: string + walletAddresses*: seq[string] + amount*: int + +proc toJsonNode*(self: AirdropDetails): JsonNode = + result = %* { + "chainId": self.chainId, + "contractAddress": self.contractAddress, + "walletAddresses": self.walletAddresses, + "amount": self.amount + } + +proc toAirdropDetails*(jsonObj: JsonNode): AirdropDetails = + result = AirdropDetails() + discard jsonObj.getProp("chainId", result.chainId) + discard jsonObj.getProp("contractAddress", result.contractAddress) + + var walletsObj: JsonNode + if(jsonObj.getProp("walletAddresses", walletsObj) and walletsObj.kind == JArray): + for walletAddr in walletsObj: + result.walletAddresses.add(walletAddr.getStr) + + discard jsonObj.getProp("amount", result.amount) \ No newline at end of file diff --git a/src/app_service/service/community_tokens/dto/community_token.nim b/src/app_service/service/community_tokens/dto/community_token.nim index f53d4888c4..5129a71da3 100644 --- a/src/app_service/service/community_tokens/dto/community_token.nim +++ b/src/app_service/service/community_tokens/dto/community_token.nim @@ -1,4 +1,4 @@ -import json, json, sequtils +import json, sequtils import ../../../../backend/response_type include ../../../common/json_utils import ../../../common/conversion diff --git a/src/app_service/service/community_tokens/service.nim b/src/app_service/service/community_tokens/service.nim index 699021df90..bf5e8dfdbf 100644 --- a/src/app_service/service/community_tokens/service.nim +++ b/src/app_service/service/community_tokens/service.nim @@ -18,6 +18,8 @@ import ../community/dto/community import ./dto/deployment_parameters import ./dto/community_token +import ./airdrop_details + include async_tasks export community_token @@ -105,6 +107,19 @@ QtObject: let data = CommunityTokenDeployedStatusArgs(communityId: tokenDto.communityId, contractAddress: tokenDto.address, deployState: deployState) self.events.emit(SIGNAL_COMMUNITY_TOKEN_DEPLOY_STATUS, data) + self.events.on(PendingTransactionTypeDto.CollectibleAirdrop.event) do(e: Args): + var receivedData = TransactionMinedArgs(e) + let airdropDetails = toAirdropDetails(parseJson(receivedData.data)) + if not receivedData.success: + error "Collectible airdrop failed", contractAddress=airdropDetails.contractAddress + return + try: + # add holders to db + discard addTokenOwners(airdropDetails.chainId, airdropDetails.contractAddress, + airdropDetails.walletAddresses, airdropDetails.amount) + except RpcException: + error "Error adding collectible token owners", message = getCurrentExceptionMsg() + proc deployCollectiblesEstimate*(self: Service): int = try: let response = tokens_backend.deployCollectiblesEstimate() @@ -129,8 +144,8 @@ QtObject: let response = tokens_backend.deployCollectibles(chainId, %deploymentParams, %txData, password) let contractAddress = response.result["contractAddress"].getStr() let transactionHash = response.result["transactionHash"].getStr() - echo "!!! Contract address ", contractAddress - echo "!!! Transaction hash ", transactionHash + debug "Deployed contract address ", contractAddress=contractAddress + debug "Deployment transaction hash ", transactionHash=transactionHash var communityToken: CommunityTokenDto communityToken.tokenType = TokenType.ERC721 @@ -193,7 +208,24 @@ QtObject: let addressFrom = self.contractOwner(collectibleAndAmount.communityToken.chainId, collectibleAndAmount.communityToken.address) let txData = TransactionDataDto(source: parseAddress(addressFrom)) #TODO estimate fee in UI let response = tokens_backend.mintTo(collectibleAndAmount.communityToken.chainId, collectibleAndAmount.communityToken.address, %txData, password, walletAddresses, collectibleAndAmount.amount) - echo "!!! Transaction hash ", response.result.getStr() + let transactionHash = response.result.getStr() + debug "Deployment transaction hash ", transactionHash=transactionHash + + let airdropDetails = AirdropDetails( + chainId: collectibleAndAmount.communityToken.chainId, + contractAddress: collectibleAndAmount.communityToken.address, + walletAddresses: walletAddresses, + amount: collectibleAndAmount.amount) + + # observe transaction state + self.transactionService.watchTransaction( + transactionHash, + addressFrom, + collectibleAndAmount.communityToken.address, + $PendingTransactionTypeDto.CollectibleAirdrop, + $airdropDetails.toJsonNode(), + collectibleAndAmount.communityToken.chainId, + ) except RpcException: error "Error minting collectibles", message = getCurrentExceptionMsg() diff --git a/src/app_service/service/transaction/dto.nim b/src/app_service/service/transaction/dto.nim index e8565afa40..301d1750c1 100644 --- a/src/app_service/service/transaction/dto.nim +++ b/src/app_service/service/transaction/dto.nim @@ -16,6 +16,7 @@ type BuyStickerPack = "BuyStickerPack" WalletTransfer = "WalletTransfer" CollectibleDeployment = "CollectibleDeployment" + CollectibleAirdrop = "CollectibleAirdrop" proc event*(self:PendingTransactionTypeDto):string = result = "transaction:" & $self diff --git a/src/backend/community_tokens.nim b/src/backend/community_tokens.nim index eea92d312b..e4ad8eab83 100644 --- a/src/backend/community_tokens.nim +++ b/src/backend/community_tokens.nim @@ -30,4 +30,8 @@ proc contractOwner*(chainId: int, contractAddress: string): RpcResponse[JsonNode proc deployCollectiblesEstimate*(): RpcResponse[JsonNode] {.raises: [Exception].} = let payload = %*[] - return core.callPrivateRPC("collectibles_deployCollectiblesEstimate", payload) \ No newline at end of file + return core.callPrivateRPC("collectibles_deployCollectiblesEstimate", payload) + +proc addTokenOwners*(chainId: int, contractAddress: string, walletAddresses: seq[string], amount: int): RpcResponse[JsonNode] {.raises: [Exception].} = + let payload = %* [chainId, contractAddress, walletAddresses, amount] + return core.callPrivateRPC("collectibles_addTokenOwners", payload)