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

chore: fix sudt backend cors & support mysql8 #567

Merged
merged 1 commit into from
Nov 30, 2023
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
47 changes: 47 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/samples/sudt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"http-errors": "2.0.0",
"koa": "2.14.1",
"koa-body": "6.0.1",
"mysql": "npm:[email protected]",
"mysql2": "3.6.1",
"typeorm": "0.3.17"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/samples/sudt/src/actors/sudt.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class SudtModel extends JSONStore<Record<string, never>> {
return true
})
if (currentTotalCapacity.lt(needCapacity)) {
inputs = inputs.concat(omnilock.loadCapacity(needCapacity.sub(currentTotalCapacity)))
inputs = inputs.concat(omnilock.loadCapacity(needCapacity.sub(currentTotalCapacity)).inputs)
}
if (currentTotalSudt.lt(amount)) throw new InternalServerError('not enough sudt balance')

Expand Down
6 changes: 6 additions & 0 deletions packages/samples/sudt/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Koa from 'koa'
import { koaBody } from 'koa-body'
import cors from '@koa/cors'
import { getGenesisScriptsConfig, initialKuai } from '@ckb-js/kuai-core'
import { KoaRouterAdapter, CoR } from '@ckb-js/kuai-io'
import SudtController from './controllers/sudt.controller'
Expand Down Expand Up @@ -34,6 +35,10 @@ const initiateDataSource = async () => {
return dataSource
}

process.on('uncaughtException', (error) => {
console.log(error)
})

export const bootstrap = async () => {
const kuaiCtx = await initialKuai()
const kuaiEnv = kuaiCtx.getRuntimeEnvironment()
Expand Down Expand Up @@ -80,6 +85,7 @@ export const bootstrap = async () => {

const koaRouterAdapter = new KoaRouterAdapter(cor)

app.use(cors())
app.use(koaRouterAdapter.routes())

// while (true) {
Expand Down
24 changes: 14 additions & 10 deletions packages/samples/sudt/src/controllers/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ export class AccountController extends BaseController {
super()
}

async getOrCreateAccount(address: string) {
const repo = this._dataSource.getRepository(Account)
const account = await repo.findOneBy({ address })
if (account) {
return account
}

appRegistry.findOrBind<OmnilockModel>(new ActorReference('omnilock', `/${getLock(address).args}/`))

return repo.save(repo.create({ address }))
}

@Post('/mint/:typeId')
async mint(@Body() { from, to, amount }: MintRequest, @Param('typeId') typeId: string) {
if (!from || from.length === 0 || !to || !amount) {
Expand All @@ -45,12 +57,7 @@ export class AccountController extends BaseController {
if (!address) {
throw new Request('invalid address')
}

const repo = this._dataSource.getRepository(Account)
const account = await repo.findBy({ address })
if (!account) {
repo.save(repo.create({ address }))
}
await this.getOrCreateAccount(address)

const omniLockModel = appRegistry.findOrBind<OmnilockModel>(
new ActorReference('omnilock', `/${getLock(address).args}/`),
Expand Down Expand Up @@ -82,10 +89,7 @@ export class AccountController extends BaseController {
@Get('/:address/assets')
async accountAssets(@Param('address') address: string) {
const tokens = await this._dataSource.getRepository(Token).find()
const account = await this._dataSource.getRepository(Account).findOneBy({ address })
if (!account) {
throw SudtResponse.err(404, 'account not found')
}
const account = await this.getOrCreateAccount(address)

const assets = await this._dataSource.getRepository(Asset).findBy({ accountId: account.id })
const assetsMap = assets.reduce((acc, cur) => {
Expand Down
Loading