Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tx fee #323

Merged
merged 4 commits into from
Oct 17, 2024
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
45 changes: 24 additions & 21 deletions src/providers/abstract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,10 +840,10 @@ export class AbstractProvider<C = FetchRequest> implements Provider {
* Get the latest Quai rate for a zone.
*
* @param {Zone} zone - The zone to get the rate for.
* @param {number} [amt=1] - The amount to get the rate for. Default is `1`
* @returns {Promise<bigint>} A promise that resolves to the latest Quai rate.
* @param {number} [amt=1] - The amount in quais to get the rate for. Default is `1`
* @returns {Promise<bigint>} A promise that resolves to the latest Quai -> Qi rate for the given amount.
*/
async getLatestQuaiRate(zone: Zone, amt: number = 1): Promise<bigint> {
async getLatestQuaiRate(zone: Zone, amt: bigint): Promise<bigint> {
const blockNumber = await this.getBlockNumber(toShard(zone));
return this.getQuaiRateAtBlock(zone, blockNumber, amt);
}
Expand All @@ -856,18 +856,19 @@ export class AbstractProvider<C = FetchRequest> implements Provider {
* @param {number} [amt=1] - The amount to get the rate for. Default is `1`
* @returns {Promise<bigint>} A promise that resolves to the Quai rate at the specified block.
*/
async getQuaiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: number = 1): Promise<bigint> {
async getQuaiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: bigint): Promise<bigint> {
let resolvedBlockTag = this._getBlockTag(toShard(zone), blockTag);
if (typeof resolvedBlockTag !== 'string') {
resolvedBlockTag = await resolvedBlockTag;
}

return await this.#perform({
method: 'getQuaiRateAtBlock',
blockTag: resolvedBlockTag,
amt,
zone: zone,
});
return getBigInt(
await this.#perform({
method: 'getQuaiRateAtBlock',
blockTag: resolvedBlockTag,
amt: Number(amt),
zone: zone,
}),
);
}

/**
Expand Down Expand Up @@ -923,7 +924,7 @@ export class AbstractProvider<C = FetchRequest> implements Provider {
* @param {number} [amt=1] - The amount to get the rate for. Default is `1`
* @returns {Promise<bigint>} A promise that resolves to the latest Qi rate.
*/
async getLatestQiRate(zone: Zone, amt: number = 1): Promise<bigint> {
async getLatestQiRate(zone: Zone, amt: bigint): Promise<bigint> {
const blockNumber = await this.getBlockNumber(toShard(zone));
return this.getQiRateAtBlock(zone, blockNumber, amt);
}
Expand All @@ -936,18 +937,20 @@ export class AbstractProvider<C = FetchRequest> implements Provider {
* @param {number} [amt=1] - The amount to get the rate for. Default is `1`
* @returns {Promise<bigint>} A promise that resolves to the Qi rate at the specified block.
*/
async getQiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: number = 1): Promise<bigint> {
async getQiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: bigint): Promise<bigint> {
let resolvedBlockTag = this._getBlockTag(toShard(zone), blockTag);
if (typeof resolvedBlockTag !== 'string') {
resolvedBlockTag = await resolvedBlockTag;
}

return await this.#perform({
method: 'getQiRateAtBlock',
blockTag: resolvedBlockTag,
amt,
zone: zone,
});
return getBigInt(
await this.#perform({
method: 'getQiRateAtBlock',
blockTag: resolvedBlockTag,
amt: Number(amt),
zone: zone,
}),
);
}

/**
Expand Down Expand Up @@ -1429,7 +1432,7 @@ export class AbstractProvider<C = FetchRequest> implements Provider {
gasPrice: (async () => {
try {
const value = await this.#perform({ method: 'getGasPrice', txType, zone: zone });
return getBigInt(value, '%response') * BigInt(Math.pow(10, txType ? 9 : 0));
return getBigInt(value, '%response');
} catch (error) {
console.log(error);
}
Expand All @@ -1438,7 +1441,7 @@ export class AbstractProvider<C = FetchRequest> implements Provider {
minerTip: (async () => {
try {
const value = txType ? await this.#perform({ method: 'getMinerTip', zone: zone }) : 0;
return getBigInt(value, '%response') * BigInt(Math.pow(10, txType ? 9 : 0));
return getBigInt(value, '%response');
// eslint-disable-next-line no-empty
} catch (error) {}
return null;
Expand Down
32 changes: 32 additions & 0 deletions src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2981,4 +2981,36 @@ export interface Provider extends ContractRunner, EventEmitterable<ProviderEvent
* @returns {Promise<txpoolInspectResponse>} A promise resolving to the transaction pool inspect.
*/
txPoolInspect(zone: Zone): Promise<txpoolInspectResponse>;

/**
* Resolves to the current Quai rate for the given amount.
*
* @param {bigint} amt - The amount in quais to get the rate for.
* @returns {Promise<bigint>} A promise resolving to the latest Quai rate.
*/
getQiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: bigint): Promise<bigint>;

/**
* Resolves to the current Quai rate for the given amount.
*
* @param {bigint} amt - The amount in quais to get the rate for.
* @returns {Promise<bigint>} A promise resolving to the latest Quai rate.
*/
getLatestQiRate(zone: Zone, amt: bigint): Promise<bigint>;

/**
* Resolves to the current Quai rate for the given amount.
*
* @param {bigint} amt - The amount in quais to get the rate for.
* @returns {Promise<bigint>} A promise resolving to the latest Quai rate.
*/
getQuaiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: bigint): Promise<bigint>;

/**
* Resolves to the current Quai rate for the given amount.
*
* @param {bigint} amt - The amount in quai to get the rate for.
* @returns {Promise<bigint>} A promise resolving to the latest Quai->Qi rate.
*/
getLatestQuaiRate(zone: Zone, amt: bigint): Promise<bigint>;
}
9 changes: 6 additions & 3 deletions src/wallet/qi-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export class QiHDWallet extends AbstractHDWallet {
* @param {string} address - The address to locate.
* @returns {NeuteredAddressInfo | PaymentChannelAddressInfo | null} The address info or null if not found.
*/
private locateAddressInfo(address: string): NeuteredAddressInfo | PaymentChannelAddressExtendedInfo | null {
public locateAddressInfo(address: string): NeuteredAddressInfo | PaymentChannelAddressExtendedInfo | null {
// First, try to get standard address info
let addressInfo = this.getAddressInfo(address);
if (addressInfo) {
Expand Down Expand Up @@ -449,10 +449,13 @@ export class QiHDWallet extends AbstractHDWallet {
);

const gasLimit = await this.provider.estimateGas(tx);
const feeData = await this.provider.getFeeData(originZone, false);
const gasPrice = denominations[1]; // 0.005 Qi
const minerTip = (gasLimit * gasPrice) / 100n; // 1% extra as tip
// const feeData = await this.provider.getFeeData(originZone, true);
// const conversionRate = await this.provider.getLatestQuaiRate(originZone, feeData.gasPrice);

// 5.6 Calculate total fee for the transaction using the gasLimit, gasPrice, and minerTip
const totalFee = gasLimit * (feeData.gasPrice ?? 1n) + gasLimit * (feeData.minerTip ?? 0n);
const totalFee = gasLimit * gasPrice + minerTip;

// Get new selection with fee
selection = fewestCoinSelector.performSelection(spendTarget, totalFee);
Expand Down
Loading