UNPKG

@astonic-io/astonic-sdk

Version:

Official SDK for interacting with the Astonic Protocol

147 lines (146 loc) 6.85 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.unwrap = exports.wrap = exports.increaseAllowance = exports.getSymbolFromTokenAddress = exports.getBrokerAddressFromRegistry = exports.validateSignerOrProvider = exports.validateSigner = exports.getChainId = void 0; const ethers_1 = require("ethers"); const weth_json_1 = __importDefault(require("./abis/weth.json")); /** * Gets the chain ID from a signer or provider * @param signerOrProvider an ethers provider or signer * @returns the chain ID */ function getChainId(signerOrProvider) { return __awaiter(this, void 0, void 0, function* () { const provider = ethers_1.Signer.isSigner(signerOrProvider) ? signerOrProvider.provider : signerOrProvider; return (yield provider.getNetwork()).chainId; }); } exports.getChainId = getChainId; /** * Ensures that given signer is truly a a connected signer * @param signer an ethers signer * @throws if signer is invalid or not connected */ function validateSigner(signer) { if (!ethers_1.Signer.isSigner(signer)) { throw new Error('A valid signer must be provided'); } if (!ethers_1.providers.Provider.isProvider(signer.provider)) { throw new Error('Signer must be connected to a provider'); } } exports.validateSigner = validateSigner; /** * Ensures that given signerOrProvider is truly a provider or a connected signer * @param signerOrProvider an ethers provider or signer * @throws if signerOrProvider is invalid or not connected */ function validateSignerOrProvider(signerOrProvider) { const isSigner = ethers_1.Signer.isSigner(signerOrProvider); const isProvider = ethers_1.providers.Provider.isProvider(signerOrProvider); if (!isSigner && !isProvider) { throw new Error('A valid signer or provider must be provided'); } if (isSigner && !ethers_1.providers.Provider.isProvider(signerOrProvider.provider)) { throw new Error('Signer must be connected to a provider'); } } exports.validateSignerOrProvider = validateSignerOrProvider; /** * Returns the broker address from the Planq registry * @param signerOrProvider an ethers provider or signer * @returns the broker address */ function getBrokerAddressFromRegistry(signerOrProvider) { return __awaiter(this, void 0, void 0, function* () { const planqRegistryAddress = '0x9DabFe01de024C681320eb80FBc64EccEaa58ca2'; const brokerIdentifier = 'Broker'; const registryAbi = [ 'function getAddressForString(string calldata identifier) external view returns (address)', ]; const contract = new ethers_1.Contract(planqRegistryAddress, registryAbi, signerOrProvider); const brokerAddress = yield contract.getAddressForString(brokerIdentifier); if (brokerAddress === ethers_1.constants.AddressZero) { throw Error('Broker address not found in the registry'); } return brokerAddress; }); } exports.getBrokerAddressFromRegistry = getBrokerAddressFromRegistry; /** * Returns the symbol of an erc20 token * @param tokenAddr the address of the erc20 token * @param signerOrProvider an ethers provider or signer * @returns the symbol of the erc20 token */ function getSymbolFromTokenAddress(tokenAddr, signerOrProvider) { return __awaiter(this, void 0, void 0, function* () { const erc20Abi = ['function symbol() external view returns (string memory)']; const contract = new ethers_1.Contract(tokenAddr, erc20Abi, signerOrProvider); return contract.symbol(); }); } exports.getSymbolFromTokenAddress = getSymbolFromTokenAddress; /** * Returns a populated tx obj for increasing the allowance of a spender for a given erc20 token by a given amount * @param tokenAddr the address of the erc20 token * @param spender the address of the spender * @param amount the amount to increase the allowance by * @param signerOrProvider an ethers signer or provider * @returns the populated TransactionRequest object */ function increaseAllowance(tokenAddr, spender, amount, signerOrProvider) { return __awaiter(this, void 0, void 0, function* () { const abi = [ 'function approve(address spender, uint256 value) external returns (bool)', ]; // TODO, not all ERC-20 contracts support increaseAllowance // Add a check for that here const contract = new ethers_1.Contract(tokenAddr, abi, signerOrProvider); return yield contract.populateTransaction.approve(spender, amount); }); } exports.increaseAllowance = increaseAllowance; /** * Returns a populated tx obj for increasing the allowance of a spender for a given erc20 token by a given amount * @param tokenAddr the address of the erc20 token * @param spender the address of the spender * @param amount the amount to increase the allowance by * @param signerOrProvider an ethers signer or provider * @returns the populated TransactionRequest object */ function wrap(tokenAddr, amount, signerOrProvider) { return __awaiter(this, void 0, void 0, function* () { const contract = new ethers_1.Contract(tokenAddr, weth_json_1.default, signerOrProvider); return yield contract.populateTransaction.deposit({ value: amount }); }); } exports.wrap = wrap; /** * Returns a populated tx obj for increasing the allowance of a spender for a given erc20 token by a given amount * @param tokenAddr the address of the erc20 token * @param spender the address of the spender * @param amount the amount to increase the allowance by * @param signerOrProvider an ethers signer or provider * @returns the populated TransactionRequest object */ function unwrap(tokenAddr, amount, signerOrProvider) { return __awaiter(this, void 0, void 0, function* () { const contract = new ethers_1.Contract(tokenAddr, weth_json_1.default, signerOrProvider); return yield contract.populateTransaction.withdraw(amount); }); } exports.unwrap = unwrap;