krc-20代币转移的node.js示例代码
import { createTransactions, PrivateKey, PublicKey, UtxoContext, UtxoProcessor, type IPaymentOutput, type IUtxoEntry, type RpcClient } from "../../wasm/kaspa"
export default class Treasury {
rpc: RpcClient
privateKey: PrivateKey
publicKey: PublicKey
address: string
processor: UtxoProcessor
context: UtxoContext
constructor(rpc: RpcClient, networkId: string, privateKey: string) {
this.rpc = rpc
this.privateKey = new PrivateKey(privateKey)
this.publicKey = this.privateKey.toPublicKey()
this.address = this.publicKey.toAddress(networkId).toString()
this.processor = new UtxoProcessor({ rpc, networkId })
this.context = new UtxoContext({ processor: this.processor })
this.register()
}
async send(outputs: IPaymentOutput[]) {
const { transactions, summary } = await createTransactions({
entries: this.context,
outputs,
changeAddress: this.address,
priorityFee: 0n,
})
const rpc = this.processor.rpc
for (const transaction of transactions) {
transaction.sign([this.privateKey.toString()])
await transaction.submit(rpc)
}
return summary.finalTransactionId!
}
private register() {
this.processor.addEventListener("utxo-proc-start", async () => {
await this.context.clear()
await this.context.trackAddresses([this.address])
})
this.processor.start()
}
}
调用:
import Treasury from "."
import type { RpcClient } from "../../wasm/kaspa"
import { Inscription } from "KasplexBuilder"
import { kaspaToSompi, ScriptBuilder, addressFromScriptPublicKey, createTransactions } from "../../wasm/kaspa"
type Commitment = {
address: string,
script: ScriptBuilder
}
export default class KRC20 extends Treasury {
commitments: Map<string, Commitment> = new Map()
constructor(rpc: RpcClient, networkId: string, privateKey: string) {
super(rpc, networkId, privateKey)
this.registerCommitments()
}
async transfer(ticker: string, recipient: string, amount: BigInt) {
const script = new ScriptBuilder()
const inscription = new Inscription('transfer', {
tick: ticker,
amt: amount.toString(),
to: recipient
})
inscription.write(script, this.publicKey.toXOnlyPublicKey().toString())
const address = addressFromScriptPublicKey(script.createPayToScriptHashScript(), this.processor.networkId!)!.toString()
const transaction = await this.send([{
address: address,
amount: kaspaToSompi("0.2")!
}])
this.commitments.set(transaction, { address, script })
return transaction
}
registerCommitments() {
this.processor.addEventListener('maturity', async (e) => {
if (e.data.type !== 'outgoing') return
const hash = e.data.id.toString()
const commitment = this.commitments.get(hash)
if (!commitment) return
const { entries } = await this.rpc.getUtxosByAddresses([commitment.address])
const entry = entries.find((entry) => entry.outpoint.transactionId === hash)!
const { transactions } = await createTransactions({
entries: [entry],
outputs: [],
changeAddress: this.address,
priorityFee: 1000n,
networkId: this.processor.networkId
})
const transaction = transactions[0]
const signature = transaction.createInputSignature(0, this.privateKey)
transaction.fillInput(0, commitment.script.encodePayToScriptHashSignatureScript(signature))
await transaction.submit(this.rpc)
this.commitments.delete(hash)
})
}
}
requires KasplexBuilder library
感动 | 同情 | 无聊 | 愤怒 | 搞笑 | 难过 | 高兴 | 路过 |
相关文章
-
没有相关内容