Skip to content

Commit

Permalink
refactor: loan module message to send (#21)
Browse files Browse the repository at this point in the history
Refactored the loan modules to handle messageToSend creation on
FolksAccount, abstracting from the used spoke chain.

Removed prepareRaw from the evm loan module.
  • Loading branch information
palace22 authored May 29, 2024
1 parent 88aa468 commit a1a086c
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 345 deletions.
147 changes: 138 additions & 9 deletions src/chains/evm/common/utils/message.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { concat, isHex } from "viem";

import { UINT16_LENGTH } from "../../../../common/constants/bytes.js";
import {
UINT16_LENGTH,
UINT256_LENGTH,
UINT8_LENGTH,
} from "../../../../common/constants/bytes.js";
import { FINALITY } from "../../../../common/constants/message.js";
import { ChainType } from "../../../../common/types/chain.js";
import { Action } from "../../../../common/types/message.js";
import { TokenType } from "../../../../common/types/token.js";
import {
convertToGenericAddress,
getRandomGenericAddress,
isGenericAddress,
} from "../../../../common/utils/address.js";
import { convertNumberToBytes } from "../../../../common/utils/bytes.js";
import {
convertBooleanToByte,
convertNumberToBytes,
} from "../../../../common/utils/bytes.js";
import { exhaustiveCheck } from "../../../../utils/exhaustive-check.js";

import type { GenericAddress } from "../../../../common/types/chain.js";
Expand Down Expand Up @@ -48,6 +56,45 @@ export function buildMessagePayload(
]);
}

export function extraArgsToBytes(
tokenAddr: GenericAddress,
recipientAddr: GenericAddress,
amount: bigint,
): Hex {
if (!isGenericAddress(tokenAddr)) throw Error("Unknown token address format");
if (!isGenericAddress(recipientAddr))
throw Error("Unknown recipient address format");

return concat([
"0x1b366e79",
tokenAddr,
recipientAddr,
convertNumberToBytes(amount, UINT256_LENGTH),
]);
}

export function buildSendTokenExtraArgsWhenRemoving(
tokenType: TokenType,
spokeAddress: GenericAddress,
hubTokenAddress: GenericAddress,
amount: bigint,
): Hex {
if (tokenType === TokenType.NATIVE || tokenType === TokenType.ERC20)
return "0x";
return extraArgsToBytes(hubTokenAddress, spokeAddress, BigInt(amount));
}

export function buildSendTokenExtraArgsWhenAdding(
tokenType: TokenType,
spokeTokenAddress: GenericAddress,
hubPoolAddress: GenericAddress,
amount: bigint,
): Hex {
if (tokenType === TokenType.NATIVE || tokenType === TokenType.ERC20)
return "0x";
return extraArgsToBytes(spokeTokenAddress, hubPoolAddress, amount);
}

