Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tea2x committed Jul 15, 2024
0 parents commit 3a7669d
Show file tree
Hide file tree
Showing 18 changed files with 4,379 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# .gitignore
node_modules
.vscode
18 changes: 18 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
MIT License
Copyright (c) 2023 [Your Name]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
194 changes: 194 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
# CKB DAO Transaction Builder
This package provides a set of functions to build, sign, and send Nervos DAO transactions that are compatible with **joyID** lock and **omni** lock. It includes functions for depositing, withdrawing, and unlocking DAO cells. The package is designed to work with the CKB blockchain and its associated tools.

## Installation
To install this package, run the following command in your terminal:

```bash
npm install joy-dao
```

## Usage
### Importing the Package
Before using the package, you need to add a config.ts file to your project main directory:
```json
export const ISMAINNET = false;

export const NODE_URL = ISMAINNET
? "https://mainnet.ckb.dev/"
: "https://testnet.ckb.dev/";
export const INDEXER_URL = ISMAINNET
? "https://mainnet.ckb.dev/indexer"
: "https://testnet.ckb.dev/indexer";

export const JOYID_CELLDEP = ISMAINNET
? {
codeHash:
"0xd00c84f0ec8fd441c38bc3f87a371f547190f2fcff88e642bc5bf54b9e318323",
hashType: "type",
outPoint: {
txHash:
"0xf05188e5f3a6767fc4687faf45ba5f1a6e25d3ada6129dae8722cb282f262493",
index: "0x0",
},
depType: "depGroup",
}
: {
codeHash:
"0xd23761b364210735c19c60561d213fb3beae2fd6172743719eff6920e020baac",
hashType: "type",
outPoint: {
txHash:
"0x4dcf3f3b09efac8995d6cbee87c5345e812d310094651e0c3d9a730f32dc9263",
index: "0x0",
},
depType: "depGroup",
};

export const OMNILOCK_CELLDEP = ISMAINNET
? {
codeHash:
"0x9b819793a64463aed77c615d6cb226eea5487ccfc0783043a587254cda2b6f26",
hashType: "type",
outPoint: {
txHash:
"0xdfdb40f5d229536915f2d5403c66047e162e25dedd70a79ef5164356e1facdc8",
index: "0x0",
},
depType: "code",
}
: {
codeHash:
"0xf329effd1c475a2978453c8600e1eaf0bc2087ee093c3ee64cc96ec6847752cb",
hashType: "type",
outPoint: {
txHash:
"0xec18bf0d857c981c3d1f4e17999b9b90c484b303378e94de1a57b0872f5d4602",
index: "0x0",
},
depType: "code",
};

export const NETWORK_CONFIG = ISMAINNET
? {
PREFIX: "ckb",
SCRIPTS: {
SECP256K1_BLAKE160: {
CODE_HASH:
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
HASH_TYPE: "type",
TX_HASH:
"0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c",
INDEX: "0x0",
DEP_TYPE: "depGroup",
},
SECP256K1_BLAKE160_MULTISIG: {
CODE_HASH:
"0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8",
HASH_TYPE: "type",
TX_HASH:
"0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c",
INDEX: "0x1",
DEP_TYPE: "depGroup",
},
DAO: {
CODE_HASH:
"0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
HASH_TYPE: "type",
TX_HASH:
"0xe2fb199810d49a4d8beec56718ba2593b665db9d52299a0f9e6e75416d73ff5c",
INDEX: "0x2",
DEP_TYPE: "code",
},
},
}
: {
PREFIX: "ckt",
SCRIPTS: {
SECP256K1_BLAKE160: {
CODE_HASH:
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
HASH_TYPE: "type",
TX_HASH:
"0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37",
INDEX: "0x0",
DEP_TYPE: "depGroup",
SHORT_ID: 0,
},
SECP256K1_BLAKE160_MULTISIG: {
CODE_HASH:
"0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8",
HASH_TYPE: "type",
TX_HASH:
"0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37",
INDEX: "0x1",
DEP_TYPE: "depGroup",
SHORT_ID: 1,
},
DAO: {
CODE_HASH:
"0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
HASH_TYPE: "type",
TX_HASH:
"0x8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f",
INDEX: "0x2",
DEP_TYPE: "code",
},
},
};

// shannon per KB
export const FEE_RATE = ISMAINNET ? 2000 : 1500;
export const MIN_FEE_RATE = 1000;
// 104CKB for joyidLock/moniLock + DAO cell
export const DAO_MINIMUM_CAPACITY = 104;
// 63CKB for joyidLock/moniLock cell
export const MINIMUM_CHANGE_CAPACITY = 63;
export const CKB_SHANNON_RATIO = 100_000_000;

// TODO because payFeeByFeeRate doesn't fully support joyID
export const JOYID_SIGNATURE_PLACEHOLDER_DEFAULT = "0x" + "0".repeat(1000);

export const EXPLORER_PREFIX = ISMAINNET
? "https://explorer.nervos.org/transaction/"
: "https://pudge.explorer.nervos.org/transaction/";

export const COTA_AGGREGATOR_URL = ISMAINNET
? "https://cota.nervina.dev/mainnet-aggregator"
: "https://cota.nervina.dev/aggregator";

```
You might want to just adjust `FEE_RATE`, `MIN_FEE_RATE`, `DAO_MINIMUM_CAPACITY` and `MINIMUM_CHANGE_CAPACITY`

