import fs from "fs"
import {AptosAccount} from "../account";
import {AptosClient} from "../providers";
import {HexString} from "../utils";
import {Module} from "../aptos_types";
import {FaucetClient} from "../plugins";


const NODE_URL = "https://fullnode.devnet.aptoslabs.com";

export const TEST_ACCOUNT = {
    address: "0xa272d39841bac0be1e8a8f5f4ab3185be67527cd7f5d8f3973df6140961299f9",
    publicKeyHex: "0x32d6d757b70c307f98f971b5cf5b13efe51d9b95cb3c7818a79f9eeeddd39f9c",
    privateKeyHex: "0x5bdd35adf43166f6310100513d779870a4c6a206750c13a784cf17e15b43f6f4",
};
const client = new AptosClient(NODE_URL);
const faucetClient = new FaucetClient(NODE_URL, "https://faucet.devnet.aptoslabs.com")

export class Client {
    static async deploy() {
        const account = AptosAccount.fromAptosAccountObject({
            privateKeyHex: TEST_ACCOUNT.privateKeyHex,
        });
        const packageMetadata = fs.readFileSync("static/move/package-metadata.bcs");
        const moduleData = fs.readFileSync("static/move/demo.mv");
        const hash = await client.publishPackage(
            account,
            new HexString(packageMetadata.toString("hex")).toUint8Array(),
            [new Module(new HexString(moduleData.toString("hex")).toUint8Array())]
        );
        await client.waitForTransaction(hash);
        return client.getTransactionByHash(hash);
    }

    static async callMove(payload: any) {
        return client.view(payload);
    }

    static async faucet() {
        const response = await faucetClient.fundAccount(TEST_ACCOUNT.address, 100000000);
        const hash = response[0]
        await client.waitForTransaction(hash)
        return client.getTransactionByHash(hash)
    }
}