Skip to content

Commit

Permalink
refactor: generic makeCallTx
Browse files Browse the repository at this point in the history
  • Loading branch information
iuricmp committed Oct 15, 2024
1 parent 26aa656 commit 0a2c8b4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions mobile/app/post/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down Expand Up @@ -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 (
Expand Down
44 changes: 36 additions & 8 deletions mobile/redux/features/linkingSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,50 @@ export const requestLoginForGnokeyMobile = createAsyncThunk<boolean>("tx/request
return await Linking.openURL(`land.gno.gnokey://tologin?callback=${callback}`);
})

export const makeCallTxAndRedirect = createAsyncThunk<MakeTxResponse, { bech32: string, postContent: string }, ThunkExtra>("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<MakeTxResponse, MakeTxAndRedirectParams, ThunkExtra>("tx/makeCallTxAndRedirectToSign", async (props, thunkAPI) => {
const { callerAddressBech32, postContent } = props;

const packagePath = "gno.land/r/berty/social";
const fnc = "PostMessage";
const args: Array<string> = [postContent];
const gasFee = "1000000ugnot";
const gasWanted = BigInt(10000000);
const args: Array<string> = [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<MakeTxResponse, MakeTxParams, ThunkExtra>("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<void, string, ThunkExtra>("tx/broadcastTxCommit", async (signedTx, thunkAPI) => {
Expand Down

0 comments on commit 0a2c8b4

Please sign in to comment.