|
1 | 1 | import { H256 } from "./H256";
|
2 |
| -import { AssetOutPoint } from "./transaction/AssetTransferTransaction"; |
| 2 | +import { AssetTransferTransaction, AssetTransferInput, AssetOutPoint, AssetTransferOutput } from "./transaction/AssetTransferTransaction"; |
| 3 | +import { AssetTransferAddress } from "../AssetTransferAddress"; |
3 | 4 |
|
4 | 5 | export type AssetData = {
|
5 | 6 | assetType: H256;
|
@@ -58,4 +59,28 @@ export class Asset {
|
58 | 59 | transactionOutputIndex: index,
|
59 | 60 | };
|
60 | 61 | }
|
| 62 | + |
| 63 | + transfer(recipients: { address: AssetTransferAddress, amount: number }[], options: { nonce?: number } = {}): AssetTransferTransaction { |
| 64 | + const { outPoint, assetType } = this; |
| 65 | + const { nonce = 0 } = options; |
| 66 | + |
| 67 | + const outputSum = recipients.map(r => r.amount).reduce((a, b) => a + b); |
| 68 | + if (outputSum !== this.amount) { |
| 69 | + throw "The sum of recipients' amount must equal to the asset amount"; |
| 70 | + } |
| 71 | + |
| 72 | + return new AssetTransferTransaction(17, { |
| 73 | + burns: [], |
| 74 | + inputs: [new AssetTransferInput({ |
| 75 | + prevOut: outPoint, |
| 76 | + lockScript: Buffer.from([]), |
| 77 | + unlockScript: Buffer.from([]), |
| 78 | + })], |
| 79 | + outputs: recipients.map(recipient => new AssetTransferOutput({ |
| 80 | + ...recipient.address.getLockScriptHashAndParameters(), |
| 81 | + assetType, |
| 82 | + amount: recipient.amount |
| 83 | + })), |
| 84 | + }, nonce); |
| 85 | + } |
61 | 86 | }
|
0 commit comments