diff --git a/i18n/locales/en/common.json b/i18n/locales/en/common.json index 2524381f..5c17b1f0 100644 --- a/i18n/locales/en/common.json +++ b/i18n/locales/en/common.json @@ -42,7 +42,7 @@ "SELECT": "Select", "CLOSE_TOAST": "Close toast", "CHANGES_REGISTERED": "Changes registered!", - "VOTING_FOR": "Voting For {{delegateName}} ", + "VOTING_FOR": "Voting For {{name}} ", "OPEN_IN_EXPLORER": "Open in Explorer", "SIGN_TEXT": "I sign this to prove ownership of my wallet", "NETWORK_SWITCH_ERROR_TITLE": "Error while switching network", diff --git a/src/app/components/WalletOverview/WalletAddress.tsx b/src/app/components/WalletOverview/WalletAddress.tsx index 0eda85e7..eb45d460 100644 --- a/src/app/components/WalletOverview/WalletAddress.tsx +++ b/src/app/components/WalletOverview/WalletAddress.tsx @@ -57,7 +57,7 @@ export const WalletAddress = ({ walletData }: WalletOverviewProperties) => { {votingDelegate !== undefined && (
diff --git a/src/app/hooks/useArkConnect.ts b/src/app/hooks/useArkConnect.ts index 9123d0e0..eaa329d1 100644 --- a/src/app/hooks/useArkConnect.ts +++ b/src/app/hooks/useArkConnect.ts @@ -14,7 +14,12 @@ import { SignTransactionRequest, SignTransactionResponse, SignVoteRequest, + SignVoteRequestVersioned, SignVoteResponse, + SignVoteResponseVersioned, + Version, + VoteTypeV1_0, + VoteTypeV1_9, } from "@/app/lib/Network"; class NoArkExtensionException extends Error { @@ -23,6 +28,62 @@ class NoArkExtensionException extends Error { } } +const getVersion = (): string | null => { + if (!window.arkconnect) { + // eslint-disable-next-line unicorn/no-null + return null; + } + + if (typeof window.arkconnect.version !== "function") { + return "1.0.0"; + } + + return window.arkconnect.version(); +}; + +const formatVoteRequest = ( + request: SignVoteRequest, + version: V, +): SignVoteRequestVersioned => { + if (version === "1.0.0" || version === "1.8.0" || version === null) { + return { + ...request, + vote: + request.vote && + ({ + amount: request.vote.amount, + delegateAddress: request.vote.address, + } as V extends "1.0.0" | "1.8.0" | null ? VoteTypeV1_0 : VoteTypeV1_9), + unvote: + request.unvote && + ({ + amount: request.unvote.amount, + delegateAddress: request.unvote.address, + } as V extends "1.0.0" | "1.8.0" | null ? VoteTypeV1_0 : VoteTypeV1_9), + }; + } + return request as SignVoteRequestVersioned; +}; + +const formatVoteResponse = ( + response: SignVoteResponseVersioned, + version: V, +): SignVoteResponse => { + if (version === "1.0.0" || version === "1.8.0" || version === null) { + const versionedResponse = response as SignVoteResponseVersioned< + "1.0.0" | "1.8.0" | null + >; + return { + ...response, + voteAddress: versionedResponse.voteDelegateAddress, + voteName: versionedResponse.voteDelegateName, + unvoteAddress: versionedResponse.unvoteDelegateAddress, + unvoteName: versionedResponse.unvoteDelegateName, + }; + } + return response as SignVoteResponse; +}; + export const useArkConnect = (): ArkConnectState => { const [isConnecting, setIsConnecting] = useState(false); const [isErrored, setIsErrored] = useState(false); @@ -129,9 +190,11 @@ export const useArkConnect = (): ArkConnectState => { throw new NoArkExtensionException(); } - const response = (await window.arkconnect.signVote(request)) as - | SignVoteResponse - | undefined; + const version = getVersion(); + const formattedRequest = formatVoteRequest(request, version); + const response = (await window.arkconnect.signVote( + formattedRequest, + )) as SignVoteResponseVersioned | undefined; if (!isTruthy(response)) { throw new NoArkExtensionException(); @@ -139,7 +202,7 @@ export const useArkConnect = (): ArkConnectState => { setIsVoting(false); - return response; + return formatVoteResponse(response, version); } catch (error) { setIsVoting(false); diff --git a/src/app/lib/Network/contracts.ts b/src/app/lib/Network/contracts.ts index a86b4f08..b286d94a 100644 --- a/src/app/lib/Network/contracts.ts +++ b/src/app/lib/Network/contracts.ts @@ -60,23 +60,64 @@ export interface SignTransactionResponse { export interface SignVoteRequest { vote?: { amount: number; - delegateAddress: string; + address: string; }; unvote?: { amount: number; - delegateAddress: string; + address: string; }; fee: number; } -export interface SignVoteResponse { - id: string; - sender: string; +export type Version = null | string; + +export interface VoteTypeV1_9 { + amount: number; + address: string; +} + +export interface VoteTypeV1_0 { + amount: number; + delegateAddress: string; +} +export interface SignVoteRequestVersioned { + vote?: V extends "1.0.0" | "1.8.0" | null ? VoteTypeV1_0 : VoteTypeV1_9; + unvote?: V extends "1.0.0" | "1.8.0" | null ? VoteTypeV1_0 : VoteTypeV1_9; + fee: number; +} + +export interface ResponseVoteTypeV1_9 { + voteAddress?: string; + votePublicKey?: string; + unvoteAddress?: string; + unvotePublicKey?: string; +} + +export interface ResponseVoteTypeV1_0 { voteDelegateAddress?: string; voteDelegateName?: string; - votePublicKey?: string; unvoteDelegateAddress?: string; unvoteDelegateName?: string; +} + +export type SignVoteResponseVersioned = { + id: string; + sender: string; + exchangeCurrency: string; + fee: number; + convertedFee: number; +} & (V extends "1.0.0" | "1.8.0" | null + ? ResponseVoteTypeV1_0 + : ResponseVoteTypeV1_9); + +export interface SignVoteResponse { + id: string; + sender: string; + voteAddress?: string; + voteName?: string; + votePublicKey?: string; + unvoteAddress?: string; + unvoteName?: string; unvotePublicKey?: string; exchangeCurrency: string; fee: number; @@ -129,11 +170,14 @@ export interface ArkConnectExtension { signTransaction: ( transactionRequest: SignTransactionRequest, ) => Promise; - signVote: (voteRequest: SignVoteRequest) => Promise; + signVote: ( + voteRequest: SignVoteRequestVersioned, + ) => Promise>; signMessage: (options: { message: string }) => Promise<{ message: string; signatory: string; signature: string; }>; + version: () => string; loaded: boolean; } diff --git a/src/domains/vote/components/VoteModal/VoteModal.tsx b/src/domains/vote/components/VoteModal/VoteModal.tsx index 6768822f..6315210e 100644 --- a/src/domains/vote/components/VoteModal/VoteModal.tsx +++ b/src/domains/vote/components/VoteModal/VoteModal.tsx @@ -28,7 +28,7 @@ export interface VoteInput { export interface VoteType { amount: number; - delegateAddress: string; + address: string; } export const VoteModal = ({ @@ -105,14 +105,14 @@ export const VoteModal = ({ if (voteState.votes.length > 0) { voteInput.vote = { amount: 0, - delegateAddress: voteState.votes[0], + address: voteState.votes[0], }; } if (voteState.unvotes.length > 0) { voteInput.unvote = { amount: 0, - delegateAddress: voteState.unvotes[0], + address: voteState.unvotes[0], }; }