# material/v1.ts

    import { BigNumber, ethers } from "ethers";
    import {
      CallbackType,
      triggerContractByContractName,
      viewContractByContractName,
    } from "@evo/utils/ethers/contractHelper";
    import { TransactionResponse } from "@ethersproject/providers";
    import { LandId } from "@evo/config/constants";
    import { pad0xBegin } from "@evo/utils/common/utils";

    /**
     * Covert material Id to token Id.
     * @param landId Land Id
     * @param provider Provider
     * @param id Material Id
     * @param callback Callback
     * @returns any
     */
    export const materialIdEncode = async (
      landId: LandId,
      provider: ethers.providers.Provider,
      id: string,
      callback?: CallbackType
    ): Promise<string> => {
      const results = await viewContractByContractName(landId, provider, "material", "encode", [id], callback);
      return results.length ? (results[0] as BigNumber).toHexString() : ethers.utils.hexValue(0);
    };

    /**
     * Batch get material balance
     * @param landId Land Id
     * @param provider Provider
     * @param accounts Array of address
     * @param materialIdList Material token ids
     * @param callback Callback
     * @returns any
     */
    export const materialBalanceOfBatch = async (
      landId: LandId,
      provider: ethers.providers.Provider,
      accounts: string[],
      materialIdList: string[],
      callback?: CallbackType
    ): Promise<string[]> => {
      const [result] =
        (await viewContractByContractName(
          landId,
          provider,
          "material",
          "balanceOfBatch",
          [accounts, materialIdList.map((materialId) => pad0xBegin(materialId))],
          callback
        )) || [];

      if (Array.isArray(result)) {
        return result.map((item) => (BigNumber.isBigNumber(item) ? BigNumber.from(item).toString() : "0"));
      }

      return new Array(materialIdList.length).fill("0");
    };

    /**
     * Batch transfer material
     * @param landId Land Id
     * @param signer Signer
     * @param from Material owner
     * @param to Recipient
     * @param materialIdList Array of material token Ids
     * @param amounts Array of transfer amounts
     * @param data Additional data
     * @param callback Callback
     * @returns any
     */
    export const materialBatchTransfer = (
      landId: LandId,
      signer: ethers.Signer,
      from: string,
      to: string,
      materialIdList: string[],
      amounts: string[],
      data = "0x",
      callback?: CallbackType
    ): Promise<TransactionResponse> => {
      return triggerContractByContractName(
        landId,
        signer,
        "material",
        "safeBatchTransferFrom",
        [from, to, materialIdList.map((materialId) => pad0xBegin(materialId)), amounts, data],
        callback
      );
    };