Skip to content

Commit

Permalink
Merge pull request #395 from nervosnetwork/rc/v0.25.0
Browse files Browse the repository at this point in the history
[ᚬmaster] Rc/v0.25.0
  • Loading branch information
Keith-CY authored Nov 15, 2019
2 parents d17812d + de2b776 commit 9402509
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 129 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [0.25.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.25.0-alpha.0...v0.25.0) (2019-11-16)

**Note:** Version bump only for package ckb-sdk-js





# [0.25.0-alpha.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.25.0-alpha.0...v0.25.0-alpha.1) (2019-11-13)


### Bug Fixes

* **core:** fix the minimal epoch composition in generateDaoWithdrawTx method ([c87fb6a](https://github.com/nervosnetwork/ckb-sdk-js/commit/c87fb6afe901f8b215f34c5d00117af429477596))





# [0.25.0-alpha.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.24.2...v0.25.0-alpha.0) (2019-11-12)


Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

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

The ckb-sdk-js is still under development and NOT production ready. You should get familiar with CKB transaction structure and RPC before using it.

<details>
<summary>ToC</summary>
<p>
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"packages": [
"packages/*"
],
"version": "0.25.0-alpha.0"
"version": "0.25.0"
}
19 changes: 19 additions & 0 deletions packages/ckb-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [0.25.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.25.0-alpha.0...v0.25.0) (2019-11-16)

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





# [0.25.0-alpha.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.25.0-alpha.0...v0.25.0-alpha.1) (2019-11-13)


### Bug Fixes

* **core:** fix the minimal epoch composition in generateDaoWithdrawTx method ([c87fb6a](https://github.com/nervosnetwork/ckb-sdk-js/commit/c87fb6afe901f8b215f34c5d00117af429477596))





# [0.25.0-alpha.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.24.2...v0.25.0-alpha.0) (2019-11-12)


Expand Down
102 changes: 0 additions & 102 deletions packages/ckb-sdk-core/examples/depositAndWithdraw.js

This file was deleted.

137 changes: 137 additions & 0 deletions packages/ckb-sdk-core/examples/nervosDAO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* eslint-disable */
const Core = require('../lib').default
const nodeUrl = process.env.NODE_URL || 'http://localhost:8114' // example node url

const core = new Core(nodeUrl)

const sk = process.env.PRIV_KEY || '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' // example private key
const pk = core.utils.privateKeyToPublicKey(sk)

const pkh = `0x${core.utils.blake160(pk, 'hex')}`
const addr = core.utils.privateKeyToAddress(sk)

const loadCells = async () => {
await core.loadSecp256k1Dep()
const lockHash = core.generateLockHash(
pkh
)
await core.loadCells({
lockHash,
start: BigInt(0),
end: BigInt(1000),
save: true
})
}

const deposit = async () => {
await loadCells()
const depositTx = await core.generateDaoDepositTransaction({
fromAddress: addr,
capacity: BigInt(10200000000),
fee: BigInt(100000)
})
const signed = core.signTransaction(sk)(depositTx)

const txHash = await core.rpc.sendTransaction(signed)
const depositOutPoint = {
txHash,
index: '0x0'
}
console.log(`const depositOutPoint = ${JSON.stringify(depositOutPoint)}`)
}

const depositOutPoint = {
"txHash": "0x052788721f8b2b74490b27e597dea5388f8fd344aa1dfe80a457634cc1335531",
"index": "0x0"
}

const logDepositEpoch = async () => {
const tx = await core.rpc.getTransaction(depositOutPoint.txHash)
if (tx.txStatus.blockHash) {
const b = await core.rpc.getBlock(tx.txStatus.blockHash)
const epoch = b.header.epoch
console.log(`const depositEpoch = ${JSON.stringify(core.utils.parseEpoch(epoch), null, 2)}`)
} else {
console.log('not committed')
}
}

const depositEpoch = {
"length": "0x64",
"index": "0x29",
"number": "0xf2"
}

const starWithdrawing = async () => {
await loadCells()
const tx = await core.generateDaoWithdrawStartTransaction({
outPoint: depositOutPoint,
fee: 10000
})
const signed = core.signTransaction(sk)(tx)
const txHash = await core.rpc.sendTransaction(signed)
const outPoint = {
txHash,
index: '0x0'
}
console.log(`const startWithDrawOutPoint = ${JSON.stringify(outPoint, null, 2)}`)
}

const startWithDrawOutPoint = {
"txHash": "0x84615264f586b21f4a7f29501ff3d8d52674e344960a1d790720fdc87c92570d",
"index": "0x0"
}

const logStartWithdrawingEpoch = async () => {
const tx = await core.rpc.getTransaction(startWithDrawOutPoint.txHash)
if (tx.txStatus.blockHash) {
const b = await core.rpc.getBlock(tx.txStatus.blockHash)
const epoch = b.header.epoch
console.log(`const startWithdrawingEpoch = ${JSON.stringify(core.utils.parseEpoch(epoch), null, 2)}`)
} else {
console.log('not committed')
}
}

const startWithdrawingEpoch = {
"length": "0x64",
"index": "0x2a",
"number": "0xf9"
}

const logCurrentEpoch = async () => {
core.rpc.getTipHeader().then(h => console.log(core.utils.parseEpoch(h.epoch)))
}

const withdraw = async () => {
await core.loadDaoDep()
await core.loadSecp256k1Dep()
await loadCells()
const tx = await core.generateDaoWithdrawTransaction({
depositOutPoint,
withdrawOutPoint: startWithDrawOutPoint,
fee: BigInt(100000)
})
const signed = core.signTransaction(sk)(tx)
const txHash = await core.rpc.sendTransaction(signed)
const outPoint = {
txHash,
index: '0x0'
}
console.log(`const withdraw = ${JSON.stringify(outPoint, null, 2)}`)
}

const withDrawOutPoint = {
"txHash": "0x0cdb8b50d269ad0e82c8b1f2c075ddfb3d3655b6babc6958619bddc09bb4df18",
"index": "0x0"
}


// deposit()
// logDepositEpoch()
// starWithdrawing()
// logStartWithdrawingEpoch()
// logCurrentEpoch()
// withdraw()

// setInterval(logCurrentEpoch, 1000)
4 changes: 2 additions & 2 deletions packages/ckb-sdk-core/examples/sendTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ const bootstrap = async () => {
const rawTransaction = await core.generateRawTransaction({
fromAddress: addresses.testnetAddress,
toAddress,
capacity: 600000000000,
fee: 100000,
capacity: BigInt(600000000000),
fee: BigInt(100000),
safeMode: true,
cells: unspentCells,
deps: core.config.secp256k1Dep,
Expand Down
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.25.0-alpha.0",
"version": "0.25.0",
"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 @@ -31,9 +31,9 @@
"url": "https://github.com/nervosnetwork/ckb-sdk-js/issues"
},
"dependencies": {
"@nervosnetwork/ckb-sdk-rpc": "0.25.0-alpha.0",
"@nervosnetwork/ckb-sdk-utils": "0.25.0-alpha.0",
"@nervosnetwork/ckb-types": "0.25.0-alpha.0"
"@nervosnetwork/ckb-sdk-rpc": "0.25.0",
"@nervosnetwork/ckb-sdk-utils": "0.25.0",
"@nervosnetwork/ckb-types": "0.25.0"
},
"gitHead": "a876b8ea5644dbc27b4ab992f15d09a6a20ede89"
"gitHead": "aea800f907c0dbce98e1f41eadb2e8df0339f7d0"
}
20 changes: 11 additions & 9 deletions packages/ckb-sdk-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,11 @@ class Core {
BigInt(DAO_LOCK_PERIOD_EPOCHS) *
BigInt(DAO_LOCK_PERIOD_EPOCHS)
const minimalSince = this.absoluteEpochSince(
depositEpoch.number + lockEpochs,
depositEpoch.index,
depositEpoch.length
{
length: BigInt(depositEpoch.length),
index: BigInt(depositEpoch.index),
number: BigInt(BigInt(depositEpoch.number) + lockEpochs),
}
)
const outputCapacity = await this.rpc.calculateDaoMaximumWithdraw(depositOutPoint, withdrawBlockHeader.hash)
const targetCapacity = BigInt(outputCapacity)
Expand Down Expand Up @@ -446,7 +448,7 @@ class Core {
inputs: [
{
previousOutput: withdrawOutPoint,
since: minimalSince,
since: `0x${minimalSince.toString(16)}`,
},
],
outputs,
Expand All @@ -459,12 +461,12 @@ class Core {
}
}

private absoluteEpochSince = (length: string, index: string, number: string) => {
private absoluteEpochSince = ({ length, index, number }: {length: bigint, index: bigint, number: bigint}): bigint => {
const epochSince = (BigInt(0x20) << BigInt(56)) +
(BigInt(length) << BigInt(40)) +
(BigInt(index) << BigInt(24)) +
BigInt(number)
return `0x${epochSince.toString(16)}`
(length << BigInt(40)) +
(index << BigInt(24)) +
number
return epochSince
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ckb-sdk-core/src/loadCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const loadCells = async ({
to = getMinBigInt(to, BigInt(tipBlockNumber))

if (to < from) {
throw new Error(`start(${start}) should not be less than end(${end})`)
throw new Error(`start(${start}) should not be less than end(${to})`)
}

const range = to - from
Expand Down
Loading

0 comments on commit 9402509

Please sign in to comment.