zodor-protocol-web3
Version:
SDK to interact with zodor protocol
75 lines • 3.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Erc20 = void 0;
const ethers_1 = require("ethers");
const Erc20_json_1 = __importDefault(require("../abi/Erc20.json"));
const ContractFactory_1 = require("../factories/ContractFactory");
const utils_1 = require("../utils");
const WalletFactory_1 = require("../factories/WalletFactory");
class Erc20 {
contract;
network;
constructor(options) {
const { network, contractRunner, contractAddress, privateKey, customRpcUrl } = options;
if (!network)
throw new Error('network is required');
if (contractRunner) {
const contractFactory = new ContractFactory_1.ContractFactory();
this.contract = contractFactory.createContract(contractAddress, Erc20_json_1.default.abi, contractRunner);
}
else {
const rpcProvider = (0, utils_1.getRpcProvider)(network, customRpcUrl);
let wallet;
if (privateKey) {
const walletFactory = new WalletFactory_1.WalletFactory(rpcProvider);
wallet = walletFactory.createWallet(privateKey);
}
const contractFactory = new ContractFactory_1.ContractFactory();
this.contract = contractFactory.createContract(contractAddress, Erc20_json_1.default.abi, wallet || rpcProvider);
}
this.network = network;
}
async name() {
return (await this.contract.name());
}
async symbol() {
return (await this.contract.symbol());
}
async decimals() {
const decimalValue = await this.contract.decimals();
return Number(decimalValue);
}
async totalSupply() {
const totalSupply = await this.contract.totalSupply();
const decimals = await this.decimals();
return Number((0, ethers_1.formatEther)((0, ethers_1.formatUnits)(totalSupply.toString(), Number(decimals))));
}
async balanceOf(walletAddress) {
const balance = await this.contract.balanceOf(walletAddress);
const decimals = await this.decimals();
return Number((0, ethers_1.formatUnits)(balance.toString(), Number(decimals)));
}
async allowance(walletAdress, spender) {
return await this.contract.allowance(walletAdress, spender);
}
async approve(spender, amount) {
const decimals = await this.decimals();
const amountInWei = (0, ethers_1.formatUnits)(amount.toString(), Number(decimals));
return await this.contract.approve(spender, amountInWei);
}
async transfer(to, amount) {
const decimals = await this.decimals();
const amountInWei = (0, ethers_1.formatUnits)(amount.toString(), Number(decimals));
return await this.contract.transfer(to, amountInWei);
}
async transferFrom(from, to, amount) {
const decimals = await this.decimals();
const amountInWei = (0, ethers_1.formatUnits)(amount.toString(), Number(decimals));
return await this.contract.transfer(from, to, amountInWei);
}
}
exports.Erc20 = Erc20;
//# sourceMappingURL=erc20.js.map