# Hathor Tokens

<details>

<summary><mark style="color:purple;">Straight to the code!</mark></summary>

Instantiate the Cryptum SDK first:

```javascript
const sdk = new CryptumSdk({
  environment: 'testnet',
  apiKey: 'YOUR-API-KEY',
})
```

### Create token

Create a new token in Hathor blockchain.

**\*Obs: In Hathor, you will always spend 1% of the amount of tokens you are minting in HTR, that is, to mint 1000 tokens you need to spend 10 HTR.**

**`sdk.token.create(opts)`**

* `opts.protocol` (string)(**required**) - blockchain protocol must be `HATHOR`.
* `opts.wallet` (Wallet)(**required**) - wallet to sign the transaction with.
* `opts.name` (string)(**required**) - token name.
* `opts.symbol` (string)(**required**) - token symbol.
* `opts.amount` (string)(**required**) - token amount to be first minted.
* `opts.mintAuthorityAddress` (string)(**optional**) - wallet address to be the mint authority. Required if you want to mint more tokens later.
* `opts.meltAuthorityAddress` (string)(**optional**) - wallet address to be the melt authority. Required if you want to burn tokens later.

This function returns the transaction hash which is also the token UID (token identifier).

```javascript
const { hash } = await sdk.token.create({
  protocol: 'HATHOR',
  wallet,
  symbol: 'TEST',
  name: 'TEST',
  amount: '1000000',
  mintAuthorityAddress: 'address...',
  meltAuthorityAddress: 'address...',
})
```

### Transfer tokens

Transfer tokens in Hathor blockchain.

**`sdk.token.transfer(opts)`**

* `opts.protocol` (string)(**required**) - blockchain protocol must be `HATHOR`.
* `opts.wallet` (Wallet)(**required**) - wallet to sign the transaction with.
* `opts.token` (string)(**required**) - token UID to transfer or `HTR` if you're transferring the native token HTR.
* `opts.amount` (string)(**required**) - token amount to be transferred.
* `opts.destination` (string)(**optional**) - destination address.

This function returns the transaction hash.

```javascript
const { hash } = await sdk.token.transfer({
  protocol: 'HATHOR',
  wallet,
  token: '00000957924c03a56e773a34...7d7a9f6aceb24efeff',
  destination: 'WmpvgigZ4p...sbK8pyV45WtP',
  amount: '0.1',
})
```

### Mint tokens

Mint Hathor tokens.

**\*Obs: In Hathor, you will always spend 1% of the amount of tokens you are minting in HTR, that is, to mint 1000 tokens you need to spend 10 HTR.**

#### `sdk.token.mint(opts)`

* `opts.protocol` (string)(**required**) - blockchain must be `HATHOR`.
* `opts.wallet` (Wallet)(**required**) - wallet minting tokens.
* `opts.token` (string)(**required**) - token identifier to be minted.
* `opts.amount` (string)(**required**) - token amount to be minted.
* `opts.destination` (string)(**required**) - destination wallet address to receive the minted tokens.
* `opts.mintAuthorityAddress` (string)(**optional**) - wallet address to be the mint authority. Required if you want to mint more tokens later.

This function returns the hash of this minting transaction from the blockchain.

```javascript
// Mint 100 tokens
const { hash } = await sdk.token.mint({
  protocol: 'HATHOR',
  wallet,
  token: '00000...',
  destination: 'WmpvgigZ4pNVLRPW2...sbK8pyV45WtP',
  amount: '100',
  mintAuthorityAddress: 'address...',
})
```

### Burn tokens

Burn (melt) tokens in Hathor blockchain.

#### `sdk.token.burn(opts)`

* `opts.protocol` (string)(**required**) - blockchain must be `HATHOR`.
* `opts.wallet` (Wallet)(**required**) - wallet to sign the transaction with.
* `opts.token` (string)(**required**) - token identifier to be burnt.
* `opts.amount` (string)(**required**) - token amount to be burnt.
* `opts.meltAuthorityAddress` (string)(**optional**) - wallet address to be the melt authority. Required if you want to melt more tokens later.

This function returns hash of the burning transaction from the blockchain.

```javascript
// burn 5 tokens
const { hash } = await sdk.token.burn({
  protocol: 'HATHOR',
  wallet,
  token: '000000...',
  amount: '5',
  meltAuthorityAddress: 'address...',
})
```

### Create token with UTXOs

#### `sdk.transaction.createHathorTokenTransactionFromUTXO(opts)`

* `opts.type` (TransactionType)(**required**) - transaction type. It must be `"HATHOR_TOKEN_CREATION"`.
* `opts.inputs` (array of Input)(**required**) - array of inputs to include in the transaction.
  * `opts.inputs[].txHash` (string) - transaction hash of the UTXO.
  * `opts.inputs[].index` (number) - index of the UTXO output.
  * `opts.inputs[].privateKey` (string) - input private key to sign the transaction with.
