Skip to content

Commit

Permalink
Merge pull request #415 from TosiDrop/master
Browse files Browse the repository at this point in the history
Release 3.1.2
  • Loading branch information
reqlez authored Mar 3, 2023
2 parents 15d0fcf + d046989 commit dd9130c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 23 deletions.
60 changes: 60 additions & 0 deletions server/service/cardano.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import {
Address,
AssetName,
Assets,
BaseAddress,
BigNum,
MultiAsset,
RewardAddress,
ScriptHash,
TransactionHash,
TransactionInput,
TransactionOutput,
TransactionUnspentOutput,
Value,
} from "@emurgo/cardano-serialization-lib-nodejs";
import { CARDANO_NETWORK } from "..";
import { KoiosTypes } from "../types/koios";
import { convertHexToBuffer } from "../utils";
import { createErrorWithCode, HttpStatusCode } from "../utils/error";
import { CardanoNetwork } from "../utils/interfaces";

Expand Down Expand Up @@ -60,4 +72,52 @@ export namespace CardanoService {
);
return buffer.toString("hex");
}

export function convertAssetsToValue(utxo: KoiosTypes.UTxO): Value {
const multiAsset = MultiAsset.new();
const assetsGroupedByPolicyId: Record<string, KoiosTypes.NativeToken[]> =
{};
utxo.asset_list.forEach((asset) => {
const key = asset.policy_id;
if (assetsGroupedByPolicyId[key]) {
assetsGroupedByPolicyId[key].push(asset);
} else {
assetsGroupedByPolicyId[key] = [asset];
}
});
Object.keys(assetsGroupedByPolicyId).forEach((policyId) => {
const assets = Assets.new();
assetsGroupedByPolicyId[policyId].forEach((asset) => {
assets.insert(
AssetName.new(convertHexToBuffer(asset.asset_name)),
BigNum.from_str(asset.quantity)
);
});
multiAsset.insert(
ScriptHash.from_bytes(convertHexToBuffer(policyId)),
assets
);
});
const value = Value.new(BigNum.from_str(utxo.value));
if (utxo.asset_list.length > 0) {
value.set_multiasset(multiAsset);
}
return value;
}

export function createUtxo(
utxo: KoiosTypes.UTxO,
address: string
): TransactionUnspentOutput {
const utxoValue = CardanoService.convertAssetsToValue(utxo);
const input = TransactionInput.new(
TransactionHash.from_hex(utxo.tx_hash),
Number(utxo.tx_index)
);
const output = TransactionOutput.new(
Address.from_bech32(address),
utxoValue
);
return TransactionUnspentOutput.new(input, output);
}
}
31 changes: 8 additions & 23 deletions server/service/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ import {
TransactionBody,
TransactionBuilder,
TransactionBuilderConfigBuilder,
TransactionHash,
TransactionInput,
TransactionOutput,
TransactionUnspentOutput,
TransactionUnspentOutputs,
TransactionWitnessSet,
Value,
} from "@emurgo/cardano-serialization-lib-nodejs";
import { CardanoService } from "./cardano";
import { KoiosService } from "./koios";
Expand Down Expand Up @@ -78,8 +73,8 @@ export namespace TxService {
const txBuilder = TransactionBuilder.new(txBuilderConfig);
txBuilder.set_ttl_bignum(BigNum.from_str((tip.abs_slot + 3600).toString()));

/** add delegation certs */
const certs = Certificates.new();

if (accountInformation.status !== "registered") {
certs.add(
Certificate.new_stake_registration(
Expand All @@ -101,34 +96,24 @@ export namespace TxService {

txBuilder.set_certs(certs);

/** assemble utxos from stake address */
const addressesRelatedToStakeAddress =
await KoiosService.getAddressesFromStakeAddress(stakeAddress);
const getAddressesInformation = await KoiosService.getAddressesInformation(
const addressesInformation = await KoiosService.getAddressesInformation(
addressesRelatedToStakeAddress
);

const utxosOutput = TransactionUnspentOutputs.new();
getAddressesInformation.forEach((info) => {
const availableUtxos = TransactionUnspentOutputs.new();
addressesInformation.forEach((info) => {
const utxos = info.utxo_set;
utxos.forEach((utxo) => {
const input = TransactionInput.new(
TransactionHash.from_hex(utxo.tx_hash),
Number(utxo.tx_index)
);
const output = TransactionOutput.new(
Address.from_bech32(info.address),
Value.new(BigNum.from_str(utxo.value))
);
utxosOutput.add(TransactionUnspentOutput.new(input, output));
const availableUtxo = CardanoService.createUtxo(utxo, info.address);
availableUtxos.add(availableUtxo);
});
});

txBuilder.add_inputs_from(utxosOutput, 0);

txBuilder.add_inputs_from(availableUtxos, 0);
txBuilder.add_change_if_needed(Address.from_bech32(delegatorAddress));

const txBody = txBuilder.build();

const transaction = Transaction.new(txBody, TransactionWitnessSet.new());

return {
Expand Down
8 changes: 8 additions & 0 deletions server/types/koios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,17 @@ export namespace KoiosTypes {
utxo_set: UTxO[];
}

export interface NativeToken {
policy_id: string;
asset_name: string;
fingerprint: string;
quantity: string;
}

export interface UTxO {
value: string;
tx_hash: string;
tx_index: string;
asset_list: NativeToken[];
}
}
4 changes: 4 additions & 0 deletions server/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,7 @@ export function parseVmDeliveredRewards(
export function convertPoolIdToBech32(poolIdInHex: string) {
return converter("pool").toBech32(poolIdInHex);
}

export function convertHexToBuffer(_: string): Uint8Array {
return Buffer.from(_, "hex");
}

0 comments on commit dd9130c

Please sign in to comment.