mirror of
https://github.com/lux-apps/papers.git
synced 2026-10-30 18:15:21 +00:00
Restructured from flat .tex/.pdf files into per-paper subdirectories. Added Antje Worring as co-author on all papers. Synced PDFs to pdfs/ for download site.
61 lines
1.7 KiB
TeX
61 lines
1.7 KiB
TeX
\section{On-Chain Query Execution}
|
|
|
|
|
|
\subsection{Gas-Metered GraphVM}
|
|
|
|
Smart contracts can execute GraphQL queries through precompiles:
|
|
|
|
\begin{lstlisting}[language=Solidity, basicstyle=\small\ttfamily]
|
|
interface IGChainQuery {
|
|
// Execute compiled query bytecode
|
|
function executeQuery(
|
|
bytes calldata queryBytecode,
|
|
bytes calldata params
|
|
) external view returns (bytes memory result);
|
|
|
|
// Register persistent query
|
|
function registerQuery(
|
|
string calldata name,
|
|
bytes calldata queryBytecode
|
|
) external returns (bytes32 queryId);
|
|
|
|
// Execute registered query
|
|
function executeRegistered(
|
|
bytes32 queryId,
|
|
bytes calldata params
|
|
) external view returns (bytes memory result);
|
|
}
|
|
\end{lstlisting}
|
|
|
|
\subsection{Example: On-Chain Portfolio Valuation}
|
|
|
|
\begin{lstlisting}[language=Solidity, basicstyle=\small\ttfamily]
|
|
contract PortfolioManager {
|
|
IGChainQuery gchain;
|
|
|
|
function getUserTVL(address user) external view returns (uint256) {
|
|
// Query cross-chain balances
|
|
bytes memory query = compileQuery(
|
|
"query($user: Address!) { "
|
|
" addressBalances(address: $user) { "
|
|
" valueUSD "
|
|
" } "
|
|
"}"
|
|
);
|
|
|
|
bytes memory params = abi.encode(user);
|
|
bytes memory result = gchain.executeQuery(query, params);
|
|
|
|
// Parse result
|
|
uint256 totalUSD = 0;
|
|
uint256[] memory balances = abi.decode(result, (uint256[]));
|
|
for (uint i = 0; i < balances.length; i++) {
|
|
totalUSD += balances[i];
|
|
}
|
|
|
|
return totalUSD;
|
|
}
|
|
}
|
|
\end{lstlisting}
|
|
|