Skip to content

Commit

Permalink
New translations api.mdx (Swedish)
Browse files Browse the repository at this point in the history
  • Loading branch information
benface committed Dec 16, 2024
1 parent 1ed1c35 commit ee03f97
Showing 1 changed file with 87 additions and 77 deletions.
164 changes: 87 additions & 77 deletions website/pages/sv/developing/graph-ts/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ The `@graphprotocol/graph-ts` library provides the following APIs:

The `apiVersion` in the subgraph manifest specifies the mapping API version which is run by Graph Node for a given subgraph.

| Version | Versionsanteckningar |
| :-: | --- |
| 0.0.9 | Adds new host functions [`eth_get_balance`](#balance-of-an-address) & [`hasCode`](#check-if-an-address-is-a-contract-or-eoa) |
| 0.0.8 | Adds validation for existence of fields in the schema when saving an entity. |
| 0.0.7 | Added `TransactionReceipt` and `Log` classes to the Ethereum types<br />Added `receipt` field to the Ethereum Event object |
| 0.0.6 | Added `nonce` field to the Ethereum Transaction object<br />Added `baseFeePerGas` to the Ethereum Block object |
| 0.0.5 | AssemblyScript upgraded to version 0.19.10 (this includes breaking changes, please see the [`Migration Guide`](/release-notes/assemblyscript-migration-guide))<br />`ethereum.transaction.gasUsed` renamed to `ethereum.transaction.gasLimit` |
| 0.0.4 | Added `functionSignature` field to the Ethereum SmartContractCall object |
| 0.0.3 | Added `from` field to the Ethereum Call object<br />`etherem.call.address` renamed to `ethereum.call.to` |
| 0.0.2 | Added `input` field to the Ethereum Transaction object |
| Version | Versionsanteckningar |
| :-----: | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 0.0.9 | Adds new host functions [`eth_get_balance`](#balance-of-an-address) & [`hasCode`](#check-if-an-address-is-a-contract-or-eoa) |
| 0.0.8 | Adds validation for existence of fields in the schema when saving an entity. |
| 0.0.7 | Added `TransactionReceipt` and `Log` classes to the Ethereum types<br />Added `receipt` field to the Ethereum Event object |
| 0.0.6 | Added `nonce` field to the Ethereum Transaction object<br />Added `baseFeePerGas` to the Ethereum Block object |
| 0.0.5 | AssemblyScript upgraded to version 0.19.10 (this includes breaking changes, please see the [`Migration Guide`](/release-notes/assemblyscript-migration-guide))<br />`ethereum.transaction.gasUsed` renamed to `ethereum.transaction.gasLimit` |
| 0.0.4 | Added `functionSignature` field to the Ethereum SmartContractCall object |
| 0.0.3 | Added `from` field to the Ethereum Call object<br />`etherem.call.address` renamed to `ethereum.call.to` |
| 0.0.2 | Added `input` field to the Ethereum Transaction object |

### Inbyggda typer

Expand Down Expand Up @@ -163,7 +163,7 @@ _Math_
#### TypedMap

```typescript
import { TypedMap } from '@graphprotocol/graph-ts'
import { TypedMap } from "@graphprotocol/graph-ts";
```

`TypedMap` can be used to store key-value pairs. See [this example](https://github.com/graphprotocol/aragon-subgraph/blob/29dd38680c5e5104d9fdc2f90e740298c67e4a31/individual-dao-subgraph/mappings/constants.ts#L51).
Expand All @@ -179,7 +179,7 @@ The `TypedMap` class has the following API:
#### Bytes

```typescript
import { Bytes } from '@graphprotocol/graph-ts'
import { Bytes } from "@graphprotocol/graph-ts";
```

`Bytes` is used to represent arbitrary-length arrays of bytes. This includes Ethereum values of type `bytes`, `bytes32`, etc.
Expand All @@ -205,7 +205,7 @@ _Operators_
#### Address

```typescript
import { Address } from '@graphprotocol/graph-ts'
import { Address } from "@graphprotocol/graph-ts";
```

`Address` extends `Bytes` to represent Ethereum `address` values.
Expand All @@ -218,7 +218,7 @@ It adds the following method on top of the `Bytes` API:
### Store API

```typescript
import { store } from '@graphprotocol/graph-ts'
import { store } from "@graphprotocol/graph-ts";
```

The `store` API allows to load, save and remove entities from and to the Graph Node store.
Expand All @@ -231,24 +231,24 @@ Följande är ett vanligt mönster för att skapa entiteter från Ethereum-händ

```typescript
// Importera händelseklassen Transfer som genererats från ERC20 ABI
import { Transfer as TransferEvent } from '../generated/ERC20/ERC20'
import { Transfer as TransferEvent } from "../generated/ERC20/ERC20";

// Importera entitetstypen Transfer som genererats från GraphQL-schemat
import { Transfer } from '../generated/schema'
import { Transfer } from "../generated/schema";

// Händelsehanterare för överföring
export function handleTransfer(event: TransferEvent): void {
// Skapa en Transfer-entitet, med transaktionshash som enhets-ID
let id = event.transaction.hash
let transfer = new Transfer(id)
let id = event.transaction.hash;
let transfer = new Transfer(id);

// Ange egenskaper för entiteten med hjälp av händelseparametrarna
transfer.from = event.params.from
transfer.to = event.params.to
transfer.amount = event.params.amount
transfer.from = event.params.from;
transfer.to = event.params.to;
transfer.amount = event.params.amount;

// Spara entiteten till lagret
transfer.save()
transfer.save();
}
```

Expand All @@ -263,10 +263,10 @@ Each entity must have a unique ID to avoid collisions with other entities. It is
Om en entitet redan finns kan den laddas från lagret med följande:

```typescript
let id = event.transaction.hash // eller hur ID konstrueras
let transfer = Transfer.load(id)
let id = event.transaction.hash; // eller hur ID konstrueras
let transfer = Transfer.load(id);
if (transfer == null) {
transfer = new Transfer(id)
transfer = new Transfer(id);
}

// Använd överföringsenheten som tidigare
Expand All @@ -286,10 +286,10 @@ The store API facilitates the retrieval of entities that were created or updated
- For some subgraphs, these missed lookups can contribute significantly to the indexing time.

```typescript
let id = event.transaction.hash // eller hur ID konstrueras
let transfer = Transfer.loadInBlock(id)
let id = event.transaction.hash; // eller hur ID konstrueras
let transfer = Transfer.loadInBlock(id);
if (transfer == null) {
transfer = new Transfer(id)
transfer = new Transfer(id);
}

// Använd överföringsenheten som tidigare
Expand Down Expand Up @@ -343,7 +343,7 @@ transfer.amount = ...
Det är också möjligt att avaktivera egenskaper med en av följande två instruktioner:

```typescript
transfer.from.unset()
transfer.from.unset();
transfer.from = null
```

Expand All @@ -353,14 +353,14 @@ Updating array properties is a little more involved, as the getting an array fro

```typescript
// Detta kommer inte att fungera
entity.numbers.push(BigInt.fromI32(1))
entity.save()
entity.numbers.push(BigInt.fromI32(1));
entity.save();

// Detta kommer att fungera
let numbers = entity.numbers
numbers.push(BigInt.fromI32(1))
entity.numbers = numbers
entity.save()
let numbers = entity.numbers;
numbers.push(BigInt.fromI32(1));
entity.numbers = numbers;
entity.save();
```

#### Ta bort entiteter från lagret
Expand Down Expand Up @@ -398,12 +398,12 @@ type Transfer @entity {
and a `Transfer(address,address,uint256)` event signature on Ethereum, the `from`, `to` and `amount` values of type `address`, `address` and `uint256` are converted to `Address` and `BigInt`, allowing them to be passed on to the `Bytes!` and `BigInt!` properties of the `Transfer` entity:

```typescript
let id = event.transaction.hash
let transfer = new Transfer(id)
transfer.from = event.params.from
transfer.to = event.params.to
transfer.amount = event.params.amount
transfer.save()
let id = event.transaction.hash;
let transfer = new Transfer(id);
transfer.from = event.params.from;
transfer.to = event.params.to;
transfer.amount = event.params.amount;
transfer.save();
```

#### Händelser och Block/Transaktionsdata
Expand Down Expand Up @@ -489,16 +489,19 @@ En vanlig mönster är att komma åt kontraktet från vilket en händelse härst

```typescript
// Importera den genererade kontraktsklassen och den genererade klassen för överföringshändelser
import { ERC20Contract, Transfer as TransferEvent } from '../generated/ERC20Contract/ERC20Contract'
import {
ERC20Contract,
Transfer as TransferEvent,
} from "../generated/ERC20Contract/ERC20Contract";
// Importera den genererade entitetsklassen
import { Transfer } from '../generated/schema'
import { Transfer } from "../generated/schema";

export function handleTransfer(event: TransferEvent) {
// Bind kontraktet till den adress som skickade händelsen
let contract = ERC20Contract.bind(event.address)
let contract = ERC20Contract.bind(event.address);

// Åtkomst till tillståndsvariabler och funktioner genom att anropa dem
let erc20Symbol = contract.symbol()
let erc20Symbol = contract.symbol();
}
```

Expand All @@ -515,12 +518,12 @@ If the read-only methods of your contract may revert, then you should handle tha
- For example, the Gravity contract exposes the `gravatarToOwner` method. This code would be able to handle a revert in that method:

```typescript
let gravitera = gravitera.bind(event.address)
let callResult = gravitera_gravatarToOwner(gravatar)
let gravitera = gravitera.bind(event.address);
let callResult = gravitera_gravatarToOwner(gravatar);
if (callResult.reverted) {
log.info('getGravatar reverted', [])
log.info("getGravatar reverted", []);
} else {
let owner = callResult.value
let owner = callResult.value;
}
```

Expand Down Expand Up @@ -579,7 +582,7 @@ let isContract = ethereum.hasCode(eoa).inner // returns false
### API för loggning

```typescript
import { log } from '@graphprotocol/graph-ts'
import { log } from "@graphprotocol/graph-ts";
```

The `log` API allows subgraphs to log information to the Graph Node standard output as well as Graph Explorer. Messages can be logged using different log levels. A basic format string syntax is provided to compose log messages from argument.
Expand All @@ -595,7 +598,11 @@ The `log` API includes the following functions:
The `log` API takes a format string and an array of string values. It then replaces placeholders with the string values from the array. The first `{}` placeholder gets replaced by the first value in the array, the second `{}` placeholder gets replaced by the second value and so on.

```typescript
log.info('Message to be displayed: {}, {}, {}', [value.toString(), anotherValue.toString(), 'already a string'])
log.info("Message to be displayed: {}, {}, {}", [
value.toString(),
anotherValue.toString(),
"already a string",
]);
```

#### Loggning av ett eller flera värden
Expand All @@ -618,11 +625,11 @@ export function handleSomeEvent(event: SomeEvent): void {
I exemplet nedan loggas endast det första värdet i argument arrayen, trots att arrayen innehåller tre värden.

```typescript
let myArray = ['A', 'B', 'C']
let myArray = ["A", "B", "C"];

export function handleSomeEvent(event: SomeEvent): void {
// Visar : "Mitt värde är: A" (Även om tre värden skickas till `log.info`)
log.info('Mitt värde är: {}', myArray)
log.info("Mitt värde är: {}", myArray);
}
```

Expand All @@ -631,11 +638,14 @@ export function handleSomeEvent(event: SomeEvent): void {
Each entry in the arguments array requires its own placeholder `{}` in the log message string. The below example contains three placeholders `{}` in the log message. Because of this, all three values in `myArray` are logged.

```typescript
let myArray = ['A', 'B', 'C']
let myArray = ["A", "B", "C"];

export function handleSomeEvent(event: SomeEvent): void {
// Visar: "Mitt första värde är: A, andra värdet är: B, tredje värdet är: C"
log.info('My first value is: {}, second value is: {}, third value is: {}', myArray)
log.info(
"My first value is: {}, second value is: {}, third value is: {}",
myArray
);
}
```

Expand All @@ -646,7 +656,7 @@ För att visa ett specifikt värde i arrayen måste det indexeras och tillhandah
```typescript
export function handleSomeEvent(event: SomeEvent): void {
// Visar : "Mitt tredje värde är C"
log.info('My third value is: {}', [myArray[2]])
log.info("My third value is: {}", [myArray[2]]);
}
```

Expand All @@ -655,21 +665,21 @@ export function handleSomeEvent(event: SomeEvent): void {
I exemplet nedan loggas blocknummer, blockhash och transaktionshash från en händelse:

```typescript
import { log } from '@graphprotocol/graph-ts'
import { log } from "@graphprotocol/graph-ts";

export function handleSomeEvent(event: SomeEvent): void {
log.debug('Block number: {}, block hash: {}, transaction hash: {}', [
log.debug("Block number: {}, block hash: {}, transaction hash: {}", [
event.block.number.toString(), // "47596000"
event.block.hash.toHexString(), // "0x..."
event.transaction.hash.toHexString(), // "0x..."
])
]);
}
```

### IPFS API

```typescript
import { ipfs } from '@graphprotocol/graph-ts'
import { ipfs } from "@graphprotocol/graph-ts"
```

Smart contracts occasionally anchor IPFS files on chain. This allows mappings to obtain the IPFS hashes from the contract and read the corresponding files from IPFS. The file data will be returned as `Bytes`, which usually requires further processing, e.g. with the `json` API documented later on this page.
Expand All @@ -678,45 +688,45 @@ För att läsa en fil från IPFS med en given IPFS-hash eller sökväg görs fö

```typescript
// Placera detta i en händelsehanterare i mappningen
let hash = 'QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D'
let data = ipfs.cat(hash)
let hash = "QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D";
let data = ipfs.cat(hash);

// Sökvägar som `QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/Makefile`
// som inkluderar filer i kataloger stöds också
let path = 'QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/Makefile'
let data = ipfs.cat(path)
let path = "QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/Makefile";
let data = ipfs.cat(path);
```

**Note:** `ipfs.cat` is not deterministic at the moment. If the file cannot be retrieved over the IPFS network before the request times out, it will return `null`. Due to this, it's always worth checking the result for `null`.

It is also possible to process larger files in a streaming fashion with `ipfs.map`. The function expects the hash or path for an IPFS file, the name of a callback, and flags to modify its behavior:

```typescript
import { JSONValue, Value } from '@graphprotocol/graph-ts'
import { JSONValue, Value } from "@graphprotocol/graph-ts";

export function processItem(value: JSONValue, userData: Value): void {
// Se JSONValue-dokumentationen för mer information om hur man hanterar
// med JSON-värden
let obj = value.toObject()
let id = obj.get('id')
let title = obj.get('title')
let obj = value.toObject();
let id = obj.get("id");
let title = obj.get("title");

if (!id || !title) {
return
return;
}

// Callbacks kan också skapa enheter
let newItem = new Item(id)
newItem.title = title.toString()
newitem.parent = userData.toString() // Ange parent till "parentId"
newitem.save()
let newItem = new Item(id);
newItem.title = title.toString();
newitem.parent = userData.toString(); // Ange parent till "parentId"
newitem.save();
}

// Placera detta i en händelsehanterare i mappningen
ipfs.map('Qm...', 'processItem', Value.fromString('parentId'), ['json'])
ipfs.map("Qm...", "processItem", Value.fromString("parentId"), ["json"]);

// Alternativt kan du använda `ipfs.mapJSON`.
ipfs.mapJSON('Qm...', 'processItem', Value.fromString('parentId'))
ipfs.mapJSON("Qm...", "processItem", Value.fromString("parentId"));
```

The only flag currently supported is `json`, which must be passed to `ipfs.map`. With the `json` flag, the IPFS file must consist of a series of JSON values, one value per line. The call to `ipfs.map` will read each line in the file, deserialize it into a `JSONValue` and call the callback for each of them. The callback can then use entity operations to store data from the `JSONValue`. Entity changes are stored only when the handler that called `ipfs.map` finishes successfully; in the meantime, they are kept in memory, and the size of the file that `ipfs.map` can process is therefore limited.
Expand All @@ -726,7 +736,7 @@ On success, `ipfs.map` returns `void`. If any invocation of the callback causes
### Crypto API

```typescript
import { crypto } from '@graphprotocol/graph-ts'
import { crypto } from "@graphprotocol/graph-ts";
```

The `crypto` API makes a cryptographic functions available for use in mappings. Right now, there is only one:
Expand All @@ -736,7 +746,7 @@ The `crypto` API makes a cryptographic functions available for use in mappings.
### JSON API

```typescript
import { json, JSONValueKind } from '@graphprotocol/graph-ts'
import { json, JSONValueKind } from "@graphprotocol/graph-ts"
```

JSON data can be parsed using the `json` API:
Expand Down

0 comments on commit ee03f97

Please sign in to comment.