Skip to content

Commit

Permalink
Merge pull request #580 from nervosnetwork/rc/v0.102.2
Browse files Browse the repository at this point in the history
Merge pull request#576from nervosnetwork/master
  • Loading branch information
Keith-CY authored Mar 2, 2022
2 parents 6d56ed8 + 087b9c3 commit 0b09dc6
Show file tree
Hide file tree
Showing 19 changed files with 382 additions and 22 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.102.2](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.101.0...v0.102.2) (2022-03-02)

* **core:** add `calculateDaoMaximumWithdraw` to calculate maximum withdraw capacity based on withdrawing block hash or withdrawing cell outpoint([#578](https://github.com/nervosnetwork/ckb-sdk-js/pull/578))
* **utils:** add `extractDAOData` to deserialize DAO field in block header ([#578](https://github.com/nervosnetwork/ckb-sdk-js/pull/578))
* **utils:** add `calculateMaximumWithdraw` to calculate maximum withdraw capacity offline ([#578](https://github.com/nervosnetwork/ckb-sdk-js/pull/578))





## [0.102.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.101.0...v0.102.1) (2022-02-17)

**Note:** Version bump only for package ckb-sdk-js
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[![License](https://img.shields.io/npm/l/@nervosnetwork/ckb-sdk-core.svg)](./LICENSE)
[![Telegram Group](https://cdn.rawgit.com/Patrolavia/telegram-badge/8fe3382b/chat.svg)](https://t.me/nervos_ckb_dev)
[![SNYK](https://github.com/nervosnetwork/ckb-sdk-js/workflows/SNYK/badge.svg)](https://github.com/nervosnetwork/ckb-sdk-js/actions?query=workflow%3ASNYK)
[![Deploy Docs](https://github.com/nervosnetwork/ckb-sdk-js/workflows/Deploy%20Docs/badge.svg)](https://nervosnetwork.github.io/ckb-sdk-js/classes/ckb.html)
[![Deploy Docs](https://github.com/nervosnetwork/ckb-sdk-js/workflows/Deploy%20Docs/badge.svg)](https://nervosnetwork.github.io/ckb-sdk-js/classes/_nervosnetwork_ckb_sdk_core.default.html)

JavaScript SDK for Nervos [CKB](https://github.com/nervosnetwork/ckb).

Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
],
"npmClient": "yarn",
"useWorkspaces": true,
"version": "0.102.1"
"version": "0.102.2"
}
10 changes: 10 additions & 0 deletions packages/ckb-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.102.2](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.101.0...v0.102.2) (2022-03-02)

### Features

* add `calculateDaoMaximumWithdraw` to calculate maximum withdraw capacity based on withdrawing block hash or withdrawing cell outpoint([#578](https://github.com/nervosnetwork/ckb-sdk-js/pull/578))





## [0.102.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.101.0...v0.102.1) (2022-02-17)

**Note:** Version bump only for package @nervosnetwork/ckb-sdk-core
Expand Down
130 changes: 130 additions & 0 deletions packages/ckb-sdk-core/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,134 @@ describe('ckb', () => {
expect(tx).toEqual(expected)
})
})

describe('calculate dao maximum withdraw', () => {
it('normal withdraw hash', async () => {
ckb.rpc = {
getTransaction: jest.fn().mockResolvedValueOnce({
txStatus: { status: 'committed' },
transaction: {
outputs: [
{
"capacity":"0xe8d4a51000",
"lock":{
"args":"0xf601cac75568afec3b9c9af1e1ff730062007685",
"codeHash":"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType":"type"
},
"type":{
"args":"0x",
"codeHash":"0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
"hashType":"type"
}
}
],
outputsData: ["0x0000000000000000","0x"]
}
}),
getHeader: jest.fn()
.mockResolvedValueOnce({ dao: '0x1aaf2ca6847c223c3ef9e8c069c9250020212a6311e2d30200609349396eb407' })
.mockResolvedValueOnce({ dao: '0x9bafffa73e432e3c94c6f9db34cb25009f9e4efe4b5fd60200ea63c6d4ffb407' })
}
const res = await ckb.calculateDaoMaximumWithdraw({ tx: '', index: '0x0' }, '')
expect(res).toBe('0xe8df95141e')
ckb.rpc = rpc
})
it('another normal withdraw hash', async () => {
ckb.rpc = {
getTransaction: jest.fn().mockResolvedValueOnce({
"transaction":{
"outputs":[
{
"capacity":"0x6fc23ac00",
"lock":{
"args":"0x4cc2e6526204ae6a2e8fcf12f7ad472f41a1606d5b9624beebd215d780809f6aa10000001000000030000000990000009cdfb2824302e0cd0ee1fb4ac9849c8c2348ab84f2e7d2c6e12e8b6f5f5f378d69000000100000003000000031000000deec13a7b8e100579541384ccaf4b5223733e4a5483c3aec95ddc4c1d5ea5b2201340000004cc2e6526204ae6a2e8fcf12f7ad472f41a1606d5b9624beebd215d780809f6a153ab336340f7985b8b9e412b7968fedabd69a9cb0040000000000c0",
"codeHash":"0x5a2506bb68d81a11dcadad4cb7eae62a17c43c619fe47ac8037bc8ce2dd90360",
"hashType":"type"
},
"type":null
},
{
"capacity":"0x83e607de09",
"lock":{
"args":"0x2718f00d61e6fb37eed98cfdf6b30bde38cad8f6",
"codeHash":"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType":"type"
},
"type":null
}
],
"outputsData":[
"0x",
"0x"
],
},
"txStatus":{
"blockHash":"0x8c14f374c5934613dbb0f9cbebb0edaabe259291a08e2dd3afbb291b1b8be359",
"reason":null,
"status":"committed"
}
}),
getHeader: jest.fn()
.mockResolvedValueOnce({ dao: '0x7e9a4d29f532433cb8dbe56b64ce2500b4c9c67a3fccda020098a1f224a4b707' })
.mockResolvedValueOnce({ dao: '0xd87090655733433c2395d67a64ce2500d73e963e54ccda0200f244c504a4b707' })
}
const res = await ckb.calculateDaoMaximumWithdraw({index: '0x0', txHash: '0xabd4f1b9e914cd859cb7ecf4f57009ef7cd2d84a799ed61acff904bdf5fea91a'}, '0x04914c83fa9ea4126279ebe2d2cdff74235f63227821882e4e16f6a908f43691')
expect(res).toBe('0x6fc23ac9b')
ckb.rpc = rpc
})
it('normal withdraw outputpoint', async () => {
ckb.rpc = {
getTransaction: jest.fn().mockResolvedValue({
"transaction":{
"outputs":[
{
"capacity":"0x6fc23ac00",
"lock":{
"args":"0x4cc2e6526204ae6a2e8fcf12f7ad472f41a1606d5b9624beebd215d780809f6aa10000001000000030000000990000009cdfb2824302e0cd0ee1fb4ac9849c8c2348ab84f2e7d2c6e12e8b6f5f5f378d69000000100000003000000031000000deec13a7b8e100579541384ccaf4b5223733e4a5483c3aec95ddc4c1d5ea5b2201340000004cc2e6526204ae6a2e8fcf12f7ad472f41a1606d5b9624beebd215d780809f6a153ab336340f7985b8b9e412b7968fedabd69a9cb0040000000000c0",
"codeHash":"0x5a2506bb68d81a11dcadad4cb7eae62a17c43c619fe47ac8037bc8ce2dd90360",
"hashType":"type"
},
"type":null
},
{
"capacity":"0x83e607de09",
"lock":{
"args":"0x2718f00d61e6fb37eed98cfdf6b30bde38cad8f6",
"codeHash":"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType":"type"
},
"type":null
}
],
"outputsData":[
"0x",
"0x"
],
},
"txStatus":{
"blockHash":"0x8c14f374c5934613dbb0f9cbebb0edaabe259291a08e2dd3afbb291b1b8be359",
"reason":null,
"status":"committed"
}
}),
getHeader: jest.fn()
.mockResolvedValueOnce({ dao: '0x7e9a4d29f532433cb8dbe56b64ce2500b4c9c67a3fccda020098a1f224a4b707' })
.mockResolvedValueOnce({ dao: '0xd87090655733433c2395d67a64ce2500d73e963e54ccda0200f244c504a4b707' })
}
const res = await ckb.calculateDaoMaximumWithdraw(
{index: '0x0', txHash: '0xabd4f1b9e914cd859cb7ecf4f57009ef7cd2d84a799ed61acff904bdf5fea91a'},
{index: '0x0', txHash: '0x04914c83fa9ea4126279ebe2d2cdff74235f63227821882e4e16f6a908f43691' }
)
expect(res).toBe('0x6fc23ac9b')
ckb.rpc = rpc
})
it('exception', async () => {
ckb.rpc = {
getTransaction: jest.fn().mockResolvedValueOnce({ txStatus: {}})
}
await expect(ckb.calculateDaoMaximumWithdraw({ tx: '', index: '0x0' }, '')).rejects.toThrow('Transaction is not committed yet')
ckb.rpc = rpc
})
})
})
10 changes: 5 additions & 5 deletions packages/ckb-sdk-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nervosnetwork/ckb-sdk-core",
"version": "0.102.1",
"version": "0.102.2",
"description": "JavaScript SDK for Nervos Network CKB Project",
"author": "Nervos <[email protected]>",
"homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme",
Expand Down Expand Up @@ -33,10 +33,10 @@
"url": "https://github.com/nervosnetwork/ckb-sdk-js/issues"
},
"dependencies": {
"@nervosnetwork/ckb-sdk-rpc": "0.102.1",
"@nervosnetwork/ckb-sdk-utils": "0.102.1",
"@nervosnetwork/ckb-types": "0.102.1",
"@nervosnetwork/ckb-sdk-rpc": "0.102.2",
"@nervosnetwork/ckb-sdk-utils": "0.102.2",
"@nervosnetwork/ckb-types": "0.102.2",
"tslib": "2.3.1"
},
"gitHead": "78ef8aea8ca68f25528c8eb3aed4506ea4a37241"
"gitHead": "6a6de605956caed395ea9fedd776516b852c8aea"
}
31 changes: 31 additions & 0 deletions packages/ckb-sdk-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,37 @@ class CKB {
}
}

public calculateDaoMaximumWithdraw = async (
depositOutPoint: CKBComponents.OutPoint,
withdraw: CKBComponents.Hash | CKBComponents.OutPoint
): Promise<string> => {
let tx = await this.rpc.getTransaction(depositOutPoint.txHash)
if (tx.txStatus.status !== 'committed') throw new Error('Transaction is not committed yet')
const depositBlockHash = tx.txStatus.blockHash
let celloutput = tx.transaction.outputs[+depositOutPoint.index]
let celloutputData = tx.transaction.outputsData[+depositOutPoint.index]
let withdrawBlockHash: CKBComponents.Hash
if (typeof withdraw === 'string') {
withdrawBlockHash = withdraw
} else {
tx = await this.rpc.getTransaction(withdraw.txHash)
if (tx.txStatus.status !== 'committed') throw new Error('Transaction is not committed yet')
withdrawBlockHash = tx.txStatus.blockHash
celloutput = tx.transaction.outputs[+withdraw.index]
celloutputData = tx.transaction.outputsData[+withdraw.index]
}
const [depositHeader, withDrawHeader] = await Promise.all([
this.rpc.getHeader(depositBlockHash),
this.rpc.getHeader(withdrawBlockHash)
])
return utils.calculateMaximumWithdraw(
celloutput,
celloutputData,
depositHeader.dao,
withDrawHeader.dao
)
}

#secp256k1DepsShouldBeReady = () => {
if (!this.config.secp256k1Dep) {
throw new ParameterRequiredException('Secp256k1 dep')
Expand Down
8 changes: 8 additions & 0 deletions packages/ckb-sdk-rpc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.102.2](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.101.0...v0.102.2) (2022-03-02)

**Note:** Version bump only for package @nervosnetwork/ckb-sdk-rpc





## [0.102.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.101.0...v0.102.1) (2022-02-17)

**Note:** Version bump only for package @nervosnetwork/ckb-sdk-rpc
Expand Down
8 changes: 4 additions & 4 deletions packages/ckb-sdk-rpc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nervosnetwork/ckb-sdk-rpc",
"version": "0.102.1",
"version": "0.102.2",
"description": "RPC module of @nervosnetwork/ckb-sdk-core",
"author": "Nervos <[email protected]>",
"homepage": "https://github.com/nervosnetwork/ckb-sdk-js/packages/ckb-rpc#readme",
Expand Down Expand Up @@ -33,12 +33,12 @@
"url": "https://github.com/nervosnetwork/ckb-sdk-js/issues"
},
"dependencies": {
"@nervosnetwork/ckb-sdk-utils": "0.102.1",
"@nervosnetwork/ckb-sdk-utils": "0.102.2",
"axios": "0.21.4",
"tslib": "2.3.1"
},
"devDependencies": {
"@nervosnetwork/ckb-types": "0.102.1"
"@nervosnetwork/ckb-types": "0.102.2"
},
"gitHead": "78ef8aea8ca68f25528c8eb3aed4506ea4a37241"
"gitHead": "6a6de605956caed395ea9fedd776516b852c8aea"
}
11 changes: 11 additions & 0 deletions packages/ckb-sdk-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.102.2](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.101.0...v0.102.2) (2022-03-02)

### Features

* add `extractDAOData` to deserialize DAO field in block header ([#578](https://github.com/nervosnetwork/ckb-sdk-js/pull/578))
* add `calculateMaximumWithdraw` to calculate maximum withdraw capacity offline ([#578](https://github.com/nervosnetwork/ckb-sdk-js/pull/578))





## [0.102.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.101.0...v0.102.1) (2022-02-17)

**Note:** Version bump only for package @nervosnetwork/ckb-sdk-utils
Expand Down
6 changes: 5 additions & 1 deletion packages/ckb-sdk-utils/__tests__/convertors/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { toUint16Le, toUint32Le, toUint64Le, hexToBytes, bytesToHex } = require('../../lib/convertors')
const { toUint16Le, toUint32Le, toUint64Le, hexToBytes, bytesToHex, toBigEndian } = require('../../lib/convertors')
const { HexStringWithout0xException } = require('../../lib/exceptions')

const {
Expand Down Expand Up @@ -68,3 +68,7 @@ describe('bytes to hex', () => {
expect(bytesToHex(bytes)).toEqual(expected)
})
})

describe('to big endian', () => {
expect(toBigEndian('0x3ef9e8c069c92500')).toBe('0x0025c969c0e8f93e')
})
33 changes: 32 additions & 1 deletion packages/ckb-sdk-utils/__tests__/utils/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { privateKeyToPublicKey, privateKeyToAddress, scriptToHash, rawTransactionToHash } = require('../..')
const { privateKeyToPublicKey, privateKeyToAddress, scriptToHash, rawTransactionToHash, calculateMaximumWithdraw, extractDAOData } = require('../..')
const exceptions = require('../../lib/exceptions')
const rawTransactionToHashFixtures = require('./rawTransactionToHash.fixtures.json')

Expand Down Expand Up @@ -82,3 +82,34 @@ describe('privateKeyToAddress', () => {
}),
).toBe(fixture.testnetAddress)
})

describe('calculate-maximum-withdraw', () => {
const outputCell = {
"capacity":"0xe8d4a51000",
"lock":{
"args":"0xf601cac75568afec3b9c9af1e1ff730062007685",
"codeHash":"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType":"type"
},
"type":{
"args":"0x",
"codeHash":"0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
"hashType":"type"
}
}
expect(
calculateMaximumWithdraw(
outputCell,
'0x0000000000000000',
'0x1aaf2ca6847c223c3ef9e8c069c9250020212a6311e2d30200609349396eb407',
'0x9bafffa73e432e3c94c6f9db34cb25009f9e4efe4b5fd60200ea63c6d4ffb407'
)).toBe('0xe8df95141e')
})

describe('extract header dao', () => {
const DAOData = extractDAOData('0x1aaf2ca6847c223c3ef9e8c069c9250020212a6311e2d30200609349396eb407')
expect(DAOData.c).toBe('0x3c227c84a62caf1a')
expect(DAOData.ar).toBe('0x0025c969c0e8f93e')
expect(DAOData.s).toBe('0x02d3e211632a2120')
expect(DAOData.u).toBe('0x07b46e3949936000')
})
Loading

0 comments on commit 0b09dc6

Please sign in to comment.