### Install Knowledge Graph SDK Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Installs the Knowledge Graph SDK using npm. This is the initial step to begin interacting with The Graph's knowledge graph functionalities. ```sh npm install @graphprotocol/grc-20 ``` -------------------------------- ### Full Publishing Flow Example Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md This comprehensive example illustrates the complete process of publishing an edit on-chain. It covers wallet creation, space deployment, entity creation, IPFS publishing, fetching transaction data, and sending the transaction. ```ts import { privateKeyToAccount, } from 'viem/accounts'; import { Graph, Ipfs, getWalletClient, } from '@graphprotocol/grc-20'; const addressPrivateKey = '0xTODO'; const { address } = privateKeyToAccount(addressPrivateKey); // Take the address and enter it in Faucet to get some testnet ETH https://faucet.conduit.xyz/geo-test-zc16z3tcvf const smartAccountWalletClient = await getWalletClient({ privateKey: addressPrivateKey, }); console.log('addressPrivateKey', addressPrivateKey); console.log('address', address); // console.log('smartAccountWalletClient', smartAccountWalletClient); const space = await Graph.createSpace({ editorAddress: address, name: 'test', network: 'TESTNET', }); console.log('space', space); const spaceId = space.id; const { ops, id } = await Graph.createEntity({ name: 'test name', description: 'test description', }); console.log('entity id', id); const { cid } = await Ipfs.publishEdit({ name: 'Edit name', ops, author: address, }); console.log('cid', cid); const result = await fetch(`${Graph.TESTNET_API_ORIGIN}/space/${spaceId}/edit/calldata`, { method: 'POST', body: JSON.stringify({ cid }), }); console.log('edit result', result); const editResultJson = await result.json(); console.log('editResultJson', editResultJson); const { to, data } = editResultJson; console.log('to', to); console.log('data', data); const txResult = await smartAccountWalletClient.sendTransaction({ // @ts-expect-error - TODO: fix the types error account: smartAccountWalletClient.account, to: to, value: 0n, data: data, }); console.log('txResult', txResult); ``` -------------------------------- ### Full Example Flow: Creating and Linking Entities Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Illustrates a comprehensive workflow including creating properties (Age, Likes), types (Person), images (covers), and entities (Restaurant, Person). It shows how to link entities using relations and collect all operations. ```typescript import { Graph } from '@graphprotocol/grc-20'; const ops: Array = []; // create an age property const { id: agePropertyId, ops: createAgePropertyOps } = Graph.createProperty({ dataType: 'NUMBER', name: 'Age', }); ops.push(...createAgePropertyOps); // create a likes property const { id: likesPropertyId, ops: createLikesPropertyOps } = Graph.createProperty({ dataType: 'RELATION', name: 'Likes', }); ops.push(...createLikesPropertyOps); // create a person type const { id: personTypeId, ops: createPersonTypeOps } = Graph.createType({ name: 'Person', cover: personCoverId, properties: [agePropertyId, likesPropertyId], }); ops.push(...createPersonTypeOps); // create an restaurant cover image const { id: restaurantCoverId, ops: createRestaurantCoverOps } = await Graph.createImage({ url: 'https://example.com/image.png', }); ops.push(...createRestaurantCoverOps); // create a restaurant entity with a website property const restaurantTypeId = 'A9QizqoXSqjfPUBjLoPJa2'; const { id: restaurantId, ops: createRestaurantOps } = Graph.createEntity({ name: 'Yum Yum', description: 'A restaurant serving fusion cuisine', cover: restaurantCoverId, types: [restaurantTypeId], values: [ { property: WEBSITE_PROPERTY, value: 'https://example.com', }, ], }); ops.push(...createRestaurantOps); // create a person cover image const { id: personCoverId, ops: createPersonCoverOps } = await Graph.createImage({ url: 'https://example.com/avatar.png', }); ops.push(...createPersonCoverOps); // create a person entity with a likes relation to the restaurant entity const { id: personId, ops: createPersonOps } = Graph.createEntity({ name: 'Jane Doe', types: [personTypeId], cover: personCoverId, values: [ { property: agePropertyId, value: serializeNumber(42), }, { property: likesPropertyId, value: restaurantId, }, ], }); ops.push(...createPersonOps); ``` -------------------------------- ### Create Graph Entity with Properties and Relations Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Provides a comprehensive example of creating an entity, including its name, description, associated types, cover image, properties, and relations to other entities. Relations can also include their own properties. ```ts import { Graph } from '@graphprotocol/grc-20'; // create an entity const { id: restaurantId, ops: createRestaurantOps } = Graph.createEntity({ name: 'name of the entity', description: 'description of the entity', types: […listOfTypeIds], cover: imageId, values: [ { property: propertyId, value: 'value of the property' } ], relations: { // relation property [propertyId]: { toEntity: 'id of the entity', id: 'id of the relation', // optional position: positionString, // optional values: [ { property: propertyId, value: 'value of the property' } ], }, }, }); ``` -------------------------------- ### Build Library Source: https://github.com/graphprotocol/grc-20-ts/blob/main/CONTRIBUTING.md Builds the project's library using pnpm. ```bash pnpm build ``` -------------------------------- ### Run Tests Source: https://github.com/graphprotocol/grc-20-ts/blob/main/CONTRIBUTING.md Executes the test suite for the project using pnpm. ```bash pnpm test ``` -------------------------------- ### Run Linting Source: https://github.com/graphprotocol/grc-20-ts/blob/main/CONTRIBUTING.md Applies linting rules to the codebase using pnpm. ```bash pnpm lint ``` -------------------------------- ### Deploying a Space Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md This snippet demonstrates how to programmatically deploy a space using the Graph API. It currently only supports deploying PERSONAL governance mode spaces and can target testnet or mainnet. ```ts import { Graph } from '@graphprotocol/grc-20'; // needs to be a valid address const editorAddress = '0x000000000000000000000000000000000000'; const spaceName = 'Example-Name'; const spaceId = await Graph.createSpace({ initialEditorAddress, spaceName, // Optionally specify TESTNET or MAINNET. Defaults to MAINNET network: 'TESTNET', }); ``` -------------------------------- ### Publishing an Edit On-Chain with Wallet Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md This snippet demonstrates how to publish an edit on-chain using a provided private key to create a wallet client. It includes steps for obtaining testnet ETH and sending a transaction. ```ts import { privateKeyToAccount, } from 'viem/accounts'; import { getWalletClient, } from '@graphprotocol/grc-20'; const addressPrivateKey = '0xTODO'; const { address } = privateKeyToAccount(addressPrivateKey); // Take the address and enter it in Faucet to get some testnet ETH https://faucet.conduit.xyz/geo-test-zc16z3tcvf const smartAccountWalletClient = await getWalletClient({ privateKey: addressPrivateKey, }); // publish an edit to IPFS // get the calldata for the edit const txResult = await smartAccountWalletClient.sendTransaction({ to: to, value: 0n, data: data, }); ``` -------------------------------- ### Generate Protobuf Files Source: https://github.com/graphprotocol/grc-20-ts/blob/main/CONTRIBUTING.md Generates necessary protobuf files for the project using pnpm. ```bash pnpm generate:protobuf ``` -------------------------------- ### Create Graph Image Entity Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Demonstrates how to create an image entity, which can be linked to other entities. It requires a URL or a Blob object for the image data. ```ts import { Graph } from '@graphprotocol/grc-20'; // create an image const { id: imageId, ops: createImageOps } = await Graph.createImage({ url: 'https://example.com/image.png', // blob: new Blob([fs.readFileSync(path.join(__dirname, 'cover.png'))], { type: 'image/png' }); }); ``` -------------------------------- ### Publish New Version Source: https://github.com/graphprotocol/grc-20-ts/blob/main/CONTRIBUTING.md Publishes a new version of the project, including committing changes, building, publishing, and creating a GitHub release. ```bash pnpm changeset version # commit the changes pnpm build pnpm changeset publish git push origin # e.g. v0.23.0 gh release create ``` -------------------------------- ### Publishing an Edit to IPFS Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Shows how to publish a set of operations (ops) as an 'Edit' to IPFS using the `Ipfs.publishEdit` method. This involves binary encoding the operations and uploading them, with the SDK handling the IPFS CID prefixing. ```typescript import { Ipfs } from '@graphprotocol/grc-20'; const {cid} = await Ipfs.publishEdit({ name: 'Edit name', ops: ops, author: '0x000000000000000000000000000000000000', network: 'TESTNET', // optional, defaults to MAINNET }) ``` -------------------------------- ### Publishing an Edit On-Chain with Geo Account Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md This snippet shows how to publish an edit on-chain using a Geo Account's private key to obtain a smart account wallet client. It highlights that transaction costs are sponsored during early access. ```ts import { getSmartAccountWalletClient } from '@graphprotocol/grc-20'; // IMPORTANT: Be careful with your private key. Don't commit it to version control. // You can get your private key using https://www.geobrowser.io/export-wallet const privateKey = `0x${privateKeyFromGeoWallet}`; const smartAccountWalletClient = await getSmartAccountWalletClient({ privateKey, // rpcUrl, // optional }); // publish an edit to IPFS // get the calldata for the edit const txResult = await smartAccountWalletClient.sendTransaction({ to: to, value: 0n, data: data, }); ``` -------------------------------- ### Creating Entities with Value Options Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Demonstrates how to create entities with text and number values, specifying language and unit options respectively. This is useful for internationalization and standardized data representation. ```typescript const { id: textEntityId, ops: createTextEntityOps } = Graph.createEntity({ values: [ { property: someTextPropertyId, value: 'Hello', options: { type: 'text', language: Id('dad6e52a-5e94-4e55-9411-cfe3a3c3ea64'), }, }, { property: someNumberPropertyId, value: Graph.serializeNumber(42), options: { type: 'number', unit: Id('016c9b1c-d8a8-4e4d-9e84-4e40878bb235'), }, }, ], }); ``` -------------------------------- ### Create Graph Type Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Illustrates the process of defining a new type for entities in the graph. This includes providing a name for the type and associating it with a list of property IDs. ```ts import { Graph } from '@graphprotocol/grc-20'; // create a type const { id: personTypeId, ops: createPersonTypeOps } = Graph.createType({ name: 'name of the type', properties: […listOfPropertyIds], }); ``` -------------------------------- ### Create New Changeset Source: https://github.com/graphprotocol/grc-20-ts/blob/main/CONTRIBUTING.md Creates a new changeset for tracking changes in a Pull Request using pnpm. ```bash pnpm changeset ``` -------------------------------- ### Create Graph Property Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Shows how to define a new property within the graph. This involves specifying the property's name and its data type, such as TEXT, NUMBER, TIME, POINT, CHECKBOX, or RELATION. ```ts import { Graph } from '@graphprotocol/grc-20'; // create a property const propertyResult = Graph.createProperty({ name: 'name of the property', dataType: 'TEXT', // TEXT | NUMBER | TIME | POINT | CHECKBOX | RELATION, }); ``` -------------------------------- ### Publishing an Edit On-Chain Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Details how to publish an edit to a space on-chain after it has been uploaded to IPFS. It involves fetching the correct calldata and contract addresses based on the space's governance structure and then sending a transaction using a wallet client. ```typescript import { Graph } from '@graphprotocol/grc-20'; // You'll need to know your space id and have an IPFS hash ahead of time const spaceId = 'space-id'; const cid = 'ipfs://hash'; // This returns the correct contract address and calldata depending on the space id // You can also use Graph.MAINNET_API_ORIGIN for mainnet (currently not working) const result = await fetch(`${Graph.TESTNET_API_ORIGIN}/space/${spaceId}/edit/calldata`, { method: "POST", body: JSON.stringify({ cid }), }); const { to, data } = await result.json(); const txResult = await walletClient.sendTransaction({ to: to, value: 0n, data: data, }); ``` -------------------------------- ### Serialize Graph Entity Values Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Illustrates how to serialize different data types (number, boolean, date, point) into their string representations for use in graph entities. This ensures data consistency and proper storage. ```ts import { Graph } from '@graphprotocol/grc-20'; const { id: personId, ops: createPersonOps } = Graph.createEntity({ values: [ { property: someNumberPropertyId, value: Graph.serializeNumber(42), }, { property: someCheckboxPropertyId, value: Graph.serializeBoolean(true), }, { property: someDatePropertyId, value: Graph.serializeDate(new Date()), }, { property: somePointPropertyId, value: Graph.serializePoint([1, 2]), }, ] }); ``` -------------------------------- ### Generate Unique Entity ID Source: https://github.com/graphprotocol/grc-20-ts/blob/main/README.md Demonstrates how to generate a globally unique identifier for entities within The Graph using the SDK's `Id.generate()` method. This ID is crucial for referencing entities. ```ts import { Id } from '@graphprotocol/grc-20'; const newId = Id.generate(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.