From a3a822b95c42e460d0f627002dae002b0f411ccc Mon Sep 17 00:00:00 2001 From: Cheelax Date: Thu, 10 Oct 2024 16:01:17 +0200 Subject: [PATCH 1/4] implement ip info and send supabase --- client/src/ui/actions/Start.tsx | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/client/src/ui/actions/Start.tsx b/client/src/ui/actions/Start.tsx index 4ef70593..65680fe7 100644 --- a/client/src/ui/actions/Start.tsx +++ b/client/src/ui/actions/Start.tsx @@ -55,7 +55,37 @@ export const Start: React.FC = ({ mode, handleGameMode }) => { proof_verify_hint, beta, } = await fetchVrfData(); - + //send to supabase + if (import.meta.env.VITE_SEND_TO_SUPABASE) { + try { + const responseIp = await fetch( + `https://ipinfo.io/json?token=${import.meta.env.VITE_IPINFO_KEY}`, + ); + const { country } = await responseIp.json(); + const accountAddress = account?.address; + const response = await fetch( + "https://rjzlpqqdiwqkglbzeaxa.supabase.co/functions/v1/zkube-payment", + { + method: "POST", + headers: { + Authorization: `Bearer ${import.meta.env.VITE_SUPABASE_KEY}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + wallet: accountAddress, + country: country, + timestamp: new Date().toISOString(), + }), + }, + ); + + if (!response.ok) { + console.error("Failed to send data to Supabase"); + } + } catch (error) { + console.error("Error sending data to Supabase:", error); + } + } await start({ account: account as Account, mode: new Mode(mode).into(), From db360f0d1b9c2820e22112b76fad18988810648c Mon Sep 17 00:00:00 2001 From: Cheelax Date: Mon, 14 Oct 2024 16:44:12 +0200 Subject: [PATCH 2/4] wip improve supabase --- client/src/ui/actions/Start.tsx | 80 ++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 27 deletions(-) diff --git a/client/src/ui/actions/Start.tsx b/client/src/ui/actions/Start.tsx index 65680fe7..844625cd 100644 --- a/client/src/ui/actions/Start.tsx +++ b/client/src/ui/actions/Start.tsx @@ -55,14 +55,59 @@ export const Start: React.FC = ({ mode, handleGameMode }) => { proof_verify_hint, beta, } = await fetchVrfData(); - //send to supabase + // //send to supabase + // if (import.meta.env.VITE_SEND_TO_SUPABASE) { + // try { + // const responseIp = await fetch( + // `https://ipinfo.io/json?token=${import.meta.env.VITE_IPINFO_KEY}`, + // ); + // const { country } = await responseIp.json(); + // const accountAddress = account?.address; + // const response = await fetch( + // "https://rjzlpqqdiwqkglbzeaxa.supabase.co/functions/v1/zkube-payment", + // { + // method: "POST", + // headers: { + // Authorization: `Bearer ${import.meta.env.VITE_SUPABASE_KEY}`, + // "Content-Type": "application/json", + // }, + // body: JSON.stringify({ + // wallet: accountAddress, + // country: country, + // timestamp: new Date().toISOString(), + // }), + // }, + // ); + + // if (!response.ok) { + // console.error("Failed to send data to Supabase"); + // } + // } catch (error) { + // console.error("Error sending data to Supabase:", error); + // } + // } + await start({ + account: account as Account, + mode: new Mode(mode).into(), + price: + mode === ModeType.Daily + ? settings.daily_mode_price + : settings.normal_mode_price, + seed, + x: proof_gamma_x, + y: proof_gamma_y, + c: proof_c, + s: proof_s, + sqrt_ratio_hint: proof_verify_hint, + beta: beta, + }); + handleGameMode(); + } finally { if (import.meta.env.VITE_SEND_TO_SUPABASE) { try { - const responseIp = await fetch( - `https://ipinfo.io/json?token=${import.meta.env.VITE_IPINFO_KEY}`, - ); - const { country } = await responseIp.json(); const accountAddress = account?.address; + const transactionHash = "test"; + const response = await fetch( "https://rjzlpqqdiwqkglbzeaxa.supabase.co/functions/v1/zkube-payment", { @@ -73,36 +118,17 @@ export const Start: React.FC = ({ mode, handleGameMode }) => { }, body: JSON.stringify({ wallet: accountAddress, - country: country, - timestamp: new Date().toISOString(), + transactionHash: transactionHash, }), }, ); - if (!response.ok) { - console.error("Failed to send data to Supabase"); - } + const result = await response.json(); + console.log("Data sent successfully:", result); } catch (error) { console.error("Error sending data to Supabase:", error); } } - await start({ - account: account as Account, - mode: new Mode(mode).into(), - price: - mode === ModeType.Daily - ? settings.daily_mode_price - : settings.normal_mode_price, - seed, - x: proof_gamma_x, - y: proof_gamma_y, - c: proof_c, - s: proof_s, - sqrt_ratio_hint: proof_verify_hint, - beta: beta, - }); - handleGameMode(); - } finally { setIsLoading(false); } }, [account, mode, settings]); From a0af2788d43745595d688211a444bfc187a36fd2 Mon Sep 17 00:00:00 2001 From: Cheelax Date: Wed, 16 Oct 2024 10:20:16 +0200 Subject: [PATCH 3/4] finalize implementation --- client/package.json | 1 + client/pnpm-lock.yaml | 65 +++++++++++++++++++++++++++++++- client/src/dojo/systems.ts | 3 +- client/src/ui/actions/Start.tsx | 67 ++++++++------------------------- client/src/utils/supabase.ts | 7 ++++ 5 files changed, 89 insertions(+), 54 deletions(-) create mode 100644 client/src/utils/supabase.ts diff --git a/client/package.json b/client/package.json index da4c0565..bc01cd06 100644 --- a/client/package.json +++ b/client/package.json @@ -56,6 +56,7 @@ "@radix-ui/react-visually-hidden": "^1.1.0", "@starknet-react/chains": "^0.1.7", "@starknet-react/core": "2.9.0", + "@supabase/supabase-js": "^2.45.4", "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "date-fns": "^4.1.0", diff --git a/client/pnpm-lock.yaml b/client/pnpm-lock.yaml index 39acba3e..d42ee875 100644 --- a/client/pnpm-lock.yaml +++ b/client/pnpm-lock.yaml @@ -107,6 +107,9 @@ dependencies: '@starknet-react/core': specifier: 2.9.0 version: 2.9.0(get-starknet-core@3.3.0)(react@18.3.1)(starknet@6.11.0) + '@supabase/supabase-js': + specifier: ^2.45.4 + version: 2.45.4 class-variance-authority: specifier: ^0.7.0 version: 0.7.0 @@ -6097,6 +6100,63 @@ packages: zod: 3.23.8 dev: false + /@supabase/auth-js@2.65.0: + resolution: {integrity: sha512-+wboHfZufAE2Y612OsKeVP4rVOeGZzzMLD/Ac3HrTQkkY4qXNjI6Af9gtmxwccE5nFvTiF114FEbIQ1hRq5uUw==} + dependencies: + '@supabase/node-fetch': 2.6.15 + dev: false + + /@supabase/functions-js@2.4.1: + resolution: {integrity: sha512-8sZ2ibwHlf+WkHDUZJUXqqmPvWQ3UHN0W30behOJngVh/qHHekhJLCFbh0AjkE9/FqqXtf9eoVvmYgfCLk5tNA==} + dependencies: + '@supabase/node-fetch': 2.6.15 + dev: false + + /@supabase/node-fetch@2.6.15: + resolution: {integrity: sha512-1ibVeYUacxWYi9i0cf5efil6adJ9WRyZBLivgjs+AUpewx1F3xPi7gLgaASI2SmIQxPoCEjAsLAzKPgMJVgOUQ==} + engines: {node: 4.x || >=6.0.0} + dependencies: + whatwg-url: 5.0.0 + dev: false + + /@supabase/postgrest-js@1.16.1: + resolution: {integrity: sha512-EOSEZFm5pPuCPGCmLF1VOCS78DfkSz600PBuvBND/IZmMciJ1pmsS3ss6TkB6UkuvTybYiBh7gKOYyxoEO3USA==} + dependencies: + '@supabase/node-fetch': 2.6.15 + dev: false + + /@supabase/realtime-js@2.10.2: + resolution: {integrity: sha512-qyCQaNg90HmJstsvr2aJNxK2zgoKh9ZZA8oqb7UT2LCh3mj9zpa3Iwu167AuyNxsxrUE8eEJ2yH6wLCij4EApA==} + dependencies: + '@supabase/node-fetch': 2.6.15 + '@types/phoenix': 1.6.5 + '@types/ws': 8.5.10 + ws: 8.17.1 + transitivePeerDependencies: + - bufferutil + - utf-8-validate + dev: false + + /@supabase/storage-js@2.7.0: + resolution: {integrity: sha512-iZenEdO6Mx9iTR6T7wC7sk6KKsoDPLq8rdu5VRy7+JiT1i8fnqfcOr6mfF2Eaqky9VQzhP8zZKQYjzozB65Rig==} + dependencies: + '@supabase/node-fetch': 2.6.15 + dev: false + + /@supabase/supabase-js@2.45.4: + resolution: {integrity: sha512-E5p8/zOLaQ3a462MZnmnz03CrduA5ySH9hZyL03Y+QZLIOO4/Gs8Rdy4ZCKDHsN7x0xdanVEWWFN3pJFQr9/hg==} + dependencies: + '@supabase/auth-js': 2.65.0 + '@supabase/functions-js': 2.4.1 + '@supabase/node-fetch': 2.6.15 + '@supabase/postgrest-js': 1.16.1 + '@supabase/realtime-js': 2.10.2 + '@supabase/storage-js': 2.7.0 + transitivePeerDependencies: + - bufferutil + - utf-8-validate + dev: false + /@surma/rollup-plugin-off-main-thread@2.2.3: resolution: {integrity: sha512-lR8q/9W7hZpMWweNiAKU7NQerBnzQQLvi8qnTDU/fxItPhtZVMbPV3lbCwjhIlNBe9Bbr5V+KHshvWmVSG9cxQ==} dependencies: @@ -6390,6 +6450,10 @@ packages: resolution: {integrity: sha512-dISoDXWWQwUquiKsyZ4Ng+HX2KsPL7LyHKHQwgGFEA3IaKac4Obd+h2a/a6waisAoepJlBcx9paWqjA8/HVjCw==} dev: false + /@types/phoenix@1.6.5: + resolution: {integrity: sha512-xegpDuR+z0UqG9fwHqNoy3rI7JDlvaPh2TY47Fl80oq6g+hXT+c/LEuE43X48clZ6lOfANl5WrPur9fYO1RJ/w==} + dev: false + /@types/prop-types@15.7.12: resolution: {integrity: sha512-5zvhXYtRNRluoE/jAp4GVsSduVUzNWKkOZrCDBWYtE7biZywwdC2AcEzg+cSMLFRfVgeAFqpfNabiPjxFddV1Q==} @@ -12969,7 +13033,6 @@ packages: optional: true utf-8-validate: optional: true - dev: true /y18n@4.0.3: resolution: {integrity: sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==} diff --git a/client/src/dojo/systems.ts b/client/src/dojo/systems.ts index 14c15512..11dc5bac 100644 --- a/client/src/dojo/systems.ts +++ b/client/src/dojo/systems.ts @@ -109,6 +109,7 @@ export function systems({ }); notify(successMessage, transaction); + return transaction_hash; } catch (error: any) { console.error("Error executing transaction:", error); if (!error?.message) { @@ -138,7 +139,7 @@ export function systems({ }; const start = async ({ account, ...props }: SystemTypes.Start) => { - await handleTransaction( + return await handleTransaction( account, () => client.play.start({ account, ...props }), "Game has been started.", diff --git a/client/src/ui/actions/Start.tsx b/client/src/ui/actions/Start.tsx index ab346602..de31941e 100644 --- a/client/src/ui/actions/Start.tsx +++ b/client/src/ui/actions/Start.tsx @@ -12,6 +12,7 @@ import { createFaucetClaimHandler } from "@/utils/faucet"; import { useContract } from "@starknet-react/core"; import { erc20ABI } from "@/utils/erc20"; import { useCredits } from "@/hooks/useCredits"; +import supabase from "@/utils/supabase"; interface BalanceData { balance: { @@ -105,38 +106,9 @@ export const Start: React.FC = ({ mode, handleGameMode }) => { proof_verify_hint, beta, } = await fetchVrfData(); - // //send to supabase - // if (import.meta.env.VITE_SEND_TO_SUPABASE) { - // try { - // const responseIp = await fetch( - // `https://ipinfo.io/json?token=${import.meta.env.VITE_IPINFO_KEY}`, - // ); - // const { country } = await responseIp.json(); - // const accountAddress = account?.address; - // const response = await fetch( - // "https://rjzlpqqdiwqkglbzeaxa.supabase.co/functions/v1/zkube-payment", - // { - // method: "POST", - // headers: { - // Authorization: `Bearer ${import.meta.env.VITE_SUPABASE_KEY}`, - // "Content-Type": "application/json", - // }, - // body: JSON.stringify({ - // wallet: accountAddress, - // country: country, - // timestamp: new Date().toISOString(), - // }), - // }, - // ); - - // if (!response.ok) { - // console.error("Failed to send data to Supabase"); - // } - // } catch (error) { - // console.error("Error sending data to Supabase:", error); - // } - // } - await start({ + //send to supabase + + const transactionHash = await start({ account: account as Account, mode: new Mode(mode).into(), price: @@ -151,34 +123,25 @@ export const Start: React.FC = ({ mode, handleGameMode }) => { sqrt_ratio_hint: proof_verify_hint, beta: beta, }); - handleGameMode(); - } finally { + if (import.meta.env.VITE_SEND_TO_SUPABASE) { try { - const accountAddress = account?.address; - const transactionHash = "test"; - - const response = await fetch( - "https://rjzlpqqdiwqkglbzeaxa.supabase.co/functions/v1/zkube-payment", - { - method: "POST", - headers: { - Authorization: `Bearer ${import.meta.env.VITE_SUPABASE_KEY}`, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - wallet: accountAddress, - transactionHash: transactionHash, - }), + const { error } = await supabase.functions.invoke("zkube-payment", { + body: { + timestamp: new Date().toISOString(), + transactionHash: transactionHash, }, - ); + }); - const result = await response.json(); - console.log("Data sent successfully:", result); + if (error) { + console.error("Failed to send data to Supabase", error); + } } catch (error) { console.error("Error sending data to Supabase:", error); } } + handleGameMode(); + } finally { setIsLoading(false); } }, [account, mode, settings, credits]); diff --git a/client/src/utils/supabase.ts b/client/src/utils/supabase.ts new file mode 100644 index 00000000..fd61cdbb --- /dev/null +++ b/client/src/utils/supabase.ts @@ -0,0 +1,7 @@ +import { createClient } from "@supabase/supabase-js"; + +const supabaseUrl = import.meta.env.VITE_SUPABASE_URL; +const supabaseKey = import.meta.env.VITE_SUPABASE_ANON_KEY; +const supabase = createClient(supabaseUrl, supabaseKey); + +export default supabase; From ef1eb847e566357dcb471025d08d96aadc300ac7 Mon Sep 17 00:00:00 2001 From: Cheelax Date: Wed, 16 Oct 2024 10:27:20 +0200 Subject: [PATCH 4/4] add guard --- client/src/utils/supabase.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/client/src/utils/supabase.ts b/client/src/utils/supabase.ts index fd61cdbb..28abb0e7 100644 --- a/client/src/utils/supabase.ts +++ b/client/src/utils/supabase.ts @@ -2,6 +2,11 @@ import { createClient } from "@supabase/supabase-js"; const supabaseUrl = import.meta.env.VITE_SUPABASE_URL; const supabaseKey = import.meta.env.VITE_SUPABASE_ANON_KEY; + +if (import.meta.env.VITE_SEND_TO_SUPABASE && (!supabaseUrl || !supabaseKey)) { + throw new Error("Missing Supabase environment variables"); +} + const supabase = createClient(supabaseUrl, supabaseKey); export default supabase;