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 ringABI from "../../../config/abi/common/abi-ring.json";
import { triggerContractByContractName, viewContractByContractName, triggerContract, viewContract, getContract, } from "../../../utils/tronweb/contractHelper";
/**
 * ERC20 Allowance
 * @param tronWeb Tronweb
 * @param contractName Contract name
 * @param from From, account address
 * @param spender Spender
 * @param callback Callback
 * @returns String amount
 */
export const erc20Allowance = (landId, tronWeb, contractName, from, spender, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const results = yield viewContractByContractName(landId, tronWeb, contractName, "allowance", [from, spender], callback);
    return (results === null || results === void 0 ? void 0 : results._isBigNumber) ? results.toHexString() : "0x00";
});
/**
 * ERC20 Allowance By Contract Address
 * @param tronWeb Tronweb
 * @param contractAddress Contract address
 * @param from From
 * @param spender Spender
 * @param callback Callback
 * @returns String amount
 */
export const erc20AllowanceByContractAddress = (tronWeb, contractAddress, from, spender, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(tronWeb, contractAddress);
    if (contract) {
        const results = yield viewContract(contract, "allowance", [from, spender], callback);
        return (results === null || results === void 0 ? void 0 : results._isBigNumber) ? results.toHexString() : "0x00";
    }
    else {
        callback && callback.errorCallback({ error: new Error(`erc20AllowanceByContractAddress failed to get contract.`) });
        return "0x00";
    }
});
/**
 * ERC20 Approve
 * @param tronWeb Tronweb
 * @param contractName Contract name
 * @param spender Spender
 * @param value Value
 * @param callback Callback
 */
export const erc20Approve = (landId, tronWeb, contractName, spender, value = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", callback) => __awaiter(void 0, void 0, void 0, function* () {
    return triggerContractByContractName(landId, tronWeb, contractName, "approve(address,uint256)", [spender, value], callback);
});
/**
 * ERC20 Approve By Contract Address
 * @param tronWeb Tronweb
 * @param contractAddress Contract address
 * @param spender Spender
 * @param value Value
 * @param callback Callback
 */
export const erc20ApproveByContractAddress = (tronWeb, contractAddress, spender, value = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(tronWeb, contractAddress);
    return triggerContract(contract, "approve(address,uint256)", [spender, value], callback);
});
export const erc20TransferByContractAddress = (signer, contractAddress, dst, wad, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const contract = yield getContract(signer, contractAddress, ringABI);
    return triggerContract(contract, "transfer(address,uint256)", [dst, wad], callback);
});
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 === null || results === void 0 ? void 0 : results._isBigNumber) ? results.toHexString() : "0x00";
});
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 === null || results === void 0 ? void 0 : results._isBigNumber) ? results.toHexString() : "0x00";
});