* `opts.tokenName` (string)(**required**) - token name.
* `opts.tokenSymbol` (string)(**required**) - token symbol.
* `opts.amount` (string)(**required**) - token amount to be first minted.
* `opts.address` (string)(**required**) - destination wallet address to receive the minted tokens.
* `opts.changeAddress` (string)(**required**) - wallet address to receive the change in HTR if there's any.
* `opts.mintAuthorityAddress` (string)(**optional**) - wallet address to be the mint authority. Required if you want to mint more tokens later.
* `opts.meltAuthorityAddress` (string)(**optional**) - wallet address to be the melt authority. Required if you want to burn tokens later.

```javascript
// Create token transaction, mint 100 tokens and do not want to mint or burn tokens later
const transaction = await sdk.transaction.createHathorTokenTransactionFromUTXO({
  type: TransactionType.HATHOR_TOKEN_CREATION,
  inputs: [
    {
      txHash: 'cf4c5da8b45...3785df8687f55c337299cc38c',
      index: 0,
      privateKey: '696007545...ed03b2af3b900a678318160',
    },
  ],
  tokenSymbol: 'TOK',
  tokenName: 'TOKEN',
  amount: '100',
  address: 'address-1',
  changeAddress: 'address-1',
})
// Create token transaction, mint 3000 tokens and want to mint or burn tokens later
const transaction = await sdk.transaction.createHathorTokenTransactionFromUTXO({
  type: TransactionType.HATHOR_TOKEN_CREATION,
  inputs: [
    {
      txHash: 'cf4c5da8b45...3785df8687f55c337299cc38c',
      index: 0,
      privateKey: '696007545...ed03b2af3b900a678318160',
    },
  ],
  tokenSymbol: 'TOK',
  tokenName: 'TOKEN',
  amount: '3000',
  address: 'address-1',
  changeAddress: 'address-1',
  mintAuthorityAddress: 'address-1',
  meltAuthorityAddress: 'address-2',
})
const { hash } = await sdk.transaction.sendTransaction(transaction)
```

### Mint tokens with UTXOs

#### `sdk.transaction.createHathorTokenTransactionFromUTXO(opts)`

* `opts.type` (TransactionType)(**required**) - transaction type. It must be `"HATHOR_TOKEN_MINT"`.
* `opts.inputs` (array of Input)(**required**) - array of inputs to include in the transaction.
  * `opts.inputs[].txHash` (string) - transaction hash of the UTXO.
  * `opts.inputs[].index` (number) - index of the UTXO output.
  * `opts.inputs[].privateKey` (string) - input private key to sign the transaction with.
* `opts.tokenUid` (string)(**required**) - token uid.
* `opts.amount` (string)(**required**) - token amount to be minted.
* `opts.address` (string)(**required**) - destination wallet address to receive the minted tokens.
* `opts.changeAddress` (string)(**required**) - wallet address to receive the change in HTR if there's any.
* `opts.mintAuthorityAddress` (string)(**optional**) - wallet address to be the mint authority. Required if you want to mint more tokens later.

```javascript
// Create token transaction to mint 100 tokens
const transaction = await sdk.transaction.createHathorTokenTransactionFromUTXO({
  type: TransactionType.HATHOR_TOKEN_MINT,
  inputs: [
    {
      txHash: 'cf4c5da8b45...3785df8687f55c337299cc38c',
      index: 0,
      privateKey: '696007545...ed03b2af3b900a678318160',
    },
  ],
  tokenUid: '0000...00000',
  amount: '100',
  address: 'address-1',
  changeAddress: 'address-1',
  mintAuthorityAddress: 'address-1',
})
const { hash } = await sdk.transaction.sendTransaction(transaction)
```

### Burn tokens with UTXOs

#### `sdk.transaction.createHathorTokenTransactionFromUTXO(opts)`

* `opts.type` (TransactionType)(**required**) - transaction type. It must be `"HATHOR_TOKEN_MELT"`.
* `opts.inputs` (array of Input)(**required**) - array of inputs to include in the transaction.
  * `opts.inputs[].txHash` (string) - transaction hash of the UTXO.
  * `opts.inputs[].index` (number) - index of the UTXO output.
  * `opts.inputs[].privateKey` (string) - input private key to sign the transaction with.
* `opts.tokenUid` (string)(**required**) - token uid.
* `opts.amount` (string)(**required**) - token amount to be minted.
* `opts.address` (string)(**required**) - destination wallet address to receive the HTR tokens.
* `opts.changeAddress` (string)(**required**) - wallet address to receive the change in custom token if there's any.
* `opts.meltAuthorityAddress` (string)(**optional**) - wallet address to be the melt authority. Required if you want to melt more tokens later.

