1.29.1 • Published 11 months ago

tfgrid-api-client v1.29.1

Weekly downloads
244
License
ISC
Repository
-
Last release
11 months ago

TF Grid API client

JavaScript Style Guide NPM version

Client provides all the tooling around tfchain and its modules, including creating entities, twins, farms.. etc

Installation

npm install tfgrid-api-client

API Definition

Available methods:

init

inits the client and returns a promise

const Client = require("tfgrid-api-client");

const url = "urlToWebsocket";
const mnemonic = "some words";

const client = new Client(url, mnemonic);

try {
  await client.init();
} catch (err) {
  return err;
}

createEntity

Creates an entity based on following information:

  • name: name of the entity.
  • countryID: ID of the country where the entity is located
  • cityID: ID of the city where the entity is located
  • callback: optional callback
const name = "foobar";
const countryID = 1;
const cityID = 1;
// This call wont be blocking and will return the block where the tx is included
const block = await client.createEntity(name, countryID, cityID, callback: optional)
console.log(`Transaction included in block with hash: ${block.toHex()}`)

Note: An entity is always linked to a private keypair, only one entity can be created per keypair.

updateEntity

updates an entity based on following information:

  • name: name of the entity.
  • countryID: ID of the country where the entity is located
  • cityID: ID of the city where the entity is located
  • callback: optional callback
// This call wont be blocking and will return the block where the tx is included
const block = await client.updateEntity(name, countryID, cityID, callback: optional)
console.log(`Transaction included in block with hash: ${block.toHex()}`)

getEntityByID

Fetches an entity from storage based on an ID.

const entity = await client.getEntityByID(1);

listEntities

Fetches all entities from storage.

const entity = await client.listEntities();

deleteEntity

Deletes the entity linked to the private key.

await client.deleteEntity(callback: optional)

createTwin

Creates a twin based on following information:

  • relay: a relay server address (optional)
  • pk: the public key of the twins encryption key (optional)
  • callback: optional callback
const relay = "relay.dev.grid.tf" || null;
const pk = "somepublickey" || null;
// This call wont be blocking and will return the block where the tx is included
const block = await client.createTwin(relay, pk, callback: optional)
console.log(`Transaction included in block with hash: ${block.toHex()}`)

Note: A twin is by default anonymous, check addTwinEntity to add an entity to a twin.

getTwinByID

Fetches twin from storage based on an ID.

const twin = await client.getTwinByID(1);

listTwins

Fetches all twins from storage.

const entity = await client.listTwins();

deleteTwin

Deletes a twin from storage based on an ID. Only the creator of this twin can delete this twin.

await client.deleteTwin(1);

addTwinEntity

Add an entity to a twin. The entity that is being added must sign a message composed of the twinID and entityID. Only the twin's owner can add an entity to it's twin.

  • entityID: entity ID to add.
  • twinID: twin ID to update.
  • signature: signature signed by private key of entity
  • callback: optional callback

example:

const entityID = 0;
const twinID = 0;

// the entity that owns this entity can sign this with his private key
const signedMessage = await client.sign(entityID, twinID);

// This call wont be blocking and will return the block where the tx is included
const block = await client.addTwinEntity(
  twinID,
  entityID,
  signedMessage,
  callback
);
console.log(`Transaction included in block with hash: ${block.toHex()}`);

If the signature of the signedMessage is valid, this entity id will be added to this twin.

removeTwinEntity

Removes an entity from a twin. Only the twin's owner can remove an entity from it's twin.

  • entityID: entity ID to remove.
  • twinID: twin ID to update.
  • callback: optional callback

example:

// This call wont be blocking and will return the block where the tx is included
const block = await client.removeTwinEntity(twinID, entityID, callback);
console.log(`Transaction included in block with hash: ${block.toHex()}`);

sign

Sign an entityID and twinID combination and returns a signed message.

  • entityID: entity ID.
  • twinID: twin ID.
const signedMessage = await client.sign(entityID, twinID);

vest

Vest an amount of tokens for a specific duration, if the tft price provided is equal to the real tft price. It unlocks the current and previous vesting months.

locked, perBlock, startingBlock, tftPrice

  • locked: amount of tokens to lock
  • perBlock: amount of tokens that unlock every block (1 block = 6 seconds)
  • startingBlock: block number to start the vesting on
  • tftPrice: price of tft that will trigger unlock condition (decimal number eg: 0.50)
  • callback: optional callback

example:

// This call wont be blocking and will return the block where the tx is included
const block = await client.vest(
  locked,
  perBlock,
  startingBlock,
  tftPrice,
  callback
);
console.log(`Transaction included in block with hash: ${block.toHex()}`);

