Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allowance state override in deposit #44

Merged
merged 4 commits into from
Jun 7, 2024
Merged
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
38 changes: 38 additions & 0 deletions src/chains/evm/common/constants/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { FolksTokenId } from "../../../../common/types/token.js";

import { EVM_FOLKS_CHAIN_ID } from "./chain.js";

import type { EvmFolksChainId } from "../types/chain.js";
import type { Erc20ContractSlot } from "../types/tokens.js";

export const CONTRACT_SLOT: Partial<
palace22 marked this conversation as resolved.
Show resolved Hide resolved
Record<
EvmFolksChainId,
{ erc20: Partial<Record<FolksTokenId, Erc20ContractSlot>> }
>
> = {
[EVM_FOLKS_CHAIN_ID.AVALANCHE_FUJI]: {
erc20: {
[FolksTokenId.USDC]: {
balanceOf: 0n,
allowance: 0n,
},
},
},
[EVM_FOLKS_CHAIN_ID.BASE_SEPOLIA]: {
erc20: {
[FolksTokenId.USDC]: {
balanceOf: 9n,
allowance: 10n,
},
},
},
[EVM_FOLKS_CHAIN_ID.ETHEREUM_SEPOLIA]: {
erc20: {
[FolksTokenId.USDC]: {
balanceOf: 0n,
allowance: 0n,
},
},
},
} as const;
20 changes: 20 additions & 0 deletions src/chains/evm/common/types/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { EvmFolksChainId } from "./chain.js";
import type { EvmAddress } from "../../../../common/types/address.js";
import type {
FolksTokenId,
TokenType,
} from "../../../../common/types/token.js";

export type Erc20ContractSlot = {
balanceOf: bigint;
allowance: bigint;
};

export type AllowanceStateOverride = {
owner: EvmAddress;
spender: EvmAddress;
folksChainId: EvmFolksChainId;
folksTokenId: FolksTokenId;
tokenType: TokenType;
amount: bigint;
};
38 changes: 37 additions & 1 deletion src/chains/evm/common/utils/contract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { BaseError, ContractFunctionRevertedError, getContract } from "viem";
import {
BaseError,
ContractFunctionRevertedError,
encodeAbiParameters,
getContract,
keccak256,
} from "viem";

import { ChainType } from "../../../../common/types/chain.js";
import { convertFromGenericAddress } from "../../../../common/utils/address.js";
Expand Down Expand Up @@ -83,3 +89,33 @@ export function extractRevertErrorName(err: unknown): string | undefined {
}
}
}

export function getBalanceOfSlotHash(address: EvmAddress, slot: bigint) {
return keccak256(
encodeAbiParameters(
[{ type: "address" }, { type: "uint256" }],
[address, slot],
),
);
}

export function getAllowanceSlotHash(
owner: EvmAddress,
spender: EvmAddress,
slot: bigint,
) {
return keccak256(
encodeAbiParameters(
[{ type: "address" }, { type: "bytes32" }],
[
spender,
keccak256(
encodeAbiParameters(
[{ type: "address" }, { type: "uint256" }],
[owner, slot],
),
),
],
),
);
}
53 changes: 53 additions & 0 deletions src/chains/evm/common/utils/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { encodeAbiParameters } from "viem";

import { TokenType } from "../../../../common/types/token.js";
import { CONTRACT_SLOT } from "../constants/tokens.js";

import { getAllowanceSlotHash } from "./contract.js";

import type { FolksTokenId } from "../../../../common/types/token.js";
import type { EvmFolksChainId } from "../types/chain.js";
import type { AllowanceStateOverride } from "../types/tokens.js";

export function getContractSlot(folksChainId: EvmFolksChainId) {
const contractSlot = CONTRACT_SLOT[folksChainId];
if (!contractSlot) {
throw new Error(
`Contract slot not found for folksChainId: ${folksChainId}`,
);
}
return contractSlot;
}

export function getFolksTokenContractSlot(
folksChainId: EvmFolksChainId,
folksTokenId: FolksTokenId,
) {
const contractSlot = getContractSlot(folksChainId);

const folksTokenContractSlot = contractSlot.erc20[folksTokenId];
if (!folksTokenContractSlot) {
throw new Error(
`Contract slot not found for folksTokenId: ${folksTokenId}`,
);
}
return folksTokenContractSlot;
}

export function getAllowanceStateOverride(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we ever need to override multiple allowances? If so maybe we should change this function to take in array as parameter

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowanceStatesOverride: Array<AllowanceStateOverride>,
) {
return allowanceStatesOverride
.filter(
(aso) =>
aso.tokenType == TokenType.ERC20 || aso.tokenType == TokenType.CIRCLE,
)
.map((aso) => ({
slot: getAllowanceSlotHash(
aso.owner,
aso.spender,
getFolksTokenContractSlot(aso.folksChainId, aso.folksTokenId).allowance,
),
value: encodeAbiParameters([{ type: "uint256" }], [aso.amount]),
}));
}
28 changes: 28 additions & 0 deletions src/chains/evm/spoke/modules/folks-evm-loan.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { multicall } from "viem/actions";

import { ChainType } from "../../../../common/types/chain.js";
import { TokenType } from "../../../../common/types/token.js";
import { convertFromGenericAddress } from "../../../../common/utils/address.js";
import { getSpokeTokenDataTokenAddress } from "../../../../common/utils/chain.js";
import {
calcNextPeriodReset,
calcPeriodNumber,
} from "../../../../common/utils/formulae.js";
import { getEvmSignerAccount } from "../../common/utils/chain.js";
import { sendERC20Approve } from "../../common/utils/contract.js";
import { getAllowanceStateOverride } from "../../common/utils/tokens.js";
import { getHubTokenData } from "../../hub/utils/chain.js";
import {
getBridgeRouterSpokeContract,
Expand Down Expand Up @@ -136,6 +140,21 @@ export const prepare = {
spokeChain.bridgeRouterAddress,
);

const spender = convertFromGenericAddress(
spokeTokenData.spokeAddress,
ChainType.EVM,
);
const stateDiff = getAllowanceStateOverride([
{
owner: sender,
spender,
folksChainId: spokeChain.folksChainId,
folksTokenId: spokeTokenData.folksTokenId,
tokenType: spokeTokenData.tokenType,
amount,
},
]);

// get adapter fees
const msgValue = await bridgeRouter.read.getSendFee([messageToSend]);

Expand All @@ -145,6 +164,15 @@ export const prepare = {
{
value: msgValue,
...transactionOptions,
stateOverride: [
{
address: convertFromGenericAddress(
getSpokeTokenDataTokenAddress(spokeTokenData),
ChainType.EVM,
),
stateDiff,
},
],
},
);

Expand Down