Skip to content

Commit be7e7ed

Browse files
committed
Add transfer() to Asset class
1 parent 8bd0537 commit be7e7ed

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/primitives/Asset.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { H256 } from "./H256";
2-
import { AssetOutPoint } from "./transaction/AssetTransferTransaction";
2+
import { AssetTransferTransaction, AssetTransferInput, AssetOutPoint, AssetTransferOutput } from "./transaction/AssetTransferTransaction";
3+
import { AssetTransferAddress } from "../AssetTransferAddress";
34

45
export type AssetData = {
56
assetType: H256;
@@ -58,4 +59,28 @@ export class Asset {
5859
transactionOutputIndex: index,
5960
};
6061
}
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+
}
6186
}

0 commit comments

Comments
 (0)