Skip to content

Commit

Permalink
chore: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
PainterPuppets committed May 14, 2023
1 parent 5fdff6e commit 2b46199
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@ckb-lumos/base": "0.19.0",
"@ckb-lumos/config-manager": "0.19.0",
"@ckb-lumos/hd": "0.19.0",
"@ckb-lumos/helpers": "0.20.0-alpha.2",
"@ckb-lumos/helpers": "0.19.0",
"@iarna/toml": "2.2.5",
"chalk": "4.1.2",
"enquirer": "2.3.6",
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/builtin-tasks/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { encodeToAddress } from '@ckb-lumos/helpers'
import { Config } from '@ckb-lumos/config-manager'
import { ContractDeployer } from '../contract'
import type { MessageSigner } from '../contract'
import { signMessageByCkbCli } from '../ckbcli'
import { signMessageByCkbCli } from '../ckb-cli'
import { task, subtask } from '../config/config-env'
import { paramTypes } from '../params'
import { getUserConfigPath } from '../project-structure'
Expand All @@ -24,7 +24,7 @@ subtask('contract:deploy')
.addParam('address', 'address of the contract deployer', '', paramTypes.string, false)
.addParam(
'feeRate',
"Per transaction's fee, deployment may involve more than one transaction. default: [1000] shannon/Byte",
"per transaction's fee, deployment may involve more than one transaction. default: [1000] shannons/Byte",
1000,
paramTypes.number,
true,
Expand All @@ -34,17 +34,17 @@ subtask('contract:deploy')
const workspace = (await run('contract:get-workspace')) as string

if (!name) {
throw new Error('Please specify the name of the contract, not support deploying all contracts for now.')
throw new KuaiError(ERRORS.BUILTIN_TASKS.NOT_SPECIFY_CONTRACT)
}

if (!address) {
throw new Error('please specify address of deployer')
throw new KuaiError(ERRORS.BUILTIN_TASKS.NOT_SPECIFY_DEPLOYER_ADDRESS)
}

const conrtactBinPath = path.join(workspace, `build/release/${name}`)

if (!existsSync(conrtactBinPath)) {
throw new KuaiError(ERRORS.BUILTIN_TASKS.UNSUPPORTED_SIGNER, {
throw new KuaiError(ERRORS.BUILTIN_TASKS.CONTRACT_RELEASE_FILE_NOT_FOUND, {
var: name,
})
}
Expand All @@ -69,13 +69,13 @@ subtask('contract:deploy')

const result = await deployer.deploy(conrtactBinPath, address, { feeRate })
console.info('deploy success, txHash: ', result.txHash)
console.info('done')
return result.txHash
})

subtask('contract:sign-message')
.addParam('message', 'message to be signed', '', paramTypes.string, false)
.addParam('address', 'the address of message signer', '', paramTypes.string, false)
.addParam('signer', 'the sign method of signer, default: ckb-cli', 'ckb-cli', paramTypes.string, true)
.addParam('signer', 'signer provider, default: ckb-cli', 'ckb-cli', paramTypes.string, true)
.setAction(async ({ message, address, signer }): Promise<string> => {
if (signer === 'ckb-cli') {
const password = await read({ prompt: `Input ${address}'s password for sign messge by ckb-cli:`, silent: true })
Expand Down Expand Up @@ -112,7 +112,7 @@ interface BuildArgs {

subtask('contract:build')
.addParam('name', 'contract name', '', paramTypes.string, true)
.addParam('release', 'Build contracts in release mode', false, paramTypes.boolean, true)
.addParam('release', 'build contracts in release mode', false, paramTypes.boolean, true)
.setAction(async ({ name, release }: BuildArgs, { run }) => {
const workspace = await run('contract:get-workspace')
execSync(`cd ${workspace} && capsule build${name ? ` --name ${name}` : ''}${release ? ' --release' : ''}`, {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/core/src/config/config-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function loadConfigAndTasks(args: KuaiArguments = {}): Promise<Kuai
}

const config: KuaiConfig = {
...{ networks: DEFAULT_NETWORKDS },
networks: DEFAULT_NETWORKDS,
...userConfig,
kuaiArguments: {
...DEFAULT_KUAI_ARGUMENTS,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export const DEFAULT_NETWORKDS: {
scripts: config.predefined.AGGRON4.SCRIPTS,
},
devnet: {
rpcUrl: 'http://localhost:8114',
rpcUrl: 'http://127.0.0.1:8114',
prefix: 'ckt',
},
'docker-node': {
rpcUrl: 'http://localhost:8114',
rpcUrl: 'http://127.0.0.1:8114',
prefix: 'ckt',
},
}
23 changes: 16 additions & 7 deletions packages/core/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ export type MessageSigner = (message: string, fromInfo: FromInfo) => Promise<Sig
export class ContractDeployer {
#rpc: RPC
#index: Indexer
#config: config.Config
#signer: MessageSigner
#network: {
rpcUrl: string
config: config.Config
}

constructor(
private readonly signer: MessageSigner,
private readonly network: {
signer: MessageSigner,
network: {
rpcUrl: string
config: config.Config
},
) {
this.#signer = signer
this.#rpc = new RPC(network.rpcUrl)
this.#index = new Indexer(network.rpcUrl)
this.#config = network.config
this.#network = network
}

async deploy(
Expand All @@ -45,15 +50,19 @@ export class ContractDeployer {
cellProvider: this.#index,
fromInfo,
scriptBinary: contractBin,
config: this.#config,
config: this.#network.config,
})

let { txSkeleton } = result
const { scriptConfig, typeId } = result

txSkeleton = await commons.common.payFee(txSkeleton, [fromInfo], feeRate, undefined, { config: this.#config })
txSkeleton = await commons.common.payFee(txSkeleton, [fromInfo], feeRate, undefined, {
config: this.#network.config,
})
txSkeleton = commons.common.prepareSigningEntries(txSkeleton)
const signatures = await Promise.all(txSkeleton.signingEntries.map(({ message }) => this.signer(message, fromInfo)))
const signatures = await Promise.all(
txSkeleton.signingEntries.map(({ message }) => this.#signer(message, fromInfo)),
)
const signedTx = sealTransaction(txSkeleton, signatures)
const txHash = await this.#rpc.sendTransaction(signedTx)

Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/errors-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ Please run: npm install --save-dev typescript`,
},
CONTRACT_RELEASE_FILE_NOT_FOUND: {
code: 'CONTRACT_RELEASE_FILE_NOT_FOUND',
message: '%var% contract release file not found, please check conrtact is exists or build it first',
message: '%var% contract release file not found, please check if conrtact exists or build it first',
},
NOT_SPECIFY_CONTRACT: {
code: 'NOT_SPECIFY_CONTRACT',
message: 'Please specify the name of the contract',
},
NOT_SPECIFY_DEPLOYER_ADDRESS: {
code: 'NOT_SPECIFY_DEPLOYER_ADDRESS',
message: 'Please specify address of deploye',
},
},
}

0 comments on commit 2b46199

Please sign in to comment.