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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
    for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
        to[j] = from[i];
    return to;
};
import { Keypair, PublicKey, Transaction, } from "@solana/web3.js";
import { MsgExecuteContract } from "@terra-money/terra.js";
import { MsgExecuteContract as MsgExecuteContractInjective } from "@injectivelabs/sdk-ts";
import { bigIntToBytes, decodeAddress, getApplicationAddress, makeApplicationCallTxnFromObject, makePaymentTxnWithSuggestedParamsFromObject, OnApplicationComplete, } from "algosdk";
import BN from "bn.js";
import { isNativeDenom } from "../terra";
import { getMessageFee, optin } from "../algorand";
import { Bridge__factory } from "../ethers-contracts";
import { createBridgeFeeTransferInstruction } from "../solana";
import { createAttestTokenInstruction } from "../solana/tokenBridge";
import { callFunctionNear, hashAccount, textToHexString, textToUint8Array, uint8ArrayToHex, } from "../utils";
import { safeBigIntToNumber } from "../utils/bigint";
import { createNonce } from "../utils/createNonce";
import { getIsWrappedAssetNear } from ".";
import { isNativeDenomInjective, isNativeDenomXpla } from "../cosmwasm";
import { MsgExecuteContract as XplaMsgExecuteContract } from "@xpla/xpla.js";
import { attestToken as attestTokenAptos } from "../aptos";
export function attestFromEth(tokenBridgeAddress, signer, tokenAddress, overrides) {
    if (overrides === void 0) { overrides = {}; }
    return __awaiter(this, void 0, void 0, function () {
        var bridge, v, receipt;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
                    return [4 /*yield*/, bridge.attestToken(tokenAddress, createNonce(), overrides)];
                case 1:
                    v = _a.sent();
                    return [4 /*yield*/, v.wait()];
                case 2:
                    receipt = _a.sent();
                    return [2 /*return*/, receipt];
            }
        });
    });
}
export function attestFromTerra(tokenBridgeAddress, walletAddress, asset) {
    return __awaiter(this, void 0, void 0, function () {
        var nonce, isNativeAsset;
        return __generator(this, function (_a) {
            nonce = Math.round(Math.random() * 100000);
            isNativeAsset = isNativeDenom(asset);
            return [2 /*return*/, new MsgExecuteContract(walletAddress, tokenBridgeAddress, {
                    create_asset_meta: {
                        asset_info: isNativeAsset
                            ? {
                                native_token: { denom: asset },
                            }
                            : {
                                token: {
                                    contract_addr: asset,
                                },
                            },
                        nonce: nonce,
                    },
                })];
        });
    });
}
/**
 * Creates attestation message
 * @param tokenBridgeAddress Address of Inj token bridge contract
 * @param walletAddress Address of wallet in inj format
 * @param asset Name or address of the asset to be attested
 * For native assets the asset string is the denomination.
 * For foreign assets the asset string is the inj address of the foreign asset
 * @returns Message to be broadcast
 */