getPrice

Fetches the TFT Price.

const price = await client.getPrice();

getBalance

Fetches your account's balance.

const balance = await client.getBalance();

tfStoreGet

Set a value in tf key-value store

await client.tfStoreSet("name", "Ashraf", (res) => {
  if (res instanceof Error) {
    console.log(res);
  }
});

tfStoreGet

console.log(await client.tfStoreGet("name"));

Example callback function

// This call will block until status is Finalized and tx is included in a block and validated
await client.createEntity(name, countryID, cityID, (res) => {
  if (res instanceof Error) {
    console.log(res);
    exit(1);
  }

  const { events = [], status } = res;
  console.log(`Current status is ${status.type}`);

  if (status.isFinalized) {
    console.log(`Transaction included at blockHash ${status.asFinalized}`);

    // Loop through Vec<EventRecord> to display all events
    events.forEach(({ phase, event: { data, method, section } }) => {
      console.log(`\t' ${phase}: ${section}.${method}:: ${data}`);
    });
    exit(1);
  }
});
1.29.0

11 months ago

1.29.1

11 months ago

1.21.0

1 year ago

1.25.0

1 year ago

1.22.0

1 year ago

1.26.0

1 year ago

1.23.0

1 year ago

1.27.0

1 year ago

1.24.1

1 year ago

1.24.0

1 year ago

1.18.0

1 year ago

1.19.0

1 year ago

1.19.1

1 year ago

1.17.0

1 year ago

1.14.0

2 years ago

1.15.0

2 years ago

1.10.0

2 years ago

1.12.1

2 years ago

1.12.0

2 years ago

1.16.3

2 years ago

1.16.2

2 years ago

1.16.1

2 years ago

1.16.0

2 years ago

1.16.4

2 years ago

1.11.0

2 years ago

1.13.0

2 years ago

1.6.0

2 years ago

1.9.0

2 years ago

1.5.0

2 years ago

1.8.0

2 years ago

1.4.0

2 years ago

1.7.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

3 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.12

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago

0.0.1-beta.61

3 years ago

0.0.1-beta.62

3 years ago

0.0.1-beta.60

3 years ago

0.0.1-beta.59

3 years ago

0.0.1-beta.58

3 years ago

0.0.1-beta.57

3 years ago

0.0.1-beta.54

3 years ago

0.0.1-beta.53

3 years ago

0.0.1-beta.56

3 years ago

0.0.1-beta.55

3 years ago

0.0.1-beta.52

3 years ago

0.0.1-beta.47

3 years ago

0.0.1-beta.46

3 years ago

0.0.1-beta.49

3 years ago

0.0.1-beta.48

3 years ago

0.0.1-beta.50

3 years ago

0.0.1-beta.51

3 years ago

0.0.1-beta.43

3 years ago

0.0.1-beta.45

3 years ago

0.0.1-beta.42

3 years ago

0.0.1-beta.41

3 years ago

0.0.1-beta.40

3 years ago

0.0.1-beta.39

3 years ago

0.0.1-beta.36

3 years ago

0.0.1-beta.38

3 years ago

0.0.1-beta.37

3 years ago

0.0.1-beta.35

3 years ago

0.0.1-beta.34

3 years ago

0.0.1-beta.33

3 years ago

0.0.1-beta.32

3 years ago

0.0.1-beta.29

3 years ago

0.0.1-beta.28

3 years ago

0.0.1-beta.31

3 years ago

0.0.1-beta.30

3 years ago

0.0.1-beta.27

3 years ago

0.0.1-beta.26

3 years ago

0.0.1-beta.25

3 years ago

0.0.1-beta.24

3 years ago

0.0.1-beta.23

3 years ago

0.0.1-beta.21

3 years ago

0.0.1-beta.20

3 years ago

0.0.1-beta.22

3 years ago

0.0.1-beta.19

3 years ago

0.0.1-beta.18

3 years ago

0.0.1-beta.17

3 years ago

0.0.1-beta.14

3 years ago

0.0.1-beta.13

3 years ago

0.0.1-beta.16

3 years ago

0.0.1-beta.15

3 years ago

0.0.1-beta.11

3 years ago

0.0.1-beta.9

3 years ago

0.0.1-beta.10

3 years ago

0.0.1-beta.8

3 years ago

0.0.1-beta.7

3 years ago

0.0.1-beta.6

3 years ago

0.0.1-beta.5

3 years ago

0.0.1-beta.4

3 years ago

0.0.1-beta.3

3 years ago

0.0.1-beta.2

3 years ago

0.0.1-beta.1

3 years ago

0.0.1-beta.0

3 years ago