Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mintTokens rpc #290

Merged
merged 10 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import { RpcApiError } from '../../../src'

describe('Token', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await setup()
})

afterAll(async () => {
await container.stop()
})

let from: string

async function setup (): Promise<void> {
from = await container.getNewAddress()

await createToken(from, 'DBTC')
await container.generate(1)

await createToken(from, 'DETH')
await container.generate(1)
}

async function createToken (address: string, symbol: string): Promise<void> {
const defaultMetadata = {
symbol,
name: symbol,
isDAT: true,
mintable: true,
tradeable: true,
collateralAddress: address
}
await client.token.createToken({ ...defaultMetadata })
}

it('should mintTokens', async () => {
let tokenBalances = await client.account.getTokenBalances()

expect(tokenBalances.length).toStrictEqual(0)

const data = await client.token.mintTokens('5@DBTC')
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved

expect(typeof data).toStrictEqual('string')
expect(data.length).toStrictEqual(64)

await container.generate(1)

tokenBalances = await client.account.getTokenBalances()

expect(tokenBalances.length).toStrictEqual(1)
expect(tokenBalances[0]).toStrictEqual('5.00000000@1')
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
})

it('should not mintTokens for non-existence token', async () => {
const promise = client.token.mintTokens('5@ETH')

await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow('Invalid Defi token: ETH\', code: 0, method: minttokens')
})

it('should mintTokens with utxos', async () => {
let tokenBalances = await client.account.getTokenBalances()

expect(tokenBalances.length).toStrictEqual(1)

const { txid, vout } = await container.fundAddress(from, 10)

const data = await client.token.mintTokens('5@DETH', [{ txid, vout }])

expect(typeof data).toStrictEqual('string')
expect(data.length).toStrictEqual(64)

await container.generate(1)

tokenBalances = await client.account.getTokenBalances()

expect(tokenBalances.length).toStrictEqual(2)
expect(tokenBalances[1]).toStrictEqual('5.00000000@2')
})
})
29 changes: 22 additions & 7 deletions packages/jellyfish-api-core/src/category/token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ApiClient } from '../.'

type TokenRegexType = `${number}@${string}`

/**
* Token RPCs for DeFi Blockchain
*/
Expand All @@ -20,12 +22,12 @@ export class Token {
* @param {boolean} metadata.mintable default = true
* @param {boolean} metadata.tradeable default = true
* @param {string} metadata.collateralAddress for keeping collateral amount
* @param {CreateTokenUTXO[]} utxos array of specific UTXOs to spend
* @param {UTXO[]} utxos array of specific UTXOs to spend
* @param {string} utxos.txid
* @param {number} utxos.vout
* @return {Promise<string>}
*/
async createToken (metadata: CreateTokenMetadata, utxos: CreateTokenUTXO[] = []): Promise<string> {
async createToken (metadata: CreateTokenMetadata, utxos: UTXO[] = []): Promise<string> {
const defaultMetadata = {
isDAT: false,
mintable: true,
Expand Down Expand Up @@ -81,6 +83,19 @@ export class Token {
async getToken (symbolKey: string): Promise<TokenResult> {
return await this.client.call('gettoken', [symbolKey], 'number')
}

/**
* Creates a transaction to mint tokens.
*
* @param {TokenRegexType} payload
* @param {UTXO[]} [utxos = []]
* @param {string} [utxos.txid]
* @param {number} [utxos.vout]
* @return {Promise<string>}
*/
async mintTokens (payload: TokenRegexType, utxos: UTXO[] = []): Promise<string> {
return await this.client.call('minttokens', [payload, utxos], 'number')
}
}

export interface TokenResult {
Expand Down Expand Up @@ -124,13 +139,13 @@ export interface UpdateTokenMetadata {
finalize?: boolean
}

export interface CreateTokenUTXO {
txid: string
vout: number
}

export interface TokenPagination {
start: number
including_start: boolean
limit: number
}

export interface UTXO {
txid: string
vout: number
}
21 changes: 19 additions & 2 deletions website/docs/jellyfish/api/token.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Creates a token with given metadata.

```ts title="client.token.createToken()"
interface token {
createToken (metadata: CreateTokenMetadata, utxos: CreateTokenUTXO[] = []): Promise<string>
createToken (metadata: CreateTokenMetadata, utxos: UTXO[] = []): Promise<string>
}

interface CreateTokenMetadata {
Expand All @@ -31,7 +31,7 @@ interface CreateTokenMetadata {
collateralAddress: string
}

interface CreateTokenUTXO {
interface UTXO {
txid: string
vout: number
}
Expand Down Expand Up @@ -134,3 +134,20 @@ interface TokenInfo {
collateralAddress: string
}
```

## mintTokens

Creates a transaction to mint tokens.

```ts title="client.token.mintTokens()"
interface token {
mintTokens (payload: TokenRegexType, utxos: UTXO[] = []): Promise<string>
}

type TokenRegexType = `${number}@${string}`

interface UTXO {
txid: string
vout: number
}
```