export function attestFromInjective(tokenBridgeAddress, walletAddress, asset) {
    return __awaiter(this, void 0, void 0, function () {
        var nonce, isNativeAsset;
        return __generator(this, function (_a) {
            nonce = Math.round(Math.random() * 100000);
            isNativeAsset = isNativeDenomInjective(asset);
            return [2 /*return*/, MsgExecuteContractInjective.fromJSON({
                    contractAddress: tokenBridgeAddress,
                    sender: walletAddress,
                    msg: {
                        asset_info: isNativeAsset
                            ? {
                                native_token: { denom: asset },
                            }
                            : {
                                token: {
                                    contract_addr: asset,
                                },
                            },
                        nonce: nonce,
                    },
                    action: "create_asset_meta",
                })];
        });
    });
}
export function attestFromXpla(tokenBridgeAddress, walletAddress, asset) {
    var nonce = Math.round(Math.random() * 100000);
    var isNativeAsset = isNativeDenomXpla(asset);
    return new XplaMsgExecuteContract(walletAddress, tokenBridgeAddress, {
        create_asset_meta: {
            asset_info: isNativeAsset
                ? {
                    native_token: { denom: asset },
                }
                : {
                    token: {
                        contract_addr: asset,
                    },
                },
            nonce: nonce,
        },
    });
}
export function attestFromSolana(connection, bridgeAddress, tokenBridgeAddress, payerAddress, mintAddress, commitment) {
    return __awaiter(this, void 0, void 0, function () {
        var nonce, transferIx, messageKey, attestIx, transaction, blockhash;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    nonce = createNonce().readUInt32LE(0);
                    return [4 /*yield*/, createBridgeFeeTransferInstruction(connection, bridgeAddress, payerAddress)];
                case 1:
                    transferIx = _a.sent();
                    messageKey = Keypair.generate();
                    attestIx = createAttestTokenInstruction(tokenBridgeAddress, bridgeAddress, payerAddress, mintAddress, messageKey.publicKey, nonce);
                    transaction = new Transaction().add(transferIx, attestIx);
                    return [4 /*yield*/, connection.getLatestBlockhash(commitment)];
                case 2:
                    blockhash = (_a.sent()).blockhash;
                    transaction.recentBlockhash = blockhash;
                    transaction.feePayer = new PublicKey(payerAddress);
                    transaction.partialSign(messageKey);
                    return [2 /*return*/, transaction];
            }
        });
    });
}
/**
 * Attest an already created asset
 * If you create a new asset on algorand and want to transfer it elsewhere,
 * you create an attestation for it on algorand... pass that vaa to the target chain..
 * submit it.. then you can transfer from algorand to that target chain
 * @param client An Algodv2 client
 * @param tokenBridgeId The ID of the token bridge
 * @param senderAcct The account paying fees
 * @param assetId The asset index
 * @returns Transaction ID
 */
