Skip to content

Commit

Permalink
Add min_output_ada test
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusbfs committed Nov 19, 2024
1 parent 4b54284 commit 80081e3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "npm run test-serialization ; npm run test-api",
"test-serialization": "NODE_OPTIONS=--experimental-vm-modules jest serialization",
"test-api": "npm run generate-cdl-definitions ; NODE_OPTIONS=--experimental-vm-modules jest api",
"test-implementation": "NODE_OPTIONS=--experimental-vm-modules jest hash fees",
"test-implementation": "NODE_OPTIONS=--experimental-vm-modules jest fees hash min_output_ada",
"codegen": "tsx conway-cddl/codegen/main.ts",
"generate-grammar-bundle": "npx ohm generateBundles -e -t tests/api/grammar.ohm",
"generate-cdl-definitions": "make typedefs",
Expand Down
2 changes: 1 addition & 1 deletion src/min_ada_calculator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ class MinOutputAdaCalculator {
* @param data_cost - The data cost parameters, including coins per byte.
* @returns The minimum ADA required as a `BigNum`.
*/
export function minAdaForOutput(output: TransactionOutput, data_cost: DataCost): BigNum {
export function min_ada_for_output(output: TransactionOutput, data_cost: DataCost): BigNum {
return MinOutputAdaCalculator.new(output, data_cost).calculate_ada();
}
35 changes: 35 additions & 0 deletions tests/min_output_ada/min_output_ada.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { jest } from '@jest/globals'

import { Address } from "../../src/address";
import {
BigNum,
DataCost,
TransactionOutput,
Value,
} from "../../src/generated";

import { min_ada_for_output } from '../../src/min_ada_calculator'

describe('min_ada_for_output', () => {
test('min_ada_for_output calculates minimum ADA required for output', () => {

/* tslint:disable-next-line */
const address = new Address(undefined);
const coin = BigNum.from_str('1000000'); // 1 ADA in lovelace
const value = Value.new(coin);
const output = TransactionOutput.new(address, value);

jest.spyOn(output, 'clone').mockReturnValue(output);
jest.spyOn(output, 'to_bytes').mockReturnValue(new Uint8Array(100));

const dataCost = new DataCost(BigNum._from_number(34482));

const minAda = min_ada_for_output(output, dataCost);

const expectedMinAda = BigNum._from_number(100)
.checked_add(BigNum._from_number(160))
.checked_mul(dataCost.coins_per_byte());

expect(minAda.to_str()).toBe(expectedMinAda.to_str());
});
})

0 comments on commit 80081e3

Please sign in to comment.