var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { ethers } from "ethers";
import ringABI from "../../../config/abi/common/abi-ring.json";
import { triggerContractByContractName, viewContractByContractName, triggerContract, viewContract, getContract, } from "../../../utils/ethers/contractHelper";
/**
 * ERC20 Allowance
 * @param provider Ethers provider
 * @param contractName Contract name
 * @param from From, account address
 * @param spender Spender
 * @param callback Callback
 * @returns String amount
 */
export const erc20Allowance = (landId, provider, contractName, from, spender, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const results = yield viewContractByContractName(landId, provider, contractName, "allowance", [from, spender], callback);
    return results.length ? results[0].toHexString() : ethers.utils.hexValue(0);
});
/**
 * ERC20 Allowance By Contract Address
 * @param provider Ethers provider
 * @param contractAddress Contract address
 * @param from From
 * @param spender Spender
 * @param callback Callback
 * @returns String amount
 */
export const erc20AllowanceByContractAddress = (provider, contractAddress, from, spender, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, contractAddress, ringABI);
    if (contract) {
        const results = yield viewContract(contract, "allowance", [from, spender], callback);
        return results.length ? results[0].toHexString() : ethers.utils.hexValue(0);
    }
    else {
        callback && callback.errorCallback({ error: new Error(`erc20AllowanceByContractAddress failed to get contract.`) });
        return ethers.utils.hexValue(0);
    }
});
/**
 * ERC20 Approve
 * @param signer Ethers signer
 * @param contractName Contract name
 * @param spender Spender
 * @param value Value
 * @param callback Callback
 */
export const erc20Approve = (landId, signer, contractName, spender, value = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", callback) => {
    return triggerContractByContractName(landId, signer, contractName, "approve(address,uint256)", [spender, value], callback);
};
/**
 * ERC20 Approve By Contract Address
 * @param signer Ethers signer
 * @param contractAddress Contract address
 * @param spender Spender
 * @param value Value
 * @param callback Callback
 */
export const erc20ApproveByContractAddress = (signer, contractAddress, spender, value = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(signer, contractAddress, ringABI);
    // if (contract) {
    return triggerContract(contract, "approve(address,uint256)", [spender, value], callback);
    // } else {
    //   callback && callback.errorCallback({ error: new Error(`erc20ApproveByContractAddress failed to get contract.`) });
    // }
});
/**
 * ERC20 Transfer
 * @param signer Signer
 * @param contractAddress Token contract address
 * @param dst Recipient
 * @param wad Amount
 * @param callback Callback
 * @returns any
 */
export const erc20TransferByContractAddress = (signer, contractAddress, dst, wad, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(signer, contractAddress, ringABI);
    // if (contract) {
    return triggerContract(contract, "transfer(address,uint256)", [dst, wad], callback);
    // } else {
    //   callback && callback.errorCallback({ error: new Error(`erc20TransferByContractAddress failed to get contract.`) });
    // }
});
/**
 * Get token balance by address
 * @param provider Provider
 * @param tokenAddress ERC20 contract address
 * @param account Address
 * @param callback Callbcak
 * @returns any
 */
export const erc20BalanceOf = (provider, tokenAddress, account, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, tokenAddress, ringABI);
    const results = yield viewContract(contract, "balanceOf(address)", [account], callback);
    return results.length ? results[0].toHexString() : ethers.utils.hexValue(0);
});
/**
 * Get token total supply
 * @param provider Provider
 * @param tokenAddress ERC20 contract address
 * @param callback Callback
 * @returns any
 */
export const erc20TotalSupply = (provider, tokenAddress, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(provider, tokenAddress, ringABI);
    const results = yield viewContract(contract, "totalSupply", [], callback);
    return results.length ? results[0].toHexString() : ethers.utils.hexValue(0);
});
