Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 8 additions & 8 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"@dashevo/evo-sdk": "^3.0.1",
"@dashevo/evo-sdk": "^3.1.0-dev.1",
"react": "^19.2.4",
"react-dom": "^19.2.4"
},
Expand Down
41 changes: 30 additions & 11 deletions src/components/CreateListing.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useState, useCallback } from 'react';
import { useDashMutation } from '../hooks/useDashMutation';
import { Document, IdentitySigner } from '@dashevo/evo-sdk';

const CONTRACT_ID = 'YOUR_CONTRACT_ID';
const IDENTITY_ID = 'YOUR_IDENTITY_ID';
const PRIVATE_KEY = 'YOUR_PRIVATE_KEY_WIF';
const PRIVATE_KEY_WIF = 'YOUR_PRIVATE_KEY_WIF';
const SIGNING_KEY_INDEX = 0;

export function CreateListing() {
const [make, setMake] = useState('');
Expand All @@ -13,23 +15,40 @@ export function CreateListing() {

const mutation = useDashMutation(
useCallback(
(sdk) =>
sdk.documents.create({
contractId: CONTRACT_ID,
documentType: 'listing',
document: {
async (sdk) => {
// Fetch the owner identity to get the signing key
const identity = await sdk.identities.fetch(IDENTITY_ID);
if (!identity) throw new Error('Identity not found');

const identityKey = identity.publicKeys[SIGNING_KEY_INDEX];
if (!identityKey) throw new Error('Signing key not found');

// Create a signer with the private key
const signer = new IdentitySigner();
signer.addKeyFromWif(PRIVATE_KEY_WIF);

// Build the document
const document = new Document({
documentTypeName: 'listing',
dataContractId: CONTRACT_ID,
ownerId: IDENTITY_ID,
properties: {
make,
model,
year,
priceUsd: price,
mileageKm: 0,
status: 'available',
},
identityId: IDENTITY_ID,
privateKeyWif: PRIVATE_KEY,
signingKeyIndex: 0,
nonce: sdk.identities.contractNonce(IDENTITY_ID, CONTRACT_ID),
}),
});

// Broadcast the create transition
await sdk.documents.create({
document,
identityKey,
signer,
});
},
[make, model, year, price],
),
);
Expand Down
6 changes: 3 additions & 3 deletions src/components/IdentityViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export function IdentityViewer() {
{error && <p className="error">{error}</p>}
{identity && (
<div className="identity-card">
<p><strong>ID:</strong> {identity.getId().toString()}</p>
<p><strong>Balance:</strong> {identity.getBalance().toString()} credits</p>
<p><strong>Public keys:</strong> {identity.getPublicKeys().length}</p>
<p><strong>ID:</strong> {identity.id.toString()}</p>
<p><strong>Balance:</strong> {identity.balance.toString()} credits</p>
<p><strong>Public keys:</strong> {identity.publicKeys.length}</p>
</div>
)}
</div>
Expand Down
10 changes: 5 additions & 5 deletions src/components/ListingsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const CONTRACT_ID = 'YOUR_CONTRACT_ID';

export function ListingsList() {
const { data: results, isLoading, error, refetch } = useDocuments({
contractId: CONTRACT_ID,
documentType: 'listing',
dataContractId: CONTRACT_ID,
documentTypeName: 'listing',
where: [['status', '==', 'available']],
orderBy: [['priceUsd', 'asc']],
limit: 20,
Expand All @@ -22,10 +22,10 @@ export function ListingsList() {
<ul>
{[...results.entries()].map(([id, doc]) => {
if (!doc) return null;
const d = doc.getData();
const d = doc.properties as Record<string, unknown>;
return (
<li key={id}>
<strong>{d.year} {d.make} {d.model}</strong> — ${d.priceUsd}
<li key={id.toString()}>
<strong>{d.year as number} {d.make as string} {d.model as string}</strong> — ${d.priceUsd as number}
</li>
);
})}
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export function useDocuments(query: DocumentsQuery) {
[JSON.stringify(query)],
);
}

export type { DocumentsQuery };
6 changes: 5 additions & 1 deletion src/hooks/useTokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ export function useTokenBalance(identityId: string, tokenId: string) {
return useDashQuery(
async (sdk) => {
const balances = await sdk.tokens.identityBalances(identityId, [tokenId]);
return balances.get(tokenId) ?? 0n;
// Map keys are Identifier objects; find the matching entry by string comparison
for (const [id, balance] of balances.entries()) {
if (id.toString() === tokenId) return balance;
}
return 0n;
},
[identityId, tokenId],
);
Expand Down