From 0a2c8b4c365e9563ba8cde3ebb1da10758045a23 Mon Sep 17 00:00:00 2001 From: Iuri Pereira <689440+iuricmp@users.noreply.github.com> Date: Tue, 15 Oct 2024 09:02:17 +0100 Subject: [PATCH] refactor: generic makeCallTx --- mobile/app/post/index.tsx | 4 +-- mobile/redux/features/linkingSlice.ts | 44 ++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/mobile/app/post/index.tsx b/mobile/app/post/index.tsx index 5f0b354..8c6b763 100644 --- a/mobile/app/post/index.tsx +++ b/mobile/app/post/index.tsx @@ -6,7 +6,7 @@ import TextInput from "@gno/components/textinput"; import { Stack, useNavigation, useRouter } from "expo-router"; import { useEffect, useState } from "react"; import { KeyboardAvoidingView, Platform } from "react-native"; -import { broadcastTxCommit, hasParam, makeCallTxAndRedirect, selectAccount, selectQueryParams, selectQueryParamsAddress, useAppDispatch, useAppSelector } from "@gno/redux"; +import { broadcastTxCommit, hasParam, makeCallTxAndRedirectToSign, selectAccount, selectQueryParams, useAppDispatch, useAppSelector } from "@gno/redux"; export default function Search() { const [postContent, setPostContent] = useState(""); @@ -50,7 +50,7 @@ export default function Search() { const onPressPost = async () => { if (!account || !account.bech32) throw new Error("No active account: " + JSON.stringify(account)); - await dispatch(makeCallTxAndRedirect({ bech32: account.bech32, postContent })).unwrap(); + await dispatch(makeCallTxAndRedirectToSign({callerAddressBech32: account.bech32, postContent})).unwrap(); } return ( diff --git a/mobile/redux/features/linkingSlice.ts b/mobile/redux/features/linkingSlice.ts index a736fc2..4b17e83 100644 --- a/mobile/redux/features/linkingSlice.ts +++ b/mobile/redux/features/linkingSlice.ts @@ -27,22 +27,50 @@ export const requestLoginForGnokeyMobile = createAsyncThunk("tx/request return await Linking.openURL(`land.gno.gnokey://tologin?callback=${callback}`); }) -export const makeCallTxAndRedirect = createAsyncThunk("tx/makeCallTx", async ({ bech32, postContent }, thunkAPI) => { - console.log("making a tx to: ", bech32); +type MakeTxAndRedirectParams = { + postContent: string, + callerAddressBech32: string, +}; - const gnonative = thunkAPI.extra.gnonative; - const address = await gnonative.addressFromBech32(bech32); +export const makeCallTxAndRedirectToSign = createAsyncThunk("tx/makeCallTxAndRedirectToSign", async (props, thunkAPI) => { + const { callerAddressBech32, postContent } = props; + + const packagePath = "gno.land/r/berty/social"; + const fnc = "PostMessage"; + const args: Array = [postContent]; const gasFee = "1000000ugnot"; const gasWanted = BigInt(10000000); - const args: Array = [postContent]; - const res = await gnonative.makeCallTx("gno.land/r/berty/social", "PostMessage", args, gasFee, gasWanted, address) + const res = await thunkAPI.dispatch(makeCallTx({packagePath, fnc, args, gasFee, gasWanted, callerAddressBech32 })).unwrap(); setTimeout(() => { - const params = [`tx=${encodeURIComponent(res.txJson)}`, `address=${bech32}`, `client_name=dSocial`, `reason=Post a message`]; + const params = [`tx=${encodeURIComponent(res.txJson)}`, `address=${callerAddressBech32}`, `client_name=dSocial`, `reason=Post a message`]; Linking.openURL('land.gno.gnokey://tosign?' + params.join('&')) }, 500) - return res + + return res; +}) + +type MakeTxParams = { + packagePath: string, + fnc: string, + args: string[], + gasFee: string, + gasWanted: bigint, + send?: string, + memo?: string, + callerAddressBech32: string, +}; + +export const makeCallTx = createAsyncThunk("tx/makeCallTx", async (props, thunkAPI) => { + const {packagePath, fnc, callerAddressBech32, gasFee, gasWanted, args } = props; + + console.log("making a tx for: ", callerAddressBech32); + + const gnonative = thunkAPI.extra.gnonative; + const address = await gnonative.addressFromBech32(callerAddressBech32); + + return await gnonative.makeCallTx(packagePath, fnc, args, gasFee, gasWanted, address) }) export const broadcastTxCommit = createAsyncThunk("tx/broadcastTxCommit", async (signedTx, thunkAPI) => {