To use the package, you need to import it in your project. Here is an example of how to do this:
```javascript
import { collectDeposits, collectWithdrawals, buildDepositTransaction, buildWithdrawTransaction, buildUnlockTransaction, batchDaoCells } from 'joy-dao';
```

### Collecting Deposits and Withdrawals
You can use the collectDeposits and collectWithdrawals functions to fetch DAO deposits and withdrawals for a given CKB address.
```javascript
const deposits = await collectDeposits('your-ckb-address');
const withdrawals = await collectWithdrawals('your-ckb-address');
```

### Building Transactions
The package provides functions to build deposit, withdrawal, and unlock transactions. Here are some examples:
```javascript
const depositTx = await buildDepositTransaction(ckbAddress, 1000000000n);
const withdrawalTx = await buildWithdrawTransaction(ckbAddress, depositCell);
const unlockTx = await buildUnlockTransaction(ckbAddress, withdrawalCell);
```

### Batch Processing
You can also use the batchDaoCells function to batch deposit, withdrawal, and unlock transactions.
```javascript
const cells = [...]; // An array of deposit cells or withdrawal cells
const batchTx = await batchDaoCells(ckbAddress, cells);
```

### License
This package is licensed under the MIT License. See the LICENSE file for more information.

### Contributing
Contributions are welcome If you find any issues or have suggestions for improvement, please open an issue or submit a pull request.
84 changes: 84 additions & 0 deletions dist/config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
export declare const ISMAINNET = false;
export declare const NODE_URL: string;
export declare const INDEXER_URL: string;
export declare const JOYID_CELLDEP: {
codeHash: string;
hashType: string;
outPoint: {
txHash: string;
index: string;
};
depType: string;
};
export declare const OMNILOCK_CELLDEP: {
codeHash: string;
hashType: string;
outPoint: {
txHash: string;
index: string;
};
depType: string;
};
export declare const NETWORK_CONFIG: {
PREFIX: string;
SCRIPTS: {
SECP256K1_BLAKE160: {
CODE_HASH: string;
HASH_TYPE: string;
TX_HASH: string;
INDEX: string;
DEP_TYPE: string;
SHORT_ID?: undefined;
};
SECP256K1_BLAKE160_MULTISIG: {
CODE_HASH: string;
HASH_TYPE: string;
TX_HASH: string;
INDEX: string;
DEP_TYPE: string;
SHORT_ID?: undefined;
};
DAO: {
CODE_HASH: string;
HASH_TYPE: string;
TX_HASH: string;
INDEX: string;
DEP_TYPE: string;
};
};
} | {
PREFIX: string;
SCRIPTS: {
SECP256K1_BLAKE160: {
CODE_HASH: string;
HASH_TYPE: string;
TX_HASH: string;
INDEX: string;
DEP_TYPE: string;
SHORT_ID: number;
};
SECP256K1_BLAKE160_MULTISIG: {
CODE_HASH: string;
HASH_TYPE: string;
TX_HASH: string;
INDEX: string;
DEP_TYPE: string;
SHORT_ID: number;
};
DAO: {
CODE_HASH: string;
HASH_TYPE: string;
TX_HASH: string;
INDEX: string;
DEP_TYPE: string;
};
};
};
export declare const FEE_RATE: number;
export declare const MIN_FEE_RATE = 1000;
export declare const DAO_MINIMUM_CAPACITY = 104;
export declare const MINIMUM_CHANGE_CAPACITY = 63;
export declare const CKB_SHANNON_RATIO = 100000000;
export declare const JOYID_SIGNATURE_PLACEHOLDER_DEFAULT: string;
export declare const EXPLORER_PREFIX: string;
export declare const COTA_AGGREGATOR_URL: string;
Loading

0 comments on commit 3a7669d

Please sign in to comment.