```javascript
// Create token transaction to melt 100 tokens
const transaction = await sdk.transaction.createHathorTokenTransactionFromUTXO({
  type: TransactionType.HATHOR_TOKEN_MELT,
  inputs: [
    {
      txHash: 'cf4c5da8b45...3785df8687f55c337299cc38c',
      index: 0,
      privateKey: '696007545...ed03b2af3b900a678318160',
    },
  ],
  tokenUid: '0000...00000',
  amount: '100',
  address: 'address-1',
  changeAddress: 'address-1',
  meltAuthorityAddress: 'address-1',
})
const { hash } = await sdk.transaction.sendTransaction(transaction)
```

</details>

### **Creating new Token** <a href="#hathor" id="hathor"></a>

#### <mark style="color:purple;">Step 1</mark> <a href="#step-1" id="step-1"></a>

The first prerequisite you will need is the creation of an account and a Project (API Key) on Cryptum Dashboard.

#### <mark style="color:purple;">Step 2</mark> <a href="#step-2" id="step-2"></a>

Supposing you already installed our SDK available on GitHub [here](https://github.com/blockforce-official/cryptum-sdk), all we need to do is to instantiate it like this:

```javascript
const CryptumSdk = require('cryptum-sdk')

const sdk = new CryptumSdk({
  environment: 'testnet',  // 'testnet', 'mainnet'
  apiKey: "YOUR-API-KEY-HERE",
})
```

<mark style="color:purple;">Step 3</mark>

Then you must instantiate the wallet that will be used to create the tokens:

```javascript
const wallet = await sdk.wallet.generateWallet({
    protocol: 'HATHOR',
    mnemonic: 'lorem ipsum dolor sit amet consectetur adipiscing ....'
})
```

{% hint style="info" %}
*Make sure the wallet has sufficient funds to pay for the transactions.*
{% endhint %}

<mark style="color:purple;">Step 4</mark>

The fourth step consists of creating the token passing the wallet variable you just created alongside some other arguments:

```javascript
const { hash } = await sdk.token.create({
    protocol: 'HATHOR',
    wallet,
    symbol: 'TEST',
    name: 'TEST',
    amount: '1000000',
    mintAuthorityAddress: 'ADDRESS', // optional
    meltAuthorityAddress: 'ADDRESS', // optional
})
```

{% hint style="info" %}
`wallet` will receive the newly minted tokens;\
`name` is the token name;\
`symbol` is the token symbol;\
`amount` is the amount to be first minted. All tokens in Hathor always have 2 decimals;\
`mintAuthorityAddress` is a wallet address that has the power to mint more tokens. If it's `null` then this token can no longer be minted;\
`meltAuthorityAddress` is a wallet address that has the power to burn tokens. If it's `null` then this token can no longer be minted;

The `hash` is the transaction hash which is also the token address.
{% endhint %}

{% hint style="info" %}
In Hathor, you will always spend 1% of the amount of tokens you are minting in HTR, that is, for example to mint 1000 tokens you need to spend 10 HTR. If you try to mint an amount of less than 100 tokens, it's still necessary to spend 1 HTR.
{% endhint %}

### Transfer tokens

Transfer any tokens and also the native token HTR.

```javascript
const { hash } = await sdk.token.transfer({
    protocol: 'HATHOR',
    wallet,
    token: 'HTR',
    destination: 'Wmpvgig.....K8pyV45WtP',
    amount: '0.17'
})
```

{% hint style="info" %}
`token` is the token address or `HTR`;\
`destination` is the destination address;\
`amount` is the amount to be transferred;
{% endhint %}

### Mint tokens <a href="#mint-hathor-tokens" id="mint-hathor-tokens"></a>

Mint an existing token in Hathor blockchain.

{% hint style="info" %}
Keep in mind that you need enough HTR tokens to pay for this transaction depending on the minting amount, as already explained above.
{% endhint %}

{% hint style="info" %}
Only the minting authority can mint more tokens. It is required to pass the minting authority every time this function is called to keep on minting more tokens.
{% endhint %}

```javascript
// Mint 100 tokens
const { hash } = await sdk.token.mint({
  protocol: 'HATHOR',
  wallet,
  token: '00000...',
  destination: 'WmpvgigZ4pNVLRPW2...sbK8pyV45WtP',
  amount: '100',
  mintAuthorityAddress: 'address...',
})
```

### Burn tokens <a href="#melt-hathor-tokens" id="melt-hathor-tokens"></a>

Burn (melt) existing tokens in Hathor blockchain.

{% hint style="info" %}
Only the melt authority can burn more tokens. It is required to pass the melt authority every time this function is called to keep on burning more tokens.
{% endhint %}

```javascript
// burn 5 tokens
const { hash } = await sdk.token.burn({
  protocol: 'HATHOR',
  wallet,
  token: '000000...',
  amount: '5',
  meltAuthorityAddress: 'address...',
})
```

If you want to check the full Hathor Network tokens doc, go to:

{% embed url="<https://github.com/blockforce-official/cryptum-sdk/blob/master/docs/tokens/hathor.md>" %}
