Skip to content

Commit

Permalink
Merge pull request #953 from IntersectMBO/develop
Browse files Browse the repository at this point in the history
 Fix slow delegation issue and usePendingTransaction fixes
  • Loading branch information
pmbinapps authored May 8, 2024
2 parents a7b6a58 + 3d8d728 commit 9507894
Show file tree
Hide file tree
Showing 71 changed files with 8,471 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
GTM_ID: ${{ secrets.GTM_ID }}
IP_ADDRESS_BYPASSING_BASIC_AUTH1: ${{ secrets.IP_ADDRESS_BYPASSING_BASIC_AUTH1 }}
IP_ADDRESS_BYPASSING_BASIC_AUTH2: ${{ secrets.IP_ADDRESS_BYPASSING_BASIC_AUTH2 }}
NEXT_PUBLIC_API_URL: "https://sanchogov.tools/api"
NEXT_PUBLIC_API_URL: "https://participation.sanchogov.tools"
NEXT_PUBLIC_GA4_PROPERTY_ID: ${{ secrets.NEXT_PUBLIC_GA4_PROPERTY_ID }}
NGINX_BASIC_AUTH: ${{ secrets.NGINX_BASIC_AUTH }}
PIPELINE_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
GTM_ID: ${{ secrets.GTM_ID }}
IP_ADDRESS_BYPASSING_BASIC_AUTH1: ${{ secrets.IP_ADDRESS_BYPASSING_BASIC_AUTH1 }}
IP_ADDRESS_BYPASSING_BASIC_AUTH2: ${{ secrets.IP_ADDRESS_BYPASSING_BASIC_AUTH2 }}
NEXT_PUBLIC_API_URL: "https://dev-sanchonet.govtool.byron.network/api"
NEXT_PUBLIC_API_URL: "https://participation.sanchogov.tools"
NEXT_PUBLIC_GA4_PROPERTY_ID: ${{ secrets.NEXT_PUBLIC_GA4_PROPERTY_ID }}
NGINX_BASIC_AUTH: ${{ secrets.NGINX_BASIC_AUTH }}
PIPELINE_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
GTM_ID: ${{ secrets.GTM_ID }}
IP_ADDRESS_BYPASSING_BASIC_AUTH1: ${{ secrets.IP_ADDRESS_BYPASSING_BASIC_AUTH1 }}
IP_ADDRESS_BYPASSING_BASIC_AUTH2: ${{ secrets.IP_ADDRESS_BYPASSING_BASIC_AUTH2 }}
NEXT_PUBLIC_API_URL: "https://staging.govtool.byron.network/api"
NEXT_PUBLIC_API_URL: "https://participation.sanchogov.tools"
NEXT_PUBLIC_GA4_PROPERTY_ID: ${{ secrets.NEXT_PUBLIC_GA4_PROPERTY_ID }}
NGINX_BASIC_AUTH: ${{ secrets.NGINX_BASIC_AUTH }}
PIPELINE_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
GTM_ID: ${{ secrets.GTM_ID }}
IP_ADDRESS_BYPASSING_BASIC_AUTH1: ${{ secrets.IP_ADDRESS_BYPASSING_BASIC_AUTH1 }}
IP_ADDRESS_BYPASSING_BASIC_AUTH2: ${{ secrets.IP_ADDRESS_BYPASSING_BASIC_AUTH2 }}
NEXT_PUBLIC_API_URL: "https://test-sanchonet.govtool.byron.network/api"
NEXT_PUBLIC_API_URL: "https://participation.sanchogov.tools"
NEXT_PUBLIC_GA4_PROPERTY_ID: ${{ secrets.NEXT_PUBLIC_GA4_PROPERTY_ID }}
NGINX_BASIC_AUTH: ${{ secrets.NGINX_BASIC_AUTH }}
PIPELINE_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ changes.

## [Unreleased]

