@streamr/data-v2
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

DATAv2 token contract

Continuous Integration Audit by LimeChain Audit by Isentropy

NPM package contents

The smart contract files are under /contracts. Here's how to include them in Solidity:

import "@streamr/data-v2/contracts/DATAv2.sol";

Also included: JS/TypeScript utilities to get a nicely typed DATAv2 instance. Here's a sample code for plain node.js:

const { JsonRpcProvider } = require("@ethersproject/providers")
const { formatEther, parseEther } = require("@ethersproject/units")
const { Wallet } = require("@ethersproject/wallet")
const { deployToken } = require("@streamr/data-v2")

const key = process.env.PRIVATE_KEY
const provider = new JsonRpcProvider(process.env.ETHEREUM_URL)
const wallet = new Wallet(key, provider)

async function main() {
    const token = await deployToken(wallet)
    console.log("Before: %s", formatEther(await token.balanceOf(wallet.address)))
    await (await token.grantRole(await token.MINTER_ROLE(), wallet.address)).wait()
    await (await token.mint(wallet.address, parseEther("10"))).wait()
    console.log("After: %s", formatEther(await token.balanceOf(wallet.address)))
}
main().catch(console.error)

And here's another sample code for TypeScript, run with ts-node:

import { JsonRpcProvider } from "@ethersproject/providers"
import { getTokenAt } from "@streamr/data-v2"
import type { DATAv2 } from "@streamr/data-v2"

const provider: JsonRpcProvider = new JsonRpcProvider(process.env.ETHEREUM_URL)
const token: DATAv2 = await getTokenAt(process.env.TOKEN_ADDRESS, provider)
console.log("Symbol:", await token.symbol())

List of files

File Description Deployed
contracts/CrowdsaleToken.sol The current (or "old") DATA token ETH 0x0cf0...23
contracts/DATAv2.sol The new DATA token ETH 0x8f6...b76 BSC 0x0864...2a xDAI 0x256...47D
contracts/DataTokenMigrator.sol Migrator contract that acts as UpgradeAgent for the old token ETH 0xc7...c16c
contracts/IERC677.sol Interface of ERC677 as defined in the LINK token
contracts/IERC677Receiver.sol Interface of ERC677Receiver also defined in the LINK token
contracts/MockRecipient.sol IERC677Receiver implementation for the purpose of testing
test/DATAv2-test.js DATAv2.sol tests
test/DataTokenMigrator-test.js DataTokenMigrator.sol tests
contracts/DATAv2onPolygon.sol * DATAv2 deployed on MATIC/Polygon chain, extended slightly for bridging MATIC 0x3a9...34

* added after the audit

DATAv2 features

Some of the following features are inherited from OpenZeppelin contracts:

Feature Associated function File where the function is
ERC-677 transferAndCall contracts/DATAv2.sol
EIP-2612 permit @openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol
Minting grantRole, revokeRole @openzeppelin/contracts/access/AccessControl.sol
Minting mint contracts/DATAv2.sol
Burning burn, burnFrom @openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol

Minting

DATAv2 has an admin role (controlled by Streamr) that can add and remove minters, who can mint tokens tokens at will. The admin and minters will be contracts that are not controlled by a single wallet.

The whole old DATA supply is minted as part of migration, and new tokens may be minted later as decided by the community process in the form of Streamr Improvement Proposals, see e.g. SIP-6.

ERC677 functionality

DATAv2 follows the convention of LINK and other tokens with regard to the callback function onTokenTransfer invoked by transferAndCall: DATAv2 does not check the bool return value of onTokenTransfer. Instead onTokenTransfer should revert if the transfer is to be rejected by the recipient contract. If the recipient is a contract, it must implement onTokenTransfer or else transferAndCall reverts.

Migration process

  1. We deploy the DATAv2 and DataTokenMigrator contracts
  2. We call grantRole(id('MINTER_ROLE'), minter.address) on the DATAv2 contract
  3. Minter calls mint(migrator.address, tokenSupply) on the DATAv2 contract to mint the tokens belonging to old DATA holders into the DataTokenMigrator contract
  4. We call setUpgradeAgent(migrator.address) in the old DATA contract to start the upgrade
  5. DATA token holders call upgrade(amountWei) in the old DATA contract

Result:

  • Old DATA tokens are burned
  • DATAv2 tokens are transferred from the DataTokenMigrator to the holders

At all times, DATAv2 tokens left in the DataTokenMigrator are the unmigrated DATA tokens, and they should be equal in number to the tokenSupply of the old DATA contract

DATAv2 contract anatomy

OpenZeppelin contracts were used where available to implement the features listed in the above table.

Explanations of the origins and intents of all code found in DATAv2.sol:

Lines Explanation
1...12 OpenZeppelin and interface imports, DATAv2 token properties
16...46 Adapted from @openzeppelin/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol to implement the minting feature. The AccessControl.sol is a bit of a can of worms when it comes to state-space: the "role admin" can set up any constellation of roles and grant those roles and their administration to anyone; its implementation is quite elegant though, so it didn't feel very significantly worse than a "brute" mapping (address => bool) isMinter type of implementation, especially since it's all library code. Added isMinter convenience function.
48...74 Adapted from LINK token to implement the ERC-677 token transfer hook. Recipient smart contracts can now react to DATAv2 token transfer which makes approve+transferFrom two-transaction flow avoidable if not entirely redundant. Decided to go for the simply ERC-677 instead of the ERC-777 because of some misfeatures on ERC-777 (defaultOperators, higher plain transfer gas price) as well as uncertain adoption at this stage.
76...103 Allow the admin to change token symbol and name just in case, e.g. to support token rebranding, and graceful end-of-life after the possible next migration

Dependencies

Readme

Keywords

none

Package Sidebar

Install

npm i @streamr/data-v2

Weekly Downloads

7

Version

1.0.2

License

MIT

Unpacked Size

118 kB

Total Files

11

Last publish

Collaborators

  • samigabor
  • fonty1
  • hpihkala
  • tumppi
  • harbu
  • mariusz_io
  • pzsavola
  • juslesan
  • jtakalai
  • streamr-dev
  • teogeb
  • samt1803