Skip to content

Commit

Permalink
chore(fuel): replace fuels with sub packages (#718)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedragon authored May 7, 2024
1 parent 95735a9 commit 74a682c
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 442 deletions.
8 changes: 6 additions & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@
"dependencies": {
"@aptos-labs/ts-sdk": "~1.13.0",
"@coral-xyz/borsh": "^0.29.0",
"@fuel-ts/abi-typegen": "^0.82.0",
"@fuel-ts/abi-typegen": "0.81.0",
"@fuel-ts/account": "0.81.0",
"@fuel-ts/address": "0.81.0",
"@fuel-ts/contract": "0.81.0",
"@fuel-ts/program": "0.81.0",
"@fuel-ts/transactions": "0.81.0",
"@mysten/sui.js": "~0.51.0",
"@project-serum/anchor": "^0.26.0",
"@sentio/bigdecimal": "9.1.1-patch.3",
Expand All @@ -78,7 +83,6 @@
"chalk": "^5.3.0",
"csv-parse": "^5.5.3",
"ethers": "npm:@sentio/[email protected]",
"fuels": "^0.81.0",
"js-sha3": "^0.9.3",
"lru-cache": "^10.2.0",
"mkdirp": "^1.0.4",
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/fuel/asset-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Data_FuelCall, FuelAssetHandlerConfig_AssetFilter } from '@sentio/proto
import { FuelNetwork, getRpcEndpoint } from './network.js'
import { FuelContext } from './context.js'
import { decodeFuelTransaction } from './transaction.js'
import { InputType, OutputType, Provider } from 'fuels'
import { Provider } from '@fuel-ts/account'
import { InputType, OutputType } from '@fuel-ts/transactions'

export class FuelAssetProcessor implements FuelBaseProcessor<FuelAssetProcessorConfig> {
callHandlers: CallHandler<Data_FuelCall>[] = []
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/fuel/base-processor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FuelProcessor, FuelProcessorConfig } from './fuel-processor.js'
import { JsonAbi } from 'fuels'
import { JsonAbi } from '@fuel-ts/abi-coder'
import { FuelCall, FuelContext } from './context.js'
import { FuelFetchConfig } from './transaction.js'
import { FuelChainId } from '@sentio/chain'
Expand Down
30 changes: 22 additions & 8 deletions packages/sdk/src/fuel/codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@ export async function codegen(abisDir: string, outDir: string) {
}

function patchImport(contents: string) {
return contents.replace(/from\s+['"](\..+)['"]/g, `from '\$1.js'`)
return contents
.replace(
`import { Interface, Contract, ContractFactory } from "fuels";`,
`import { Contract, } from "@fuel-ts/program";
import { ContractFactory } from "@fuel-ts/contract";
import { Interface } from "@fuel-ts/abi-coder";`
)
.replace(
`import type { Provider, Account, AbstractAddress, BytesLike, DeployContractOptions, StorageSlot } from "fuels";
`,
`import type { Provider, Account } from "@fuel-ts/account";
import type { AbstractAddress, BytesLike } from "@fuel-ts/interfaces";
import type { DeployContractOptions } from "@fuel-ts/contract";
import type { StorageSlot } from "@fuel-ts/transactions";`
)
.replace(/from\s+['"](\..+)['"]/g, `from '\$1.js'`)
}

function patchEnumType(contents: string) {
Expand Down Expand Up @@ -65,12 +80,13 @@ async function codegenInternal(abisDir: string, outDir: string): Promise<number>

mkdirp.sync(outDir)
mkdirp.sync(path.join(outDir, 'factories'))

let count = 0
abiTypeGen.files.forEach((file) => {
if (!file.path.endsWith('.hex.ts')) {
let content = patchImport(file.contents)
content = patchEnumType(content)
writeFileSync(file.path, content)
count++
}
})

Expand All @@ -89,11 +105,8 @@ import {${abi.name}__factory } from './factories/${abi.name}__factory.js'
import {${abi.commonTypesInUse.join(',')}} from './common.js'
import {${importedTypes.join(',')}} from './${abi.name}.js'
import type {
BigNumberish,
BN,
BytesLike,
} from 'fuels';
import type { BigNumberish, BN } from '@fuel-ts/math';
import type { BytesLike } from '@fuel-ts/interfaces';
namespace ${name} {
Expand All @@ -116,9 +129,10 @@ ${abi.functions.map((f) => genOnCallFunction(name, f)).join('\n')}
}
`
writeFileSync(filePath, content)
count++
}

return allABIFiles.length
return count
}

function genCallType(f: IFunction) {
Expand Down
9 changes: 1 addition & 8 deletions packages/sdk/src/fuel/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseContext, Labels, normalizeLabels } from '../core/index.js'
import { ChainId } from '@sentio/chain'
import { RecordMetaData } from '@sentio/protos'
import { BaseAssetId, InputType, InvocationCallResult } from 'fuels'
import { InvocationCallResult } from '@fuel-ts/program'
import { FuelTransaction } from './transaction.js'

export type FuelCall = InvocationCallResult
Expand All @@ -21,13 +21,6 @@ export class FuelContext extends BaseContext {
}

protected getMetaDataInternal(name: string, labels: Labels): RecordMetaData {
let address = ''
for (const input of this.transaction?.transaction?.inputs || []) {
if (input.type == InputType.Coin && input.assetId == BaseAssetId) {
address = input.owner
}
}

return {
address: this.contractAddress,
contractName: this.contractName,
Expand Down
5 changes: 4 additions & 1 deletion packages/sdk/src/fuel/fuel-processor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Data_FuelCall, FuelCallFilter } from '@sentio/protos'
import { FuelCall, FuelContext } from './context.js'
import { bn, Contract, Interface, InvocationCallResult, JsonAbi, Provider } from 'fuels'
import { Provider } from '@fuel-ts/account'
import { Contract, InvocationCallResult } from '@fuel-ts/program'
import { Interface, JsonAbi } from '@fuel-ts/abi-coder'
import { bn } from '@fuel-ts/math'
import { FuelNetwork, getRpcEndpoint } from './network.js'
import {
decodeFuelTransactionWithAbi,
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/fuel/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FuelChainId } from '@sentio/chain'
import { FUEL_BETA_5_NETWORK_URL, FUEL_NETWORK_URL } from 'fuels'
import { FUEL_BETA_5_NETWORK_URL, FUEL_NETWORK_URL } from '@fuel-ts/account/configs'

export type FuelNetwork = FuelChainId
export const FuelNetwork = <const>{
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/fuel/tests/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import abi from './abis/counter-contract-abi.json'
import testData from './test-data.json'
import testTransferData from './transfer-data.json'
import { FuelAssetProcessor } from '../asset-processor.js'
import { BaseAssetId } from 'fuels'
import { afterAll } from '@jest/globals'
import { State } from '@sentio/runtime'
import { BaseAssetId } from '@fuel-ts/address/configs'

describe('fuel network tests', () => {
const ADDRESS = '0x730adcb9974977e0f4fd46488b6aac04dade7d846d15ca026bff61279e265813'
Expand Down
19 changes: 7 additions & 12 deletions packages/sdk/src/fuel/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import {
AbiMap,
arrayify,
assembleTransactionSummary,
bn,
processGqlReceipt,
Provider,
TransactionCoder,
TransactionSummary
} from 'fuels'
import { AbiMap, assembleTransactionSummary, processGqlReceipt, Provider, TransactionSummary } from '@fuel-ts/account'
import { TransactionCoder } from '@fuel-ts/transactions'
import { bn } from '@fuel-ts/math'
import { arrayify } from '@fuel-ts/utils'

export type FuelFetchConfig = {
includeFailed?: boolean
Expand All @@ -25,7 +19,7 @@ export function decodeFuelTransaction(gqlTransaction: any, provider: Provider):
const rawPayload = arrayify(gqlTransaction.rawPayload)

const [decodedTransaction] = new TransactionCoder().decode(rawPayload, 0)
const { gasPerByte, gasPriceFactor, maxInputs, gasCosts } = provider.getChain().consensusParameters
const { gasCosts, maxInputs, gasPerByte, gasPriceFactor } = provider.getChain().consensusParameters
const blockNumber = gqlTransaction.status?.block?.header?.height
return {
...assembleTransactionSummary({
Expand All @@ -50,7 +44,8 @@ export function decodeFuelTransactionWithAbi(gqlTransaction: any, abiMap: AbiMap

const receipts = gqlTransaction.receipts?.map(processGqlReceipt) || []

const { gasPerByte, gasPriceFactor, maxInputs, gasCosts } = provider.getChain().consensusParameters
const { gasCosts, maxInputs, gasPerByte, gasPriceFactor } = provider.getChain().consensusParameters

const blockNumber = gqlTransaction.status?.block?.header?.height

return {
Expand Down
Loading

0 comments on commit 74a682c

Please sign in to comment.