@arcana/ca-sdk
Version:
Arcana Network's chain abstraction SDK for unified balance in Web3 apps
65 lines (64 loc) • 2.48 kB
JavaScript
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 RequestBase from "../common/base";
import { nativeRequestParseSimulation } from "../common/utils";
import { simulate } from "./common";
const logger = getLogger();
class FuelNativeTransfer extends RequestBase {
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;
}
parseSimulation(input) {
return nativeRequestParseSimulation({
...input,
bridge: this.input.options.bridge,
chain: this.input.chain,
});
}
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;