@coinmeca/ethers
Version:
Solidty helpers and utilities for using ethers.
149 lines (148 loc) • 7.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ERC721 = ERC721;
const hardhat_1 = require("hardhat");
const ethers_1 = require("ethers");
const utils_1 = require("../utils");
async function ERC721(token) {
token = typeof token === 'string' ? await hardhat_1.ethers.getContractAtFromArtifact(JSON.parse(require('fs').readFileSync(require('path').resolve(__dirname, '../artifacts/ERC721.sol/ERC721.json'))), token) : token;
const name = typeof token?.name === 'function' ? await token?.name() : typeof token?.name === 'string' ? token?.name : null;
const symbol = typeof token?.symbol === 'function' ? await token?.symbol() : typeof token?.symbol === 'string' ? token?.symbol : null;
const address = typeof token?.getAddress === 'function' ? await token?.getAddress() : typeof token?.address === 'string' ? token?.address : null;
const module = (token, user) => {
const getId = async (key, display) => {
const id = await token.getId(key);
if (display) {
console.log(utils_1.color.lightGray(`------------------------- Order NFT --------------------------`));
console.log(`🖼️ Token ID: ${id}`);
console.log(utils_1.color.lightGray(`--------------------------------------------------------------`));
}
return id;
};
const getKey = async (id, display) => {
const key = await token.getKey(typeof id === 'number' ? ethers_1.Typed.uint(id) : ethers_1.Typed.bytes32(id));
if (display) {
console.log(utils_1.color.lightGray(`------------------------- Order NFT --------------------------`));
console.log(`🔑 Order Key: ${key}`);
console.log(utils_1.color.lightGray(`--------------------------------------------------------------`));
}
return key;
};
const tokenURI = async (id, display) => {
const uri = await token.tokenURI(typeof id === 'number' ? ethers_1.Typed.uint(id) : ethers_1.Typed.bytes32(id));
if (display) {
console.log(utils_1.color.lightGray(`------------------------- Order NFT --------------------------`));
console.log(`Token ID: ${id}`);
console.log(`Token IMG: ${uri}`);
console.log(utils_1.color.lightGray(`--------------------------------------------------------------`));
}
return uri;
};
const tokenIMG = async (id, display) => {
const img = await token.tokenIMG(typeof id === 'number' ? ethers_1.Typed.uint(id) : ethers_1.Typed.bytes32(id));
if (display) {
console.log(utils_1.color.lightGray(`------------------------- Order NFT --------------------------`));
console.log(`Token ID: ${id}`);
console.log(`Token IMG: ${img}`);
console.log(utils_1.color.lightGray(`--------------------------------------------------------------`));
}
return img;
};
const totalSupply = async () => {
return await token?.totalSupply();
};
const balanceOf = async (owner, display) => {
const balance = parseInt(await token.balanceOf((0, utils_1.a)(owner)));
if (display) {
console.log(utils_1.color.lightGray(`------------------------- Order NFT --------------------------`));
console.log(`Owner: ${(0, utils_1.a)(owner)}`);
console.log(`Tokens: ${balance}`);
console.log(utils_1.color.lightGray(`--------------------------------------------------------------`));
}
return balance;
};
const tokensOf = async (owner, display) => {
const tokens = await token.tokensOf((0, utils_1.a)(owner));
if (display) {
console.log(utils_1.color.lightGray(`------------------------- Order NFT --------------------------`));
console.log(`Owner: ${(0, utils_1.a)(owner)}`);
console.log(`Tokens: ${tokens}`);
console.log(utils_1.color.lightGray(`--------------------------------------------------------------`));
}
return tokens;
};
const keysOf = async (owner, display) => {
const keys = await token.keysOf((0, utils_1.a)(owner));
if (display) {
console.log(utils_1.color.lightGray(`------------------------- Order NFT --------------------------`));
console.log(`Owner: ${(0, utils_1.a)(owner)}`);
console.log(`Keys: ${keys}`);
console.log(utils_1.color.lightGray(`--------------------------------------------------------------`));
}
return keys;
};
const ownerOf = async (id, display) => {
const owner = await token.ownerOf(typeof id === 'number' ? ethers_1.Typed.uint(id) : ethers_1.Typed.bytes32(id));
if (display) {
console.log(utils_1.color.lightGray(`------------------------- Order NFT --------------------------`));
console.log(`Owner: ${(0, utils_1.a)(owner)}`);
console.log(`Token ID: ${id}`);
console.log(utils_1.color.lightGray(`--------------------------------------------------------------`));
}
return owner;
};
const transfer = async (to, id) => {
return await token.transfer((0, utils_1.a)(to), typeof id === 'number' ? ethers_1.Typed.uint(id) : ethers_1.Typed.bytes32(id));
};
const transferFrom = async (from, to, id) => {
return await token.transferFrom((0, utils_1.a)(from), (0, utils_1.a)(to), typeof id === 'number' ? ethers_1.Typed.uint(id) : ethers_1.Typed.bytes32(id));
};
const safeTransferFrom = async (from, to, id, data) => {
return data
? await token['safeTransferFrom(address,address,uint256)']((0, utils_1.a)(from), (0, utils_1.a)(to), typeof id === 'number' ? ethers_1.Typed.uint(id) : ethers_1.Typed.bytes32(id))
: await token['safeTransferFrom(address,address,uint256,bytes)']((0, utils_1.a)(from), (0, utils_1.a)(to), typeof id === 'number' ? ethers_1.Typed.uint(id) : ethers_1.Typed.bytes32(id), data);
};
const approve = async (to, id) => {
return await token.approve((0, utils_1.a)(to), id);
};
const getApproved = async (id) => {
return await token.getApproved(id);
};
const setApprovalForAll = async (operator, approve) => {
await token.setApprovalForAll((0, utils_1.a)(operator), approve);
};
const isApprovedForAll = async (owner, operator) => {
return token.isApprovedForAll((0, utils_1.a)(owner), (0, utils_1.a)(operator));
};
return {
name,
symbol,
address,
totalSupply,
getId,
getKey,
tokenURI,
tokenIMG,
balanceOf,
tokensOf,
keysOf,
ownerOf,
transfer,
transferFrom,
safeTransferFrom,
approve,
getApproved,
setApprovalForAll,
isApprovedForAll,
contract: token,
};
};
const use = (user) => {
return module(token.connect(user.signer), user);
};
return {
...module(token),
use
};
}
exports.default = ERC721;