import { browser } from "$app/environment"; import { toast } from "@zerodevx/svelte-toast"; import { goto } from "$app/navigation"; import { PUBLIC_COINOS_NETWORK, PUBLIC_COINOS_URL } from "$env/static/public"; import { page } from "$app/stores"; import { get as getStore } from "svelte/store"; export function scroll(section: any) { if (getStore(page).url.pathname !== "/") goto("/"); setTimeout(() => section.scrollIntoView({ behavior: "smooth" }), 500); } let base = browser ? "" : PUBLIC_COINOS_URL; export let punk = (k: string) => Math.floor((parseInt(k.slice(-2), 16) / 256) * 64) + 1 + ".webp"; export let g = (url: string, fetch: any, headers: object) => fetch(base + url, { headers }) .then((r: Response) => r.text()) .then((body: string) => { try { return JSON.parse(body); } catch (e) { throw new Error(body); } }); export let get = (url: string, headers = {}) => { return fetch(base + url, { headers }) .then(async (r) => { if (r.ok) return r.text(); throw new Error(await r.text()); }) .then((body) => { try { return JSON.parse(body); } catch (e) { return body; } }); }; export let post = async ( url: string, body: object, headers: HeadersInit | undefined = undefined ) => { headers = { "content-type": "application/json", accept: "application/json", ...headers, }; let response = await fetch(base + url, { method: "POST", body: JSON.stringify(body), headers, }) .then(async (r) => { if (r.ok) return r.text(); let text = await r.text(); let message; try { ({ message } = JSON.parse(text)); } catch (e) {} if (message) throw new Error(message); throw new Error(text); }) .then((body) => { let json; try { json = JSON.parse(body); } catch (e) { throw new Error(body); } if (json.error instanceof Error) throw json.error; if (json instanceof Error) throw body; if (json.name === "Error") throw new Error(json.message); return json; }); return response; }; export let copy = (text: string) => { navigator.clipboard.writeText(text); success("Copied!"); }; export let copyNoNewlines = (text: string) => { let stripped = text.replace(/\n/g, " ").replace(/\s+/g, " "); navigator.clipboard.writeText(stripped); success("Copied!"); }; export function reverseFormat(val: string, locale: string): number { let parts = new Intl.NumberFormat(locale).formatToParts(1111.1); let group = parts.find((part) => part.type === "group")!.value; let decimal = parts.find((part) => part.type === "decimal")!.value; let reversedVal = val.replace(new RegExp("\\" + group, "g"), ""); reversedVal = reversedVal.replace(new RegExp("\\" + decimal, "g"), "."); return Number.isNaN(reversedVal) ? 0 : +reversedVal; } export let protectedRoutes: RegExp[] = [/customers/, /settings/, /payments/]; let recent: string[] = []; export let success = (m: string, clear: boolean = true) => { if (recent.includes(m)) return; recent.push(m); setTimeout(() => (recent = []), 5000); if (clear) toast.pop(); toast.push(m, { theme: { "--toastBarBackground": "#F5F7FA", }, }); }; export let warning = (m: string, clear: boolean = true) => { if (clear) toast.pop(); toast.push(m, { theme: { "--toastBarBackground": "#FFCE22", }, }); }; export let fail = (m: string, clear: boolean = true) => { if (clear) toast.pop(); toast.push(m, { theme: { "--toastBarBackground": "#E93535", }, }); }; export let info = (m: string) => { toast.pop(); toast.push(m, { theme: { "--toastBarBackground": "white", }, }); }; export let login = async ( user: { username: string; password: string }, cookies: any, ip: string ) => { let maxAge = 380 * 24 * 60 * 60; let res = await fetch(base + "/login", { method: "POST", body: JSON.stringify(user), headers: { "content-type": "application/json", accept: "application/json", "cf-connecting-ip": ip, }, }); if (res.status === 401) { let text = await res.text(); if (text.startsWith("2fa")) throw new Error("2fa"); } let { user: u, token } = await res.json(); if (!token) throw new Error("Login failed"); let expires = new Date(); expires.setSeconds(expires.getSeconds() + maxAge); let opts = { path: "/", expires }; if (u.language) cookies.set("lang", u.language, opts); cookies.set("username", user.username, opts); cookies.set("token", token, opts); }; export let auth = (cookies: any) => ({ authorization: `Bearer ${cookies.get("token")}`, }); export let btc = (fiat: number, rate: number): number => Math.round((fiat * sats) / rate); export let fiat = (amount: number, rate: number): number => (amount * rate) / sats; export let fd = async (req: Request): Promise => { let obj: Record = Object.fromEntries(await req.formData()); for (let k in obj) (obj[k] === "undefined" && (obj[k] = undefined)) || (obj[k] === "false" && (obj[k] = false)); return obj; }; export let f = (s: number, currency: string): string => { try { return new Intl.NumberFormat("en-US", { style: "currency", currency, }) .format(s) .replace("CA", ""); } catch (e) { return ""; } }; export let s = (s: number): string => new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 }).format(s); export let sat = (s: number): string => { s = Math.abs(s); let p = Math.floor(Math.log(s) / Math.LN10 + 0.000000001); let d = Math.floor((p + 1) / 3); return ( "⚡️" + (parseInt(s.toString()) > 0 ? new Intl.NumberFormat("en-US", { maximumFractionDigits: 2 }).format( s / Math.pow(1000, d) ) + ["", "K", "M", "G", "T", "P", "E", "Z"][d] : 0 ).toString() ); }; export let sats = 100000000; export let back = (): any => browser && (history.lengt
const { DataTypes } = require('sequelize');

const attributes = {
  id: {
    type: DataTypes.INTEGER(11),
    primaryKey: true,
    autoIncrement: true,
    allowNull: false
  },
  name: {
    type: DataTypes.STRING(255),
    allowNull: false
  },
  code: {
    type: DataTypes.STRING(255)
  }
}
const options = {
  tableName: "networks",
  comment: "",
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      unique: true,
      fields: ['name', 'code']
    }
  ]
};

db["Network"] = db.define("networks_model", attributes, options);