Skip to content

Commit

Permalink
fix(sdk-coin-ada): add multi-asset to init builder flow
Browse files Browse the repository at this point in the history
Ticket: WIN-736
  • Loading branch information
Vijay-Jagannathan committed Nov 1, 2023
1 parent 973e52f commit 5c75ac6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
32 changes: 26 additions & 6 deletions modules/sdk-coin-ada/src/lib/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
this.output({
address: output.address().to_bech32(),
amount: output.amount().coin().to_str(),
multiAssets: output.amount().multiasset() || undefined,
});
}

Expand Down Expand Up @@ -234,12 +235,31 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
// reset the outputs collection because now our last output has changed
outputs = CardanoWasm.TransactionOutputs.new();
this._transactionOutputs.forEach((output) => {
outputs.add(
CardanoWasm.TransactionOutput.new(
CardanoWasm.Address.from_bech32(output.address),
CardanoWasm.Value.new(CardanoWasm.BigNum.from_str(output.amount))
)
);
if (output.multiAssets) {
const policyId = output.multiAssets.keys().get(0);
const assets = output.multiAssets.get(policyId);
const assetName = assets!.keys().get(0);
const quantity = assets!.get(assetName);
let txOutputBuilder = CardanoWasm.TransactionOutputBuilder.new();
const outputAmount = CardanoWasm.BigNum.from_str(output.amount);
const toAddress = CardanoWasm.Address.from_bech32(output.address);
txOutputBuilder = txOutputBuilder.with_address(toAddress);
let txOutputAmountBuilder = txOutputBuilder.next();
const multiAsset = CardanoWasm.MultiAsset.new();
const asset = CardanoWasm.Assets.new();
asset.insert(assetName, quantity!);
multiAsset.insert(policyId, asset);
txOutputAmountBuilder = txOutputAmountBuilder.with_coin_and_asset(outputAmount, multiAsset);
const txOutput = txOutputAmountBuilder.build();
outputs.add(txOutput);
} else {
outputs.add(
CardanoWasm.TransactionOutput.new(
CardanoWasm.Address.from_bech32(output.address),
CardanoWasm.Value.new(CardanoWasm.BigNum.from_str(output.amount))
)
);
}
});
if (this._changeAddress && this._senderBalance) {
const changeAddress = CardanoWasm.Address.from_bech32(this._changeAddress);
Expand Down
23 changes: 23 additions & 0 deletions modules/sdk-coin-ada/test/unit/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ describe('ADA Transaction Builder', async () => {
should.equal(txBroadcast, testData.rawTx.unsignedTx5);
});

it('should initialize and build tx with asset data', async () => {
const policyId = '279c909f348e533da5808898f87f9a14bb2c3dfbbacccd631d927a3f';
const assetName = '534e454b';
const quantity = '6000000';
const preBuiltTx = new Transaction(coins.get('tada'));
preBuiltTx.fromRawTransaction(testData.rawTx.unsignedTx3);
const txBuilder = factory.getTransferBuilder();
txBuilder.initBuilder(preBuiltTx);

const tx = (await txBuilder.build()) as Transaction;
const txData = tx.toJson();
const expectedAssetName = CardanoWasm.AssetName.new(Buffer.from(assetName, 'hex'));
const expectedPolicyId = CardanoWasm.ScriptHash.from_bytes(Buffer.from(policyId, 'hex'));
txData.outputs[0].should.have.property('multiAssets');
(txData.outputs[0].multiAssets as CardanoWasm.MultiAsset)
.get_asset(expectedPolicyId, expectedAssetName)
.to_str()
.should.equal(quantity);
txData.id.should.equal(testData.rawTx.txHash3);
const txBroadcast = tx.toBroadcastFormat();
should.equal(txBroadcast, testData.rawTx.unsignedTx3);
});

it('should build a consolidate tx with single asset', async () => {
const txBuilder = factory.getTransferBuilder();
txBuilder.input({
Expand Down

0 comments on commit 5c75ac6

Please sign in to comment.