nft-folder-blockchain-connector-besu
Version:
A NestJS-based blockchain connector that communicates with specific smart contracts from the NFT Folder project.
96 lines • 5.62 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenMintService = void 0;
const common_1 = require("@nestjs/common");
const ethers_1 = require("ethers");
const token_base_services_1 = require("./token-base.services");
const blockchain_service_1 = require("../../shared/blockchain.service");
const configuration_service_1 = require("../../configuration/configuration.service");
const token_read_dto_1 = require("../dto/token-read.dto");
const token_asset_dto_1 = require("../dto/token.asset.dto");
const token_metadata_dto_1 = require("../dto/token.metadata.dto");
const token_hierarchy_dto_1 = require("../dto/token.hierarchy.dto");
const event_service_1 = require("./event.service");
let TokenMintService = class TokenMintService extends token_base_services_1.TokenBaseService {
blockchainService;
configurationService;
eventService;
ethersProvider;
constructor(blockchainService, configurationService, eventService, ethersProvider) {
super(blockchainService, configurationService);
this.blockchainService = blockchainService;
this.configurationService = configurationService;
this.eventService = eventService;
this.ethersProvider = ethersProvider;
}
async mintToken(dto, appendToHierarchy) {
try {
const transactionResponse = appendToHierarchy
? await this.tokenInstance.mintTokenAndAppendToHierarchy(this.blockchainService.returnSignerAddress(), dto.remoteId, dto.asset.uri, dto.asset.hash, dto.metadata.uri, dto.metadata.hash, dto.additionalData, dto.parentIds)
: await this.tokenInstance.mintToken(this.blockchainService.returnSignerAddress(), dto.remoteId, dto.asset.uri, dto.asset.hash, dto.metadata.uri, dto.metadata.hash, dto.additionalData);
await this.blockchainService.waitForTheNextBlock();
const transactionReceipt = await this.ethersProvider.getTransactionReceipt(transactionResponse.hash);
const decodedLogs = this.decodeLogs(transactionReceipt);
const transactionTimestamp = await this.fetchTransactionTimestampForReceipt(transactionReceipt);
return this.assembleDto(decodedLogs, transactionTimestamp);
}
catch (err) {
this.handleError(err);
throw err;
}
}
decodeLogs(transactionReceipt) {
const contractInterface = new ethers_1.Interface(this.tokenInstance.interface.fragments);
const decodedLogs = this.eventService.decodeLogs(contractInterface, transactionReceipt, [
'TokenMinted',
'NodeAppendedToHierarchy',
]);
this.logger.log(`### Token Minted ###
TxId: ${transactionReceipt.hash}
Token ${decodedLogs?.[0]?.args[1]} minted
TokenMinted: ${decodedLogs?.[0]?.args}
NodeAppendedToHierarchy: ${decodedLogs?.[1]?.args}`);
return decodedLogs;
}
async fetchTransactionTimestampForReceipt(transactionReceipt) {
return this.blockchainService.fetchTransactionTimestamp(transactionReceipt.hash);
}
assembleDto(decodedLogs, transactionTimestamp) {
const remoteId = decodedLogs[0].args[2];
const tokenAssetDto = new token_asset_dto_1.TokenAssetDto(decodedLogs[0].args[3], decodedLogs[0].args[4]);
const tokenMetadataDto = new token_metadata_dto_1.TokenMetadataDto(decodedLogs[0].args[5], decodedLogs[0].args[6]);
const additionalData = decodedLogs[0].args[7];
const tokenHierarchyDto = decodedLogs[1]
? new token_hierarchy_dto_1.TokenHierarchyDto(true, [], decodedLogs[1].args[1].map((id) => Number(id)))
: undefined;
const ownerAddress = decodedLogs[0].args[0];
const minterAddress = decodedLogs[0].args[0];
const createdOn = transactionTimestamp;
const lastUpdatedOn = transactionTimestamp;
const tokenId = Number(decodedLogs[0].args[1]);
const tokenAddress = this.configurationService.getBlockchainConfiguration().tokenAddress;
return new token_read_dto_1.TokenReadDto(remoteId, tokenAssetDto, tokenMetadataDto, additionalData, tokenHierarchyDto, ownerAddress, minterAddress, createdOn, lastUpdatedOn, tokenId, tokenAddress);
}
};
exports.TokenMintService = TokenMintService;
exports.TokenMintService = TokenMintService = __decorate([
(0, common_1.Injectable)(),
__param(3, (0, common_1.Inject)('EthersProvider')),
__metadata("design:paramtypes", [blockchain_service_1.BlockchainService,
configuration_service_1.ConfigurationService,
event_service_1.EventService,
ethers_1.JsonRpcProvider])
], TokenMintService);
//# sourceMappingURL=token-mint.service.js.map