Skip to content
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
4 changes: 2 additions & 2 deletions client/src/methods/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const AVALANCHE_CHAIN_IDS = {
export const AVALANCHE_NETWORK_IDS = {
MAINNET: 1,
FUJI: 5,
LOCAL: 1337,
LOCAL: 12345,
} as const;

export const P_CHAIN_MAINNET_ID = '11111111111111111111111111111111LpoYY';
Expand All @@ -29,4 +29,4 @@ export const C_CHAIN_ALIAS = 'C';

export const MAINNET_NETWORK_ID = 1;
export const TESTNET_NETWORK_ID = 5;
export const LOCAL_NETWORK_ID = 1337;
export const LOCAL_NETWORK_ID = 12345;
19 changes: 16 additions & 3 deletions client/src/methods/pChain/getFeeState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,23 @@ export async function getFeeState<chain extends Chain | undefined>(
params: {},
});

// Convert to BigInt
let capacity = BigInt(feeState.capacity);
const excess = BigInt(feeState.excess);
const price = BigInt(feeState.price);

// WORKAROUND: On local networks with Etna upgrade not yet active or freshly started,
// capacity can be 0 which causes all transactions to fail with "gas exceeds capacity".
// Set a default capacity of 1,000,000 to allow transactions to proceed.
// This matches the default maxGasCapacity from AvalancheGo's local network params.
if (capacity === 0n) {
capacity = BigInt(1_000_000);
}

return {
...feeState,
capacity: BigInt(feeState.capacity),
excess: BigInt(feeState.excess),
price: BigInt(feeState.price),
capacity,
excess,
price,
};
}
22 changes: 9 additions & 13 deletions client/src/methods/wallet/signXPTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getTxFromBytes } from "../../utils/getTxFromBytes.js";
import { getUtxosForAddress } from "../../utils/getUtxosForAddress.js";
import { AvalancheWalletRpcSchema } from "./avalancheWalletRPCSchema.js";
import { SepkSignatureLength } from "./constants.js";
import { getContextFromURI } from "./getContextFromURI.js";
import { getContextFromURI, getHRP } from "./getContextFromURI.js";
import {
SignXPTransactionParameters,
SignXPTransactionReturnType,
Expand Down Expand Up @@ -88,20 +88,16 @@ export async function signXPTransaction(
const paramAc = parseAvalancheAccount(account);
const xpAccount = paramAc?.xpAccount || client.xpAccount;

let isTestnet, networkId;
if (client.chain?.testnet) {
isTestnet = client.chain?.testnet;
networkId = isTestnet ? 5 : 1;
} else {
const context = params.context || (await getContextFromURI(client));
isTestnet = context.networkID === 5;
networkId = context.networkID;
}
// Always get the actual network ID from the node to determine the correct HRP
// Don't rely on chain.testnet flag as it doesn't distinguish between different test networks
const context = params.context || (await getContextFromURI(client));
const networkId = context.networkID;
const hrp = getHRP(networkId);

if (xpAccount) {
const xpAddress = publicKeyToXPAddress(
xpAccount.publicKey,
isTestnet ? "fuji" : "avax"
hrp
);

if (typeof txOrTxHex === "string") {
Expand Down Expand Up @@ -165,7 +161,7 @@ export async function signXPTransaction(
utxo
.getOutputOwners()
.addrs.findIndex(
(add) => add.toString(isTestnet ? "fuji" : "avax") === xpAddress
(add) => add.toString(hrp) === xpAddress
),
]);
}
Expand All @@ -191,7 +187,7 @@ export async function signXPTransaction(
const credentialIndex = credentials.length - 1;

const signerIndex = signingOwners.findIndex(
(owner) => owner.toString(isTestnet ? "fuji" : "avax") === xpAddress
(owner) => owner.toString(hrp) === xpAddress
);

if (signerIndex !== -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { P_CHAIN_ALIAS } from "../../consts.js";
import { getFeeState } from "../../pChain/getFeeState.js";
import { baseFee as getBaseFee } from "../../public/baseFee.js";
import { prepareExportTxn as prepareExportTxnCChain } from "../cChain/prepareExportTxn.js";
import { getContextFromURI } from "../getContextFromURI.js";
import { getContextFromURI, getHRP } from "../getContextFromURI.js";
import { prepareImportTxn as prepareImportTxnPChain } from "../pChain/prepareImportTxn.js";
import { sendXPTransaction } from "../sendXPTransaction.js";
import { SendParameters, SendReturnType } from "../types/send.js";
Expand Down Expand Up @@ -38,7 +38,7 @@ export async function transferCtoPChain(
client,
params.account,
P_CHAIN_ALIAS,
context.networkID === 5 ? "fuji" : "avax"
getHRP(context.networkID)
);

// Validate the P chain address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getBalance } from "../../pChain/getBalance.js";
import { getFeeState } from "../../pChain/getFeeState.js";
import { baseFee as getBaseFee } from "../../public/baseFee.js";
import { prepareImportTxn as prepareImportTxnCChain } from "../cChain/prepareImportTxn.js";
import { getContextFromURI } from "../getContextFromURI.js";
import { getContextFromURI, getHRP } from "../getContextFromURI.js";
import { prepareExportTxn as prepareExportTxnPChain } from "../pChain/prepareExportTxn.js";
import { sendXPTransaction } from "../sendXPTransaction.js";
import { SendParameters, SendReturnType } from "../types/send.js";
Expand All @@ -25,7 +25,7 @@ export async function transferPtoCChain(
params: TransferPtoCChainParameters
): Promise<TransferPtoCChainReturnType> {
const context = params.context || (await getContextFromURI(client));
const isTestnet = context.networkID === 5;
const hrp = getHRP(context.networkID);

// Note: CChain and PChain have different bech32 addresses for seedless accounts on core
// The value in both can be same for seed accounts
Expand All @@ -37,7 +37,7 @@ export async function transferPtoCChain(
client,
params.account,
C_CHAIN_ALIAS,
isTestnet ? "fuji" : "avax"
hrp
);
}

Expand All @@ -49,7 +49,7 @@ export async function transferPtoCChain(
client,
params.account,
P_CHAIN_ALIAS,
isTestnet ? "fuji" : "avax"
hrp
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { pvm } from "@avalabs/avalanchejs";
import { AvalancheWalletCoreClient } from "../../../clients/createAvalancheWalletCoreClient.js";
import { getBalance } from "../../pChain/getBalance.js";
import { getFeeState } from "../../pChain/getFeeState.js";
import { getContextFromURI } from "../getContextFromURI.js";
import { getContextFromURI, getHRP } from "../getContextFromURI.js";
import { prepareBaseTxn } from "../pChain/prepareBaseTxn.js";
import { sendXPTransaction } from "../sendXPTransaction.js";
import { SendParameters, SendReturnType } from "../types/send.js";
Expand All @@ -21,14 +21,13 @@ export async function transferPtoPChain(
params: TransferPtoPChainParameters
): Promise<TransferPtoPChainReturnType> {
const context = params.context || (await getContextFromURI(client));
const isTestnet = context.networkID === 5;

// Get the current account P chain address
const currentAccountPChainAddress = await getBech32AddressFromAccountOrClient(
client,
params.account,
"P",
isTestnet ? "fuji" : "avax"
getHRP(context.networkID)
);

// Validate the destination address
Expand Down