UNPKG

@arcana/ca-sdk

Version:

Arcana Network's chain abstraction SDK for unified balance in Web3 apps

57 lines (56 loc) 2.24 kB
import { Universe } from "@arcana/ca-common"; import Decimal from "decimal.js"; import { Account } from "fuels"; import { getLogger } from "../../logger"; import { cosmosFillCheck, divDecimals } from "../../utils"; import { requestTimeout } from "../../utils"; import { NativeRequestBase } from "../common/native.base"; import { simulate } from "./common"; const logger = getLogger(); class FuelNativeTransfer extends NativeRequestBase { constructor(input) { super(input); this.input = input; this.allowances = null; this.destinationUniverse = Universe.FUEL; if (!this.input.fuel?.tx) { throw new Error("Invalid request"); } if (!this.input.fuel.address) { throw new Error("fuel address missing"); } this.tx = this.input.fuel.tx; this.fuelAddress = this.input.fuel.address; } async simulateTx() { logger.debug("fuel: reached simulate tx"); const nativeCurrency = this.input.chain.nativeCurrency; if (this.simulateTxRes) { let gasFee = new Decimal(0); if (!this.input.options.bridge) { const { assembledRequest } = await this.input.fuel.provider.assembleTx({ feePayerAccount: new Account(this.input.fuel.address), request: this.input.fuel.tx, }); gasFee = divDecimals(BigInt(assembledRequest.maxFee.toString()) * 2n, nativeCurrency.decimals); } return { ...this.simulateTxRes, gasFee, }; } this.simulateTxRes = await simulate(this.tx, this.fuelAddress, this.input.fuel.provider, this.input.chainList); if (this.input.options.bridge && this.simulateTxRes) { this.simulateTxRes.gasFee = new Decimal(0); } return this.simulateTxRes; } async waitForFill(_, intentID) { const ac = new AbortController(); await Promise.race([ requestTimeout(3, ac), cosmosFillCheck(intentID, this.input.options.networkConfig.GRPC_URL, this.input.options.networkConfig.COSMOS_URL, ac), ]); } } export default FuelNativeTransfer;