Skip to content

Commit

Permalink
fix: get block working
Browse files Browse the repository at this point in the history
  • Loading branch information
roninjin10 committed Nov 23, 2024
1 parent 0d8bfca commit 921cc80
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"dependencies": {
"@tauri-apps/api": "^2.1.1",
"@tevm/ts-plugin": "1.0.0-next.124",
"tevm": "1.0.0-next.126",
"viem": "^2.21.49"
}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ async fn get_block(state: tauri::State<'_, Mutex<AppState>>) -> Result<Option<Bl

#[tauri::command]
async fn request(state: tauri::State<'_, Mutex<AppState>>, request: serde_json::Value) -> Result<serde_json::Value, String> {
println!("Request: {}", serde_json::to_string_pretty(&request).unwrap());
let mut response = json!({"jsonrpc": "2.0"});

if let Some(id) = request.get("id") {
Expand Down
78 changes: 71 additions & 7 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { invoke } from '@tauri-apps/api/core';
import { createMemoryClient, type EIP1193RequestFn, type MemoryClient } from 'tevm';
import { createMemoryClient, type EIP1193RequestFn, type JsonRpcResponse, type MemoryClient } from 'tevm';
import { type PublicClient, custom, createPublicClient } from 'viem';
import {
PUBLIC_EXECUTION_RPC,
Expand All @@ -16,7 +16,18 @@
createPublicClient({
transport: custom({
request: (request) => {
return invoke('request', request) as any;
return invoke('request', {request:
{
...request,
jsonrpc: '2.0',
id: crypto.randomUUID(),
}
}).then((response: any) => {
if (response.error) throw response.error
return response.result
}).catch(e => {
throw e
}) as any;
}
})
})
Expand All @@ -29,7 +40,9 @@
rpcUrl: rpcUrl,
consensusRpc: CONSENSUS_RPC,
chainId: 1
});
}).catch(e => {
return e as string
}) as string;
};
const fork = () => {
Expand All @@ -44,18 +57,28 @@
}
const getBlock = () => {
client.getBlock({blockTag: 'latest'}).then(latestBlock => {
return client.getBlock({blockTag: 'latest'}).then(latestBlock => {
block = latestBlock
})
};
$effect(() => {
if (!startMessage) return;
const interval = setInterval(getBlock, 2000);
let timeoutId: NodeJS.Timeout;
let isRunning = true
const pollBlock = async () => {
await getBlock();
if (!isRunning) return
timeoutId = setTimeout(pollBlock, 10_000);
};
pollBlock();
// Clean up effect
return () => {
clearInterval(interval);
isRunning = false
clearTimeout(timeoutId);
};
});
</script>
Expand All @@ -67,7 +90,48 @@
<p>{startMessage}</p>
{/if}
{#if block}
<pre>{JSON.stringify(block, null, 2)}</pre>
<table>
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Hash</td>
<td>{block.hash}</td>
</tr>
<tr>
<td>Number</td>
<td>{block.number}</td>
</tr>
<tr>
<td>Parent Hash</td>
<td>{block.parentHash}</td>
</tr>
<tr>
<td>Timestamp</td>
<td>{new Date(Number(block.timestamp) * 1000).toLocaleString()}</td>
</tr>
<tr>
<td>Gas Used</td>
<td>{block.gasUsed.toString()}</td>
</tr>
<tr>
<td>Gas Limit</td>
<td>{block.gasLimit.toString()}</td>
</tr>
<tr>
<td>Base Fee</td>
<td>{block.baseFeePerGas?.toString() ?? 'N/A'}</td>
</tr>
<tr>
<td>Transactions</td>
<td>{block.transactions.length} transactions</td>
</tr>
</tbody>
</table>
{/if}

<p>Tauri <a href="https://v1.tauri.app/v1/guides/getting-started/setup/sveltekit">docs</a></p>
Expand Down

0 comments on commit 921cc80

Please sign in to comment.