Skip to content

Commit

Permalink
chore!: slim down chainInfoFragment and GasCostsFragment (#3286)
Browse files Browse the repository at this point in the history
* chore: slim down `GasCostsFragment`

* make breaking

* fix types

* fix fixture

* remove `latestBlock` from `chainInfo`

* Update rabid-rabbits-ribbit.md

* optimize block querying
  • Loading branch information
nedsalk authored Oct 10, 2024
1 parent 87da048 commit 55bfa6d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 422 deletions.
5 changes: 5 additions & 0 deletions .changeset/rabid-rabbits-ribbit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": minor
---

chore!: slim down `chainInfoFragment` and `GasCostsFragment`
184 changes: 20 additions & 164 deletions packages/account/src/providers/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -362,167 +362,6 @@ fragment DependentCostFragment on DependentCost {
}

fragment GasCostsFragment on GasCosts {
version
add
addi
aloc
and
andi
bal
bhei
bhsh
burn
cb
cfei
cfsi
div
divi
ecr1
eck1
ed19
eq
exp
expi
flag
gm
gt
gtf
ji
jmp
jne
jnei
jnzi
jmpf
jmpb
jnzf
jnzb
jnef
jneb
lb
log
lt
lw
mint
mlog
modOp
modi
moveOp
movi
mroo
mul
muli
mldv
noop
not
or
ori
poph
popl
pshh
pshl
ret
rvrt
sb
sll
slli
srl
srli
srw
sub
subi
sw
sww
time
tr
tro
wdcm
wqcm
wdop
wqop
wdml
wqml
wddv
wqdv
wdmd
wqmd
wdam
wqam
wdmm
wqmm
xor
xori
alocDependentCost {
...DependentCostFragment
}
bldd {
...DependentCostFragment
}
bsiz {
...DependentCostFragment
}
cfe {
...DependentCostFragment
}
cfeiDependentCost {
...DependentCostFragment
}
call {
...DependentCostFragment
}
ccp {
...DependentCostFragment
}
croo {
...DependentCostFragment
}
csiz {
...DependentCostFragment
}
ed19DependentCost {
...DependentCostFragment
}
k256 {
...DependentCostFragment
}
ldc {
...DependentCostFragment
}
logd {
...DependentCostFragment
}
mcl {
...DependentCostFragment
}
mcli {
...DependentCostFragment
}
mcp {
...DependentCostFragment
}
mcpi {
...DependentCostFragment
}
meq {
...DependentCostFragment
}
retd {
...DependentCostFragment
}
s256 {
...DependentCostFragment
}
scwq {
...DependentCostFragment
}
smo {
...DependentCostFragment
}
srwq {
...DependentCostFragment
}
swwq {
...DependentCostFragment
}
contractRoot {
...DependentCostFragment
}
Expand All @@ -532,6 +371,10 @@ fragment GasCostsFragment on GasCosts {
vmInitialization {
...DependentCostFragment
}
s256 {
...DependentCostFragment
}
ecr1
newStoragePerByte
}

Expand Down Expand Up @@ -561,9 +404,6 @@ fragment consensusParametersFragment on ConsensusParameters {

fragment chainInfoFragment on ChainInfo {
name
latestBlock {
...blockFragment
}
daHeight
consensusParameters {
...consensusParametersFragment
Expand Down Expand Up @@ -687,6 +527,22 @@ query estimatePredicates($encodedTransaction: HexString!) {
}
}

query getLatestBlock {
chain {
latestBlock {
...blockFragment
}
}
}

query getLatestBlockHeight {
chain {
latestBlock {
height
}
}
}

query getBlock($blockId: BlockId, $height: U32) {
block(id: $blockId, height: $height) {
...blockFragment
Expand Down
50 changes: 22 additions & 28 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import type {
GqlDryRunFailureStatusFragment,
GqlDryRunSuccessStatusFragment,
GqlFeeParameters as FeeParameters,
GqlGasCosts as GasCosts,
GqlGasCostsFragment as GasCosts,
GqlPredicateParameters as PredicateParameters,
GqlScriptParameters as ScriptParameters,
GqlTxParameters as TxParameters,
GqlPageInfo,
GqlRelayedTransactionFailed,
Requester,
GqlBlockFragment,
} from './__generated__/operations';
import type { Coin } from './coin';
import type { CoinQuantity, CoinQuantityLike } from './coin-quantity';
Expand Down Expand Up @@ -167,12 +168,6 @@ export type ChainInfo = {
name: string;
baseChainHeight: BN;
consensusParameters: ConsensusParameters;
latestBlock: {
id: string;
height: BN;
time: string;
transactions: Array<{ id: string }>;
};
};

/**
Expand Down Expand Up @@ -213,7 +208,7 @@ export type TransactionCost = {
// #endregion cost-estimation-1

const processGqlChain = (chain: GqlChainInfoFragment): ChainInfo => {
const { name, daHeight, consensusParameters, latestBlock } = chain;
const { name, daHeight, consensusParameters } = chain;

const {
contractParams,
Expand Down Expand Up @@ -267,14 +262,6 @@ const processGqlChain = (chain: GqlChainInfoFragment): ChainInfo => {
},
gasCosts,
},
latestBlock: {
id: latestBlock.id,
height: bn(latestBlock.height),
time: latestBlock.header.time,
transactions: latestBlock.transactions.map((i) => ({
id: i.id,
})),
},
};
};

Expand Down Expand Up @@ -744,8 +731,12 @@ Supported fuel-core version: ${supportedVersion}.`
* @returns A promise that resolves to the latest block number.
*/
async getBlockNumber(): Promise<BN> {
const { chain } = await this.operations.getChain();
return bn(chain.latestBlock.height, 10);
const {
chain: {
latestBlock: { height },
},
} = await this.operations.getLatestBlockHeight();
return bn(height);
}

/**
Expand Down Expand Up @@ -1476,19 +1467,22 @@ Supported fuel-core version: ${supportedVersion}.`
* @returns A promise that resolves to the block or null.
*/
async getBlock(idOrHeight: string | number | 'latest'): Promise<Block | null> {
let variables;
if (typeof idOrHeight === 'number') {
variables = { height: bn(idOrHeight).toString(10) };
} else if (idOrHeight === 'latest') {
variables = { height: (await this.getBlockNumber()).toString(10) };
} else if (idOrHeight.length === 66) {
variables = { blockId: idOrHeight };
let block: GqlBlockFragment | undefined | null;

if (idOrHeight === 'latest') {
const {
chain: { latestBlock },
} = await this.operations.getLatestBlock();
block = latestBlock;
} else {
variables = { blockId: bn(idOrHeight).toString(10) };
const isblockId = typeof idOrHeight === 'string' && idOrHeight.length === 66;
const variables = isblockId
? { blockId: idOrHeight }
: { height: bn(idOrHeight).toString(10) };
const response = await this.operations.getBlock(variables);
block = response.block;
}

const { block } = await this.operations.getBlock(variables);

if (!block) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TransactionType, OutputType } from '@fuel-ts/transactions';
import { arrayify, hexlify } from '@fuel-ts/utils';
import { clone } from 'ramda';

import type { GqlGasCosts } from '../__generated__/operations';
import type { GasCosts } from '../provider';
import { calculateMetadataGasForTxCreate } from '../utils/gas';

import { hashTransaction } from './hash-transaction';
Expand Down Expand Up @@ -118,7 +118,7 @@ export class CreateTransactionRequest extends BaseTransactionRequest {
});
}

metadataGas(gasCosts: GqlGasCosts): BN {
metadataGas(gasCosts: GasCosts): BN {
return calculateMetadataGasForTxCreate({
contractBytesSize: bn(arrayify(this.witnesses[this.bytecodeWitnessIndex] || '0x').length),
gasCosts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { type TransactionUpload, TransactionType } from '@fuel-ts/transactions';
import { arrayify, hexlify } from '@fuel-ts/utils';
import { clone } from 'ramda';

import type { GqlGasCosts } from '../__generated__/operations';
import type { ChainInfo } from '../provider';
import type { ChainInfo, GasCosts } from '../provider';
import { calculateMetadataGasForTxUpload, calculateMinGasForTxUpload } from '../utils';

import { hashTransaction } from './hash-transaction';
Expand Down Expand Up @@ -123,7 +122,7 @@ export class UploadTransactionRequest extends BaseTransactionRequest {
*
* @returns metadata gas cost for the upload transaction.
*/
metadataGas(gasCosts: GqlGasCosts): BN {
metadataGas(gasCosts: GasCosts): BN {
return calculateMetadataGasForTxUpload({
gasCosts,
txBytesSize: this.byteSize(),
Expand Down
Loading

0 comments on commit 55bfa6d

Please sign in to comment.