Skip to content

Commit

Permalink
chore: merge develop into main (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsANameToo authored May 28, 2024
2 parents 78ab870 + 4c35b1c commit f16f818
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 16 deletions.
2 changes: 1 addition & 1 deletion i18n/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/WalletOverview/WalletAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const WalletAddress = ({ walletData }: WalletOverviewProperties) => {
{votingDelegate !== undefined && (
<Tooltip
content={t("VOTING_FOR", {
delegateName: votingDelegate.username,
name: votingDelegate.username,
})}
>
<div>
Expand Down
71 changes: 67 additions & 4 deletions src/app/hooks/useArkConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import {
SignTransactionRequest,
SignTransactionResponse,
SignVoteRequest,
SignVoteRequestVersioned,
SignVoteResponse,
SignVoteResponseVersioned,
Version,
VoteTypeV1_0,
VoteTypeV1_9,
} from "@/app/lib/Network";

class NoArkExtensionException extends Error {
Expand All @@ -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 = <V extends Version = Version>(
request: SignVoteRequest,
version: V,
): SignVoteRequestVersioned<V> => {
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<V>;
};

const formatVoteResponse = <V extends Version = Version>(
response: SignVoteResponseVersioned<V>,
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);
Expand Down Expand Up @@ -129,17 +190,19 @@ 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<typeof version> | undefined;

if (!isTruthy(response)) {
throw new NoArkExtensionException();
}

setIsVoting(false);

return response;
return formatVoteResponse(response, version);
} catch (error) {
setIsVoting(false);

Expand Down
58 changes: 51 additions & 7 deletions src/app/lib/Network/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<V extends Version = Version> {
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<V extends Version = Version> = {
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;
Expand Down Expand Up @@ -129,11 +170,14 @@ export interface ArkConnectExtension {
signTransaction: (
transactionRequest: SignTransactionRequest,
) => Promise<SignTransactionResponse>;
signVote: (voteRequest: SignVoteRequest) => Promise<SignVoteResponse>;
signVote: <V extends Version = Version>(
voteRequest: SignVoteRequestVersioned<V>,
) => Promise<SignVoteResponseVersioned<V>>;
signMessage: (options: { message: string }) => Promise<{
message: string;
signatory: string;
signature: string;
}>;
version: () => string;
loaded: boolean;
}
6 changes: 3 additions & 3 deletions src/domains/vote/components/VoteModal/VoteModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface VoteInput {

export interface VoteType {
amount: number;
delegateAddress: string;
address: string;
}

export const VoteModal = ({
Expand Down Expand Up @@ -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],
};
}

Expand Down

0 comments on commit f16f818

Please sign in to comment.