Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sim replace frontend rpc calls with genlayerjs #600

Merged
merged 34 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2d9ddfc
install genlayer js
Nov 6, 2024
12f635c
contract queries OK
Nov 8, 2024
5913705
get transaction with genlayer client
Nov 8, 2024
bcefe89
update transaction unit tests
Nov 8, 2024
4154bf4
accounts and client working
Nov 11, 2024
219bb9f
extract shorten address hook, update unit tests
Nov 11, 2024
2f7471e
cleanup todos
Nov 13, 2024
eeb0788
cleanup
Nov 13, 2024
bdfb79e
cleanup comments
Nov 13, 2024
6d64fab
Merge branch 'main' into 580-sim-replace-frontend-rpc-calls-with-genl…
Nov 14, 2024
c5b9951
Merge branch 'main' into 580-sim-replace-frontend-rpc-calls-with-genl…
Nov 19, 2024
3372694
fix pkg lock
Nov 19, 2024
110719a
allow returning null tx
Nov 19, 2024
7c577d3
update params for leader only
Nov 19, 2024
b5df725
Merge branch 'main' into 580-sim-replace-frontend-rpc-calls-with-genl…
Nov 19, 2024
525c8e2
update genlayer-js and use new contract schema methods
Nov 20, 2024
4c967a7
bump genlayer-js version to include support for leaderOnly param
Nov 20, 2024
5c1d938
Merge branch 'main' into 580-sim-replace-frontend-rpc-calls-with-genl…
Nov 22, 2024
267c809
Merge branch 'main' into 580-sim-replace-frontend-rpc-calls-with-genl…
denishacquin Nov 25, 2024
5d63cf3
Merge branch 'main' into 580-sim-replace-frontend-rpc-calls-with-genl…
Nov 28, 2024
44a4d03
Merge branch 'main' into 580-sim-replace-frontend-rpc-calls-with-genl…
Nov 28, 2024
2f6945d
fix typing with genlayer-js/genvm
Nov 28, 2024
334e121
update genlayer-js
Nov 28, 2024
b6a650d
fix cleanup local tx logic
Nov 28, 2024
9298ea5
fix unit test
Nov 28, 2024
709674e
remove redundant typing and use genlayer-js types as truth
Nov 28, 2024
d4fc374
fix precommit
Nov 28, 2024
ec9996f
bump genlayer-js version
Dec 2, 2024
a68d306
update leaderOnly casing in contract queries
Dec 2, 2024
cb68a39
regen package lock
Dec 2, 2024
95041de
Merge branch 'main' into 580-sim-replace-frontend-rpc-calls-with-genl…
Dec 2, 2024
2228f71
Merge branch 'main' into 580-sim-replace-frontend-rpc-calls-with-genl…
Dec 2, 2024
48f4ae1
bumb genlayer-js version
Dec 2, 2024
cad1e11
Merge branch 'main' into 580-sim-replace-frontend-rpc-calls-with-genl…
cristiam86 Dec 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/protocol_rpc/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def get_transaction_count(

def get_transaction_by_hash(
transactions_processor: TransactionsProcessor, transaction_hash: str
) -> dict:
) -> dict | None:
return transactions_processor.get_transaction_by_hash(transaction_hash)


Expand Down
1,542 changes: 1,252 additions & 290 deletions frontend/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"defu": "^6.1.4",
"dexie": "^4.0.4",
"floating-vue": "^5.2.2",
"genlayer-js": "^0.3.3",
"hash-sum": "^2.0.0",
"jump.js": "^1.0.2",
"lodash-es": "^4.17.21",
Expand Down
59 changes: 0 additions & 59 deletions frontend/src/clients/web3.ts

This file was deleted.

10 changes: 4 additions & 6 deletions frontend/src/components/Simulator/AccountItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { ref } from 'vue';
import CopyTextButton from '../global/CopyTextButton.vue';
import { TrashIcon, CheckCircleIcon } from '@heroicons/vue/16/solid';

import type { Account } from 'genlayer-js/types';

Check warning on line 8 in frontend/src/components/Simulator/AccountItem.vue

View check run for this annotation

Codecov / codecov/patch

frontend/src/components/Simulator/AccountItem.vue#L8

Added line #L8 was not covered by tests
const store = useAccountsStore();

