-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.ts
223 lines (175 loc) · 5.19 KB
/
contract.ts
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { PUBLIC_CONSUMER_CHANNEL, PUBLIC_CONSUMER_TOKEN, PUBLIC_CONTRACT_ADDRESS, PUBLIC_CONTRACT_CODE_HASH, PUBLIC_SECRET_CHAIN_ENDPOINT, PUBLIC_SECRET_CHAIN_ID, PUBLIC_SECRET_CHANNEL } from "$env/static/public";
import { coinFromString, type IbcResponse, type Permit, type SecretNetworkClient, type TxResponse, type TxSender } from "secretjs";
import { getClient } from "./clients";
import { toNumber, toSecretIBCDenom } from "$lib/utils";
import { cells, cloverCount, fertilizerCount, shovelCount } from "$lib/state";
import { parseConfig } from "./game";
import { Powerup, type Cell, type MainResult } from "$lib/types";
import { processLocalTxResFailure, processLocalTxResSuccess, processRemoteTxResFailure, processIBCResSuccess } from "$lib/middleware";
export const hookWrapper = (msg: object) => {
return JSON.stringify({
wasm: {
contract: PUBLIC_CONTRACT_ADDRESS,
msg
}
})
}
export const executeOverIBC = async (
client: SecretNetworkClient,
contractMsg: object,
amount: string
) => {
return await client.tx.ibc.transfer({
sender: client.address,
receiver: PUBLIC_CONTRACT_ADDRESS,
token: coinFromString(amount + PUBLIC_CONSUMER_TOKEN),
source_port: "transfer",
source_channel: PUBLIC_CONSUMER_CHANNEL,
memo: hookWrapper(contractMsg),
timeout_timestamp: String(Math.floor(Date.now()/1000) + 90)
})
}
export const openCell = async (
client: SecretNetworkClient,
cell_id: number,
permit: Permit,
powerups: Powerup[],
amount: string,
autobuy: boolean,
toastStore: any
) => {
const contractMsg : any = {
open_cell: {
cell_id,
powerups,
permit
}
}
if (autobuy) {
contractMsg.open_cell["power_up_autopay"] = true
}
let res : TxResponse
try {
res = await executeOverIBC(
client,
contractMsg,
amount
)
console.log("OPEN CELL RES:", res)
} catch (e) {
console.error("OPEN CELL ERROR:", e)
processLocalTxResFailure(e, toastStore);
return;
}
processLocalTxResSuccess(res, toastStore);
let ibcRes : IbcResponse
try {
ibcRes = await res.ibcResponses[0];
console.log("OPEN CELL IBC RES:", ibcRes)
} catch (e) {
console.error("OPEN CELL IBC ERROR:", e)
processRemoteTxResFailure(e, toastStore);
return;
}
processIBCResSuccess(ibcRes, toastStore);
return ibcRes;
}
export const buyPowerup = async (
client: SecretNetworkClient,
permit: Permit,
powerups: Powerup[],
amount: string,
toastStore: any
) => {
const contractMsg = {
buy_powerups: {
powerups,
permit
}
}
let res : TxResponse
try {
res = await executeOverIBC(
client,
contractMsg,
amount
)
console.log("BUY POWERUP RES:", res)
} catch (e) {
console.error("BUY POWERUP ERROR:", e)
processLocalTxResFailure(e, toastStore);
return;
}
processLocalTxResSuccess(res, toastStore);
let ibcRes : IbcResponse
try {
ibcRes = await res.ibcResponses[0];
console.log("BUY POWERUP IBC RES:", ibcRes)
} catch (e) {
console.error("BUY POWERUP IBC ERROR:", e)
processRemoteTxResFailure(e, toastStore);
return;
}
processIBCResSuccess(ibcRes, toastStore);
return ibcRes;
}
export const queryContract = async (
query: object
) : Promise<any> => {
const client = getClient(
PUBLIC_SECRET_CHAIN_ID,
PUBLIC_SECRET_CHAIN_ENDPOINT
);
return await client.query.compute.queryContract({
contract_address: PUBLIC_CONTRACT_ADDRESS,
code_hash: PUBLIC_CONTRACT_CODE_HASH ?? "",
query
})
}
export const getMainPageInfo = async (
permit?: Permit
) => {
let res : MainResult = await queryContract({
main: {
permit
}
})
if (typeof res === "string" && (res as string).includes("error")) {
res = await queryContract({ main: {} })
}
console.log("MAIN PAGE INFO:", res)
const tokenOnSecret = toSecretIBCDenom(
PUBLIC_CONSUMER_TOKEN,
PUBLIC_SECRET_CHANNEL
)
const foundConfig = res.network_configs.find(
(nc) => nc[0] === tokenOnSecret
)
if (foundConfig) {
parseConfig(foundConfig[1])
} else {
console.error("FATAL: The contract has no config fot this chain")
}
cells.set(
res.cells
.map((c : Cell, i: number) => ({ id: i+1, open_at: new Date(c.open_at * 1000) }))
)
if (res.powerups) {
for (const [pup, count] of res.powerups) {
if (pup == Powerup.Clover) {
cloverCount.set(count)
} else if (pup == Powerup.Fertilizer) {
fertilizerCount.set(count)
} else if (pup == Powerup.Shovel) {
shovelCount.set(count)
}
}
}
return res
}
export const queryConfig = async () => {
const tokenOnSecret = toSecretIBCDenom(
PUBLIC_CONSUMER_TOKEN,
PUBLIC_SECRET_CHANNEL
)
}