Skip to content

Commit

Permalink
feat(sudt): sudt create, update and detail
Browse files Browse the repository at this point in the history
  • Loading branch information
Daryl-L committed Oct 13, 2023
1 parent 95a0924 commit 97057b0
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 72 deletions.
93 changes: 92 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 48 additions & 32 deletions packages/samples/sudt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ method: POST

```javascript
{
"symbol": "USDT",
"name": "USDT",
"amount": "100000",
"decimal": "18",
"description": "",
"website": "",
"icon": ""
"code": 200,
"data": {
"symbol": "USDT",
"name": "USDT",
"account": "", // the args of account in omnilock
"supply": "100000",
"decimal": 18,
"description": "",
"website": "",
"icon": "",
"typeId": "",
"args": "", // the args of sudt type script
"explorerCode": "" // the verify code from explorer
}
}
```

Expand All @@ -53,15 +60,18 @@ method: PUT

```javascript
{
"symbol": "USDT",
"name": "USDT",
"amount": "100000",
"decimal": "18",
"description": "",
"website": "",
"icon": "",
"args": "", // sudt args
"signature": ""
"code": 200,
"data": {
"symbol": "USDT",
"name": "USDT",
"amount": "100000",
"decimal": "18",
"description": "",
"website": "",
"icon": "",
"args": "", // sudt args
"signature": ""
}
}
```

Expand All @@ -84,9 +94,12 @@ method: POST

```javascript
{
"token": "", // token args
"amount": "",
"to": ""
"code": 200,
"data": {
"token": "", // token args
"amount": "",
"to": ""
}
}
```

Expand All @@ -105,17 +118,20 @@ method: GET
#### Response

```javascript
[
{
symbol: 'USDT',
name: 'USDT',
amount: '100000',
decimal: '18',
description: '',
website: '',
icon: '',
},
];
{
"code": 200,
"data": [
{
symbol: 'USDT',
name: 'USDT',
amount: '100000',
decimal: '18',
description: '',
website: '',
icon: '',
},
]
}
```

### Token Detail
Expand Down Expand Up @@ -145,7 +161,7 @@ method: GET

### Asset List

path: /assets
path: /account/:address/assets

method: GET

Expand All @@ -172,7 +188,7 @@ method: GET

### Token Transfer History

path: /token/transfer/history
path: /account/:address/assets/transfer/history

method: GET

Expand Down
7 changes: 5 additions & 2 deletions packages/samples/sudt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"build:watch": "tsc -w",
"start:prod": "node ./dist/main.js",
"test": "jest",
"doc": "typedoc"
"doc": "typedoc",
"typeorm": "typeorm-ts-node-commonjs"
},
"dependencies": {
"@ckb-js/kuai-core": "0.0.1-alpha.2",
Expand All @@ -15,7 +16,9 @@
"@ckb-lumos/lumos": "0.20.0",
"http-errors": "2.0.0",
"koa": "2.14.1",
"koa-body": "6.0.1"
"koa-body": "6.0.1",
"mysql2": "3.6.1",
"typeorm": "0.3.17"
},
"devDependencies": {
"ts-node": "10.9.1",
Expand Down
70 changes: 67 additions & 3 deletions packages/samples/sudt/src/controllers/sudt.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import type { HexString, Hash } from '@ckb-lumos/base'
import { BaseController, Controller, Body, Post } from '@ckb-js/kuai-io'
import { ActorReference } from '@ckb-js/kuai-models'
import { BadRequest } from 'http-errors'
import { BadRequest, NotFound } from 'http-errors'
import { SudtModel, appRegistry } from '../actors'
import { Tx } from '../views/tx.view'
import { getLock } from '../utils'
import { SudtResponse } from '../response'
import { BaseController, Body, Controller, Get, Param, Post, Put } from '@ckb-js/kuai-io'
import { SudtResponse } from '../../response'
import { CreateTokenRequest } from '../dto/create-token.dto'
import { DataSource, QueryFailedError } from 'typeorm'
import { Token } from '../entities/token.entity'
import { Account } from '../entities/account.entity'
import { tokenEntityToDto } from '../dto/token.dto'

@Controller('sudt')
export default class SudtController extends BaseController {
#explorerHost = process.env.EXPLORER_HOST || 'https://explorer.nervos.org'
constructor(private _dataSource: DataSource) {
super()
}

@Post('/send')
async send(
@Body() { from, to, amount, typeArgs }: { from: string[]; to: string; amount: HexString; typeArgs: Hash },
Expand Down Expand Up @@ -39,4 +49,58 @@ export default class SudtController extends BaseController {
)
return SudtResponse.ok(await Tx.toJsonString(result))
}

@Post('/token')
async createToken(@Body() req: CreateTokenRequest) {
let owner = await this._dataSource.getRepository(Account).findOneBy({ address: req.account })
if (!owner) {
owner = await this._dataSource
.getRepository(Account)
.save(this._dataSource.getRepository(Account).create({ address: req.account }))
}

try {
const token = await this._dataSource
.getRepository(Token)
.save(this._dataSource.getRepository(Token).create({ ...req, ownerId: owner.id }))
return new SudtResponse('201', { url: `${this.#explorerHost}/transaction/${token.typeId}` })
} catch (e) {
if (e instanceof QueryFailedError) {
switch (e.driverError.code) {
case 'ER_DUP_ENTRY':
return SudtResponse.err('409', { message: 'Token already exists' })
}
}

console.error(e)
}
}

@Put('/token')
async updateToken(@Body() req: CreateTokenRequest) {
const token = await this._dataSource.getRepository(Token).findOneBy({ typeId: req.typeId })
if (token) {
await this._dataSource.getRepository(Token).save({ ...token, ...req })
}

return new SudtResponse('201', {})
}

@Get('/token/:typeId')
async getToken(@Param('typeId') typeId: string) {
const token = await this._dataSource.getRepository(Token).findOneBy({ typeId })

if (token) {
return SudtResponse.ok(tokenEntityToDto(token, '0', this.#explorerHost))
} else {
throw new NotFound()
}
}

@Get('/tokens')
async listTokens() {
const tokens = await this._dataSource.getRepository(Token).find()

return SudtResponse.ok(tokens.map((token) => tokenEntityToDto(token, '0', this.#explorerHost)))
}
}
17 changes: 17 additions & 0 deletions packages/samples/sudt/src/dto/create-token.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface CreateTokenRequest {
name: string
symbol: string
supply: string
account: string
decimal: number
description: string
website: string
icon: string
typeId: string
explorerCode: string
args: string
}

export interface CreateTokenResponse {
url: string
}
Loading

0 comments on commit 97057b0

Please sign in to comment.