Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Sep 20, 2024
1 parent 0b498fe commit 7634e1c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
3 changes: 2 additions & 1 deletion compliant-reward-distribution/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const App = () => {
const [account, setAccount] = useState<string>();

const grpcClient = useRef(new ConcordiumGRPCClient(new GrpcWebFetchTransport({ baseUrl: CONFIG.node }))).current;
const capitalizedNetwork = CONFIG.network[0].toUpperCase() + CONFIG.network.substring(1);

useEffect(() => {
if (provider !== undefined) {
Expand All @@ -44,7 +45,7 @@ export const App = () => {
rel="noreferrer"
href={`https://github.com/Concordium/concordium-dapp-examples/tree/main/compliant-reward-distribution`}
>
Version {version}
Version {version} ({capitalizedNetwork})
</a>
</div>
<Link className="secondary" to="/connectWallet">
Expand Down
54 changes: 29 additions & 25 deletions compliant-reward-distribution/frontend/src/apiReqeuests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,17 @@ function createRequestOptions(method: string, body?: string): RequestInit {
}

/**
* Sends a `GET`/`POST` request to the backend and optionally parses the response into a given type `T`.
* Sends a `GET`/`POST` request to the backend.
*
* @param T - Optional return value type `T`.
* @param endpoint - The API endpoint.
* @param method - The HTTP method (`'POST'` or `'GET'`).
* @param parseReturnValue - Optional request to parse the return value into provided type `T`.
* @param body - Optional request body (required for `POST`, ignored for `GET`).
* @returns A promise that can be resolved into the parsed return value of type `T`.
* @returns A promise that can be resolved into the response.
*
* @throws An error if the method is invalid, if the body is incorrectly provided for the method,
* if the parsing of the return value fails, or if the backend responses with an error.
* or if the backend responses with an error.
*/
async function sendBackendRequest<T = undefined>(
endpoint: string,
method: string,
parseReturnValue: boolean,
body?: string,
): Promise<T> {
async function sendBackendRequest(endpoint: string, method: string, body?: string): Promise<Response> {
const api = `api/${endpoint}`;

const requestOption = createRequestOptions(method, body);
Expand All @@ -137,17 +130,25 @@ async function sendBackendRequest<T = undefined>(

throw new Error(`Unable to send request to the backend '${api}'; Error: ${JSON.stringify(parsedError)}`);
}
return response;
}

if (parseReturnValue) {
/**
* Parses the `GET`/`POST` response from the backend into a given type `T`.
*
* @param T - Return value type `T`.
* @param response - The response to be parsed.
* @returns A promise that can be resolved into the parsed return value of type `T`.
*
* @throws An error if the parsing of the return value fails.
*/
async function parseResponse<T = undefined>(response: Response): Promise<T> {
try {
// Parse the response as type `T`
try {
return (await response.json()) as T;
} catch (e) {
throw new Error(`Failed to parse the response from the backend into expected type.`);
}
return (await response.json()) as T;
} catch (e) {
throw new Error(`Failed to parse the response from the backend into expected type.`);
}

return undefined as unknown as T;
}

/**
Expand All @@ -172,7 +173,7 @@ export async function setClaimed(signer: string, signature: string, recentBlockH
},
});

return await sendBackendRequest('setClaimed', 'POST', false, body);
return await sendBackendRequest('setClaimed', 'POST', body);
}

/**
Expand Down Expand Up @@ -205,7 +206,8 @@ export async function getPendingApprovals(
},
});

return await sendBackendRequest<AccountData[]>('getPendingApprovals', 'POST', true, body);
const response = await sendBackendRequest('getPendingApprovals', 'POST', body);
return await parseResponse<AccountData[]>(response);
}

/**
Expand Down Expand Up @@ -234,7 +236,8 @@ export async function getAccountData(
},
});

return await sendBackendRequest<AccountData>('getAccountData', 'POST', true, body);
const response = await sendBackendRequest('getAccountData', 'POST', body);
return await parseResponse<AccountData>(response);
}

/**
Expand All @@ -243,7 +246,8 @@ export async function getAccountData(
* @throws An error if the backend responses with an error, or parsing of the return value fails.
*/
export async function getStatement(): Promise<CredentialStatement> {
const statement = await sendBackendRequest<{ data: AtomicStatementV2[] }>('getZKProofStatements', 'GET', true);
const response = await sendBackendRequest('getZKProofStatements', 'GET');
const statement = await parseResponse<{ data: AtomicStatementV2[] }>(response);

const credentialStatement: CredentialStatement = {
idQualifier: {
Expand Down Expand Up @@ -281,7 +285,7 @@ export async function submitTweet(signer: string, signature: string, recentBlock
},
});

return await sendBackendRequest('postTweet', 'POST', false, body);
return await sendBackendRequest('postTweet', 'POST', body);
}

/**
Expand All @@ -298,5 +302,5 @@ export async function submitZkProof(presentation: VerifiablePresentation, recent
presentation: presentation,
});

return await sendBackendRequest('postZKProof', 'POST', false, body);
return await sendBackendRequest('postZKProof', 'POST', body);
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ async fn main() -> anyhow::Result<()> {

// If the indexer is started for the first time, lookup the last block finalized
// and initialize the database.
let current_block = consensus_info.last_finalized_block_height;
let current_block = AbsoluteBlockHeight::from(17088982);
// consensus_info.last_finalized_block_height;

// This function only sets the settings in the database if they haven't been set
// before. Meaning only if the indexer is run for the first time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub const TESTNET_GENESIS_BLOCK_HASH: [u8; 32] = [
0, 13, 92, 46, 0, 232, 95, 80, 247, 150,
];

/// The string "CONCORDIUM_COMPLIANT_REWARD_DISTRIBUTION_DAPP" in bytes is used
/// The string "CONCORDIUM_COMPLIANT_REWARD_DISTRIBUTION_DAPP" is used
/// as context for signing messages and generating ZK proofs. The same account
/// can be used in different Concordium services without the risk of re-playing
/// signatures/zk-proofs across the different services due to this context
Expand Down

0 comments on commit 7634e1c

Please sign in to comment.