-
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.
Merge remote-tracking branch 'refs/remotes/origin/develop' into joyid
# Conflicts: # examples/pnpm-lock.yaml
- Loading branch information
Showing
20 changed files
with
1,744 additions
and
532 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
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,21 @@ | ||
# Lumos Works with Solana | ||
|
||
Nervos maintains a powerful lock | ||
called [Omnilock](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0042-omnilock/0042-omnilock.md) (previously named RC lock), which | ||
can use Phantom as a signer. This example will show how to use Lumos to send a transaction using Omnilock and Phantom | ||
|
||
## Quick Start | ||
|
||
> we should [build](..) Lumos project first before we start this example | ||
``` | ||
npm run build | ||
cd examples/omni-lock-solana | ||
npm start | ||
``` | ||
|
||
## Links | ||
|
||
- [Phantom](https://phantom.app/) - A crypto wallet for Solana | ||
- [Nervos Faucet](https://faucet.nervos.org/) - Claim Nervos testnet CKB | ||
- [Omnilock](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0042-omnilock/0042-omnilock.md) - Omnilock intro |
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,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Lumos ❤️ Solana & Phantom</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="index.tsx" type="module"></script> | ||
</body> | ||
</html> |
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,93 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { helpers, Script, config, commons } from "@ckb-lumos/lumos"; | ||
import ReactDOM from "react-dom"; | ||
import { asyncSleep, capacityOf, solana, transfer } from "./lib"; | ||
|
||
const App: React.FC = () => { | ||
const [solanaAddr, setSolanaAddr] = useState(""); | ||
const [omniAddr, setOmniAddr] = useState(""); | ||
const [omniLock, setOmniLock] = useState<Script>(); | ||
const [balance, setBalance] = useState("-"); | ||
|
||
const [transferAddr, setTransferAddress] = useState(""); | ||
const [transferAmount, setTransferAmount] = useState(""); | ||
|
||
const [isSendingTx, setIsSendingTx] = useState(false); | ||
const [txHash, setTxHash] = useState(""); | ||
|
||
useEffect(() => { | ||
asyncSleep(300).then(() => { | ||
if (solana) connectToWallet(); | ||
}); | ||
}, []); | ||
|
||
function connectToWallet() { | ||
solana | ||
.connect() | ||
.then(({ publicKey }) => { | ||
const solanaAddr = publicKey.toBase58(); | ||
const omniLockScript = commons.omnilock.createOmnilockScript({ | ||
auth: { flag: "SOLANA", content: solanaAddr }, | ||
}); | ||
|
||
const omniAddr = helpers.encodeToAddress(omniLockScript); | ||
|
||
setSolanaAddr(solanaAddr); | ||
setOmniAddr(omniAddr); | ||
setOmniLock(omniLockScript); | ||
|
||
return omniAddr; | ||
}) | ||
.then((omniAddr) => capacityOf(omniAddr)) | ||
.then((balance) => setBalance(balance.div(10 ** 8).toString() + " CKB")); | ||
} | ||
|
||
function onTransfer() { | ||
if (isSendingTx) return; | ||
setIsSendingTx(true); | ||
|
||
transfer({ amount: transferAmount, from: omniAddr, to: transferAddr }) | ||
.then(setTxHash) | ||
.catch((e) => { | ||
console.log(e); | ||
alert(e.message || JSON.stringify(e)); | ||
}) | ||
.finally(() => setIsSendingTx(false)); | ||
} | ||
|
||
if (!solana) return <div>Phantom is not installed</div>; | ||
if (!solanaAddr) return <button onClick={connectToWallet}>Connect to Phantom</button>; | ||
|
||
return ( | ||
<div> | ||
<ul> | ||
<li>Solana Address: {solanaAddr}</li> | ||
<li>Nervos Address(Omni): {omniAddr}</li> | ||
<li> | ||
Current Omni lock script: | ||
<pre>{JSON.stringify(omniLock, null, 2)}</pre> | ||
</li> | ||
|
||
<li>Balance: {balance}</li> | ||
</ul> | ||
|
||
<div> | ||
<h2>Transfer to</h2> | ||
<label htmlFor="address">Address</label> | ||
<input id="address" type="text" onChange={(e) => setTransferAddress(e.target.value)} placeholder="ckt1..." /> | ||
<br /> | ||
<label htmlFor="amount">Amount</label> | ||
| ||
<input id="amount" type="text" onChange={(e) => setTransferAmount(e.target.value)} placeholder="shannon" /> | ||
<br /> | ||
<button onClick={onTransfer} disabled={isSendingTx}> | ||
Transfer | ||
</button> | ||
<p>Tx Hash: {txHash}</p> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const app = document.getElementById("root"); | ||
ReactDOM.render(<App />, app); |
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,71 @@ | ||
import { BI, helpers, Indexer, RPC, config, commons } from "@ckb-lumos/lumos"; | ||
import { common, omnilock } from "@ckb-lumos/lumos/common-scripts"; | ||
import { blockchain, bytify, hexify } from "@ckb-lumos/lumos/codec"; | ||
import { Config } from "@ckb-lumos/lumos/config"; | ||
|
||
const CKB_RPC_URL = "https://testnet.ckb.dev"; | ||
const rpc = new RPC(CKB_RPC_URL); | ||
const indexer = new Indexer(CKB_RPC_URL); | ||
|
||
export const CONFIG: Config = config.TESTNET; | ||
|
||
config.initializeConfig(CONFIG); | ||
|
||
declare global { | ||
interface Window { | ||
phantom: { | ||
solana: omnilock.solana.Provider; | ||
}; | ||
} | ||
} | ||
|
||
export const solana = window.phantom.solana; | ||
|
||
export function asyncSleep(ms: number): Promise<void> { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
interface Options { | ||
from: string; | ||
to: string; | ||
amount: string; | ||
} | ||
|
||
export async function transfer(options: Options): Promise<string> { | ||
let txSkeleton = helpers.TransactionSkeleton({ cellProvider: indexer }); | ||
|
||
txSkeleton = await common.transfer(txSkeleton, [options.from], options.to, options.amount); | ||
txSkeleton = await common.payFeeByFeeRate(txSkeleton, [options.from], 1000); | ||
txSkeleton = commons.omnilock.prepareSigningEntries(txSkeleton); | ||
|
||
const signedMessage = await omnilock.solana.signMessage( | ||
txSkeleton.signingEntries.get(0)!.message, | ||
window.phantom.solana | ||
); | ||
|
||
const signedWitness = hexify( | ||
blockchain.WitnessArgs.pack({ | ||
lock: commons.omnilock.OmnilockWitnessLock.pack({ signature: bytify(signedMessage) }), | ||
}) | ||
); | ||
|
||
txSkeleton = txSkeleton.update("witnesses", (witnesses) => witnesses.set(0, signedWitness)); | ||
|
||
const signedTx = helpers.createTransactionFromSkeleton(txSkeleton); | ||
const txHash = await rpc.sendTransaction(signedTx, "passthrough"); | ||
|
||
return txHash; | ||
} | ||
|
||
export async function capacityOf(address: string): Promise<BI> { | ||
const collector = indexer.collector({ | ||
lock: helpers.parseAddress(address), | ||
}); | ||
|
||
let balance = BI.from(0); | ||
for await (const cell of collector.collect()) { | ||
balance = balance.add(cell.cellOutput.capacity); | ||
} | ||
|
||
return balance; | ||
} |
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,22 @@ | ||
{ | ||
"private": true, | ||
"name": "@lumos-examples/omni-lock-solana", | ||
"description": "", | ||
"scripts": { | ||
"start": "parcel index.html", | ||
"lint": "tsc --noEmit" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@ckb-lumos/lumos": "canary", | ||
"@types/react": "^18.0.25", | ||
"@types/react-dom": "^18.0.9", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0" | ||
}, | ||
"devDependencies": { | ||
"parcel": "^2.9.3" | ||
} | ||
} |
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,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["dom"], | ||
"jsx": "react", | ||
"module": "CommonJS", | ||
"skipLibCheck": true, | ||
"esModuleInterop": true | ||
} | ||
} |
Oops, something went wrong.
16b32a7
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.22.2