1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-26 19:49:26 +01:00
docs/developers/using-ocean-libraries/mint-datatoken.md

118 lines
3.1 KiB
Markdown
Raw Normal View History

# Mint Datatokens
2022-07-25 12:17:51 +02:00
This tutorial guides you through the process of minting datatokens and sending them to a receiver address. The tutorial assumes that you already have the address of the datatoken contract which is owned by you.
#### Prerequisites
2022-07-25 12:17:51 +02:00
* [Obtain an API key](configuration.md#obtaining-api-key-for-ethereum-node-provider)
* [Set up the .env file](configuration.md#create-a-.env-file)
* [Install the dependencies](configuration.md#setup-dependencies)
* [Create a configuration file](configuration.md#create-a-configuration-file)
2022-07-25 12:17:51 +02:00
#### Create a script to mint datatokens
Create a new file in the same working directory where configuration file (`config.py`/`config.js`) and `.env` files are present, and copy the code as listed below.
2022-07-25 12:17:51 +02:00
{% tabs %}
{% tab title="ocean.js" %}
{% code title="mint_datatoken.js" %}
```javascript
// Import dependencies
2022-07-25 12:17:51 +02:00
const { NftFactory, Datatoken } = require('@oceanprotocol/lib');
const Web3 = require('web3');
const { web3Provider, oceanConfig } = require('./config');
// Create a web3 instance
2022-07-25 12:17:51 +02:00
const web3 = new Web3(web3Provider);
// Change this
const datatokenAddress = "0xD3542e5F56655fb818F9118CE219e1D10751BC82"
const receiverAddress = "0xBE5449a6A97aD46c8558A3356267Ee5D2731ab5e"
// Create a function which will take `datatokenAddress` and `receiverAddress` as parameters
2022-07-25 12:17:51 +02:00
const mintDatatoken = async (datatokenAddress, receiverAddress) => {
const accounts = await web3.eth.getAccounts();
const publisherAccount = accounts[0];
// Create datatoken instance
2022-07-25 12:17:51 +02:00
const datatoken = new Datatoken(web3);
// Get current datatoken balance of receiver
2022-07-25 12:17:51 +02:00
let receiverBalance = await datatoken.balance(
datatokenAddress,
receiverAddress
);
console.log(`Receiver balance before mint: ${receiverBalance}`);
// Mint datatoken
2022-07-25 12:17:51 +02:00
await datatoken.mint(
datatokenAddress,
publisherAccount,
'1',
receiverAddress
);
// Get new datatoken balance of receiver
2022-07-25 12:17:51 +02:00
receiverBalance = await datatoken.balance(
datatokenAddress,
receiverAddress
);
console.log(`Receiver balance after mint: ${receiverBalance}`);
};
// Call mintDatatoken(...) function defined above
2022-07-25 12:17:51 +02:00
mintDatatoken(datatokenAddress, receiverAddress)
.then(() => {
process.exit((err) => {
console.error(err);
process.exit(1);
});
})
.catch((err) => {
console.error(err);
process.exit(1);
});
```
{% endcode %}
**Execute script**
```
node mint_datatoken.js
```
2022-07-25 12:17:51 +02:00
{% endtab %}
{% tab title="ocean.py" %}
{% code title="mint_datatoken.py" %}
```python
# Note: Ensure that .env and config.py are correctly setup
from config import web3_wallet, ocean
# Change this
datatoken_address = "0xD3542e5F56655fb818F9118CE219e1D10751BC82"
receiver_address = "0xBE5449a6A97aD46c8558A3356267Ee5D2731ab5e"
datatoken = ocean.get_datatoken(datatoken_address)
print(f"Balance before mint: {datatoken.balanceOf(receiver_address)}")
# Mint datatokens
2022-07-25 12:17:51 +02:00
datatoken.mint(
account_address=receiver_address,
value=ocean.to_wei("1"),
from_wallet=web3_wallet,
)
print(f"Balance after mint: {datatoken.balanceOf(receiver_address)}")
nt_d
```
{% endcode %}
**Execute script**
```
python mint_datatoken.py
```
2022-07-25 12:17:51 +02:00
{% endtab %}
{% endtabs %}