Skip to content

Commit

Permalink
updateToken
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Apr 19, 2021
1 parent 837ca54 commit 1ba34e6
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 21 deletions.
107 changes: 91 additions & 16 deletions packages/jellyfish-api-core/__tests__/category/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ describe('masternode', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

async function createToken (symbol: string, metadata?: any): Promise<void> {
const address = await container.call('getnewaddress')
const defaultMetadata = {
symbol,
name: symbol,
isDAT: false,
mintable: true,
tradeable: true,
collateralAddress: address
}
await client.token.createToken({ ...defaultMetadata, ...metadata })
}

beforeAll(async () => {
await container.start()
await container.waitForReady()
Expand Down Expand Up @@ -196,8 +209,8 @@ describe('masternode', () => {
}
})

// TODO(canonbrother): no error throw
it.skip('should be failed while creating token with existing symbol', async () => {
// TODO(canonbrother): no error throw and its able to create same symbol and name
it('should be failed while creating token with existing symbol', async () => {
const address = await container.call('getnewaddress')
const metadata = {
symbol: 'DOA',
Expand All @@ -211,8 +224,8 @@ describe('masternode', () => {
expect(typeof data).toBe('string')

await container.waitForWalletCoinbaseMaturity()
const tokensBefore = await client.token.listTokens()
console.log('tokensBefore: ', tokensBefore)
const tokens = await client.token.listTokens()
console.log('tokens: ', tokens)

// const t: any = async () => {
// await client.token.createToken(metadata)
Expand All @@ -223,26 +236,88 @@ describe('masternode', () => {
await container.waitForWalletCoinbaseMaturity()
const tokensAfter = await client.token.listTokens()
console.log('tokensAfter: ', tokensAfter)
// here listing DOA#130 and DOA#131 but symbol and name are same 'DOA'
} catch (err) {
console.log('err: ', err)
}
})
})

describe('listTokens', () => {
async function createToken (symbol: string, metadata: any): Promise<void> {
const address = await container.call('getnewaddress')
const defaultMetadata = {
symbol,
name: symbol,
isDAT: false,
mintable: true,
tradeable: true,
collateralAddress: address
describe('updateToken', () => {
let tokenDTESTId = ''
let tokenDABCId = ''

beforeAll(async () => {
await createToken('DTEST')
await createToken('DABC')
await container.waitForWalletCoinbaseMaturity()

const tokens = await client.token.listTokens()
for (const k in tokens) {
const token = tokens[k]
if (token.symbol === 'DTEST') {
tokenDTESTId = k
}
if (token.symbol === 'DABC') {
tokenDABCId = k
}
}
await client.token.createToken({ ...defaultMetadata, ...metadata })
}
})

it('should updateToken', async () => {
const { [tokenDTESTId]: tokenBefore } = await client.token.getToken(`DTEST#${tokenDTESTId}`)
expect(tokenBefore.symbol).toBe('DTEST')
expect(tokenBefore.name).toBe('DTEST')
expect(tokenBefore.isDAT).toBe(false)
expect(tokenBefore.mintable).toBe(true)
expect(tokenBefore.tradeable).toBe(true)
expect(tokenBefore.finalized).toBe(false)

const data = await client.token.updateToken(`DTEST#${tokenDTESTId}`, {
symbol: 'DDEST',
name: 'DDEST',
isDAT: true,
mintable: false,
tradeable: false,
finalize: false
})
expect(typeof data).toBe('string')
await container.waitForWalletCoinbaseMaturity()

const { [tokenDTESTId]: tokenAfter } = await client.token.getToken('DDEST')
expect(tokenAfter.symbol).toBe('DDEST')
expect(tokenAfter.name).toBe('DDEST')
expect(tokenAfter.isDAT).toBe(true)
expect(tokenAfter.mintable).toBe(false)
expect(tokenAfter.tradeable).toBe(false)
expect(tokenAfter.finalized).toBe(false)
})

it('should updateToken by token id', async () => {
const { [tokenDABCId]: tokenBefore } = await client.token.getToken(`DABC#${tokenDABCId}`)
expect(tokenBefore.mintable).toBe(true)

await client.token.updateToken(tokenDABCId, { mintable: false })
await container.waitForWalletCoinbaseMaturity()

const { [tokenDABCId]: tokenAfter } = await client.token.getToken(`DABC#${tokenDABCId}`)
expect(tokenAfter.mintable).toBe(false)
})

it('should updateToken by creationTx', async () => {
const { [tokenDABCId]: tokenBefore } = await client.token.getToken(`DABC#${tokenDABCId}`)
expect(tokenBefore.tradeable).toBe(true)

const { creationTx } = tokenBefore
await client.token.updateToken(creationTx, { tradeable: false })
await container.waitForWalletCoinbaseMaturity()

const { [tokenDABCId]: tokenAfter } = await client.token.getToken(`DABC#${tokenDABCId}`)
expect(tokenAfter.tradeable).toBe(false)
})
})

describe('listTokens', () => {
beforeAll(async () => {
await createToken('DBTC', { isDAT: true })
await createToken('DNOTMINT', { mintable: false })
Expand Down
32 changes: 29 additions & 3 deletions packages/jellyfish-api-core/src/category/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ export class Token {
return await this.client.call('createtoken', [{ ...defaultMetadata, ...metadata }, utxos], 'number')
}

/**
* Updates a token with given metadata
*
* @param {string} token symbolKey, id ror creation tx
* @param {UpdateTokenMetadata=} metadata
* @param {string=} metadata.symbol
* @param {string=} metadata.name
* @param {boolean=} metadata.isDAT
* @param {boolean=} metadata.mintable
* @param {boolean=} metadata.tradeable
* @param {boolean=} metadata.finalize
* @return {Promise<string>}
*/
async updateToken (token: string, metadata?: UpdateTokenMetadata): Promise<string> {
return await this.client.call('updatetoken', [token, metadata], 'number')
}

/**
* Returns information about tokens
*
Expand All @@ -58,11 +75,11 @@ export class Token {
/**
* Return information about token
*
* @param {string} symbol
* @param {string} symbolKey
* @return {Promise<TokenResult>}
*/
async getToken (symbol: string): Promise<TokenResult> {
return await this.client.call('gettoken', [symbol], 'number')
async getToken (symbolKey: string): Promise<TokenResult> {
return await this.client.call('gettoken', [symbolKey], 'number')
}
}

Expand Down Expand Up @@ -96,6 +113,15 @@ export interface CreateTokenMetadata {
collateralAddress: string
}

export interface UpdateTokenMetadata {
symbol?: string
name?: string
isDAT?: boolean
mintable?: boolean
tradeable?: boolean
finalize?: boolean
}

export interface CreateTokenUTXO {
txid: string
vout: number
Expand Down
23 changes: 21 additions & 2 deletions website/docs/jellyfish/api/token.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const something = await client.token.method()

## createToken

Returns information about tokens.
Creates a token with given metadata.

```ts title="client.token.createToken()"
interface token {
Expand All @@ -37,6 +37,25 @@ interface CreateTokenUTXO {
}
```

## updateToken

Updates a token with given metadata.

```ts title="client.token.updateToken()"
interface token {
updateToken (token: string, metadata?: UpdateTokenMetadata): Promise<string>
}

interface UpdateTokenMetadata {
symbol?: string
name?: string
isDAT?: boolean
mintable?: boolean
tradeable?: boolean
finalize?: boolean
}
```

## listTokens

Returns information about tokens.
Expand Down Expand Up @@ -87,7 +106,7 @@ Returns information about token.

```ts title="client.token.getToken()"
interface token {
getToken (symbol: string): Promise<TokenResult>
getToken (symbolKey: string): Promise<TokenResult>
}

interface TokenResult {
Expand Down

0 comments on commit 1ba34e6

Please sign in to comment.