import { ethers } from "ethers";
import type { CallbackType } from "@evo/utils/ethers/contractHelper";
import { pad0xBegin } from "@evo/utils/common/utils";
import { triggerContract, viewContract, getContract } from "@evo/utils/ethers/contractHelper";
import { TransactionResponse } from "@ethersproject/providers";
import ERC1155ABI from "@evo/config/abi/common/abi-erc1155.json";
/**
* Query if an address is an authorized operator for another address
* @param provider Provider
* @param owner The address that owns the NFTs
* @param spender The address that acts on behalf of the owner
* @param tokenContractAddress ERC1155 contract address
* @param callback Callback
* @returns any
*/
export const erc1155IsApprovedForAll = async (
provider: ethers.providers.Provider,
owner: string,
spender: string,
tokenContractAddress: string,
callback?: CallbackType
): Promise<boolean> => {
const contract = await getContract(provider, tokenContractAddress, ERC1155ABI);
const results = await viewContract(contract, "isApprovedForAll", [owner, spender]);
return results.length ? results[0] : false;
};
/**
* Get the balance of an account's tokens.
* @param provider Provider
* @param owner The address of the token holder
* @param tokenId ID of the token
* @param tokenContractAddress ERC1155 contract address
* @param callback Callback
* @returns The _owner's balance of the token type requested
*/
export const erc1155BalanceOf = async (
provider: ethers.providers.Provider,
owner: string,
tokenId: string,
tokenContractAddress: string,
callback?: CallbackType
): Promise<string> => {
const contract = await getContract(provider, tokenContractAddress, ERC1155ABI);
const results = await viewContract(contract, "balanceOf", [owner, tokenId]);
return results.length ? (results[0] as ethers.BigNumber).toHexString() : ethers.utils.hexValue(0);
};
/**
* Get the balance of multiple account/token pairs
* @param provider Provider
* @param owners The addresses of the token holders
* @param tokenIds ID of the tokens
* @param tokenContractAddress ERC1155 contract address
* @param callback Callback
* @returns The _owner's balance of the token types requested (i.e. balance for each (owner, id) pair)
*/
export const erc1155BalanceOfBatch = async (
provider: ethers.providers.Provider,
owners: string[],
tokenIds: string[],
tokenContractAddress: string,
callback?: CallbackType
): Promise<string> => {
const contract = await getContract(provider, tokenContractAddress, ERC1155ABI);
const results = await viewContract(contract, "balanceOfBatch", [owners, tokenIds]);
return results.length
? results[0].map((item: ethers.BigNumber) => {
return item.toHexString();
})
: [ethers.utils.hexValue(0)];
};
/**
* Enable or disable approval for a third party ("operator") to manage all of the caller's tokens.
* @param signer Signer
* @param contractAddress ERC1155 contract address
* @param spender Address of authorized operator
* @param approved Approved or Cancel Approved
* @param callback Callback
* @returns any
*/
export const erc1155SetApprovalForAll = async (
signer: ethers.Signer,
contractAddress: string,
spender: string,
approved: boolean,
callback?: CallbackType
): Promise<TransactionResponse> => {
const contract = await getContract(signer, contractAddress, ERC1155ABI);
return triggerContract(contract, "setApprovalForAll", [spender, approved], callback);
};
/**
* Transfers `_values` amount(s) of `_ids` from the `_from` address to the `_to` address specified (with safety call).
* @param signer Signer
* @param contractAddress ERC1155 contract address
* @param spender Address of authorized operator
* @param approved Approved or Cancel Approved
* @param callback Callback
* @returns any
*/
export const erc1155SafeBatchTransferFrom = async (
signer: ethers.Signer,
contractAddress: string,
spender: string,
approved: boolean,
callback?: CallbackType
): Promise<TransactionResponse> => {
const contract = await getContract(signer, contractAddress, ERC1155ABI);
return triggerContract(contract, "safeBatchTransferFrom", [spender, approved], callback);
};