Skip to content

Commit

Permalink
feat: add platform.getFeeState and update etna builder (#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
erictaylor authored Oct 8, 2024
1 parent b5db93b commit 47f216e
Show file tree
Hide file tree
Showing 23 changed files with 224 additions and 153 deletions.
6 changes: 4 additions & 2 deletions examples/p-chain/etna/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ const SEND_AVAX_AMOUNT: number = 0.001;
const main = async () => {
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars();

const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);

const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL);

const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);
const feeState = await pvmApi.getFeeState();

const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] });

const tx = pvm.e.newBaseTx(
{
feeState,
fromAddressesBytes: [utils.bech32ToBytes(P_CHAIN_ADDRESS)],
outputs: [
TransferableOutput.fromNative(
Expand Down
6 changes: 4 additions & 2 deletions examples/p-chain/etna/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const DAYS_TO_DELEGATE: number = 14;
const main = async () => {
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars();

const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);

const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL);

const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);
const feeState = await pvmApi.getFeeState();

const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] });

const startTime = await pvmApi.getTimestamp();
Expand All @@ -28,6 +29,7 @@ const main = async () => {
const tx = pvm.e.newAddPermissionlessDelegatorTx(
{
end,
feeState,
fromAddressesBytes: [utils.bech32ToBytes(P_CHAIN_ADDRESS)],
nodeId,
rewardAddresses: [utils.bech32ToBytes(P_CHAIN_ADDRESS)],
Expand Down
2 changes: 2 additions & 0 deletions examples/p-chain/etna/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const main = async () => {
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL);

const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);
const feeState = await pvmApi.getFeeState();

const { utxos } = await pvmApi.getUTXOs({
addresses: [P_CHAIN_ADDRESS],
Expand All @@ -19,6 +20,7 @@ const main = async () => {
const exportTx = pvm.e.newExportTx(
{
destinationChainId: context.xBlockchainID,
feeState,
fromAddressesBytes: [utils.bech32ToBytes(P_CHAIN_ADDRESS)],
outputs: [
TransferableOutput.fromNative(
Expand Down
2 changes: 2 additions & 0 deletions examples/p-chain/etna/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const main = async () => {
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL);

const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);
const feeState = await pvmApi.getFeeState();

const { utxos } = await pvmApi.getUTXOs({
sourceChain: 'X',
Expand All @@ -17,6 +18,7 @@ const main = async () => {

const importTx = pvm.e.newImportTx(
{
feeState,
fromAddressesBytes: [utils.bech32ToBytes(X_CHAIN_ADDRESS)],
sourceChainId: context.xBlockchainID,
toAddresses: [utils.bech32ToBytes(P_CHAIN_ADDRESS)],
Expand Down
24 changes: 21 additions & 3 deletions examples/p-chain/etna/utils/etna-context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Context } from '../../../../src';

import { Context, Info } from '../../../../src';
/**
* Gets the context from URI and then modifies the context
* to be used for testing example Etna transactions until Etna is enabled.
Expand All @@ -9,5 +8,24 @@ export const getEtnaContextFromURI = async (
): Promise<Context.Context> => {
const context = await Context.getContextFromURI(uri);

return context;
const info = new Info(uri);

const { etnaTime } = await info.getUpgradesInfo();

const etnaDateTime = new Date(etnaTime);
const now = new Date();

if (etnaDateTime < now) {
return context;
}

// If Etna is not enabled, we need to override the minPrice of 1n that is returned.
// This is because the minPrice of 1n is not enough to calculate a fee that exceeds the current static fees.
return {
...context,
platformFeeConfig: {
...context.platformFeeConfig,
minPrice: 10_000n,
},
};
};
6 changes: 4 additions & 2 deletions examples/p-chain/etna/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ const nodeId = getRandomNodeId();
const main = async () => {
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars();

const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);

const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL);

const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL);
const feeState = await pvmApi.getFeeState();

const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] });

const startTime = await pvmApi.getTimestamp();
Expand All @@ -37,6 +38,7 @@ const main = async () => {
{
end,
delegatorRewardsOwner: [utils.bech32ToBytes(P_CHAIN_ADDRESS)],
feeState,
fromAddressesBytes: [utils.bech32ToBytes(P_CHAIN_ADDRESS)],
nodeId,
publicKey,
Expand Down
14 changes: 14 additions & 0 deletions src/fixtures/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createDimensions } from '../vms/common/fees/dimensions';
import type { Context } from '../vms/context';

export const testContext: Context = {
Expand All @@ -16,4 +17,17 @@ export const testContext: Context = {
addSubnetDelegatorFee: 1000000n,
networkID: 1,
hrp: 'avax',
platformFeeConfig: {
weights: createDimensions({
bandwidth: 1,
dbRead: 1,
dbWrite: 1,
compute: 1,
}),
maxCapacity: 1_000_000n,
maxPerSecond: 1_000n,
targetPerSecond: 500n,
minPrice: 1n,
excessConversionConstant: 5_000n,
},
};
16 changes: 0 additions & 16 deletions src/fixtures/feeConfig.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/fixtures/pvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
} from './secp256k1';
import { bytesForInt } from './utils/bytesFor';
import { makeList, makeListBytes } from './utils/makeList';
import type { FeeState } from '../vms/pvm';

export const validator = () =>
new Validator(nodeId(), bigIntPr(), bigIntPr(), bigIntPr());
Expand Down Expand Up @@ -290,3 +291,10 @@ export const transformSubnetTxBytes = () =>
bytesForInt(10),
inputBytes(),
);

export const feeState = (): FeeState => ({
capacity: 1n,
excess: 1n,
price: 1n,
timestamp: new Date().toISOString(),
});
5 changes: 5 additions & 0 deletions src/vms/context/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getHRP } from '../../constants/networkIDs';
import { Info } from '../../info/info';
import { AVMApi } from '../avm/api';
import { PVMApi } from '../pvm';
import type { Context } from './model';

/*
Expand All @@ -10,6 +11,7 @@ export const getContextFromURI = async (
baseURL?: string,
assetDescription = 'AVAX',
): Promise<Context> => {
const pChainApi = new PVMApi(baseURL);
const xChainApi = new AVMApi(baseURL);
const { assetID: avaxAssetID } = await xChainApi.getAssetDescription(
assetDescription,
Expand All @@ -33,6 +35,8 @@ export const getContextFromURI = async (
const { networkID: networkIDstring } = await info.getNetworkId();
const networkID = Number(networkIDstring);

const platformFeeConfig = await pChainApi.getFeeConfig();

return Object.freeze({
xBlockchainID,
pBlockchainID,
Expand All @@ -49,5 +53,6 @@ export const getContextFromURI = async (
addSubnetDelegatorFee,
networkID,
hrp: getHRP(networkID),
platformFeeConfig,
});
};
5 changes: 5 additions & 0 deletions src/vms/context/model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { FeeConfig } from '../pvm';

export type Context = {
readonly networkID: number;
readonly hrp: string;
Expand All @@ -14,4 +16,7 @@ export type Context = {
readonly addPrimaryNetworkDelegatorFee: bigint;
readonly addSubnetValidatorFee: bigint;
readonly addSubnetDelegatorFee: bigint;

// Post Etna
readonly platformFeeConfig: FeeConfig;
};
13 changes: 13 additions & 0 deletions src/vms/pvm/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { createDimensions } from '../common/fees/dimensions';
import type {
FeeConfig,
FeeConfigResponse,
FeeState,
FeeStateResponse,
GetBalanceParams,
GetBalanceResponse,
GetBlockchainsResponse,
Expand Down Expand Up @@ -247,4 +249,15 @@ export class PVMApi extends AvaxApi {
excessConversionConstant: BigInt(excessConversionConstant),
};
}

async getFeeState(): Promise<FeeState> {
const resp = await this.callRpc<FeeStateResponse>('getFeeState');

return {
capacity: BigInt(resp.capacity),
excess: BigInt(resp.excess),
price: BigInt(resp.price),
timestamp: resp.timestamp,
};
}
}
Loading

0 comments on commit 47f216e

Please sign in to comment.