Skip to content

Commit

Permalink
Merge branch 'fuzd' into dev-with-private-account
Browse files Browse the repository at this point in the history
  • Loading branch information
wighawag committed Nov 26, 2023
2 parents f06c3d6 + c4b0639 commit d55884b
Show file tree
Hide file tree
Showing 8 changed files with 430 additions and 130 deletions.
4 changes: 2 additions & 2 deletions dev/wezterm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ wezterm.on('gui-startup', function(cmd)


}
tab:set_title 'stratagems'
window:set_title 'stratagems'
tab:set_title 'jolly-roger'
window:set_title 'jolly-roger'

local pane_indexer = pane:split {
args = {'bash', '-i', '-c', 'cd '.. cmd.args[1] .. '; sleep 1; pnpm indexer:dev; bash'},
Expand Down
116 changes: 62 additions & 54 deletions web/src/lib/blockchain/state/PendingState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,82 @@ const registry = new Registry();
* This allow us to optimistically update the UI with pending messages from the user
*
*/
export const pendingState = derived([syncing, state, accountData.data], ([$syncing, $state, $accountData]) => {
logger.info(`num greetings: ${$state.greetings.length}`);
export const pendingState = derived(
[syncing, state, accountData.onchainActions],
([$syncing, $state, $onchainActions]) => {
logger.info(`num greetings: ${$state.greetings.length}`);

const pendingMessages: {[from: `0x${string}`]: string} = {};
const pendingMessages: {[from: `0x${string}`]: string} = {};

const transactionsAlreadyIndexed: {[hash: string]: boolean} = {};
if ($syncing.lastSync && $syncing.lastSync.unconfirmedBlocks) {
for (const block of $syncing.lastSync.unconfirmedBlocks) {
for (const event of block.events) {
transactionsAlreadyIndexed[event.transactionHash] = true;
const transactionsAlreadyIndexed: {[hash: string]: boolean} = {};
if ($syncing.lastSync && $syncing.lastSync.unconfirmedBlocks) {
for (const block of $syncing.lastSync.unconfirmedBlocks) {
for (const event of block.events) {
transactionsAlreadyIndexed[event.transactionHash] = true;
}
}
}
}

if ($accountData) {
for (const hash of Object.keys($accountData.actions)) {
if (transactionsAlreadyIndexed[hash]) {
// if tx is already considered in the index, we can skip
continue;
}

const action = $accountData.actions[hash as `0x${string}`];
if (action.status === 'Failure') {
// tx failed so we can ignore it
// TODO? this failure can be picked up elsewhere to let the user know
// but we could also modify the PendingState type to include information here
continue;
}
if ($onchainActions) {
for (const hash of Object.keys($onchainActions)) {
if (transactionsAlreadyIndexed[hash]) {
// if tx is already considered in the index, we can skip
continue;
}

if (action.final) {
console.warn(`indexer still indexing, did not pick up tx ${hash} yet, TODO ?we can consider it not pending`);
}
const action = $onchainActions[hash as `0x${string}`];

switch (action.inclusion) {
case 'Cancelled':
// tx cancelled, we ignore it
if (action.status === 'Failure') {
// tx failed so we can ignore it
// TODO? this failure can be picked up elsewhere to let the user know
// but we could also modify the PendingState type to include information here
continue;
case 'BeingFetched':
// TODO add to state that loading is still going for txs....
// tx state is loading
}

if (action.final) {
// onchain Action is final, can track back from index
// we could check if indexer is up to speed though
// best is to simply skip here
// and if indexer is not up to speed, we can deal with it in the UI elsewere
continue;
case 'Included':
case 'NotFound':
case 'Broadcasted':
// else we consider it
}
if (action.tx.metadata && typeof action.tx.metadata === 'object' && 'message' in action.tx.metadata) {
const fromAccount = getAddress(action.tx.from);
pendingMessages[fromAccount] = action.tx.metadata.message as string;
}

switch (action.inclusion) {
case 'Cancelled':
// tx cancelled, we ignore it
continue;
case 'BeingFetched':
// TODO add to state that loading is still going for txs....
// tx state is loading
continue;
case 'Included':
case 'NotFound':
case 'Broadcasted':
// else we consider it
}
if (action.tx.metadata && typeof action.tx.metadata === 'object' && 'message' in action.tx.metadata) {
const fromAccount = getAddress(action.tx.from);
pendingMessages[fromAccount] = action.tx.metadata.message as string;
}
}
}
}

const accounts = Object.keys(pendingMessages);
if (accounts.length > 0) {
const pending = createDraft($state);
registry.handle(pending, true);
const accounts = Object.keys(pendingMessages);
if (accounts.length > 0) {
const pending = createDraft($state);
registry.handle(pending, true);

for (const from of accounts) {
const account = from as `0x${string}`;
registry.setMessageFor(account, pendingMessages[account], 0); // TODO dayTimeInSeconds ?
}
for (const from of accounts) {
const account = from as `0x${string}`;
registry.setMessageFor(account, pendingMessages[account], 0); // TODO dayTimeInSeconds ?
}

return finishDraft(pending);
} else {
return $state;
}
});
return finishDraft(pending);
} else {
return $state;
}
},
);

if (typeof window !== 'undefined') {
(window as any).pendingState = pendingState;
Expand Down
25 changes: 25 additions & 0 deletions web/src/lib/web3/AccountSignIn.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script lang="ts">
import Modal from '$lib/components/modals/Modal.svelte';
import type {account as Account} from './';
export let account: typeof Account;
let doNotAskAgainSignature: boolean = false;
</script>

<Modal>
<h3 class="text-lg font-bold">Welcome to Jolly-Roger</h3>
<p class="py-4">
In order to continue and get a safe place to save data, you'll need to sign a message. Be carefull and only sign
this message on trusted frontend.
</p>
<div class="form-control">
<label class="label cursor-pointer">
<span class="label-text">Do not ask again (trust computer)</span>
<input type="checkbox" bind:checked={doNotAskAgainSignature} class="checkbox" />
</label>
</div>
<div class="modal-action">
<button on:click={() => account.rejectLoadingStep()} class="btn btn-error">Cancel</button>
<button on:click={() => account.acceptLoadingStep({doNotAskAgainSignature})} class="btn">Sign</button>
</div>
</Modal>
29 changes: 21 additions & 8 deletions web/src/lib/web3/Web3AccountInfo.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import Modal from '$lib/components/modals/Modal.svelte';
import type {account as Account} from './';
import AccountSignIn from './AccountSignIn.svelte';
export let account: typeof Account;
</script>

Expand All @@ -16,12 +17,24 @@
{/if}

{#if $account.loadingStep}
<Modal>
<h3 class="text-lg font-bold">{$account.loadingStep}</h3>
<p class="py-4">{$account.loadingStep}</p>
<div class="modal-action">
<button on:click={() => account.rejectLoadingStep()} class="btn btn-error">Cancel</button>
<button on:click={() => account.acceptLoadingStep()} class="btn">Continue</button>
</div>
</Modal>
{#if $account.loadingStep.id == 'SIGNING'}
<Modal>
<h3 class="text-lg font-bold">Welcome to Jolly-Roger</h3>
<p class="py-4">Sign the message to access to your data.</p>
<div class="modal-action">
<button on:click={() => account.rejectLoadingStep()} class="btn btn-error">Cancel</button>
</div>
</Modal>
{:else if $account.loadingStep.id == 'WELCOME'}
<AccountSignIn {account} />
{:else}
<Modal>
<h3 class="text-lg font-bold">{$account.loadingStep.id}</h3>
<p class="py-4">{$account.loadingStep.id}</p>
<div class="modal-action">
<button on:click={() => account.rejectLoadingStep()} class="btn btn-error">Cancel</button>
<button on:click={() => account.acceptLoadingStep()} class="btn">Continue</button>
</div>
</Modal>
{/if}
{/if}
Loading

0 comments on commit d55884b

Please sign in to comment.