- Add wallet connector package [Issue 898](https://github.com/IntersectMBO/govtool/issues/898)
- Change DRep without metadata name from "Sole Voter" to "Direct Voter" [Issue 880](https://github.com/IntersectMBO/govtool/issues/880)
- Inicialize Usersnap into App [Issue 546](https://github.com/IntersectMBO/govtool/issues/546)
- Integrate frontend with metadata validation service [Issue 617](https://github.com/IntersectMBO/govtool/issues/617)
Expand Down
1 change: 1 addition & 0 deletions govtool/analytics-dashboard/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ RUN \

# Rebuild the source code only when needed
FROM base AS builder
# Set the build arguments
ARG NEXT_PUBLIC_API_URL
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const usePendingTransaction = ({

if (status.transactionConfirmed) {
clearInterval(interval);

if (isEnabled) {
const desiredResult = getDesiredResult(type, resourceId);
const queryKey = getQueryKey(type, transaction);
Expand All @@ -89,7 +90,13 @@ export const usePendingTransaction = ({
while (!isDBSyncUpdated && count < DB_SYNC_MAX_ATTEMPTS) {
count++;
// eslint-disable-next-line no-await-in-loop
const data = await refetchData(type, queryClient, queryKey);
const data = await refetchData(
type,
queryClient,
queryKey,
resourceId,
);

if (desiredResult === data) {
addSuccessAlert(t(`alerts.${type}.success`));
resetTransaction();
Expand Down
10 changes: 8 additions & 2 deletions govtool/frontend/src/context/pendingTransaction/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const refetchData = async (
type: TransactionType,
queryClient: QueryClient,
queryKey: QueryKey | undefined,
resourceId: string | undefined,
) => {
if (queryKey === undefined) return;

Expand All @@ -58,10 +59,15 @@ export const refetchData = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const data = await queryClient.getQueryData<any>(queryKey);

if (type === "delegate") return data;
if (type === "delegate") {
if (resourceId === "no confidence" || resourceId === "abstain") {
return data.dRepView;
}
return data.dRepHash;
}
if (type === "registerAsDrep" || type === "retireAsDrep")
return data.isRegisteredAsDRep;
if (type === "registerAsDirectVoter" || type === "retireAsDirectVoter")
return data.isRegisteredAsDirectVoter;
return data.isRegisteredAsSoleVoter;
return undefined;
};
5 changes: 1 addition & 4 deletions govtool/frontend/src/hooks/useDelegateToDrep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ export const useDelegateTodRep = () => {
);
certBuilder.add(retirementCert);
}
const result = await buildSignSubmitConwayCertTx({
await buildSignSubmitConwayCertTx({
certBuilder,
type: "delegate",
resourceId: dRepId,
voter,
});
if (result) {
addSuccessAlert(t("alerts.delegate.success"));
}
} catch (error) {
openWalletErrorModal({
error,
Expand Down
56 changes: 56 additions & 0 deletions govtool/packages/wallet-connector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Wallet Service

The WalletService.ts file contains the implementation of a wallet service, designed to manage wallet. It includes the enableWallet function responsible for enabling the wallet connection and allows get cip95 functions.

1. Install wallet-connector package.

```sh
yarn add wallet-connector
```

2. Import service from wallet-connector.

```javascript
import { WalletService } from "@wallet-connector";
```

3. Usage - for enable your browser wallet extension.
EXAMPLE:

```javascript
const newWalletAPI = await WalletService.enableWallet("%WALLET_NAME%");
```

# Wallet Provider

WalletProvider component which serves as a React Context Provider to facilitate wallet integration across components.

1. Install wallet-connector package

```sh
yarn add wallet-connector
```

2. Import WalletProvider from wallet-connector and wrap your app.

```javascript
import { WalletProvider } from "@wallet-connector";

<WalletProvider>
<App />
</WalletProvider>;
```

3. Usage

```javascript
import { useWalletContext } from "@wallet-connector";

const {
disconnectWallet,
enableError,
enableWallet,
isEnableLoading,
walletAPI,
} = useWalletContext();
```
63 changes: 63 additions & 0 deletions govtool/packages/wallet-connector/WalletProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { createContext, useContext, useMemo, useState } from "react";

import { WalletService } from "./WalletService";
import {
TWalletAPI,
WalletContextProviderProps,
WalletConnectorErrors,
WalletContextValues,
} from "./types";

const WalletContext = createContext<WalletContextValues | null>(null);

const WalletProvider = ({ children }: WalletContextProviderProps) => {
const [isEnableLoading, setIsEnableLoading] = useState<boolean>(false);
const [enableError, setEnableError] = useState<string | null>(null);
const [walletAPI, setWalletAPI] = useState<TWalletAPI | null>(null);

const enableWallet = async (walletName: string): Promise<void> => {
setEnableError(null);
setIsEnableLoading(true);
try {
const newWalletAPI = await WalletService.enableWallet(walletName);

if (newWalletAPI) setWalletAPI(newWalletAPI);
} catch (e) {
setEnableError(e);
throw e;
} finally {
setIsEnableLoading(false);
}
};

const disableWallet = () => {
setWalletAPI(null);
};

const value = useMemo(
() => ({
disableWallet,
enableError,
enableWallet,
isEnableLoading,
walletAPI,
}),
[disableWallet, enableError, enableWallet, isEnableLoading, walletAPI]
);

return (
<WalletContext.Provider value={value}>{children}</WalletContext.Provider>
);
};

const useWalletContext = (): WalletContextValues => {
const context = useContext(WalletContext);
if (!context)
throw new Error(
WalletConnectorErrors.USE_WALLET_CONTEXT_USED_WITHOUT_PROVIDER
);

return context;
};

export default { useWalletContext, WalletProvider };
37 changes: 37 additions & 0 deletions govtool/packages/wallet-connector/WalletService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { TWalletAPI, WalletConnectorErrors } from "./types";

export namespace WalletService {
export const enableWallet = async (
walletName: string
): Promise<TWalletAPI> => {
try {
if (!window.cardano[walletName].supportedExtensions)
throw new Error(
WalletConnectorErrors.NO_CIP30_SUPPORT.replace(
"%WALLET_NAME%",
walletName
)
);

if (
!window.cardano[walletName].supportedExtensions.some(
(item) => item.cip === 95
)
)
throw new Error(
WalletConnectorErrors.NO_CIP95_SUPPORT.replace(
"%WALLET_NAME%",
walletName
)
);

const walletApi = await window.cardano[walletName].enable({
extensions: [{ cip: 95 }],
});

return walletApi;
} catch (e) {
throw e;
}
};
}
3 changes: 3 additions & 0 deletions govtool/packages/wallet-connector/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./WalletProvider";
export * from "./WalletService";
export * from "./types";
12 changes: 12 additions & 0 deletions govtool/packages/wallet-connector/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "wallet-connector",
"version": "1.0.0",
"main": "index.ts",
"license": "MIT",
"dependencies": {
"react": "^18.3.1"
},
"devDependencies": {
"@types/react": "^18.3.1"
}
}
69 changes: 69 additions & 0 deletions govtool/packages/wallet-connector/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ReactNode } from "react";

export type WalletContextProviderProps = {
children: ReactNode;
};

export type WalletContextValues = {
disableWallet: () => void;
enableError: string | null;
enableWallet: (walletName: string) => Promise<void>;
isEnableLoading: boolean;
walletAPI: TWalletAPI | null;
};

export type Extension = {
cip: number;
};

export type TWalletAPI = {
cip95: {
getPubDRepKey(): Promise<string>;
getRegisteredPubStakeKeys(): Promise<string[]>;
getUnregisteredPubStakeKeys(): Promise<string[]>;
signData(arg0: any, arg1: any): Promise<any>;
};
experimantal: {
on(arg0: any, arg1: any): any;
off(arg0: any, arg1: any): any;
getCollateral(): any;
};
getBalance(): Promise<string>;
getChangeAddress(): Promise<string>;
getExtensions(): Promise<Extension[]>;
getNetworkId(): Promise<number>;
getRewardAddresses(): Promise<string[]>;
getUnusedAddresses(): Promise<string[]>;
getUsedAddresses(): Promise<string[]>;
getUtxos(): Promise<string[]>;
signData(arg0: any, arg1?: any): Promise<any>;
signTx(arg0: any, arg1?: any): Promise<any>;
submitTx(arg0: any): Promise<any>;
};

export type EnableExtensionPayload = {
extensions: Extension[];
};

export type CardanoBrowserWallet = {
apiVersion: string;
enable(extensions?: EnableExtensionPayload): Promise<TWalletAPI>;
icon: string;
isEnabled(): Promise<boolean>;
name: string;
supportedExtensions: Extension[];
};

declare global {
interface Window {
cardano: {
[key: string]: CardanoBrowserWallet;
};
}
}

export enum WalletConnectorErrors {
NO_CIP30_SUPPORT = "%WALLET_NAME% wallet doesn't support CIP 30 extension.",
NO_CIP95_SUPPORT = "%WALLET_NAME% wallet doesn't support CIP 95 extension.",
USE_WALLET_CONTEXT_USED_WITHOUT_PROVIDER = "useWalletContext must be used in the WalletProvider.",
}
Loading

0 comments on commit 9507894

Please sign in to comment.