MCPcopy Create free account
hub / github.com/NoteProtocol/NoteWallet / sendTokenCommon

Function sendTokenCommon

src/wallet.ts:301–352  ·  view source on GitHub ↗
(toAddress: string, tick: string, amt: bigint)

Source from the content-addressed store, hash-verified

299 }
300
301 async sendTokenCommon(toAddress: string, tick: string, amt: bigint) {
302 const tokenUtxos = await this.getTokenUtxos(tick, amt);
303 const balance = tokenUtxos.reduce(
304 (acc: bigint, cur: ITokenUtxo) => acc + BigInt(cur.amount),
305 0n
306 );
307 if (balance < amt) {
308 throw new Error("Insufficient balance");
309 }
310 //如果有误发到主地址的Token,那么可以挽救
311 const missedTokenUtxos = await this.urchain.tokenutxos(
312 [this.currentAccount.mainAddress!.scriptHash],
313 tick
314 );
315 const toAddresses: ISendToAddress[] = [
316 {
317 address: toAddress,
318 amount: MIN_SATOSHIS,
319 },
320 ];
321 if (balance > BigInt(amt) || missedTokenUtxos.length > 0) {
322 //如果有余量,那么添加找零地址toAddresses
323 toAddresses.push({
324 address: this.currentAccount.tokenAddress!.address!,
325 amount: MIN_SATOSHIS,
326 });
327 }
328 const transferData: ITransferN20Data = {
329 p: "n20",
330 op: "transfer",
331 tick,
332 amt,
333 };
334
335 const payUtxos: IUtxo[] = await this.fetchAllAccountUtxos();
336 if (missedTokenUtxos.length > 0) {
337 payUtxos.push(
338 ...missedTokenUtxos.map((utxo: IUtxo) => {
339 utxo.privateKeyWif = this.currentAccount.privateKey;
340 utxo.type = this.currentAccount.mainAddress!.type;
341 return utxo;
342 })
343 );
344 }
345
346 return {
347 payload: buildNotePayload(transferData),
348 toAddresses,
349 tokenUtxos,
350 payUtxos,
351 };
352 }
353
354 async sendTokenEstimate(
355 toAddress: string,

Callers

nothing calls this directly

Calls 2

buildNotePayloadFunction · 0.90
tokenutxosMethod · 0.80

Tested by

no test coverage detected