Skip to content

Commit

Permalink
fix: endpoint response type
Browse files Browse the repository at this point in the history
  • Loading branch information
reedrosenbluth committed Nov 18, 2021
1 parent 02ebcc7 commit 647833a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
23 changes: 11 additions & 12 deletions packages/transactions/src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ export async function estimateTransfer(
return feeRate * txBytes;
}

interface FeeEstimation {
fee: bigint,
fee_rate: bigint,
}
interface FeeEstimateResponse {
cost_scalar_change_by_byte: bigint,
estimated_cost: {
Expand All @@ -151,12 +155,7 @@ interface FeeEstimateResponse {
write_length: bigint
},
estimated_cost_scalar: bigint,
estimated_fee_rates: [
bigint, bigint, bigint
],
estimated_fees: [
bigint, bigint, bigint
]
estimations: [FeeEstimation, FeeEstimation, FeeEstimation]
}

/**
Expand All @@ -174,7 +173,7 @@ export async function estimateTransaction(
transactionPayload: Payload,
estimatedLen?: number,
network?: StacksNetwork
): Promise<[bigint, bigint, bigint]> {
): Promise<[FeeEstimation, FeeEstimation, FeeEstimation]> {

const options = {
method: 'POST',
Expand Down Expand Up @@ -204,7 +203,7 @@ export async function estimateTransaction(
}

const data: FeeEstimateResponse = await response.json();
return data.estimated_fees;
return data.estimations;
}

export type SerializationRejection = {
Expand Down Expand Up @@ -613,7 +612,7 @@ export async function makeUnsignedSTXTokenTransfer(
if (txOptions.fee === undefined || txOptions.fee === null) {
const estimatedLen = transaction.serialize().byteLength;
const txFee = await estimateTransaction(payload, estimatedLen, options.network);
transaction.setFee(txFee[1]);
transaction.setFee(txFee[1].fee);
}

if (txOptions.nonce === undefined || txOptions.nonce === null) {
Expand Down Expand Up @@ -816,7 +815,7 @@ export async function makeContractDeploy(
if (txOptions.fee === undefined || txOptions.fee === null) {
const estimatedLen = transaction.serialize().byteLength;
const txFee = await estimateTransaction(payload, estimatedLen, options.network)
transaction.setFee(txFee[1]);
transaction.setFee(txFee[1].fee);
}

if (txOptions.nonce === undefined || txOptions.nonce === null) {
Expand Down Expand Up @@ -1032,7 +1031,7 @@ export async function makeUnsignedContractCall(
if (txOptions.fee === undefined || txOptions.fee === null) {
const estimatedLen = transaction.serialize().byteLength;
const txFee = await estimateTransaction(payload, estimatedLen, options.network);
transaction.setFee(txFee[1]);
transaction.setFee(txFee[1].fee);
}

if (txOptions.nonce === undefined || txOptions.nonce === null) {
Expand Down Expand Up @@ -1360,7 +1359,7 @@ export async function sponsorTransaction(
case PayloadType.SmartContract:
case PayloadType.ContractCall:
const estimatedLen = options.transaction.serialize().byteLength;
txFee = (await estimateTransaction(options.transaction.payload, estimatedLen, network))[0];
txFee = (await estimateTransaction(options.transaction.payload, estimatedLen, network))[1].fee;
break;
default:
throw new Error(
Expand Down
48 changes: 42 additions & 6 deletions packages/transactions/tests/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,20 @@ test.only('Make STX token transfer with fee estimate', async () => {
"write_length": 1020
},
"estimated_cost_scalar": 14,
"estimated_fee_rates": [10, 1.2410714285714286, 8.958333333333332],
"estimated_fees": [200, 180, 160]
"estimations": [
{
"fee": 200,
"fee_rate": 10
},
{
"fee": 180,
"fee_rate": 1.2410714285714286
},
{
"fee": 160,
"fee_rate": 8.958333333333332
},
]
});

fetchMock.mockOnce(mockedResponse);
Expand Down Expand Up @@ -878,8 +890,20 @@ test('Estimate transaction transfer fee', async () => {
"write_length": 1020
},
"estimated_cost_scalar": 14,
"estimated_fee_rates": [10, 1.2410714285714286, 8.958333333333332],
"estimated_fees": [140, 17, 125]
"estimations": [
{
"fee": 140,
"fee_rate": 10
},
{
"fee": 17,
"fee_rate": 1.2410714285714286
},
{
"fee": 125,
"fee_rate": 8.958333333333332
},
]
});

fetchMock.mockOnce(mockedResponse);
Expand Down Expand Up @@ -1098,8 +1122,20 @@ test('Make sponsored STX token transfer with sponsor fee estimate', async () =>
"write_length": 1020
},
"estimated_cost_scalar": 14,
"estimated_fee_rates": [10, 1.2410714285714286, 8.958333333333332],
"estimated_fees": [140, 1, 125]
"estimations": [
{
"fee": 140,
"fee_rate": 10
},
{
"fee": 1,
"fee_rate": 1.2410714285714286
},
{
"fee": 125,
"fee_rate": 8.958333333333332
},
]
});

fetchMock.mockOnce(mockedResponse);
Expand Down

0 comments on commit 647833a

Please sign in to comment.