Skip to content

Commit

Permalink
feat: add delegate votes API call and integrate it with mobx voting s…
Browse files Browse the repository at this point in the history
…tore
  • Loading branch information
Szymon Masłowski committed Nov 13, 2024
1 parent 027b7a7 commit e58e175
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
2 changes: 2 additions & 0 deletions source/renderer/app/actions/voting-actions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Action from './lib/Action';
import { DelegateVotesParams } from '../api/voting/types';

export default class VotingActions {
selectWallet: Action<string> = new Action();
sendTransaction: Action<{
amount: number;
passphrase: string | null | undefined;
}> = new Action();
delegateVotes: Action<DelegateVotesParams> = new Action();
generateQrCode: Action<number> = new Action();
saveAsPDF: Action<any> = new Action();
saveAsPDFSuccess: Action<any> = new Action();
Expand Down
24 changes: 24 additions & 0 deletions source/renderer/app/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { getPublicKey } from './transactions/requests/getPublicKey';
import { getICOPublicKey } from './transactions/requests/getICOPublicKey';
// Voting requests
import { createWalletSignature } from './voting/requests/createWalletSignature';
import { delegateVotes } from './voting/requests/delegateVotes';
import { getCatalystFund } from './voting/requests/getCatalystFund';
// Wallets requests
import { updateSpendingPassword } from './wallets/requests/updateSpendingPassword';
Expand Down Expand Up @@ -207,6 +208,7 @@ import type {
CreateWalletSignatureRequest,
GetCatalystFundResponse,
CatalystFund,
DelegateVotesParams,
} from './voting/types';
import type { StakePoolProps } from '../domains/StakePool';
import type { FaultInjectionIpcRequest } from '../../../common/types/cardano-node.types';
Expand Down Expand Up @@ -2747,6 +2749,28 @@ export default class AdaApi {
throw new ApiError(error);
}
};

delegateVotes = async (params: DelegateVotesParams) => {
logger.debug('AdaApi::delegateVotes called', {
parameters: filterLogData(params),
});

try {
const response = await delegateVotes(this.config, params);
logger.debug('AdaApi::delegateVotes success', {
response,
});

return response;
} catch (error) {
logger.debug('AdaApi::delegateVotes error', {
error,
});

throw new ApiError(error);
}
};

createVotingRegistrationTransaction = async (
request: CreateVotingRegistrationRequest
): Promise<WalletTransaction> => {
Expand Down
18 changes: 18 additions & 0 deletions source/renderer/app/api/voting/requests/delegateVotes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { request } from '../../utils/request';
import { RequestConfig } from '../../common/types';
import { Transaction } from '../../transactions/types';
import { DelegateVotesParams } from '../types';

export const delegateVotes = (
config: RequestConfig,
{ dRepId, passphrase, walletId }: DelegateVotesParams
): Promise<Transaction> =>
request(
{
...config,
method: 'PUT',
path: `/v2/dreps/${dRepId}/wallets/${walletId}`,
},
{},
{ passphrase }
);
6 changes: 6 additions & 0 deletions source/renderer/app/api/voting/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ export type CatalystFund = {
registrationSnapshotTime: Date;
};
};

export type DelegateVotesParams = {
dRepId: string;
passphrase: string;
walletId: string;
};
10 changes: 9 additions & 1 deletion source/renderer/app/stores/VotingStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
GetTransactionRequest,
VotingMetadataType,
} from '../api/transactions/types';
import type { CatalystFund } from '../api/voting/types';
import type { CatalystFund, DelegateVotesParams } from '../api/voting/types';
import { EventCategories } from '../analytics';

export type VotingRegistrationKeyType = {
Expand Down Expand Up @@ -79,6 +79,7 @@ export default class VotingStore extends Store {
const { voting: votingActions } = this.actions;
votingActions.selectWallet.listen(this._setSelectedWalletId);
votingActions.sendTransaction.listen(this._sendTransaction);
votingActions.delegateVotes.listen(this._delegateVotes);
votingActions.generateQrCode.listen(this._generateQrCode);
votingActions.saveAsPDF.listen(this._saveAsPDF);
votingActions.nextRegistrationStep.listen(this._nextRegistrationStep);
Expand Down Expand Up @@ -117,6 +118,8 @@ export default class VotingStore extends Store {
this.api.ada.createWalletSignature
);
@observable
delegateVotes: Request<Buffer> = new Request(this.api.ada.delegateVotes);
@observable
getTransactionRequest: Request<GetTransactionRequest> = new Request(
this.api.ada.getTransaction
);
Expand Down Expand Up @@ -297,6 +300,11 @@ export default class VotingStore extends Store {
throw e;
}
};
_delegateVotes = async (params: DelegateVotesParams) => {
this.delegateVotes.reset();
// @ts-ignore ts-migrate(1320) FIXME: Type of 'await' operand must either be a valid pro... Remove this comment to see the full error message
const transaction = await this.delegateVotes.execute(params);
};
_sendTransaction = async ({
amount,
passphrase,
Expand Down

0 comments on commit e58e175

Please sign in to comment.