const setCurentAddress = () => {
Expand Down Expand Up @@ -35,6 +35,7 @@

const props = defineProps<{
active?: Boolean;
account: Account;

Check warning on line 38 in frontend/src/components/Simulator/AccountItem.vue

View check run for this annotation

Codecov / codecov/patch

frontend/src/components/Simulator/AccountItem.vue#L38

Added line #L38 was not covered by tests
privateKey: `0x${string}`;
canDelete?: Boolean;
}>();
Expand All @@ -61,16 +62,13 @@
class="flex grow flex-row truncate font-mono text-xs font-semibold"
:class="[!active && 'opacity-50']"
>
{{ store.accountFromPrivateKey(privateKey).address }}
{{ account.address }}

Check warning on line 65 in frontend/src/components/Simulator/AccountItem.vue

View check run for this annotation

Codecov / codecov/patch

frontend/src/components/Simulator/AccountItem.vue#L65

Added line #L65 was not covered by tests
</span>

<div
class="flex flex-row items-center gap-1 opacity-0 group-hover:opacity-100"
>
<CopyTextButton
:text="store.accountFromPrivateKey(privateKey).address"
v-tooltip="'Copy Address'"
/>
<CopyTextButton :text="account.address" v-tooltip="'Copy Address'" />

Check warning on line 71 in frontend/src/components/Simulator/AccountItem.vue

View check run for this annotation

Codecov / codecov/patch

frontend/src/components/Simulator/AccountItem.vue#L71

Added line #L71 was not covered by tests

<Transition mode="out-in" v-if="canDelete">
<button
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/Simulator/AccountSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { PlusIcon } from '@heroicons/vue/16/solid';
import { notify } from '@kyvg/vue3-notification';
import { useEventTracking } from '@/hooks';

import { createAccount } from 'genlayer-js';

Check warning on line 9 in frontend/src/components/Simulator/AccountSelect.vue

View check run for this annotation

Codecov / codecov/patch

frontend/src/components/Simulator/AccountSelect.vue#L9

Added line #L9 was not covered by tests
const store = useAccountsStore();
const { trackEvent } = useEventTracking();

Expand Down Expand Up @@ -42,6 +42,7 @@
v-for="privateKey in store.privateKeys"
:key="privateKey"
:privateKey="privateKey"
:account="createAccount(privateKey)"

Check warning on line 45 in frontend/src/components/Simulator/AccountSelect.vue

View check run for this annotation

Codecov / codecov/patch

frontend/src/components/Simulator/AccountSelect.vue#L45

Added line #L45 was not covered by tests
:active="privateKey === store.currentPrivateKey"
:canDelete="true"
v-close-popper
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/components/Simulator/ContractInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import PageSection from '@/components/Simulator/PageSection.vue';
import { CheckCircleIcon } from '@heroicons/vue/24/outline';
import EmptyListPlaceholder from '@/components/Simulator/EmptyListPlaceholder.vue';
import { PlusIcon } from '@heroicons/vue/16/solid';
import { useNodeStore } from '@/stores';
import { useWallet, useContractQueries } from '@/hooks';
import { useContractQueries, useShortAddress } from '@/hooks';

Check warning on line 6 in frontend/src/components/Simulator/ContractInfo.vue

View check run for this annotation

Codecov / codecov/patch

frontend/src/components/Simulator/ContractInfo.vue#L6

Added line #L6 was not covered by tests
import { UploadIcon } from 'lucide-vue-next';

const nodeStore = useNodeStore();
const { shortenAddress } = useWallet();
const { shorten } = useShortAddress();

Check warning on line 10 in frontend/src/components/Simulator/ContractInfo.vue

View check run for this annotation

Codecov / codecov/patch

frontend/src/components/Simulator/ContractInfo.vue#L10

Added line #L10 was not covered by tests

defineProps<{
showNewDeploymentButton: boolean;
Expand Down Expand Up @@ -37,7 +36,7 @@
Deployed at

<div class="font-semibold">
{{ shortenAddress(address) }}
{{ shorten(address) }}

Check warning on line 39 in frontend/src/components/Simulator/ContractInfo.vue

View check run for this annotation

Codecov / codecov/patch

frontend/src/components/Simulator/ContractInfo.vue#L39

Added line #L39 was not covered by tests
</div>

<CopyTextButton :text="address" />
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export * from './useUniqueId';
export * from './useEventTracking';
export * from './useRpcClient';
export * from './useWallet';
export * from './useDb';
export * from './useWebSocketClient';
export * from './useInputMap';
export * from './useContractQueries';
export * from './useFileName';
export * from './useSetupStores';
export * from './useGenlayer';
export * from './useShortAddress';
export * from './useConfig';
export * from './useTransactionListener';
72 changes: 29 additions & 43 deletions frontend/src/hooks/useContractQueries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { watch, ref, computed } from 'vue';
import { useQuery, useQueryClient } from '@tanstack/vue-query';
import type { Address, TransactionItem } from '@/types';
import type { TransactionItem } from '@/types';
import {
useContractsStore,
useTransactionsStore,
Expand All @@ -9,19 +9,19 @@
import { useDebounceFn } from '@vueuse/core';
import { notify } from '@kyvg/vue3-notification';
import { useMockContractData } from './useMockContractData';
import { useEventTracking, useRpcClient, useWallet } from '@/hooks';
import { useEventTracking, useGenlayer } from '@/hooks';
import * as calldata from '@/calldata';
import type { Address } from 'genlayer-js/types';

const schema = ref<any>();

export function useContractQueries() {
const rpcClient = useRpcClient();
const genlayer = useGenlayer();

Check warning on line 19 in frontend/src/hooks/useContractQueries.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useContractQueries.ts#L19

Added line #L19 was not covered by tests
const accountsStore = useAccountsStore();
const transactionsStore = useTransactionsStore();
const contractsStore = useContractsStore();
const queryClient = useQueryClient();
const { trackEvent } = useEventTracking();
const wallet = useWallet();
const contract = computed(() => contractsStore.currentContract);

const { mockContractId, mockContractSchema } = useMockContractData();
Expand Down Expand Up @@ -63,8 +63,9 @@
return mockContractSchema;
}

const result = await rpcClient.getContractSchema({
code: contract.value?.content ?? '',
const result = await genlayer.client?.request({
denishacquin marked this conversation as resolved.
Show resolved Hide resolved
method: 'gen_getContractSchemaForCode',
params: [contract.value?.content ?? ''],

Check warning on line 68 in frontend/src/hooks/useContractQueries.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useContractQueries.ts#L66-L68

Added lines #L66 - L68 were not covered by tests
});

schema.value = result;
Expand All @@ -84,20 +85,13 @@
if (!contract.value || !accountsStore.currentPrivateKey) {
throw new Error('Error Deploying the contract');
}
const data = [
contract.value?.content ?? '',
calldata.encode({ args }),
leaderOnly,
];

const nonce = await accountsStore.getCurrentNonce();

const signed = await wallet.signTransaction({
privateKey: accountsStore.currentPrivateKey,
data,
nonce,

const result = await genlayer.client?.deployContract({
code: contract.value?.content ?? '',
args,
leader_only: leaderOnly,

Check warning on line 92 in frontend/src/hooks/useContractQueries.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useContractQueries.ts#L88-L92

Added lines #L88 - L92 were not covered by tests
});
const result = await rpcClient.sendTransaction(signed);

Check warning on line 94 in frontend/src/hooks/useContractQueries.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useContractQueries.ts#L94

Added line #L94 was not covered by tests
const tx: TransactionItem = {
contractAddress: '',
localContractId: contract.value?.id ?? '',
Expand Down Expand Up @@ -151,25 +145,20 @@
return mockContractSchema;
}

const result = await rpcClient.getDeployedContractSchema({
address: deployedContract.value?.address ?? '',
const result = await genlayer.client?.request({
method: 'gen_getContractSchema',
denishacquin marked this conversation as resolved.
Show resolved Hide resolved
params: [deployedContract.value?.address ?? ''],

Check warning on line 150 in frontend/src/hooks/useContractQueries.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useContractQueries.ts#L148-L150

Added lines #L148 - L150 were not covered by tests
});

return result;
}

async function callReadMethod(
method: string,
args: calldata.CalldataEncodable[],
) {
async function callReadMethod(method: string, args: any[]) {

Check warning on line 156 in frontend/src/hooks/useContractQueries.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useContractQueries.ts#L156

Added line #L156 was not covered by tests
try {
const data = [calldata.encode({ method, args })];
const encodedData = wallet.encodeTransactionData(data);

const result = await rpcClient.getContractState({
to: address.value || '',
from: accountsStore.currentUserAddress,
data: encodedData,
const result = await genlayer.client?.readContract({
address: address.value as Address,
functionName: method,
args,

Check warning on line 161 in frontend/src/hooks/useContractQueries.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useContractQueries.ts#L158-L161

Added lines #L158 - L161 were not covered by tests
});

return result;
Expand All @@ -190,21 +179,17 @@
}) {
try {
if (!accountsStore.currentPrivateKey) {
throw new Error('Error Deploying the contract');
throw new Error('Error writing to contract');

Check warning on line 182 in frontend/src/hooks/useContractQueries.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useContractQueries.ts#L182

Added line #L182 was not covered by tests
}
const data = [calldata.encode({ method, args }), leaderOnly];
const to = (address.value as Address) || null;

const nonce = await accountsStore.getCurrentNonce();

const signed = await wallet.signTransaction({
privateKey: accountsStore.currentPrivateKey,
data,
nonce,
to,
const result = await genlayer.client?.writeContract({
address: address.value as Address,
functionName: method,
args,
value: BigInt(0),
leader_only: leaderOnly,

Check warning on line 190 in frontend/src/hooks/useContractQueries.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useContractQueries.ts#L185-L190

Added lines #L185 - L190 were not covered by tests
});

const result = await rpcClient.sendTransaction(signed);
transactionsStore.addTransaction({
contractAddress: address.value || '',
localContractId: contract.value?.id || '',
Expand All @@ -219,6 +204,7 @@
});
return true;
} catch (error) {
console.error(error);

Check warning on line 207 in frontend/src/hooks/useContractQueries.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useContractQueries.ts#L207

Added line #L207 was not covered by tests
throw new Error('Error writing to contract');
}
}
Expand Down
34 changes: 34 additions & 0 deletions frontend/src/hooks/useGenlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { simulator } from 'genlayer-js/chains';
import { createClient, createAccount } from 'genlayer-js';
import type { GenLayerClient } from 'genlayer-js/types';
import { watch } from 'vue';
import { useAccountsStore } from '@/stores';

let client: GenLayerClient<typeof simulator> | null = null;

export function useGenlayer() {
const accountsStore = useAccountsStore();

if (!client) {
initClient();
}

watch(
() => accountsStore.currentUserAddress,
() => {
initClient();
},
);

function initClient() {
client = createClient({
chain: simulator,
account: createAccount(accountsStore.currentPrivateKey || undefined),
});
}

return {
client,
initClient,
};
}

Check warning on line 34 in frontend/src/hooks/useGenlayer.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useGenlayer.ts#L10-L34

Added lines #L10 - L34 were not covered by tests
5 changes: 4 additions & 1 deletion frontend/src/hooks/useSetupStores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
useNodeStore,
useTutorialStore,
} from '@/stores';
import { useDb, useTransactionListener } from '@/hooks';
import { useDb, useGenlayer, useTransactionListener } from '@/hooks';
import { v4 as uuidv4 } from 'uuid';
import type { Address } from '@/types';

Expand All @@ -17,6 +17,7 @@
const nodeStore = useNodeStore();
const tutorialStore = useTutorialStore();
const db = useDb();
const genlayer = useGenlayer();

Check warning on line 20 in frontend/src/hooks/useSetupStores.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useSetupStores.ts#L20

Added line #L20 was not covered by tests
const transactionListener = useTransactionListener();
const contractFiles = await db.contractFiles.toArray();
const exampleFiles = contractFiles.filter((c) => c.example);
Expand Down Expand Up @@ -68,6 +69,8 @@
) as Address[])
: [];
}

genlayer.initClient();

Check warning on line 73 in frontend/src/hooks/useSetupStores.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useSetupStores.ts#L72-L73

Added lines #L72 - L73 were not covered by tests
};

return {
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/hooks/useShortAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const useShortAddress = () => {
function shorten(address?: string) {
if (!address) {
return '';
}

const maxChars = 4;
const displayedChars = Math.min(Math.floor(address.length / 3), maxChars);

return (
address.slice(0, displayedChars) + '...' + address.slice(-displayedChars)
);
}

return {
shorten,
};
};

Check warning on line 18 in frontend/src/hooks/useShortAddress.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/hooks/useShortAddress.ts#L2-L18

Added lines #L2 - L18 were not covered by tests
Loading