-
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(common-scripts): inject from registered locks in dao (#596)
Co-authored-by: Chen Yu <[email protected]>
- Loading branch information
Showing
5 changed files
with
270 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ckb-lumos/common-scripts": minor | ||
--- | ||
|
||
feat: support registered lock in dao operations |
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
142 changes: 142 additions & 0 deletions
142
packages/common-scripts/tests/dao-with-custom-lock.test.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,142 @@ | ||
import test, { afterEach, beforeEach } from "ava"; | ||
import { registerCustomLockScriptInfos } from "../src/common"; | ||
import { TestCellCollector } from "./helper"; | ||
import { | ||
encodeToAddress, | ||
TransactionSkeleton, | ||
TransactionSkeletonType, | ||
} from "@ckb-lumos/helpers"; | ||
import { Cell, Script } from "@ckb-lumos/base"; | ||
import { BI, parseUnit } from "@ckb-lumos/bi"; | ||
import { CellProvider } from "./cell_provider"; | ||
import { dao } from "../src"; | ||
import { Config, predefined } from "@ckb-lumos/config-manager"; | ||
import { hexify } from "@ckb-lumos/codec/lib/bytes"; | ||
import { Uint64 } from "@ckb-lumos/codec/lib/number"; | ||
import { randomBytes } from "node:crypto"; | ||
|
||
const { LINA } = predefined; | ||
|
||
const nonSystemLockCodeHash = "0x" + "aa".repeat(32); | ||
|
||
beforeEach(() => { | ||
registerCustomLockScriptInfos([ | ||
{ | ||
codeHash: nonSystemLockCodeHash, | ||
hashType: "type", | ||
lockScriptInfo: { | ||
CellCollector: TestCellCollector, | ||
async setupInputCell( | ||
txSkeleton: TransactionSkeletonType, | ||
inputCell: Cell | ||
): Promise<TransactionSkeletonType> { | ||
txSkeleton = txSkeleton.update("inputs", (inputs) => | ||
inputs.push(inputCell) | ||
); | ||
|
||
txSkeleton = txSkeleton.update("outputs", (outputs) => | ||
outputs.push(inputCell) | ||
); | ||
|
||
return txSkeleton; | ||
}, | ||
prepareSigningEntries(txSkeleton) { | ||
return txSkeleton; | ||
}, | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
// reset custom lock script infos | ||
afterEach(() => { | ||
registerCustomLockScriptInfos([]); | ||
}); | ||
|
||
test("deposit from the non-system script", async (t) => { | ||
const fromScript: Script = { | ||
codeHash: nonSystemLockCodeHash, | ||
hashType: "type", | ||
args: "0x", | ||
}; | ||
|
||
const toScript: Script = { | ||
codeHash: "0x" + "bb".repeat(32), | ||
hashType: "type", | ||
args: "0x", | ||
}; | ||
const nonSystemLockCell = { | ||
cellOutput: { | ||
capacity: parseUnit("5000000", "ckb").toHexString(), | ||
lock: fromScript, | ||
}, | ||
data: "0x", | ||
}; | ||
let txSkeleton = TransactionSkeleton({ | ||
cellProvider: new CellProvider([nonSystemLockCell]), | ||
}); | ||
|
||
txSkeleton = await dao.deposit( | ||
txSkeleton, | ||
encodeToAddress(fromScript), | ||
encodeToAddress(toScript), | ||
parseUnit("10000", "ckb"), | ||
{ enableNonSystemScript: true } | ||
); | ||
|
||
t.deepEqual(txSkeleton.get("inputs").get(0), nonSystemLockCell); | ||
t.is(txSkeleton.get("outputs").size, 2); | ||
t.deepEqual(txSkeleton.get("outputs").get(0)?.cellOutput, { | ||
capacity: parseUnit("10000", "ckb").toHexString(), | ||
lock: toScript, | ||
type: generateDaoTypeScript(LINA), | ||
}); | ||
t.deepEqual(txSkeleton.get("outputs").get(1)?.cellOutput, { | ||
capacity: BI.from(nonSystemLockCell.cellOutput.capacity) | ||
.sub(parseUnit("10000", "ckb")) | ||
.toHexString(), | ||
lock: fromScript, | ||
type: undefined, | ||
}); | ||
}); | ||
|
||
test("withdraw with registered lock script", async (t) => { | ||
const fromScript: Script = { | ||
codeHash: nonSystemLockCodeHash, | ||
hashType: "type", | ||
args: "0x", | ||
}; | ||
|
||
const nonSystemLockCell: Cell = { | ||
cellOutput: { | ||
capacity: parseUnit("5000000", "ckb").toHexString(), | ||
lock: fromScript, | ||
type: generateDaoTypeScript(LINA), | ||
}, | ||
data: hexify(Uint64.pack(0)), | ||
blockHash: hexify(randomBytes(32)), | ||
blockNumber: "0x123456", | ||
outPoint: { txHash: hexify(randomBytes(32)), index: "0x0" }, | ||
}; | ||
let txSkeleton = TransactionSkeleton({ | ||
cellProvider: new CellProvider([nonSystemLockCell]), | ||
}); | ||
|
||
txSkeleton = await dao.withdraw(txSkeleton, nonSystemLockCell, undefined, { | ||
enableNonSystemScript: true, | ||
}); | ||
|
||
t.deepEqual(txSkeleton.inputs.get(-1), nonSystemLockCell); | ||
t.deepEqual(txSkeleton.outputs.get(-1), { | ||
...nonSystemLockCell, | ||
data: hexify(Uint64.pack(0x123456)), | ||
}); | ||
}); | ||
|
||
const generateDaoTypeScript = (config: Config): Script => { | ||
return { | ||
codeHash: config.SCRIPTS.DAO!.CODE_HASH, | ||
hashType: config.SCRIPTS.DAO!.HASH_TYPE, | ||
args: "0x", | ||
}; | ||
}; |
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
ad483d9
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.
🚀 New canary release:
0.0.0-canary-ad483d9-20240125131247
ad483d9
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-magickbase.vercel.app
lumos-website-git-develop-magickbase.vercel.app