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

refactor: loan module message to send #21

Merged
merged 5 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
155 changes: 146 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 All @@ -19,6 +27,8 @@ import type {
MessageToSend,
MessageToSendBuilderParams,
} from "../../../../common/types/message.js";
import type { SpokeTokenData } from "../../../../common/types/token.js";
import type { HubTokenData } from "../../hub/types/token.js";
import type { Hex } from "viem";

export const DEFAULT_MESSAGE_PARAMS = (
Expand Down Expand Up @@ -48,6 +58,51 @@ 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 getSendTokenExtraArgsWhenRemoving(
spokeTokenData: SpokeTokenData,
hubTokenData: HubTokenData,
amount: bigint,
): Hex {
const { tokenType } = hubTokenData;
if (tokenType === TokenType.NATIVE || tokenType === TokenType.ERC20)
return "0x";
if (hubTokenData.tokenAddress === null) throw Error("Unknown token address");

return extraArgsToBytes(
hubTokenData.tokenAddress,
spokeTokenData.spokeAddress,
BigInt(amount),
);
}

export function buildSendTokenExtraArgsWhenAdding(
stefanofa marked this conversation as resolved.
Show resolved Hide resolved
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 +114,7 @@ export function buildEvmMessageToSend(
handler,
action,
data,
extraArgs,
} = messageToSendBuilderParams;
switch (action) {
case Action.CreateAccount: {
Expand All @@ -75,7 +131,7 @@ export function buildEvmMessageToSend(
data,
),
finalityLevel: FINALITY.IMMEDIATE,
extraArgs: "0x",
extraArgs,
};
return message;
}
Expand Down Expand Up @@ -117,7 +173,7 @@ export function buildEvmMessageToSend(
data,
),
finalityLevel: FINALITY.IMMEDIATE,
extraArgs: "0x",
extraArgs,
};
return message;
}
Expand All @@ -135,7 +191,7 @@ export function buildEvmMessageToSend(
convertNumberToBytes(data.folksChainIdToUnregister, UINT16_LENGTH),
),
finalityLevel: FINALITY.IMMEDIATE,
extraArgs: "0x",
extraArgs,
};
return message;
}
Expand All @@ -146,19 +202,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
2 changes: 1 addition & 1 deletion src/chains/evm/hub/modules/folks-hub-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
getSpokeChain,
getSpokeTokenData,
} from "../../../../common/utils/chain.js";
import { getSendTokenExtraArgsWhenRemoving } from "../../../../common/utils/messages.js";
import {
DEFAULT_MESSAGE_PARAMS,
buildMessagePayload,
getSendTokenExtraArgsWhenRemoving,
} from "../../common/utils/message.js";
import { getHubChain, getHubTokenData } from "../utils/chain.js";
import { getBridgeRouterHubContract } from "../utils/contract.js";
Expand Down
Loading