export function buildEvmMessageToSend(
messageToSendBuilderParams: MessageToSendBuilderParams,
): MessageToSend {
Expand All @@ -59,6 +106,7 @@ export function buildEvmMessageToSend(
handler,
action,
data,
extraArgs,
} = messageToSendBuilderParams;
switch (action) {
case Action.CreateAccount: {
Expand All @@ -75,7 +123,7 @@ export function buildEvmMessageToSend(
data,
),
finalityLevel: FINALITY.IMMEDIATE,
extraArgs: "0x",
extraArgs,
};
return message;
}
Expand Down Expand Up @@ -117,7 +165,7 @@ export function buildEvmMessageToSend(
data,
),
finalityLevel: FINALITY.IMMEDIATE,
extraArgs: "0x",
extraArgs,
};
return message;
}
Expand All @@ -135,7 +183,7 @@ export function buildEvmMessageToSend(
convertNumberToBytes(data.folksChainIdToUnregister, UINT16_LENGTH),
),
finalityLevel: FINALITY.IMMEDIATE,
extraArgs: "0x",
extraArgs,
};
return message;
}
Expand All @@ -146,19 +194,100 @@ export function buildEvmMessageToSend(
throw new Error("Not implemented yet: Action.RemoveDelegate case");
}
case Action.CreateLoan: {
throw new Error("Not implemented yet: Action.CreateLoan case");
const params = DEFAULT_MESSAGE_PARAMS(adapters);
const message: MessageToSend = {
params,
sender,
destinationChainId,
handler,
payload: buildMessagePayload(
Action.CreateLoan,
accountId,
getRandomGenericAddress(),
concat([
data.loanId,
convertNumberToBytes(data.loanTypeId, UINT16_LENGTH),
]),
),
finalityLevel: FINALITY.IMMEDIATE,
extraArgs,
};
return message;
}
case Action.DeleteLoan: {
throw new Error("Not implemented yet: Action.DeleteLoan case");
const params = DEFAULT_MESSAGE_PARAMS(adapters);
const message: MessageToSend = {
params,
sender,
destinationChainId,
handler,
payload: buildMessagePayload(
Action.DeleteLoan,
data.accountId,
getRandomGenericAddress(),
data.loanId,
),
finalityLevel: FINALITY.IMMEDIATE,
extraArgs,
};
return message;
}
case Action.Deposit: {
throw new Error("Not implemented yet: Action.Deposit case");
const params = DEFAULT_MESSAGE_PARAMS(adapters);
const message: MessageToSend = {
params,
sender,
destinationChainId,
handler,
payload: buildMessagePayload(
Action.Deposit,
accountId,
getRandomGenericAddress(),
concat([
data.loanId,
convertNumberToBytes(data.poolId, UINT8_LENGTH),
convertNumberToBytes(data.amount, UINT256_LENGTH),
]),
),
finalityLevel: FINALITY.FINALISED,
extraArgs: buildSendTokenExtraArgsWhenAdding(
extraArgs.tokenType,
extraArgs.spokeTokenAddress,
extraArgs.hubPoolAddress,
extraArgs.amount,
),
};
return message;
}
case Action.DepositFToken: {
throw new Error("Not implemented yet: Action.DepositFToken case");
}
case Action.Withdraw: {
throw new Error("Not implemented yet: Action.Withdraw case");
const params = {
...DEFAULT_MESSAGE_PARAMS(adapters),
...messageToSendBuilderParams.params,
};
const message: MessageToSend = {
params,
sender,
destinationChainId,
handler,
payload: buildMessagePayload(
Action.Withdraw,
accountId,
getRandomGenericAddress(),
concat([
data.loanId,
convertNumberToBytes(data.poolId, UINT8_LENGTH),
convertNumberToBytes(data.receiverFolksChainId, UINT16_LENGTH),
convertNumberToBytes(data.amount, UINT256_LENGTH),
convertBooleanToByte(data.isFAmount),
]),
),
finalityLevel: FINALITY.IMMEDIATE,
extraArgs,
};
return message;
}
case Action.WithdrawFToken: {
throw new Error("Not implemented yet: Action.WithdrawFToken case");
Expand Down
15 changes: 10 additions & 5 deletions src/chains/evm/hub/modules/folks-hub-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import {
getSpokeChain,
getSpokeTokenData,
} from "../../../../common/utils/chain.js";
import { getSendTokenExtraArgsWhenRemoving } from "../../../../common/utils/messages.js";
import {
DEFAULT_MESSAGE_PARAMS,
buildMessagePayload,
buildSendTokenExtraArgsWhenRemoving,
} from "../../common/utils/message.js";
import { getHubChain, getHubTokenData } from "../utils/chain.js";
import {
getHubChain,
getHubTokenAddress,
getHubTokenData,
} from "../utils/chain.js";
import { getBridgeRouterHubContract } from "../utils/contract.js";

import type {
Expand Down Expand Up @@ -64,9 +68,10 @@ export function getSendTokenAdapterFees(
convertNumberToBytes(amount, UINT256_LENGTH),
),
finalityLevel: FINALITY.FINALISED,
extraArgs: getSendTokenExtraArgsWhenRemoving(
spokeTokenData,
hubTokenData,
extraArgs: buildSendTokenExtraArgsWhenRemoving(
hubTokenData.tokenType,
spokeTokenData.spokeAddress,
getHubTokenAddress(hubTokenData),
amount,
),
};
Expand Down
8 changes: 8 additions & 0 deletions src/chains/evm/hub/utils/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HUB_CHAIN } from "../constants/chain.js";

import type {
FolksChainId,
GenericAddress,
NetworkType,
} from "../../../../common/types/chain.js";
import type { LoanType } from "../../../../common/types/module.js";
Expand Down Expand Up @@ -47,3 +48,10 @@ export function assertLoanTypeSupported(
`Loan type ${loanType} is not supported for folksTokenId: ${folksTokenId}`,
);
}

export function getHubTokenAddress(hubTokenData: HubTokenData): GenericAddress {
if (hubTokenData.tokenAddress) return hubTokenData.tokenAddress;
throw new Error(
`Hub token address not found for folksTokenId: ${hubTokenData.folksTokenId}`,
);
}
Loading

0 comments on commit a1a086c

Please sign in to comment.