Skip to content

Commit

Permalink
Merge pull request #11 from calimero-network/feat--extend-functionali…
Browse files Browse the repository at this point in the history
…ties

Feat Extend Application to have all Proposal Actions examples implemented
  • Loading branch information
alenmestrov authored Dec 11, 2024
2 parents 333f809 + eddd570 commit bd401e7
Show file tree
Hide file tree
Showing 9 changed files with 859 additions and 81 deletions.
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"axios": "^1.6.7",
"buffer": "^6.0.3",
"next": "^14.2.13",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
14 changes: 14 additions & 0 deletions app/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 47 additions & 3 deletions app/src/api/clientApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,51 @@ export interface SendProposalMessageRequest {

export interface SendProposalMessageResponse {}

export enum ProposalActionType {
ExternalFunctionCall = 'ExternalFunctionCall',
Transfer = 'Transfer',
SetNumApprovals = 'SetNumApprovals',
SetActiveProposalsLimit = 'SetActiveProposalsLimit',
SetContextValue = 'SetContextValue',
DeleteProposal = 'DeleteProposal',
}

export type FormActionType =
| 'Cross contract call'
| 'Transfer'
| 'Set context variable'
| 'Change number of approvals needed'
| 'Change number of maximum active proposals';

export interface ExternalFunctionCallAction {
type: ProposalActionType.ExternalFunctionCall;
receiver_id: string;
method_name: string;
args: Record<string, any>;
deposit: string;
gas?: string;
}

export interface TransferAction {
type: ProposalActionType.Transfer;
amount: string;
}

export interface CreateProposalRequest {
receiver: String;
action_type: string;
params: {
receiver_id?: string;
method_name?: string;
args?: string;
deposit?: string;
gas?: string;
amount?: string;
num_approvals?: number;
active_proposals_limit?: number;
key?: string;
value?: string;
proposal_id?: string;
};
}

export interface CreateProposalResponse {
Expand All @@ -42,8 +85,8 @@ export interface ApproveProposalResponse {}
export enum ClientMethod {
GET_PROPOSAL_MESSAGES = 'get_proposal_messages',
SEND_PROPOSAL_MESSAGE = 'send_proposal_messages',
CREATE_PROPOSAL_MESSAGES = 'create_new_proposal',
APPROVE_PROPOSAL_MESSAGE = 'approve_proposal',
CREATE_PROPOSAL = 'create_new_proposal',
APPROVE_PROPOSAL = 'approve_proposal',
}

export interface ClientApi {
Expand All @@ -60,4 +103,5 @@ export interface ClientApi {
approveProposal(
request: ApproveProposalRequest,
): ApiResponse<ApproveProposalResponse>;
deleteProposal(proposalId: string): ApiResponse<void>;
}
1 change: 1 addition & 0 deletions app/src/api/contractApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface ContractApi {
getContextDetails(contextId: String): ApiResponse<ContextDetails>;
getContextMembers(): ApiResponse<Members[]>;
getContextMembersCount(): ApiResponse<number>;
deleteProposal(proposalId: string): ApiResponse<void>;
}

// async removeProposal(proposalId: String): ApiResponse<boolean> {
Expand Down
55 changes: 40 additions & 15 deletions app/src/api/dataSource/LogicApiDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,51 @@ export class LogicApiDataSource implements ClientApi {
return { error };
}

const params: RpcQueryParams<CreateProposalRequest> = {
console.log('Creating proposal with request:', request);

const params: RpcQueryParams<typeof request> = {
contextId: jwtObject?.context_id ?? getContextId(),
method: ClientMethod.CREATE_PROPOSAL_MESSAGES,
argsJson: request,
method: ClientMethod.CREATE_PROPOSAL,
argsJson: {
request: request,
},
executorPublicKey: jwtObject.executor_public_key,
};

const response = await getJsonRpcClient().execute<
CreateProposalRequest,
CreateProposalResponse
>(params, config);
console.log('RPC params:', params);

if (response?.error) {
return await this.handleError(response.error, {}, this.createProposal);
}
try {
const response = await getJsonRpcClient().execute<
typeof request,
CreateProposalResponse
>(params, config);

return {
data: response.result.output as CreateProposalResponse,
error: null,
};
console.log('Raw response:', response);

if (response?.error) {
console.error('RPC error:', response.error);
return await this.handleError(response.error, {}, this.createProposal);
}

if (!response?.result?.output) {
console.error('Invalid response format:', response);
return {
error: { message: 'Invalid response format', code: 500 },
data: null,
};
}

return {
data: response.result.output as CreateProposalResponse,
error: null,
};
} catch (err) {
console.error('Unexpected error:', err);
return {
error: { message: err.message || 'Unexpected error', code: 500 },
data: null,
};
}
}

async approveProposal(
Expand All @@ -106,7 +131,7 @@ export class LogicApiDataSource implements ClientApi {

const params: RpcQueryParams<ApproveProposalRequest> = {
contextId: jwtObject?.context_id ?? getContextId(),
method: ClientMethod.APPROVE_PROPOSAL_MESSAGE,
method: ClientMethod.APPROVE_PROPOSAL,
argsJson: request,
executorPublicKey: jwtObject.executor_public_key,
};
Expand Down
Loading

0 comments on commit bd401e7

Please sign in to comment.