Skip to content

Commit

Permalink
Merge pull request #518 from nervosnetwork/rc/v0.38.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY authored Nov 27, 2020
2 parents 3439615 + 026d575 commit 9ee93ae
Show file tree
Hide file tree
Showing 21 changed files with 362 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
lerna bootstrap
yarn run docs
- name: Deploy
uses: JamesIves/github-pages-deploy-action@3.5.7
uses: JamesIves/github-pages-deploy-action@3.7.1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: gh-pages
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.38.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.38.0...v0.38.1) (2020-11-27)


### Features

* **core:** upgrade @ckb-lumos/indexer and add the example of sudt transaction ([c10ec0b](https://github.com/nervosnetwork/ckb-sdk-js/commit/c10ec0b95877e67599ee67306b1968934a5b55ca))
* **utils:** update acp lock script ([6d9a85f](https://github.com/nervosnetwork/ckb-sdk-js/commit/6d9a85f05f393216fc06650fd650f43049874a20))





# [0.38.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.37.0...v0.38.0) (2020-11-20)


Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ The rpc module will throw an error when the result contains an error field, you
3. [Send Transaction with multiple private key](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/packages/ckb-sdk-core/examples/sendTransactionWithMultiplePrivateKey.js)
4. [Deposit to and withdraw from Nervos DAO](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/packages/ckb-sdk-core/examples/nervosDAO.js)
5. [Send Transaction with Lumos Collector](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/packages/ckb-sdk-core/examples/sendTransactionWithLumosCollector.js)
6. [SUDT](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/packages/ckb-sdk-core/examples/sudt.js)


# Development Process

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.38.0"
"version": "0.38.1"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
]
},
"optionalDependencies": {
"@ckb-lumos/indexer": "0.4.5"
"@ckb-lumos/indexer": "0.14.0"
},
"devDependencies": {
"@cryptape/sdk-ts-config": "0.0.1",
Expand Down
11 changes: 11 additions & 0 deletions packages/ckb-sdk-core/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.38.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.38.0...v0.38.1) (2020-11-27)


### Features

* **core:** upgrade @ckb-lumos/indexer and add the example of sudt transaction ([c10ec0b](https://github.com/nervosnetwork/ckb-sdk-js/commit/c10ec0b95877e67599ee67306b1968934a5b55ca))





# [0.38.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.37.0...v0.38.0) (2020-11-20)


Expand Down
224 changes: 224 additions & 0 deletions packages/ckb-sdk-core/examples/sudt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/* eslint-disable camelcase */
const path = require('path')
const os = require('os')
/* eslint-disable-next-line */
const { Indexer, CellCollector } = require('@ckb-lumos/indexer')
const { SIMPLE_UDT, ANYONE_CAN_PAY_TESTNET } = require('@nervosnetwork/ckb-sdk-utils/lib/systemScripts')
const CKB = require('../lib').default

const CONFIG = {
privateKey: process.env.PRIVATE_KEY,
ckbUrl: process.env.CKB_URL || 'http://localhost:8114',
lumosDbName: process.env.LUMOS_DB_NAME || 'testnet_lomus_db',
sudtDep: {
codeHash: SIMPLE_UDT.codeHash,
hashType: SIMPLE_UDT.hashType,
outPoint: SIMPLE_UDT.testnetOutPoint,
depType: SIMPLE_UDT.depType,
},
acpDep: {
codeHash: ANYONE_CAN_PAY_TESTNET.codeHash,
hashType: ANYONE_CAN_PAY_TESTNET.hashType,
outPoint: ANYONE_CAN_PAY_TESTNET.testnetOutPoint,
depType: ANYONE_CAN_PAY_TESTNET.depType,
},
}

const CONSTANT = {
sudtCellSize: 142 * 10 ** 8,
acpCellSize: 61 * 10 ** 8,
}

class SudtAccount {
constructor(privateKey = CONFIG.privateKey, ckbUrl = CONFIG.ckbUrl) {
this.ckb = new CKB(ckbUrl)
this.indexer = new Indexer(ckbUrl, path.join(os.tmpdir(), CONFIG.lumosDbName))
this.indexer.start()

const publicKey = this.ckb.utils.privateKeyToPublicKey(privateKey)
const publicKeyHash = `0x${this.ckb.utils.blake160(publicKey, 'hex')}`
this.sender = { privateKey, publicKey, publicKeyHash }
}

getReady = async () => {
await this.ckb.loadDeps()
this.sender.lock = {
codeHash: this.ckb.config.secp256k1Dep.codeHash,
hashType: this.ckb.config.secp256k1Dep.hashType,
args: this.sender.publicKeyHash,
}
}

getCells = async () => {
await this.ckb.loadCells({ indexer: this.indexer, CellCollector, lock: this.sender.lock, save: true })
return this.ckb.cells.get(this.ckb.utils.scriptToHash(this.sender.lock))
}

getSudtCells = async tokenId => {
const cells = []
const collector = new CellCollector(this.indexer, {
lock: {
code_hash: this.sender.lock.codeHash,
hash_type: this.sender.lock.hashType,
args: this.sender.publicKeyHash,
},
type: {
code_hash: CONFIG.sudtDep.codeHash,
hash_type: CONFIG.sudtDep.hashType,
args: tokenId || this.ckb.utils.scriptToHash(this.sender.lock),
},
})
/* eslint-disable-next-line */
for await (const {
cell_output: { lock, type, capacity },
out_point,
data,
} of collector.collect()) {
cells.push({
capacity: BigInt(capacity),
lock: {
codeHash: lock.code_hash,
hashType: lock.hash_type,
args: lock.args,
},
type: {
codeHash: type.code_hash,
hashType: type.hash_type,
args: type.args,
},
outPoint: {
txHash: out_point.tx_hash,
index: out_point.index,
},
data,
sudt: BigInt(`0x${Buffer.from(data.slice(2), 'hex').reverse().toString('hex')}`),
})
}
return cells
}

createAcpCell = async amount => {
const address = this.ckb.utils.privateKeyToAddress(this.sender.privateKey, { prefix: 'ckt' })
const rawTx = this.ckb.generateRawTransaction({
fromAddress: address,
toAddress: address,
capacity: `0x${(BigInt(CONSTANT.acpCellSize) + amount).toString(16)}`,
fee: 100000n,
cells: this.ckb.cells.get(this.ckb.utils.scriptToHash(this.sender.lock)),
deps: [this.ckb.config.secp256k1Dep, CONFIG.acpDep],
})
rawTx.outputs[0].lock = {
codeHash: CONFIG.acpDep.codeHash,
hashType: CONFIG.acpDep.hashType,
args: this.sender.publicKeyHash,
}
const signedTx = this.ckb.signTransaction(this.sender.privateKey)(rawTx)
return this.ckb.rpc.sendTransaction(signedTx)
}

issue = async amount => {
const address = this.ckb.utils.privateKeyToAddress(this.sender.privateKey, { prefix: 'ckt' })
const rawTx = this.ckb.generateRawTransaction({
fromAddress: address,
toAddress: address,
capacity: CONSTANT.sudtCellSize,
fee: 100000n,
cells: this.ckb.cells.get(this.ckb.utils.scriptToHash(this.sender.lock)),
deps: [this.ckb.config.secp256k1Dep, CONFIG.sudtDep],
})

/*
* set the first output cell as the sudt-issuer
*/
rawTx.outputs[0].type = {
codeHash: CONFIG.sudtDep.codeHash,
hashType: CONFIG.sudtDep.hashType,
args: this.ckb.utils.scriptToHash(this.sender.lock),
}
rawTx.outputsData[0] = `0x${Buffer.from(amount.toString(16).padStart(32, '0'), 'hex').reverse().toString('hex')}`
const signedTx = this.ckb.signTransaction(this.sender.privateKey)(rawTx)
return this.ckb.rpc.sendTransaction(signedTx)
}

transfer = async (tokenId, amount, receiverCell) => {
const availableCells = await this.getSudtCells(tokenId)
const inputs = []
let sumSudt = 0n
/* eslint-disable-next-line */
for (const cell of availableCells) {
inputs.push(cell)
sumSudt += cell.sudt
if (amount <= sumSudt) {
/* eslint-disable-next-line */
continue
}
}

if (amount > sumSudt) {
throw new Error(`This account has ${sumSudt} sudt, which is not enough for a transaction of amount ${amount}`)
}

const address = this.ckb.utils.privateKeyToAddress(this.sender.privateKey, { prefix: 'ckt' })

const sudtTypeScript = inputs[0].type

/* transaction skeleton */
const rawTx = this.ckb.generateRawTransaction({
fromAddress: address,
toAddress: address,
capacity: `0x${inputs.reduce((sum, i) => sum + i.capacity, 0n).toString(16)}`,
fee: 0n,
cells: inputs.map(input => ({ ...input, capacity: `0x${input.capacity.toString(16)}` })),
deps: [this.ckb.config.secp256k1Dep, CONFIG.sudtDep],
safeMode: false,
changeThreshold: '0x0',
outputsData: [sumSudt - amount, amount].map(
sudt => `0x${Buffer.from(sudt.toString(16), 'hex').reverse().toString('hex').padEnd(32, '0')}`,
),
})

rawTx.outputs[0].type = sudtTypeScript

/* add receiver */
const fee = 10000n
rawTx.inputs.push({
previousOutput: receiverCell.outPoint,
since: '0x0',
})
rawTx.outputs.push({
lock: receiverCell.lock,
capacity: `0x${(BigInt(receiverCell.capacity) - fee).toString(16)}`,
type: sudtTypeScript,
})
rawTx.witnesses.push('0x')

const signedTx = this.ckb.signTransaction(this.sender.privateKey)(rawTx)
return this.ckb.rpc.sendTransaction(signedTx)
}
}

module.exports = SudtAccount

// const run = async () => {
// const account = new SudtAccount()
// await account.getReady()
// const cells = await account.getCells()

// /* issue sudt */
// const txHash = await account.issue(2000000n * BigInt(10 ** 8))
// console.log(txHash)

// /* get sudt cells */
// const sudtCells = await account.getSudtCells()
// console.log(sudtCells)

// /* transfer */
// const receiverCell = cells.find(cell => !cell.type && cell.data === '0x')
// if (!receiverCell) {
// throw new Error('Please add a secp256k1 cell to receive sudt')
// }
// const txHash = await account.transfer(null, 999n * BigInt(10 ** 8), receiverCell)
// console.log(txHash)
// }

// run()
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.38.0",
"version": "0.38.1",
"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,10 +31,10 @@
"url": "https://github.com/nervosnetwork/ckb-sdk-js/issues"
},
"dependencies": {
"@nervosnetwork/ckb-sdk-rpc": "0.38.0",
"@nervosnetwork/ckb-sdk-utils": "0.38.0",
"@nervosnetwork/ckb-types": "0.38.0",
"@nervosnetwork/ckb-sdk-rpc": "0.38.1",
"@nervosnetwork/ckb-sdk-utils": "0.38.1",
"@nervosnetwork/ckb-types": "0.38.1",
"tslib": "2.0.1"
},
"gitHead": "16e10cb506a1fb98a4b09ab33d3ba13fe122eda5"
"gitHead": "ab1eea0fa1d2aa74c6d037bd6fffd6997b7e0dc5"
}
16 changes: 10 additions & 6 deletions packages/ckb-sdk-core/src/loadCellsFromIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ export const loadCellsFromIndexer = async ({

const cells: RawTransactionParams.Cell[] = []

/* eslint-disable no-restricted-syntax */
for await (const cell of collector.collect()) {
/* eslint-disable no-restricted-syntax, camelcase */
for await (const {
data,
cell_output: { capacity, type },
out_point,
} of collector.collect()) {
cells.push({
data: cell.data,
data,
lock,
type: cell.cell_output.type,
capacity: cell.cell_output.capacity,
outPoint: { txHash: cell.out_point.tx_hash, index: cell.out_point.index },
type: type && { codeHash: type.code_hash, hashType: type.hash_type, args: type.args },
capacity,
outPoint: { txHash: out_point.tx_hash, index: out_point.index },
})
}
/* eslint-enable no-restricted-syntax */
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.38.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.38.0...v0.38.1) (2020-11-27)

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





# [0.38.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.37.0...v0.38.0) (2020-11-20)

**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.38.0",
"version": "0.38.1",
"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.38.0",
"@nervosnetwork/ckb-sdk-utils": "0.38.1",
"axios": "0.19.2",
"tslib": "2.0.1"
},
"devDependencies": {
"@nervosnetwork/ckb-types": "0.38.0"
"@nervosnetwork/ckb-types": "0.38.1"
},
"gitHead": "16e10cb506a1fb98a4b09ab33d3ba13fe122eda5"
"gitHead": "ab1eea0fa1d2aa74c6d037bd6fffd6997b7e0dc5"
}
Loading

0 comments on commit 9ee93ae

Please sign in to comment.