Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syndote: add sails #507

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions frontend/apps/syndote/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ yarn install

Create `.env` file, `.env.example` will let you know what variables are expected.

Put the latest version of the `syndote.meta.wasm` file locally in `gear-js\apps\syndote\src\assets\wasm\`, replace if necessary.

In order for all features to work as expected, the node and it's runtime version should be chosen based on the current `@gear-js/api` version.

In case of issues with the application, try to switch to another network or run your own local node and specify its address in the `.env` file. When applicable, make sure the smart contract(s) wasm files are uploaded and running in this network accordingly.
Expand Down
3 changes: 3 additions & 0 deletions frontend/apps/syndote/src/app/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { usePending } from './use-pending';
export * from './use-execute-with-pendig';
export * from './use-sign-and-send';
35 changes: 35 additions & 0 deletions frontend/apps/syndote/src/app/hooks/use-execute-with-pendig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useAlert } from '@gear-js/react-hooks';
import { usePending } from './use-pending';
import { getPanicType } from 'utils';

export type Options = {
onSuccess?: () => void;
onError?: (error?: unknown) => void;
};

export function useExecuteWithPending() {
const { setPending } = usePending();
const alert = useAlert();

const executeWithPending = async (action: () => Promise<void>, options?: Options) => {
try {
setPending(true);
await action();
options?.onSuccess?.();
} catch (error) {
console.error(error);
options?.onError?.(error);

const panicType = getPanicType(error);
const alertError = typeof error === 'string' ? error : panicType;

if (alertError) {
alert.error(alertError);
}
} finally {
setPending(false);
}
};

return { executeWithPending };
}
8 changes: 8 additions & 0 deletions frontend/apps/syndote/src/app/hooks/use-pending.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IS_LOADING } from 'atoms';
import { useAtom } from 'jotai';

export function usePending() {
const [pending, setPending] = useAtom(IS_LOADING);

return { pending, setPending };
}
28 changes: 28 additions & 0 deletions frontend/apps/syndote/src/app/hooks/use-sign-and-send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useCheckBalance } from '@dapps-frontend/hooks';
import { GenericTransactionReturn, TransactionReturn } from '@gear-js/react-hooks/dist/esm/hooks/sails/types';

export const useSignAndSend = () => {
const { checkBalance } = useCheckBalance();

const signAndSend = async (transaction: TransactionReturn<() => GenericTransactionReturn<null>>) => {
const calculatedGas = Number(transaction.extrinsic.args[2].toString());

return new Promise<void>((resolve, reject) => {
checkBalance(
calculatedGas,
async () => {
try {
const { response } = await transaction.signAndSend();
await response();
resolve();
} catch (e) {
reject(e);
}
},
() => reject(),
);
});
};

return { signAndSend };
};
1 change: 1 addition & 0 deletions frontend/apps/syndote/src/app/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './sails';
2 changes: 2 additions & 0 deletions frontend/apps/syndote/src/app/utils/sails/events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { useEventStepSubscription } from './use-event-step-subscription';
export { useEventGameCanceledSubscription } from './use-event-game-canceled-subscription';
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useProgramEvent } from '@gear-js/react-hooks';
import { useProgram } from 'app/utils';

export function useEventGameCanceledSubscription(onData: () => void) {
const program = useProgram();

useProgramEvent({
program,
serviceName: 'syndote',
functionName: 'subscribeToGameWasCancelledEvent',
onData,
});

useProgramEvent({
program,
serviceName: 'syndote',
functionName: 'subscribeToGameDeletedEvent',
onData,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useProgramEvent } from '@gear-js/react-hooks';
import { useProgram } from 'app/utils';
import { Step } from 'types';

export function useEventStepSubscription(onData: (data: Step) => void) {
const program = useProgram();

useProgramEvent({
program,
serviceName: 'syndote',
functionName: 'subscribeToStepEvent',
onData,
});
}
5 changes: 5 additions & 0 deletions frontend/apps/syndote/src/app/utils/sails/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './sails';
export * from './syndote';
export * from './events';
export * from './queries';
export * from './messages';
8 changes: 8 additions & 0 deletions frontend/apps/syndote/src/app/utils/sails/messages/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export { useAddGasToPlayerStrategyMessage } from './use-add-gas-to-player-strategy-message';
export { useCancelGameSessionMessage } from './use-cancel-game-session-message';
export { useCreateGameSessionMessage } from './use-create-game-session-message';
export { useDeleteGameMessage } from './use-delete-game-message';
export { useDeletePlayerMessage } from './use-delete-player-message';
export { useExitGameMessage } from './use-exit-game-message';
export { usePlayMessage } from './use-play-message';
export { useRegisterMessage } from './use-register-message';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { HexString } from '@gear-js/api';
import { usePrepareProgramTransaction } from '@gear-js/react-hooks';
import { useProgram } from 'app/utils';
import { Options, useExecuteWithPending, useSignAndSend } from 'app/hooks';

type Params = {
adminId: HexString;
};

export const useAddGasToPlayerStrategyMessage = () => {
const program = useProgram();
const { prepareTransactionAsync } = usePrepareProgramTransaction({
program,
serviceName: 'syndote',
functionName: 'addGasToPlayerStrategy',
});
const { signAndSend } = useSignAndSend();
const { executeWithPending } = useExecuteWithPending();

const addGasToPlayerStrategyMessage = async ({ adminId }: Params, options?: Options) =>
executeWithPending(async () => {
const { transaction } = await prepareTransactionAsync({
args: [adminId],
gasLimit: { increaseGas: 10 },
});
await signAndSend(transaction);
}, options);

return { addGasToPlayerStrategyMessage };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { HexString } from '@gear-js/api';
import { usePrepareProgramTransaction } from '@gear-js/react-hooks';
import { useProgram } from 'app/utils';
import { Options, useExecuteWithPending, useSignAndSend } from 'app/hooks';

type Params = {
adminId: HexString;
};

export const useCancelGameSessionMessage = () => {
const program = useProgram();
const { prepareTransactionAsync } = usePrepareProgramTransaction({
program,
serviceName: 'syndote',
functionName: 'cancelGameSession',
});
const { signAndSend } = useSignAndSend();
const { executeWithPending } = useExecuteWithPending();

const cancelGameSessionMessage = async ({ adminId }: Params, options?: Options) =>
executeWithPending(async () => {
const { transaction } = await prepareTransactionAsync({
args: [adminId],
gasLimit: { increaseGas: 10 },
});
await signAndSend(transaction);
}, options);

return { cancelGameSessionMessage };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { HexString } from '@gear-js/api';
import { usePrepareProgramTransaction } from '@gear-js/react-hooks';
import { useProgram } from 'app/utils';
import { Options, useExecuteWithPending, useSignAndSend } from 'app/hooks';

type Params = {
value?: bigint;
entryFee: number | string | bigint | null;
name: string;
strategyId: HexString;
};

export const useCreateGameSessionMessage = () => {
const program = useProgram();
const { prepareTransactionAsync } = usePrepareProgramTransaction({
program,
serviceName: 'syndote',
functionName: 'createGameSession',
});
const { signAndSend } = useSignAndSend();
const { executeWithPending } = useExecuteWithPending();

const createGameSessionMessage = async ({ value, entryFee, name, strategyId }: Params, options?: Options) =>
executeWithPending(async () => {
const { transaction } = await prepareTransactionAsync({
args: [entryFee, name, strategyId],
gasLimit: { increaseGas: 10 },
value,
});
await signAndSend(transaction);
}, options);

return { createGameSessionMessage };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { HexString } from '@gear-js/api';
import { usePrepareProgramTransaction } from '@gear-js/react-hooks';
import { useProgram } from 'app/utils';
import { Options, useExecuteWithPending, useSignAndSend } from 'app/hooks';

type Params = {
adminId: HexString;
};

export const useDeleteGameMessage = () => {
const program = useProgram();
const { prepareTransactionAsync } = usePrepareProgramTransaction({
program,
serviceName: 'syndote',
functionName: 'deleteGame',
});
const { signAndSend } = useSignAndSend();
const { executeWithPending } = useExecuteWithPending();

const deleteGameMessage = ({ adminId }: Params, options?: Options) =>
executeWithPending(async () => {
const { transaction } = await prepareTransactionAsync({
args: [adminId],
gasLimit: { increaseGas: 10 },
});
await signAndSend(transaction);
}, options);

return { deleteGameMessage };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { HexString } from '@gear-js/api';
import { usePrepareProgramTransaction } from '@gear-js/react-hooks';
import { useProgram } from 'app/utils';
import { Options, useExecuteWithPending, useSignAndSend } from 'app/hooks';

type Params = {
playerId: HexString;
};

export const useDeletePlayerMessage = () => {
const program = useProgram();
const { prepareTransactionAsync } = usePrepareProgramTransaction({
program,
serviceName: 'syndote',
functionName: 'deletePlayer',
});
const { signAndSend } = useSignAndSend();
const { executeWithPending } = useExecuteWithPending();

const deletePlayerMessage = ({ playerId }: Params, options?: Options) =>
executeWithPending(async () => {
const { transaction } = await prepareTransactionAsync({
args: [playerId],
gasLimit: { increaseGas: 10 },
});
await signAndSend(transaction);
}, options);

return { deletePlayerMessage };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { HexString } from '@gear-js/api';
import { usePrepareProgramTransaction } from '@gear-js/react-hooks';
import { useProgram } from 'app/utils';
import { Options, useExecuteWithPending, useSignAndSend } from 'app/hooks';

type Params = {
adminId: HexString;
};

export const useExitGameMessage = () => {
const program = useProgram();
const { prepareTransactionAsync } = usePrepareProgramTransaction({
program,
serviceName: 'syndote',
functionName: 'exitGame',
});
const { signAndSend } = useSignAndSend();
const { executeWithPending } = useExecuteWithPending();

const exitGameMessage = ({ adminId }: Params, options?: Options) =>
executeWithPending(async () => {
const { transaction } = await prepareTransactionAsync({
args: [adminId],
gasLimit: { increaseGas: 10 },
});
await signAndSend(transaction);
}, options);

return { exitGameMessage };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { HexString } from '@gear-js/api';
import { usePrepareProgramTransaction } from '@gear-js/react-hooks';
import { useProgram } from 'app/utils';
import { Options, useExecuteWithPending, useSignAndSend } from 'app/hooks';

type Params = {
adminId: HexString;
};

export const usePlayMessage = () => {
const program = useProgram();
const { prepareTransactionAsync } = usePrepareProgramTransaction({
program,
serviceName: 'syndote',
functionName: 'play',
});
const { signAndSend } = useSignAndSend();
const { executeWithPending } = useExecuteWithPending();

const playMessage = async ({ adminId }: Params, options?: Options) =>
executeWithPending(async () => {
const { transaction } = await prepareTransactionAsync({
args: [adminId],
gasLimit: BigInt(730000000000),
});
await signAndSend(transaction);
}, options);

return { playMessage };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { usePrepareProgramTransaction } from '@gear-js/react-hooks';
import { useProgram } from 'app/utils';
import { Options, useExecuteWithPending, useSignAndSend } from 'app/hooks';
import { HexString } from '@gear-js/api';

type Params = {
value?: bigint;
adminId: HexString;
strategyId: HexString;
name: string;
};

export const useRegisterMessage = () => {
const program = useProgram();
const { prepareTransactionAsync } = usePrepareProgramTransaction({
program,
serviceName: 'syndote',
functionName: 'register',
});
const { signAndSend } = useSignAndSend();
const { executeWithPending } = useExecuteWithPending();

const registerMessage = async ({ value, adminId, strategyId, name }: Params, options?: Options) =>
executeWithPending(async () => {
const { transaction } = await prepareTransactionAsync({
args: [adminId, strategyId, name],
gasLimit: { increaseGas: 10 },
value,
});
await signAndSend(transaction);
}, options);

return { registerMessage };
};
Loading
Loading