Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"npm": ">=9.0.0"
},
"dependencies": {
"@avail-project/ca-common": "2.0.0",
"@avail-project/ca-common": "github:availproject/ca-common#feat/protocol-fee-per-chain",
"@cosmjs/proto-signing": "^0.34.1",
"@cosmjs/stargate": "^0.34.1",
"@metamask/safe-event-emitter": "3.1.2",
Expand Down
5 changes: 5 additions & 0 deletions src/commons/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ export type FeeStoreData = {
}[];
protocol: {
feeBP: string;
feePerChain: {
universe: Universe;
chainID: number;
feeBP: string;
}[];
};
};
solverRoutes: {
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/ca-base/requestHandlers/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ class BridgeHandler {

const borrow = amount;

const protocolFee = feeStore.calculateProtocolFee(borrow);
const protocolFee = feeStore.calculateProtocolFee(borrow, this.params.dstChain);
intent.fees.protocol = protocolFee.toFixed();

let borrowWithFee = borrow.add(gasInToken).add(protocolFee);
Expand Down
20 changes: 12 additions & 8 deletions src/sdk/ca-base/swap/rff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from 'viem';
import {
type BridgeAsset,
type Chain,
type ChainListType,
type CosmosOptions,
type EoaToEphemeralCallMap,
Expand Down Expand Up @@ -78,7 +79,9 @@ export const createIntent = ({
feeStore,
output,
address,
dstChain,
}: {
dstChain: Chain;
assets: BridgeAsset[];
feeStore: FeeStore;
address: Hex;
Expand Down Expand Up @@ -117,7 +120,7 @@ export const createIntent = ({
intent.destination.amount = new Decimal(borrow);
intent.destination.tokenContract = output.tokenAddress;

const protocolFee = feeStore.calculateProtocolFee(borrow);
const protocolFee = feeStore.calculateProtocolFee(borrow, dstChain);
borrow = borrow.add(protocolFee);

intent.fees.protocol = protocolFee.toFixed();
Expand Down Expand Up @@ -301,12 +304,18 @@ export const createBridgeRFF = async ({
tokenAddress: `0x${string}`;
};
}) => {
const dstChain = config.chainList.getChainByID(Number(output.chainID));
if (!dstChain) {
throw Errors.chainNotFound(output.chainID);
}

logger.debug('createBridgeRFF', { input, output });

const feeStore = await getFeeStore(config.cosmosQueryClient);
const depositCalls: RFFDepositCallMap = {};

const { eoaToEphemeralCalls, intent } = createIntent({
dstChain,
assets: input.assets,
feeStore,
output,
Expand Down Expand Up @@ -426,12 +435,7 @@ export const createBridgeRFF = async ({
};
}

const chain = config.chainList.getChainByID(Number(output.chainID));
if (!chain) {
throw Errors.chainNotFound(output.chainID);
}

const ws = webSocket(chain.rpcUrls.default.webSocket[0]);
const ws = webSocket(dstChain.rpcUrls.default.webSocket[0]);
const pc = createPublicClient({
transport: ws,
});
Expand All @@ -447,7 +451,7 @@ export const createBridgeRFF = async ({
filled: false,
intentID,
promise: evmWaitForFill(
config.chainList.getVaultContractAddress(chain.id),
config.chainList.getVaultContractAddress(dstChain.id),
pc,
s.requestHash,
intentID,
Expand Down
2 changes: 2 additions & 0 deletions src/sdk/ca-base/swap/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ const _exactOutRoute = async (
tokenAddress: convertToEVMAddress(dstChainCOT.tokenAddress),
};
const intentResponse = createIntent({
dstChain,
assets: bridgeAssets,
feeStore,
output: pendingBridge,
Expand Down Expand Up @@ -934,6 +935,7 @@ const _exactInRoute = async (
decimals: dstChainCOT.decimals,
tokenAddress: convertToEVMAddress(dstChainCOT.tokenAddress),
estimatedFees: createIntent({
dstChain,
assets: bridgeAssets,
feeStore,
output: {
Expand Down
19 changes: 17 additions & 2 deletions src/sdk/ca-base/utils/api.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type Long from 'long';
import { pack, unpack } from 'msgpackr';
import { bytesToBigInt, bytesToNumber, type Hex, toHex } from 'viem';
import {
type Chain,
type ChainListType,
type CosmosQueryClient,
type FeeStoreData,
Expand Down Expand Up @@ -352,8 +353,15 @@ export class FeeStore {
return new Decimal(fulfilmentFeeBasis.fee ?? 0).div(Decimal.pow(10, decimals));
}

public calculateProtocolFee(borrow: Decimal) {
const protocolFeeBasis = new Decimal(this.data.fee.protocol.feeBP ?? 0).div(Decimal.pow(10, 4));
public calculateProtocolFee(borrow: Decimal, dstChain: Chain) {
const bp = this.data.fee.protocol.feePerChain.find((fpc) => {
return fpc.chainID === dstChain.id && dstChain.universe === fpc.universe;
});

const protocolFeeBasis = new Decimal(bp?.feeBP ?? this.data.fee.protocol.feeBP ?? 0).div(
Decimal.pow(10, 4)
);

return borrow.mul(protocolFeeBasis);
}

Expand Down Expand Up @@ -396,6 +404,7 @@ const getFeeStore = async (cosmosQueryClient: CosmosQueryClient) => {
fulfilment: [],
protocol: {
feeBP: '0',
feePerChain: [],
},
},
solverRoutes: [],
Expand All @@ -411,6 +420,12 @@ const getFeeStore = async (cosmosQueryClient: CosmosQueryClient) => {
protocol: p.value.ProtocolFees?.feeBP,
});
feeData.fee.protocol.feeBP = p.value.ProtocolFees?.feeBP.toString(10) ?? '0';
feeData.fee.protocol.feePerChain =
p.value.ProtocolFees?.perChainFeeBP.map((fbp) => ({
universe: fbp.universe,
chainID: bytesToNumber(fbp.chainID),
feeBP: fbp.feeBP.toString(10),
})) ?? [];
feeData.fee.collection =
p.value.ProtocolFees?.collectionFees.map((fee) => {
return {
Expand Down
7 changes: 6 additions & 1 deletion src/sdk/ca-base/utils/rff.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ const calculateMaxBridgeFee = async ({

const sourceChainIds: number[] = [];

const protocolFee = feeStore.calculateProtocolFee(new Decimal(borrow));
const dstChain = chainList.getChainByID(dst.chainId);
if (!dstChain) {
throw Errors.chainNotFound(dst.chainId);
}

const protocolFee = feeStore.calculateProtocolFee(new Decimal(borrow), dstChain);
let borrowWithFee = borrow.add(protocolFee);

const fulfilmentFee = feeStore.calculateFulfilmentFee({
Expand Down