|
| 1 | +import NodeService from 'services/node' |
| 2 | +import { ScriptHashType } from 'types/cell-types' |
| 3 | +import Core from '@nervosnetwork/ckb-sdk-core' |
| 4 | +import { SystemScript } from './lock-utils' |
| 5 | +import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' |
| 6 | + |
| 7 | +export default class DaoUtils { |
| 8 | + daoScript: SystemScript |
| 9 | + |
| 10 | + constructor(daoScript: SystemScript) { |
| 11 | + this.daoScript = daoScript |
| 12 | + } |
| 13 | + |
| 14 | + static daoScriptInfo: SystemScript | undefined |
| 15 | + |
| 16 | + static previousURL: string | undefined |
| 17 | + |
| 18 | + static async loadDaoScript(nodeURL: string): Promise<SystemScript> { |
| 19 | + const core = new Core(nodeURL) |
| 20 | + |
| 21 | + const genesisBlock = await core.rpc.getBlockByNumber(BigInt(0)) |
| 22 | + const systemCellTransaction = genesisBlock.transactions[0] |
| 23 | + const daoOutPoint = { |
| 24 | + txHash: systemCellTransaction.hash, |
| 25 | + index: '2' |
| 26 | + } |
| 27 | + |
| 28 | + const daoTypeHash = scriptToHash(systemCellTransaction.outputs[2].type!) |
| 29 | + |
| 30 | + return { |
| 31 | + codeHash: daoTypeHash, |
| 32 | + outPoint: daoOutPoint, |
| 33 | + hashType: ScriptHashType.Type |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + static async daoScript(nodeURL: string = NodeService.getInstance().core.rpc.node.url): Promise<SystemScript> { |
| 38 | + if (DaoUtils.daoScriptInfo && nodeURL === DaoUtils.previousURL) { |
| 39 | + return DaoUtils.daoScriptInfo |
| 40 | + } |
| 41 | + |
| 42 | + DaoUtils.daoScriptInfo = await DaoUtils.loadDaoScript(nodeURL) |
| 43 | + DaoUtils.previousURL = nodeURL |
| 44 | + |
| 45 | + return DaoUtils.daoScriptInfo |
| 46 | + } |
| 47 | + |
| 48 | + static setDaoScript(info: SystemScript) { |
| 49 | + DaoUtils.daoScriptInfo = info |
| 50 | + DaoUtils.previousURL = NodeService.getInstance().core.rpc.node.url |
| 51 | + } |
| 52 | +} |
0 commit comments