"use strict";
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.StorageNodeRegistry = void 0;
const network_contracts_1 = require("@streamr/network-contracts");
const utils_1 = require("@streamr/utils");
const tsyringe_1 = require("tsyringe");
const Identity_1 = require("../identity/Identity");
const Config_1 = require("../Config");
const RpcProviderSource_1 = require("../RpcProviderSource");
const StreamrClientError_1 = require("../StreamrClientError");
const ethereumUtils_1 = require("../ethereumUtils");
const ContractFactory_1 = require("./ContractFactory");
const contract_1 = require("./contract");
/**
 * Store a mapping of storage node addresses <-> storage node URLs
 */
let StorageNodeRegistry = class StorageNodeRegistry {
    nodeRegistryContract;
    nodeRegistryContractReadonly;
    contractFactory;
    rpcProviderSource;
    config;
    identity;
    constructor(contractFactory, rpcProviderSource, config, identity) {
        this.contractFactory = contractFactory;
        this.rpcProviderSource = rpcProviderSource;
        this.config = config;
        this.identity = identity;
        this.nodeRegistryContractReadonly = this.contractFactory.createReadContract((0, utils_1.toEthereumAddress)(this.config.contracts.storageNodeRegistryChainAddress), network_contracts_1.NodeRegistryABI, rpcProviderSource.getProvider(), 'storageNodeRegistry');
    }
    async connectToContract() {
        if (this.nodeRegistryContract === undefined) {
            const chainSigner = await this.identity.getTransactionSigner(this.rpcProviderSource);
            this.nodeRegistryContract = this.contractFactory.createWriteContract((0, utils_1.toEthereumAddress)(this.config.contracts.storageNodeRegistryChainAddress), network_contracts_1.NodeRegistryABI, chainSigner, 'storageNodeRegistry');
        }
    }
    async setStorageNodeMetadata(metadata) {
        await this.connectToContract();
        const ethersOverrides = await (0, ethereumUtils_1.getEthersOverrides)(this.rpcProviderSource, this.config);
        if (metadata !== undefined) {
            await (0, contract_1.waitForTx)(this.nodeRegistryContract.createOrUpdateNodeSelf(JSON.stringify(metadata), ethersOverrides));
        }
        else {
            await (0, contract_1.waitForTx)(this.nodeRegistryContract.removeNodeSelf(ethersOverrides));
        }
    }
    async getStorageNodeMetadata(nodeAddress) {
        const [resultNodeAddress, metadata] = await this.nodeRegistryContractReadonly.getNode(nodeAddress);
        const NODE_NOT_FOUND = '0x0000000000000000000000000000000000000000';
        if (resultNodeAddress !== NODE_NOT_FOUND) {
            return JSON.parse(metadata);
        }
        else {
            throw new StreamrClientError_1.StreamrClientError('Node not found, id: ' + nodeAddress, 'NODE_NOT_FOUND');
        }
    }
};
exports.StorageNodeRegistry = StorageNodeRegistry;
exports.StorageNodeRegistry = StorageNodeRegistry = __decorate([
    (0, tsyringe_1.scoped)(tsyringe_1.Lifecycle.ContainerScoped),
    __param(2, (0, tsyringe_1.inject)(Config_1.ConfigInjectionToken)),
    __param(3, (0, tsyringe_1.inject)(Identity_1.IdentityInjectionToken)),
    __metadata("design:paramtypes", [ContractFactory_1.ContractFactory,
        RpcProviderSource_1.RpcProviderSource, Object, Identity_1.Identity])
], StorageNodeRegistry);
//# sourceMappingURL=StorageNodeRegistry.js.map