Skip to content

Commit

Permalink
Merge pull request #259 from rainlanguage/2024-02-15-fork-latest-bloc…
Browse files Browse the repository at this point in the history
…k-on-launch

2024 02 15 fork latest block on launch
  • Loading branch information
thedavidmeister authored Feb 16, 2024
2 parents d46df66 + 2fe4c43 commit 0587b1e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 23 deletions.
8 changes: 8 additions & 0 deletions tauri-app/src-tauri/src/commands/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ pub async fn get_chainid(rpc_url: String) -> CommandResult<u64> {

Ok(chain_id_u64)
}

#[tauri::command]
pub async fn get_block_number(rpc_url: String) -> CommandResult<u64> {
let block_number = ReadableClientHttp::new_from_url(rpc_url)?
.get_block_number()
.await?;
Ok(block_number)
}
4 changes: 2 additions & 2 deletions tauri-app/src-tauri/src/commands/dotrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ pub async fn parse_dotrain(
frontmatter: &str,
rainlang: &str,
rpc_url: &str,
block_number: Option<u64>,
block_number: u64,
) -> CommandResult<Bytes> {
Ok(
parse_rainlang_on_fork(frontmatter, rainlang, rpc_url, block_number)
parse_rainlang_on_fork(frontmatter, rainlang, rpc_url, Some(block_number))
.await?
)
}
3 changes: 2 additions & 1 deletion tauri-app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod toast;
pub mod transaction_status;

mod commands;
use commands::chain::get_chainid;
use commands::chain::{get_chainid, get_block_number};
use commands::dotrain::parse_dotrain;
use commands::dotrain_add_order_lsp::{call_lsp_completion, call_lsp_hover, call_lsp_problems};
use commands::order::{order_add, order_detail, order_remove, orders_list, orders_list_write_csv};
Expand Down Expand Up @@ -51,6 +51,7 @@ fn run_tauri_app() {
order_takes_list_write_csv,
get_address_from_ledger,
get_chainid,
get_block_number,
parse_dotrain,
call_lsp_completion,
call_lsp_hover,
Expand Down
53 changes: 43 additions & 10 deletions tauri-app/src/lib/components/InputBlockNumber.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<script lang="ts">
import type { InputMask } from 'imask';
import { imask } from '@imask/svelte';
import { getForkBlockNumberFromRpc } from '$lib/utils/forkBlockNumber';
import { Button, Spinner } from 'flowbite-svelte';
export let value: number | undefined = undefined;
export let value: number = 0;
export let required = true;
let isFetching: boolean;
const maskOptions = {
mask: Number,
Expand All @@ -12,15 +15,45 @@
};
function complete({ detail }: { detail: InputMask }) {
value = detail.unmaskedValue.length === 0 ? undefined : parseInt(detail.unmaskedValue);
value = detail.unmaskedValue.length === 0 ? 0 : parseInt(detail.unmaskedValue);
}
async function setForkBlockNumberFromRpc() {
isFetching = true;
try {
let res = await getForkBlockNumberFromRpc();
value = res;
// eslint-disable-next-line no-empty
} catch {}
isFetching = false;
}
</script>

<input
{required}
value={value}
class="block w-full disabled:cursor-not-allowed disabled:opacity-50 rtl:text-right p-2.5 focus:border-primary-500 focus:ring-primary-500 dark:focus:border-primary-500 dark:focus:ring-primary-500 bg-gray-50 text-gray-900 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 border-gray-300 dark:border-gray-600 text-sm rounded-lg"
use:imask={maskOptions}
on:complete={complete}
placeholder="(latest block)"
/>


<div class="flex w-full items-start justify-start space-x-2">
<div class="relative flex w-full">
<input
{required}
value={value}
class="block w-full disabled:cursor-not-allowed disabled:opacity-50 rtl:text-right p-2.5 focus:border-primary-500 focus:ring-primary-500 dark:focus:border-primary-500 dark:focus:ring-primary-500 bg-gray-50 text-gray-900 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 border-gray-300 dark:border-gray-600 text-sm rounded-lg"
use:imask={maskOptions}
on:complete={complete}
/>
<div class="absolute right-2 flex h-10 flex-col justify-center">
<Button
color="blue"
class="px-2 py-1"
size="xs"
pill
on:click={setForkBlockNumberFromRpc}
disabled={isFetching}
>
{#if isFetching}
<Spinner size="3" class="mr-2" color="white" />
{/if}
GET LATEST
</Button>
</div>
</div>
</div>
17 changes: 14 additions & 3 deletions tauri-app/src/lib/stores/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { validatedStringStore } from '$lib/storesGeneric/settingStore';
import { cachedWritableInt } from '$lib/storesGeneric/cachedWritableStore';
import find from 'lodash/find';
import * as chains from 'viem/chains'
import { setChainIdFromRpc } from '$lib/utils/chain';
import { getChainIdFromRpc } from '$lib/utils/chain';
import { getForkBlockNumberFromRpc } from '$lib/utils/forkBlockNumber';

const BLANK_WALLET_ADDRESS = '';

Expand All @@ -27,8 +28,18 @@ export const walletAddressMatchesOrBlank = derived(walletAddress, $walletAddress
return (otherAddress: string) => $walletAddress.value === otherAddress || $walletAddress.value === BLANK_WALLET_ADDRESS;
});

rpcUrl.subscribe(value => {
rpcUrl.subscribe(async value => {
if(value.isValid) {
setChainIdFromRpc();
try {
const res: number = await getChainIdFromRpc();
chainId.set(res);
// eslint-disable-next-line no-empty
} catch {};

try {
const res: number = await getForkBlockNumberFromRpc();
forkBlockNumber.set(res);
// eslint-disable-next-line no-empty
} catch {};
}
});
9 changes: 2 additions & 7 deletions tauri-app/src/lib/utils/chain.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { get } from 'svelte/store';
import { rpcUrl, chainId } from '$lib/stores/settings';
import { rpcUrl } from '$lib/stores/settings';
import { invoke } from '@tauri-apps/api';

export async function setChainIdFromRpc() {
if(!get(rpcUrl).isValid) return;

const val: number = await invoke('get_chainid', {rpcUrl: get(rpcUrl).value});
chainId.set(val);
}
export const getChainIdFromRpc = async (): Promise<number> => invoke('get_chainid', {rpcUrl: get(rpcUrl).value});
5 changes: 5 additions & 0 deletions tauri-app/src/lib/utils/forkBlockNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { get } from 'svelte/store';
import { rpcUrl } from '$lib/stores/settings';
import { invoke } from '@tauri-apps/api';

export const getForkBlockNumberFromRpc = async (): Promise<number> => invoke('get_block_number', {rpcUrl: get(rpcUrl).value});

0 comments on commit 0587b1e

Please sign in to comment.