UNPKG

@astonic-io/astonic-sdk

Version:

Official SDK for interacting with the Astonic Protocol

133 lines (132 loc) 5.98 kB
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()); }); }; import { constants, Contract, providers, Signer } from 'ethers'; import WETH_ABI from './abis/weth.json'; /** * Gets the chain ID from a signer or provider * @param signerOrProvider an ethers provider or signer * @returns the chain ID */ export function getChainId(signerOrProvider) { return __awaiter(this, void 0, void 0, function* () { const provider = Signer.isSigner(signerOrProvider) ? signerOrProvider.provider : signerOrProvider; return (yield provider.getNetwork()).chainId; }); } /** * Ensures that given signer is truly a a connected signer * @param signer an ethers signer * @throws if signer is invalid or not connected */ export function validateSigner(signer) { if (!Signer.isSigner(signer)) { throw new Error('A valid signer must be provided'); } if (!providers.Provider.isProvider(signer.provider)) { throw new Error('Signer must be connected to a provider'); } } /** * 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 */ export function validateSignerOrProvider(signerOrProvider) { const isSigner = Signer.isSigner(signerOrProvider); const isProvider = providers.Provider.isProvider(signerOrProvider); if (!isSigner && !isProvider) { throw new Error('A valid signer or provider must be provided'); } if (isSigner && !providers.Provider.isProvider(signerOrProvider.provider)) { throw new Error('Signer must be connected to a provider'); } } /** * Returns the broker address from the Planq registry * @param signerOrProvider an ethers provider or signer * @returns the broker address */ export 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 Contract(planqRegistryAddress, registryAbi, signerOrProvider); const brokerAddress = yield contract.getAddressForString(brokerIdentifier); if (brokerAddress === constants.AddressZero) { throw Error('Broker address not found in the registry'); } return brokerAddress; }); } /** * 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 */ export function getSymbolFromTokenAddress(tokenAddr, signerOrProvider) { return __awaiter(this, void 0, void 0, function* () { const erc20Abi = ['function symbol() external view returns (string memory)']; const contract = new Contract(tokenAddr, erc20Abi, signerOrProvider); return contract.symbol(); }); } /** * 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 */ export 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 Contract(tokenAddr, abi, signerOrProvider); return yield contract.populateTransaction.approve(spender, amount); }); } /** * 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 */ export function wrap(tokenAddr, amount, signerOrProvider) { return __awaiter(this, void 0, void 0, function* () { const contract = new Contract(tokenAddr, WETH_ABI, signerOrProvider); return yield contract.populateTransaction.deposit({ value: amount }); }); } /** * 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 */ export function unwrap(tokenAddr, amount, signerOrProvider) { return __awaiter(this, void 0, void 0, function* () { const contract = new Contract(tokenAddr, WETH_ABI, signerOrProvider); return yield contract.populateTransaction.withdraw(amount); }); }