-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
106 lines (82 loc) · 2.85 KB
/
index.ts
File metadata and controls
106 lines (82 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { writable } from "svelte/store";
import { activeWalletValue, connectWallet, getAccount } from "./wallets";
import type { WalletType } from "./types";
import { getSigningClient } from "./clients";
import {
PUBLIC_CONSUMER_CHAIN_ENDPOINT,
PUBLIC_CONSUMER_CHAIN_ID,
PUBLIC_CONSUMER_TOKEN,
PUBLIC_CONTRACT_ADDRESS,
} from "$env/static/public";
import { getMainPageInfo } from "./contract";
import type { Permit, SecretNetworkClient } from "secretjs";
import { accountBalance, permitNonce, permitStore } from "$lib/state";
export const connected = writable(false);
export const signer = writable<any>();
export const address = writable<string | null>(null);
export const getEnigmaUtils = async (chainId: string) => {
const enigmaUtils = window.getEnigmaUtils!(chainId);
return enigmaUtils;
}
export const getPermit = async (client?: SecretNetworkClient) : Promise<Permit | undefined> => {
let permit : Permit | undefined = undefined;
const localPermit = localStorage.getItem(`permit`);
const nonce = localStorage.getItem(`permit_nonce`) ?? (Math.round(Math.random() * 10_000)).toString();
if (localPermit) {
permit = JSON.parse(localPermit);
if (!permit?.params.allowed_tokens.includes(PUBLIC_CONTRACT_ADDRESS)) {
permit = undefined;
localStorage.removeItem(`permit`);
}
} else if (client) {
permit = await client.utils.accessControl.permit.sign(
client.address,
PUBLIC_CONSUMER_CHAIN_ID,
"permit: "+ nonce,
[PUBLIC_CONTRACT_ADDRESS],
["owner"],
true
)
localStorage.setItem(`permit`, JSON.stringify(permit));
permitNonce.set(nonce);
} else {
//throw new Error(`Can't get Permit`);
}
if (permit) {
permitStore.set(permit);
}
return permit;
}
export const initWeb3 = async (chainId?: string | string[], wallet? : WalletType) => {
chainId ??= PUBLIC_CONSUMER_CHAIN_ID;
const walletConnected = await connectWallet(chainId)
connected.set(walletConnected);
chainId = Array.isArray(chainId) ? chainId[0] : chainId;
if (walletConnected) {
const account = await getAccount(chainId, activeWalletValue!);
if (account) {
signer.set(account.signer);
address.set(account.address);
return await getSigningClient(
chainId,
PUBLIC_CONSUMER_CHAIN_ENDPOINT,
await getEnigmaUtils(chainId),
account.address,
account.signer
);
}
}
return null;
}
export const setupContractAndListeners = async () => {
const permit = await getPermit();
await getMainPageInfo(permit);
return false;
}
export const getBalance = async (
client: SecretNetworkClient
) => {
const res = await client.query.bank.allBalances({ address: client.address });
const balance = res.balances!.find((b) => b.denom === PUBLIC_CONSUMER_TOKEN)?.amount ?? "0";
accountBalance.set(balance);
}