export function attestFromAlgorand(client, tokenBridgeId, bridgeId, senderAddr, assetId) {
    return __awaiter(this, void 0, void 0, function () {
        var tbAddr, decTbAddr, aa, txs, _a, emitterAddr, emitterOptInTxs, creatorAddr, creatorAcctInfo, bPgmName, assetInfo, result, suggParams, firstTxn, mfee, feeTxn, accts, appTxn;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    tbAddr = getApplicationAddress(tokenBridgeId);
                    decTbAddr = decodeAddress(tbAddr).publicKey;
                    aa = uint8ArrayToHex(decTbAddr);
                    txs = [];
                    return [4 /*yield*/, optin(client, senderAddr, bridgeId, BigInt(0), aa)];
                case 1:
                    _a = _b.sent(), emitterAddr = _a.addr, emitterOptInTxs = _a.txs;
                    txs.push.apply(txs, __spreadArray([], __read(emitterOptInTxs)));
                    creatorAddr = "";
                    bPgmName = textToUint8Array("attestToken");
                    if (!(assetId !== BigInt(0))) return [3 /*break*/, 4];
                    return [4 /*yield*/, client
                            .getAssetByID(safeBigIntToNumber(assetId))
                            .do()];
                case 2:
                    assetInfo = _b.sent();
                    return [4 /*yield*/, client
                            .accountInformation(assetInfo.params.creator)
                            .do()];
                case 3:
                    creatorAcctInfo = _b.sent();
                    if (creatorAcctInfo["auth-addr"] === tbAddr) {
                        throw new Error("Cannot re-attest wormhole assets");
                    }
                    _b.label = 4;
                case 4: return [4 /*yield*/, optin(client, senderAddr, tokenBridgeId, assetId, textToHexString("native"))];
                case 5:
                    result = _b.sent();
                    creatorAddr = result.addr;
                    txs.push.apply(txs, __spreadArray([], __read(result.txs)));
                    return [4 /*yield*/, client.getTransactionParams().do()];
                case 6:
                    suggParams = _b.sent();
                    firstTxn = makeApplicationCallTxnFromObject({
                        from: senderAddr,
                        appIndex: safeBigIntToNumber(tokenBridgeId),
                        onComplete: OnApplicationComplete.NoOpOC,
                        appArgs: [textToUint8Array("nop")],
                        suggestedParams: suggParams,
                    });
                    txs.push({ tx: firstTxn, signer: null });
                    return [4 /*yield*/, getMessageFee(client, bridgeId)];
                case 7:
                    mfee = _b.sent();
                    if (mfee > BigInt(0)) {
                        feeTxn = makePaymentTxnWithSuggestedParamsFromObject({
                            from: senderAddr,
                            suggestedParams: suggParams,
                            to: getApplicationAddress(tokenBridgeId),
                            amount: mfee,
                        });
                        txs.push({ tx: feeTxn, signer: null });
                    }
                    accts = [
                        emitterAddr,
                        creatorAddr,
                        getApplicationAddress(bridgeId),
                    ];
                    if (creatorAcctInfo) {
                        accts.push(creatorAcctInfo["address"]);
                    }
                    appTxn = makeApplicationCallTxnFromObject({
                        appArgs: [bPgmName, bigIntToBytes(assetId, 8)],
                        accounts: accts,
                        appIndex: safeBigIntToNumber(tokenBridgeId),
                        foreignApps: [safeBigIntToNumber(bridgeId)],
                        foreignAssets: [safeBigIntToNumber(assetId)],
                        from: senderAddr,
                        onComplete: OnApplicationComplete.NoOpOC,
                        suggestedParams: suggParams,
                    });
                    if (mfee > BigInt(0)) {
                        appTxn.fee *= 3;
                    }
                    else {
                        appTxn.fee *= 2;
                    }
                    txs.push({ tx: appTxn, signer: null });
                    return [2 /*return*/, txs];
            }
        });
    });
}
export function attestTokenFromNear(provider, coreBridge, tokenBridge, asset) {
    return __awaiter(this, void 0, void 0, function () {
        var options, messageFee, isRegistered;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    options = [];
                    return [4 /*yield*/, callFunctionNear(provider, coreBridge, "message_fee")];
                case 1:
                    messageFee = _a.sent();
                    if (!!getIsWrappedAssetNear(tokenBridge, asset)) return [3 /*break*/, 3];
                    return [4 /*yield*/, hashAccount(provider, tokenBridge, asset)];
                case 2:
                    isRegistered = (_a.sent()).isRegistered;
                    if (!isRegistered) {
                        // The account has not been registered. The first user to attest a non-wormhole token pays for the space
                        options.push({
                            contractId: tokenBridge,
                            methodName: "register_account",
                            args: { account: asset },
                            gas: new BN("100000000000000"),
                            attachedDeposit: new BN("2000000000000000000000"), // 0.002 NEAR
                        });
                    }
                    _a.label = 3;
                case 3:
                    options.push({
                        contractId: tokenBridge,
                        methodName: "attest_token",
                        args: { token: asset, message_fee: messageFee },
                        attachedDeposit: new BN("3000000000000000000000").add(new BN(messageFee)),
                        gas: new BN("100000000000000"),
                    });
                    return [2 /*return*/, options];
            }
        });
    });
}
export function attestNearFromNear(provider, coreBridge, tokenBridge) {
    return __awaiter(this, void 0, void 0, function () {
        var messageFee;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, callFunctionNear(provider, coreBridge, "message_fee")];
                case 1:
                    messageFee = (_a.sent()) + 1;
                    return [2 /*return*/, {
                            contractId: tokenBridge,
                            methodName: "attest_near",
                            args: { message_fee: messageFee },
                            attachedDeposit: new BN(messageFee),
                            gas: new BN("100000000000000"),
                        }];
            }
        });
    });
}
/**
 * Attest given token from Aptos.
 * @param tokenBridgeAddress Address of token bridge
 * @param tokenChain Origin chain ID
 * @param tokenAddress Address of token on origin chain
 * @returns Transaction payload
 */
export function attestFromAptos(tokenBridgeAddress, tokenChain, tokenAddress) {
    return attestTokenAptos(tokenBridgeAddress, tokenChain, tokenAddress);
}
