-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transaction-manager): pending transaction manager (#517)
- Loading branch information
1 parent
58d4a14
commit a5a9baa
Showing
19 changed files
with
1,135 additions
and
783 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,57 @@ | ||
# `@ckb-lumos/transaction-manager` | ||
|
||
TransactionManager is a tool for manager uncommitted cells, you can `send_transaction` via this tool and get uncommitted outputs by `collector`. | ||
`TransactionManager` offer a simple way to query and cache the pending transactions, it means you can get the pending cells without waiting for the transaction to be confirmed. | ||
|
||
## Usage | ||
## Quick Start | ||
|
||
```javascript | ||
const TransactionManager = require("@ckb-lumos/transaction-manager"); | ||
const { Indexer } = require("@ckb-lumos/ckb-indexer"); | ||
The easiest way to use the module is to use the `RPCTransactionManager` class, which uses the RPC module to query the pending transactions. | ||
|
||
// generate a new `TransactionManager` instance and start. | ||
const indexer = new Indexer("http://127.0.0.1:8114"); | ||
const transactionManager = new TransactionManager(indexer); | ||
transactionManager.start(); | ||
```ts | ||
const indexer = new RPCTransactionManager({ rpcUrl: "https://localhost:8114" }); | ||
const collector = indexer.collector({ lock: aliceLock }); | ||
|
||
// now you send transaction via `transactionManager`. | ||
const txHash = await transactionManager.send_transaction(transaction); | ||
|
||
// you can get uncommitted cells by `transactionManager.collector`. | ||
const collector = transactionManager.collector({ lock }); | ||
for await (const cell of collector.collect()) { | ||
console.log(cell); | ||
// do something with the cell | ||
} | ||
``` | ||
|
||
> Tips: | ||
> | ||
> The `collector` method accepts the same options as the `CkbIndexerQueryOptions` of the `@ckb-lumos/indexer` module, | ||
> but it is a little different from the `CkbIndexerQueryOptions` when querying pending cells. | ||
> | ||
> - `skip` is suppressed when `collector(queryOptions)`, and when `usePendingCells` is set to `true` | ||
> - `fromBlock` and `toBlock` are ignored, pending cells will be returned regardless of the block number. | ||
> - when `order` is set to `desc`, the pending cells will be returned first | ||
## A More Advanced Example | ||
|
||
### Custom Cache Storage | ||
|
||
`TransactionManager` use an in-memory cache to store the pending transactions by default, but you can also use your own cache storage by passing the `storage` options. | ||
|
||
```ts | ||
import { Store } from "@ckb-lumos/transaction-manager"; | ||
// set a prefix to avoid the key conflicts other libraries | ||
const CUSTOM_KEY_PREFIX = "__lumos_store__"; | ||
const storage: Store = { | ||
getItem(key) { | ||
const customKey = CUSTOM_KEY_PREFIX + key; | ||
const value = window.localStorage.getItem(customKey); | ||
if (!value) return value; | ||
// deep clone to avoid the value being modified by the caller | ||
return JSON.parse(value)[customKey]; | ||
}, | ||
hasItem(key) { | ||
return !!window.localStorage.getItem(CUSTOM_KEY_PREFIX + key); | ||
}, | ||
removeItem(key) { | ||
window.localStorage.removeItem(CUSTOM_KEY_PREFIX + key); | ||
}, | ||
setItem(key, value) { | ||
window.localStorage.setItem(CUSTOM_KEY_PREFIX + key, JSON.stringify(value)); | ||
}, | ||
}; | ||
|
||
new RPCTransactionManager({ rpcUrl: "http://localhost:8114", storage }); | ||
``` |
17 changes: 17 additions & 0 deletions
17
packages/transaction-manager/examples/chained-transfer-example/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Chained Transfer Example | ||
|
||
This example shows how to use the transaction manager to collect on-chain cells and pendin cells, also send tansactions to the CKB chain. | ||
|
||
## Usage | ||
|
||
```sh | ||
yarn install / npm install | ||
yarn start / npm run start | ||
``` | ||
|
||
|
||
## Example | ||
|
||
- [block](https://pudge.explorer.nervos.org/block/9233235) | ||
- [transaction 1](https://pudge.explorer.nervos.org/transaction/0x1e1530a08fad64bbb8a9c6b1cff89b9b13355f3935fd458412fb5f56c67bd704) | ||
- [transaction 2](https://pudge.explorer.nervos.org/transaction/0x50d7374811b60d4116645e705fee4a351e71813d723ae72bc05123a2ab154a6f) |
28 changes: 28 additions & 0 deletions
28
packages/transaction-manager/examples/chained-transfer-example/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "chained-transfer-example", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "ts-node src/index.ts", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.4" | ||
}, | ||
"dependencies": { | ||
"@ckb-lumos/base": "0.20.0-alpha.2", | ||
"@ckb-lumos/bi": "0.20.0-alpha.2", | ||
"@ckb-lumos/ckb-indexer": "0.20.0-alpha.2", | ||
"@ckb-lumos/codec": "0.20.0-alpha.2", | ||
"@ckb-lumos/common-scripts": "0.20.0-alpha.2", | ||
"@ckb-lumos/config-manager": "0.20.0-alpha.2", | ||
"@ckb-lumos/hd": "0.20.0-alpha.2", | ||
"@ckb-lumos/helpers": "0.20.0-alpha.2", | ||
"@ckb-lumos/transaction-manager": "0.20.0-alpha.2" | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/transaction-manager/examples/chained-transfer-example/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// TODO move the example to root of the project | ||
import { Hash } from "@ckb-lumos/base"; | ||
import { BI, parseUnit } from "@ckb-lumos/bi"; | ||
import { initializeConfig, predefined } from "@ckb-lumos/config-manager/lib"; | ||
import { | ||
encodeToAddress, | ||
sealTransaction, | ||
TransactionSkeleton, | ||
} from "@ckb-lumos/helpers"; | ||
import { common } from "@ckb-lumos/common-scripts"; | ||
import { key } from "@ckb-lumos/hd"; | ||
import { RPCTransactionManager } from "@ckb-lumos/transaction-manager"; | ||
|
||
const RPC_URL = "https://testnet.ckb.dev"; | ||
const CONFIG = predefined.AGGRON4; | ||
|
||
initializeConfig(CONFIG); | ||
|
||
const SECP256K1_BLAKE160 = CONFIG.SCRIPTS.SECP256K1_BLAKE160!; | ||
|
||
const ALICE_PRIVATE_KEY = | ||
"0x53815fbee34af63e686f5cad7db8074b4b8fd4473617dee2db0ae84d2c6325c4"; | ||
// will be used as sender address | ||
const ALICE_ADDRESS = encodeToAddress({ | ||
codeHash: SECP256K1_BLAKE160.CODE_HASH, | ||
hashType: SECP256K1_BLAKE160.HASH_TYPE, | ||
args: key.privateKeyToBlake160(ALICE_PRIVATE_KEY), | ||
}); | ||
// will be used as recipient address | ||
const BOB_ADDRESS = | ||
"ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqwtu4ea6gdaa69znt2hw3snxkenkrsz2aqe6q45t"; | ||
|
||
const _61Ckb = parseUnit("61", "ckb"); | ||
|
||
const txManager = new RPCTransactionManager({ rpcUrl: RPC_URL }); | ||
|
||
async function transfer(): Promise<Hash> { | ||
let txSkeleton = new TransactionSkeleton({ cellProvider: txManager }); | ||
txSkeleton = await common.transfer( | ||
txSkeleton, | ||
[ALICE_ADDRESS], | ||
BOB_ADDRESS, | ||
BI.from(_61Ckb) | ||
); | ||
txSkeleton = await common.payFeeByFeeRate(txSkeleton, [ALICE_ADDRESS], 1000); | ||
txSkeleton = common.prepareSigningEntries(txSkeleton); | ||
const sig = key.signRecoverable( | ||
txSkeleton.get("signingEntries").get(0)!.message, | ||
ALICE_PRIVATE_KEY | ||
); | ||
const tx = sealTransaction(txSkeleton, [sig]); | ||
return txManager.sendTransaction(tx); | ||
} | ||
|
||
/** | ||
* @description This example shows how to use transaction manager to collect cells and send a transaction. | ||
*/ | ||
async function main() { | ||
let current = 0; | ||
let times = 5; | ||
while (current++ < times) { | ||
const txHash = await transfer(); | ||
console.log(`TxHash${current}:`, txHash); | ||
} | ||
} | ||
|
||
main(); |
109 changes: 109 additions & 0 deletions
109
packages/transaction-manager/examples/chained-transfer-example/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
{ | ||
"compilerOptions": { | ||
/* Visit https://aka.ms/tsconfig to read more about this file */ | ||
|
||
/* Projects */ | ||
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ | ||
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ | ||
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ | ||
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ | ||
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ | ||
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ | ||
|
||
/* Language and Environment */ | ||
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ | ||
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ | ||
// "jsx": "preserve", /* Specify what JSX code is generated. */ | ||
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ | ||
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ | ||
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ | ||
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ | ||
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ | ||
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ | ||
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ | ||
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ | ||
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ | ||
|
||
/* Modules */ | ||
"module": "commonjs", /* Specify what module code is generated. */ | ||
// "rootDir": "./", /* Specify the root folder within your source files. */ | ||
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ | ||
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ | ||
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ | ||
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ | ||
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ | ||
// "types": [], /* Specify type package names to be included without being referenced in a source file. */ | ||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ | ||
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ | ||
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ | ||
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ | ||
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ | ||
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ | ||
// "resolveJsonModule": true, /* Enable importing .json files. */ | ||
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ | ||
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */ | ||
|
||
/* JavaScript Support */ | ||
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ | ||
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ | ||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ | ||
|
||
/* Emit */ | ||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ | ||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */ | ||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ | ||
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */ | ||
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ | ||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ | ||
// "outDir": "./", /* Specify an output folder for all emitted files. */ | ||
// "removeComments": true, /* Disable emitting comments. */ | ||
// "noEmit": true, /* Disable emitting files from a compilation. */ | ||
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ | ||
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ | ||
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ | ||
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ | ||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ | ||
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ | ||
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ | ||
// "newLine": "crlf", /* Set the newline character for emitting files. */ | ||
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ | ||
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ | ||
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ | ||
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ | ||
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */ | ||
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ | ||
|
||
/* Interop Constraints */ | ||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ | ||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ | ||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ | ||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ | ||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ | ||
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ | ||
|
||
/* Type Checking */ | ||
"strict": true, /* Enable all strict type-checking options. */ | ||
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ | ||
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ | ||
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ | ||
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ | ||
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ | ||
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ | ||
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ | ||
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ | ||
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ | ||
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ | ||
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ | ||
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ | ||
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ | ||
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ | ||
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ | ||
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ | ||
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ | ||
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ | ||
|
||
/* Completeness */ | ||
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ | ||
"skipLibCheck": true /* Skip type checking all .d.ts files. */ | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
a5a9baa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
lumos-website – ./
lumos-website.vercel.app
lumos-website-git-develop-magickbase.vercel.app
lumos-website-magickbase.vercel.app