Skip to content

Commit

Permalink
feat(rpc): add the second paramter outputsValidator in the sendTransa…
Browse files Browse the repository at this point in the history
…ction RPC method

The outputValidator should be null, default or passthrough

BREAKING CHANGE: Default outputsValidator on sending transactions requires the lock of outputs to be
the default lock script
  • Loading branch information
Keith-CY committed Jan 31, 2020
1 parent 63d1f57 commit 0c7b7b1
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/ckb-sdk-rpc/__tests__/ckb-rpc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ describe('ckb-rpc success', () => {
expect(header).toEqual(zeroBlockHeader)
})

it('estimate fee rate', async () => {
it.skip('estimate fee rate', async () => {
const feeRate = await rpc.estimateFeeRate('0x11')
expect(feeRate).toBeTruthy()
})
})

describe('send transaction', () => {
describe.skip('send transaction', () => {
it('send transaction', async () => {
const tx = {
cellDeps: [
Expand Down Expand Up @@ -166,7 +166,7 @@ describe('send transaction', () => {
outputsData: ['0x'],
witnesses: [],
}
const hash = rpc.sendTransaction(tx)
const hash = await rpc.sendTransaction(tx)
expect(hash).toBeTruthy()
})
})
Expand Down
18 changes: 18 additions & 0 deletions packages/ckb-sdk-rpc/__tests__/formatters/params.fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,23 @@
{
"expected": false
}
],
"toOutputsValidator": [
{
"param": "default",
"expected": "default"
},
{
"param": "passthrough",
"expected": "passthrough"
},
{
"param": "undefined",
"expected": "undefined"
},
{
"param": "unknown",
"exception": "Outputs validator should be default or passthrough"
}
]
}
4 changes: 2 additions & 2 deletions packages/ckb-sdk-rpc/__tests__/formatters/params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const fixtures = require('./params.fixtures.json')
describe('params formatter', () => {
describe.each(Object.keys(fixtures))('%s', methodName => {
const fixtureTable = Object.values(fixtures[methodName]).map(({ param, expected, exception }) => [
param,
expected,
param === 'undefined' ? undefined : param,
expected === 'undefined' ? undefined : expected,
exception,
])
test.each(fixtureTable)('%j => %j', (param, expected, exception) => {
Expand Down
10 changes: 8 additions & 2 deletions packages/ckb-sdk-rpc/src/defaultRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const defaultRPC: CKBComponents.Method[] = [
{
name: 'sendTransaction',
method: 'send_transaction',
paramsFormatters: [paramsFmts.toRawTransaction],
paramsFormatters: [paramsFmts.toRawTransaction, paramsFmts.toOutputsValidator],
resultFormatters: resultFmts.toHash,
},
{
Expand Down Expand Up @@ -273,9 +273,15 @@ export class DefaultRPC {
* @description rpc to send a new transaction into transaction pool
* @param {object} rawTransaction - a raw transaction includes cell deps, inputs, outputs, version, and witnesses,
* detailed info could be found in ckb-types
* @param {string} [outputsValidator] - Validates the transaction outputs before entering the tx-pool,
* an optional string parameter (enum: default | passthrough ),
* null means using default validator, passthrough means skipping outputs validation
* @return {Promise<string>} transaction hash
*/
public sendTransaction!: (tx: CKBComponents.RawTransaction) => Promise<CKBComponents.Hash>
public sendTransaction!: (
tx: CKBComponents.RawTransaction,
outputsValidator: CKBComponents.OutputsValidator,
) => Promise<CKBComponents.Hash>

/**
* @method getBlockchainInfo
Expand Down
8 changes: 8 additions & 0 deletions packages/ckb-sdk-rpc/src/paramsFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ const formatter = {
return formatter.toNumber(`0x${size.toString(16)}`)
},
toReverseOrder: (reverse: boolean = false) => !!reverse,
toOutputsValidator: (outputsValidator: CKBComponents.OutputsValidator) => {
if (!outputsValidator) return undefined
const VALIDATORS = ['default', 'passthrough']
if (VALIDATORS.indexOf(outputsValidator) > -1) {
return outputsValidator
}
throw new TypeError('Outputs validator should be default or passthrough')
},
}

export default formatter
Expand Down
2 changes: 1 addition & 1 deletion packages/ckb-sdk-rpc/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "es6",
"lib": ["es6", "dom"],
"lib": ["es6"],
"outDir": "./lib",
"typeRoots": ["types", "node_modules/@types", "node_modules"],
"types": ["@nervosnetwork/ckb-types", "@nervosnetwork/ckb-sdk-utils", "rpc"]
Expand Down
1 change: 1 addition & 0 deletions packages/ckb-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ declare namespace CKBComponents {
export type Nonce = string
export type Cycles = string
export type Size = string
export type OutputsValidator = 'default' | 'passthrough' | undefined
export enum TransactionStatus {
Pending = 'pending',
Proposed = 'proposed',
Expand Down

0 comments on commit 0c7b7b1

Please sign in to comment.