Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk-coin-ada): add multi-asset to init builder flow #4033

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small question, why are we just initializing the txOutputAmountBuilder here? is it not used in any other part of code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise looks fine to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do use it another place (when we go through the consolidation builder - #3994), but that flow is not related to the parseTransaction flow that this PR deals with

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
Loading