Skip to content

Commit

Permalink
feat: add script metadata in store (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY authored Feb 24, 2023
1 parent 03f2f94 commit 91a5ad3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
41 changes: 41 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions packages/samples/mvp-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
"@ckb-lumos/bi": "0.19.0",
"@ckb-lumos/codec": "0.19.0",
"@ckb-lumos/lumos": "0.19.0",
"@koa/cors": "4.0.0",
"dotenv": "16.0.3",
"http-errors": "2.0.0",
"koa": "2.14.1",
"koa-body": "6.0.1"
},
"devDependencies": {
"@types/koa__cors": "3.3.1",
"typescript": "4.9.4"
}
}
4 changes: 4 additions & 0 deletions packages/samples/mvp-dapp/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ router.get<never, { path: string; address: string }>('/read/:address/:path', asy
router.get<never, { address: string }>('/load/:address', async (ctx) => {
const recordModel = await getRecordModel(ctx.payload.params?.address)
const key = recordModel.getOneOfKey()
if (!key) {
ctx.err('store is not found')
return
}
const data = recordModel.get(key, ['data'])
ctx.ok(data)
})
Expand Down
2 changes: 2 additions & 0 deletions packages/samples/mvp-dapp/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export const DAPP_DATA_PREFIX = bytes.hexify(Buffer.from('mvp-dapp'))

export const DAPP_DATA_PREFIX_LEN = DAPP_DATA_PREFIX.length

export const INITIAL_RECORD_STATE = Buffer.from('{}').toString('hex')

export const TX_FEE = 100000
2 changes: 2 additions & 0 deletions packages/samples/mvp-dapp/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { koaBody } from 'koa-body'
import { initialKuai } from '@ckb-js/kuai-core'
import { config } from '@ckb-lumos/lumos'
import { KoaRouterAdapter, CoR, TipHeaderListener } from '@ckb-js/kuai-io'
import cors from '@koa/cors'
import { router } from './app.controller'
import './type-extends'
import {
Expand Down Expand Up @@ -64,6 +65,7 @@ async function bootstrap() {

const koaRouterAdapter = new KoaRouterAdapter(cor)

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

const server = app.listen(port, host, function () {
Expand Down
13 changes: 4 additions & 9 deletions packages/samples/mvp-dapp/src/omnilock/omnilock.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CellPattern, JSONStore, OutPointString, SchemaPattern, UpdateStorageVal
import { Cell, HexString } from '@ckb-lumos/base'
import { BI } from '@ckb-lumos/bi'
import { InternalServerError } from 'http-errors'
import { DAPP_DATA_PREFIX, TX_FEE } from '../const'
import { DAPP_DATA_PREFIX, INITIAL_RECORD_STATE, TX_FEE } from '../const'

/**
* add business logic in an actor
Expand All @@ -24,7 +24,7 @@ export class OmnilockModel extends JSONStore<Record<string, never>> {
}

get meta(): Record<'capacity', string> {
const cells = this.#filterCells()
const cells = Object.values(this.chainData)
const capacity = cells.reduce((acc, cur) => BigInt(cur.cell.cellOutput.capacity ?? 0) + acc, BigInt(0)).toString()
return {
capacity,
Expand All @@ -36,7 +36,7 @@ export class OmnilockModel extends JSONStore<Record<string, never>> {
outputs: Cell[]
witnesses: string[]
} {
const cells = this.#filterCells()
const cells = Object.values(this.chainData)
let currentTotalCapacity: BI = BI.from(0)
// additional 0.001 ckb for tx fee
const needCapacity = BI.from(capacity).add(TX_FEE)
Expand All @@ -54,7 +54,7 @@ export class OmnilockModel extends JSONStore<Record<string, never>> {
lock: this.lockScript!,
capacity,
},
data: DAPP_DATA_PREFIX,
data: `${DAPP_DATA_PREFIX}${INITIAL_RECORD_STATE}`,
},
{
cellOutput: {
Expand All @@ -67,9 +67,4 @@ export class OmnilockModel extends JSONStore<Record<string, never>> {
witnesses: [],
}
}

#filterCells = () => {
// TODO: is this filter necessary?
return Object.values(this.chainData).filter((v) => this.cellPattern?.(v) ?? true)
}
}

0 comments on commit 91a5ad3

Please sign in to comment.