diff --git a/src/providers/abstract-provider.ts b/src/providers/abstract-provider.ts index b06a9080..88d4f275 100644 --- a/src/providers/abstract-provider.ts +++ b/src/providers/abstract-provider.ts @@ -840,10 +840,10 @@ export class AbstractProvider 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} 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} A promise that resolves to the latest Quai -> Qi rate for the given amount. */ - async getLatestQuaiRate(zone: Zone, amt: number = 1): Promise { + async getLatestQuaiRate(zone: Zone, amt: bigint): Promise { const blockNumber = await this.getBlockNumber(toShard(zone)); return this.getQuaiRateAtBlock(zone, blockNumber, amt); } @@ -856,18 +856,19 @@ export class AbstractProvider implements Provider { * @param {number} [amt=1] - The amount to get the rate for. Default is `1` * @returns {Promise} A promise that resolves to the Quai rate at the specified block. */ - async getQuaiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: number = 1): Promise { + async getQuaiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: bigint): Promise { 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, + }), + ); } /** @@ -923,7 +924,7 @@ export class AbstractProvider implements Provider { * @param {number} [amt=1] - The amount to get the rate for. Default is `1` * @returns {Promise} A promise that resolves to the latest Qi rate. */ - async getLatestQiRate(zone: Zone, amt: number = 1): Promise { + async getLatestQiRate(zone: Zone, amt: bigint): Promise { const blockNumber = await this.getBlockNumber(toShard(zone)); return this.getQiRateAtBlock(zone, blockNumber, amt); } @@ -936,18 +937,20 @@ export class AbstractProvider implements Provider { * @param {number} [amt=1] - The amount to get the rate for. Default is `1` * @returns {Promise} A promise that resolves to the Qi rate at the specified block. */ - async getQiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: number = 1): Promise { + async getQiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: bigint): Promise { 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, + }), + ); } /** @@ -1429,7 +1432,7 @@ export class AbstractProvider 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); } @@ -1438,7 +1441,7 @@ export class AbstractProvider 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; diff --git a/src/providers/provider.ts b/src/providers/provider.ts index efe737c9..46e851e1 100644 --- a/src/providers/provider.ts +++ b/src/providers/provider.ts @@ -2981,4 +2981,36 @@ export interface Provider extends ContractRunner, EventEmitterable} A promise resolving to the transaction pool inspect. */ txPoolInspect(zone: Zone): Promise; + + /** + * Resolves to the current Quai rate for the given amount. + * + * @param {bigint} amt - The amount in quais to get the rate for. + * @returns {Promise} A promise resolving to the latest Quai rate. + */ + getQiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: bigint): Promise; + + /** + * Resolves to the current Quai rate for the given amount. + * + * @param {bigint} amt - The amount in quais to get the rate for. + * @returns {Promise} A promise resolving to the latest Quai rate. + */ + getLatestQiRate(zone: Zone, amt: bigint): Promise; + + /** + * Resolves to the current Quai rate for the given amount. + * + * @param {bigint} amt - The amount in quais to get the rate for. + * @returns {Promise} A promise resolving to the latest Quai rate. + */ + getQuaiRateAtBlock(zone: Zone, blockTag: BlockTag, amt: bigint): Promise; + + /** + * Resolves to the current Quai rate for the given amount. + * + * @param {bigint} amt - The amount in quai to get the rate for. + * @returns {Promise} A promise resolving to the latest Quai->Qi rate. + */ + getLatestQuaiRate(zone: Zone, amt: bigint): Promise; } diff --git a/src/wallet/qi-hdwallet.ts b/src/wallet/qi-hdwallet.ts index e0aca94f..d1252d4c 100644 --- a/src/wallet/qi-hdwallet.ts +++ b/src/wallet/qi-hdwallet.ts @@ -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) { @@ -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);