Skip to content

Commit

Permalink
Merge branch 'main' into rewards-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
gidonkatten authored Feb 3, 2025
2 parents 5e17b78 + d5cc44f commit 5f5a158
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 28 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @folks-finance/xchain-sdk

## 0.0.53

### Patch Changes

- [#216](https://github.com/Folks-Finance/xchain-js-sdk/pull/216) [`ea928d9`](https://github.com/Folks-Finance/xchain-js-sdk/commit/ea928d9590b39e77cdbbd28177e15652c702507e) Thanks [@moshenskyDV](https://github.com/moshenskyDV)! - Subtract arbitrum L1 fee from estimation

## 0.0.52

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@folks-finance/xchain-sdk",
"description": "The official JavaScript SDK for the Folks Finance Cross-Chain Lending Protocol",
"version": "0.0.52",
"version": "0.0.53",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand Down
17 changes: 17 additions & 0 deletions src/chains/evm/common/constants/abi/arbitrum-node-interface-abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const ArbitrumNodeInterfaceAbi = [
{
name: "gasEstimateL1Component",
type: "function",
inputs: [
{ name: "to", type: "address" },
{ name: "contractCreation", type: "bool" },
{ name: "data", type: "bytes" },
],
outputs: [
{ name: "gasEstimateForL1", type: "uint64" },
{ name: "baseFee", type: "uint256" },
{ name: "l1BaseFeeEstimate", type: "uint256" },
],
stateMutability: "payable",
},
] as const;
1 change: 1 addition & 0 deletions src/chains/evm/common/constants/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ARBITRUM_NODE_INTERFACE = "0x00000000000000000000000000000000000000C8";
109 changes: 82 additions & 27 deletions src/chains/evm/common/utils/message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { concat, isHex } from "viem";
import { concat, encodeFunctionData, getContract, isHex } from "viem";

import { BYTES32_LENGTH, UINT16_LENGTH, UINT256_LENGTH, UINT8_LENGTH } from "../../../../common/constants/bytes.js";
import { FOLKS_CHAIN_ID } from "../../../../common/constants/chain.js";
import { FINALITY } from "../../../../common/constants/message.js";
import { ChainType } from "../../../../common/types/chain.js";
import { Action } from "../../../../common/types/message.js";
Expand All @@ -9,6 +10,8 @@ import { convertFromGenericAddress, isGenericAddress } from "../../../../common/
import { convertBooleanToByte, convertNumberToBytes, getRandomBytes } from "../../../../common/utils/bytes.js";
import { isAccountId } from "../../../../common/utils/lending.js";
import { exhaustiveCheck } from "../../../../utils/exhaustive-check.js";
import { ArbitrumNodeInterfaceAbi } from "../constants/abi/arbitrum-node-interface-abi.js";
import { ARBITRUM_NODE_INTERFACE } from "../constants/address.js";

import {
getCCIPDataAdapterContract as getCcipDataAdapterContract,
Expand All @@ -31,7 +34,7 @@ import type {
} from "../../../../common/types/message.js";
import type { FolksHubTokenType, FolksSpokeTokenType } from "../../../../common/types/token.js";
import type { CCIPAny2EvmMessage } from "../types/gmp.js";
import type { Client, Hex, StateOverride } from "viem";
import type { Client, Hex, ContractFunctionArgs, StateOverride } from "viem";

export const buildMessageParams = ({
adapters,
Expand Down Expand Up @@ -443,31 +446,44 @@ export async function estimateEvmWormholeDataGasLimit(
) {
const messageId = getRandomBytes(BYTES32_LENGTH);
const wormholeDataAdapter = getWormholeDataAdapterContract(provider, wormholeDataAdapterAddress);
return await wormholeDataAdapter.estimateGas.receiveWormholeMessages(
[
encodeEvmPayloadWithMetadata(
messageBuilderParams.adapters.returnAdapterId,
returnGasLimit,
messageBuilderParams.sender,
messageBuilderParams.handler,
buildMessagePayload(
messageBuilderParams.action,
messageBuilderParams.accountId,
messageBuilderParams.userAddress,
buildEvmMessageData(messageBuilderParams),
),

const args: ContractFunctionArgs<typeof wormholeDataAdapter.abi, "payable", "receiveWormholeMessages"> = [
encodeEvmPayloadWithMetadata(
messageBuilderParams.adapters.returnAdapterId,
returnGasLimit,
messageBuilderParams.sender,
messageBuilderParams.handler,
buildMessagePayload(
messageBuilderParams.action,
messageBuilderParams.accountId,
messageBuilderParams.userAddress,
buildEvmMessageData(messageBuilderParams),
),
[],
sourceWormholeDataAdapterAddress,
sourceWormholeChainId,
messageId,
],
{
value: receiverValue,
account: wormholeRelayer,
stateOverride: [{ address: wormholeRelayer, balance: receiverValue }, ...stateOverride],
},
),
[],
sourceWormholeDataAdapterAddress,
sourceWormholeChainId,
messageId,
] as const;

const gasLimit = await wormholeDataAdapter.estimateGas.receiveWormholeMessages(args, {
value: receiverValue,
account: wormholeRelayer,
stateOverride: [{ address: wormholeRelayer, balance: receiverValue }, ...stateOverride],
});

const gasToSubtract = await getGasToSubtract(
provider,
messageBuilderParams.destinationChainId,
convertFromGenericAddress(wormholeDataAdapterAddress, ChainType.EVM),
encodeFunctionData({
abi: wormholeDataAdapter.abi,
functionName: "receiveWormholeMessages",
args,
}),
);

return gasLimit - gasToSubtract;
}

export async function estimateEvmCcipDataGasLimit(
Expand Down Expand Up @@ -498,12 +514,25 @@ export async function estimateEvmCcipDataGasLimit(
),
),
destTokenAmounts: [],
};
} as const;

const ccipDataAdapter = getCcipDataAdapterContract(provider, ccipDataAdapterAddress);
return await ccipDataAdapter.estimateGas.ccipReceive([ccipMessage], {
const gasLimit = await ccipDataAdapter.estimateGas.ccipReceive([ccipMessage], {
account: ccipRouter,
});

const gasToSubtract = await getGasToSubtract(
provider,
messageBuilderParams.destinationChainId,
convertFromGenericAddress(ccipDataAdapterAddress, ChainType.EVM),
encodeFunctionData({
abi: ccipDataAdapter.abi,
functionName: "ccipReceive",
args: [ccipMessage],
}),
);

return gasLimit - gasToSubtract;
}

export function getSendTokenStateOverride(folksChainId: FolksChainId, extraArgs: OverrideTokenData) {
Expand All @@ -527,3 +556,29 @@ export function getSendTokenStateOverride(folksChainId: FolksChainId, extraArgs:
}
return [];
}

async function getGasToSubtract(
provider: Client,
destinationChainId: FolksChainId,
to: EvmAddress,
data: Hex,
): Promise<bigint> {
switch (destinationChainId) {
case FOLKS_CHAIN_ID.ARBITRUM:
case FOLKS_CHAIN_ID.ARBITRUM_SEPOLIA:
return getArbitrumL1Estimation(provider, to, data);
default:
return 0n;
}
}

async function getArbitrumL1Estimation(provider: Client, to: EvmAddress, data: Hex): Promise<bigint> {
const nodeInterfaceContract = getContract({
abi: ArbitrumNodeInterfaceAbi,
address: ARBITRUM_NODE_INTERFACE,
client: provider,
});

const { result } = await nodeInterfaceContract.simulate.gasEstimateL1Component([to, false, data]);
return result[0];
}

0 comments on commit 5f5a158

Please sign in to comment.