"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TokenRegistry = void 0;

var _ethers = require("ethers");

var _token = require("./util/token");

var _TradeTrustERC = require("../build/contracts/TradeTrustERC721.json");

var _types = require("./types");

var _transaction = require("./util/transaction");

var _provider = require("./provider");

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

class TokenRegistry {
  /**
   * Creates a TokenRegistry instance with the specified address and ethersjs signer
   * @param instanceParameters
   * @param instanceParameters.contractAddress - Address that the TokenRegistry is located at
   * @param instanceParameters.web3Provider - An ethers.js signer instance with read/write access
   * @param instanceParameters.network - Ethereum network name
   */
  constructor({
    contractAddress,
    web3Provider = (0, _provider.getWeb3Provider)(),
    wallet,
    network = _types.EthereumNetwork.Ropsten // Default to Ropsten since we currently only operate on Ropsten

  }) {
    _defineProperty(this, "web3Provider", void 0);

    _defineProperty(this, "address", void 0);

    _defineProperty(this, "contractInstance", void 0);

    this.web3Provider = web3Provider || (0, _ethers.getDefaultProvider)(network);
    this.address = contractAddress; // doing JSON.stringify below because of ethers.js type issue: https://github.com/ethers-io/ethers.js/issues/602

    this.contractInstance = new _ethers.Contract(contractAddress, JSON.stringify(_TradeTrustERC.abi), web3Provider);

    if (wallet) {
      this.contractInstance = this.contractInstance.connect(wallet);
    }
  }

  mint(document, ownerAddress) {
    var _this = this;

    return _asyncToGenerator(function* () {
      const tokenId = (0, _token.getBatchMerkleRoot)(document);
      return (0, _transaction.waitForTransaction)(_this.contractInstance["safeMint(address,uint256)"](ownerAddress, tokenId));
    })();
  }

  ownerOf(document) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      const tokenId = (0, _token.getBatchMerkleRoot)(document);
      return _this2.contractInstance.ownerOf(tokenId);
    })();
  }

  transferTo(document, newOwnerAddress) {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      const tokenId = (0, _token.getBatchMerkleRoot)(document);
      const currentOwner = yield _this3.ownerOf(document);
      return (0, _transaction.waitForTransaction)(_this3.contractInstance["safeTransferFrom(address,address,uint256)"](currentOwner, newOwnerAddress, tokenId));
    })();
  }

}

exports.TokenRegistry = TokenRegistry;