Skip to content

Commit

Permalink
Add transfer() to Asset class
Browse files Browse the repository at this point in the history
  • Loading branch information
joojis committed Jun 28, 2018
1 parent 8bd0537 commit be7e7ed
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/primitives/Asset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { H256 } from "./H256";
import { AssetOutPoint } from "./transaction/AssetTransferTransaction";
import { AssetTransferTransaction, AssetTransferInput, AssetOutPoint, AssetTransferOutput } from "./transaction/AssetTransferTransaction";
import { AssetTransferAddress } from "../AssetTransferAddress";

export type AssetData = {
assetType: H256;
Expand Down Expand Up @@ -58,4 +59,28 @@ export class Asset {
transactionOutputIndex: index,
};
}

transfer(recipients: { address: AssetTransferAddress, amount: number }[], options: { nonce?: number } = {}): AssetTransferTransaction {
const { outPoint, assetType } = this;
const { nonce = 0 } = options;

const outputSum = recipients.map(r => r.amount).reduce((a, b) => a + b);
if (outputSum !== this.amount) {
throw "The sum of recipients' amount must equal to the asset amount";
}

return new AssetTransferTransaction(17, {
burns: [],
inputs: [new AssetTransferInput({
prevOut: outPoint,
lockScript: Buffer.from([]),
unlockScript: Buffer.from([]),
})],
outputs: recipients.map(recipient => new AssetTransferOutput({
...recipient.address.getLockScriptHashAndParameters(),
assetType,
amount: recipient.amount
})),
}, nonce);
}
}

0 comments on commit be7e7ed

Please sign in to comment.