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: prepare and write switch borrow type #39

Merged
merged 2 commits into from
Jun 6, 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
22 changes: 20 additions & 2 deletions src/chains/evm/common/utils/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,11 @@ export function buildEvmMessageData(
throw new Error("Not implemented yet: Action.Liquidate case");
}
case Action.SwitchBorrowType: {
throw new Error("Not implemented yet: Action.SwitchBorrowType case");
return concat([
data.loanId,
convertNumberToBytes(data.poolId, UINT8_LENGTH),
convertNumberToBytes(data.maxStableRate, UINT256_LENGTH),
]);
}
case Action.SendToken: {
throw new Error("Not implemented yet: Action.SendToken case");
Expand Down Expand Up @@ -441,7 +445,21 @@ export function buildEvmMessageToSend(
throw new Error("Not implemented yet: Action.Liquidate case");
}
case Action.SwitchBorrowType: {
throw new Error("Not implemented yet: Action.SwitchBorrowType case");
const message: MessageToSend = {
params,
sender,
destinationChainId,
handler,
payload: buildMessagePayload(
Action.SwitchBorrowType,
accountId,
userAddress,
data,
),
finalityLevel: FINALITY.IMMEDIATE,
extraArgs,
};
return message;
}
case Action.SendToken: {
throw new Error("Not implemented yet: Action.SendToken case");
Expand Down
78 changes: 78 additions & 0 deletions src/chains/evm/spoke/modules/folks-evm-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type {
PrepareDepositCall,
PrepareRepayCall,
PrepareRepayWithCollateralCall,
PrepareSwitchBorrowTypeCall,
PrepareWithdrawCall,
} from "../../common/types/module.js";
import type { TokenRateLimit } from "../types/pool.js";
Expand Down Expand Up @@ -339,6 +340,54 @@ export const prepare = {
spokeCommonAddress,
};
},

async switchBorrowType(
provider: Client,
sender: EvmAddress,
messageToSend: MessageToSend,
network: NetworkType,
accountId: AccountId,
loanId: LoanId,
folksTokenId: FolksTokenId,
maxStableRate: bigint,
spokeChain: SpokeChain,
transactionOptions: EstimateGasParameters = { account: sender },
): Promise<PrepareSwitchBorrowTypeCall> {
const hubTokenData = getHubTokenData(folksTokenId, network);

const spokeCommonAddress = spokeChain.spokeCommonAddress;

const spokeCommon = getSpokeCommonContract(provider, spokeCommonAddress);
const bridgeRouter = getBridgeRouterSpokeContract(
provider,
spokeChain.bridgeRouterAddress,
);

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

// get gas limits
const gasLimit = await spokeCommon.estimateGas.switchBorrowType(
[
messageToSend.params,
accountId,
loanId,
hubTokenData.poolId,
maxStableRate,
],
{
value: msgValue,
...transactionOptions,
},
);

return {
msgValue,
gasLimit,
messageParams: messageToSend.params,
spokeCommonAddress,
};
},
};

export const write = {
Expand Down Expand Up @@ -578,6 +627,35 @@ export const write = {
},
);
},

async switchBorrowType(
provider: Client,
signer: WalletClient,
accountId: AccountId,
loanId: LoanId,
poolId: number,
maxStableRate: bigint,
prepareCall: PrepareSwitchBorrowTypeCall,
) {
const { msgValue, gasLimit, messageParams, spokeCommonAddress } =
prepareCall;

const spokeCommon = getSpokeCommonContract(
provider,
spokeCommonAddress,
signer,
);

return await spokeCommon.write.switchBorrowType(
[messageParams, accountId, loanId, poolId, maxStableRate],
{
account: getEvmSignerAccount(signer),
chain: signer.chain,
gasLimit: gasLimit,
value: msgValue,
},
);
},
};

export const read = {
Expand Down
16 changes: 14 additions & 2 deletions src/common/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export type RepayWithCollateralMessageData = {
amount: bigint;
};

export type SwitchBorrowTypeMessageData = {
loanId: LoanId;
poolId: number;
maxStableRate: bigint;
};

// Extra args
export type DefaultExtraArgs = "0x";

Expand Down Expand Up @@ -155,7 +161,6 @@ export type DefaultMessageDataParams = {
| Action.DepositFToken
| Action.WithdrawFToken
| Action.Liquidate
| Action.SwitchBorrowType
| Action.SendToken;
data: DefaultMessageData;
extraArgs: DefaultExtraArgs;
Expand Down Expand Up @@ -223,6 +228,12 @@ export type RepayWithCollateralMessageDataParams = {
extraArgs: DefaultExtraArgs;
};

export type SwitchBorrowTypeDataParams = {
action: Action.SwitchBorrowType;
data: SwitchBorrowTypeMessageData;
extraArgs: DefaultExtraArgs;
};

export type MessageDataParams =
| DefaultMessageDataParams
| CreateAccountMessageDataParams
Expand All @@ -234,7 +245,8 @@ export type MessageDataParams =
| WithdrawMessageDataParams
| BorrowMessageDataParams
| RepayMessageDataParams
| RepayWithCollateralMessageDataParams;
| RepayWithCollateralMessageDataParams
| SwitchBorrowTypeDataParams;

export type MessageBuilderParams = {
userAddress: GenericAddress;
Expand Down
2 changes: 2 additions & 0 deletions src/common/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
PrepareBorrowCall as PrepareBorrowEVMCall,
PrepareRepayCall as PrepareRepayEVMCall,
PrepareRepayWithCollateralCall as PrepareRepayWithCollateralEVMCall,
PrepareSwitchBorrowTypeCall as PrepareSwitchBorrowTypeEVMCall,
} from "../../chains/evm/common/types/module.js";

export enum LoanType {
Expand All @@ -32,3 +33,4 @@ export type PrepareWithdrawCall = PrepareWithdrawEVMCall;
export type PrepareBorrowCall = PrepareBorrowEVMCall;
export type PrepareRepayCall = PrepareRepayEVMCall;
export type PrepareRepayWithCollateralCall = PrepareRepayWithCollateralEVMCall;
export type PrepareSwitchBorrowTypeCall = PrepareSwitchBorrowTypeEVMCall;
104 changes: 104 additions & 0 deletions src/xchain/modules/folks-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import type {
RepayExtraArgs,
RepayMessageData,
RepayWithCollateralMessageData,
SwitchBorrowTypeMessageData,
WithdrawMessageData,
} from "../../common/types/message.js";
import type {
Expand All @@ -58,6 +59,7 @@ import type {
PrepareDepositCall,
PrepareRepayCall,
PrepareRepayWithCollateralCall,
PrepareSwitchBorrowTypeCall,
PrepareWithdrawCall,
} from "../../common/types/module.js";
import type { FolksTokenId } from "../../common/types/token.js";
Expand Down Expand Up @@ -690,6 +692,81 @@ export const prepare = {
return exhaustiveCheck(folksChain.chainType);
}
},

async switchBorrowType(
accountId: AccountId,
loanId: LoanId,
folksTokenId: FolksTokenId,
maxStableRate: bigint,
adapters: MessageAdapters,
) {
const folksChain = FolksCore.getSelectedFolksChain();
const network = folksChain.network;

assertAdapterSupportsDataMessage(
folksChain.folksChainId,
adapters.adapterId,
);

const hubChain = getHubChain(network);
const hubTokenData = getHubTokenData(folksTokenId, network);

const spokeChain = getSpokeChain(folksChain.folksChainId, network);

const userAddress = getSignerGenericAddress({
signer: FolksCore.getFolksSigner().signer,
chainType: folksChain.chainType,
});

const data: SwitchBorrowTypeMessageData = {
loanId,
poolId: hubTokenData.poolId,
maxStableRate,
};
const messageBuilderParams: MessageBuilderParams = {
userAddress,
accountId,
adapters,
action: Action.SwitchBorrowType,
sender: spokeChain.spokeCommonAddress,
destinationChainId: hubChain.folksChainId,
handler: hubChain.hubAddress,
data,
extraArgs: "0x",
};
const feeParams: OptionalFeeParams = {};

feeParams.gasLimit = await estimateReceiveGasLimit(
FolksCore.getHubProvider(),
hubChain,
folksChain,
adapters,
messageBuilderParams,
);

const messageToSend = buildMessageToSend(
folksChain.chainType,
messageBuilderParams,
feeParams,
);

switch (folksChain.chainType) {
case ChainType.EVM:
return await FolksEvmLoan.prepare.switchBorrowType(
FolksCore.getProvider<ChainType.EVM>(folksChain.folksChainId),
convertFromGenericAddress(userAddress, folksChain.chainType),
messageToSend,
network,
accountId,
loanId,
folksTokenId,
maxStableRate,
spokeChain,
);
default:
return exhaustiveCheck(folksChain.chainType);
}
},
};

export const write = {
Expand Down Expand Up @@ -885,6 +962,33 @@ export const write = {
return exhaustiveCheck(folksChain.chainType);
}
},

async switchBorrowType(
accountId: AccountId,
loanId: LoanId,
folksTokenId: FolksTokenId,
maxStableRate: bigint,
prepareCall: PrepareSwitchBorrowTypeCall,
) {
const folksChain = FolksCore.getSelectedFolksChain();

const { poolId } = getHubTokenData(folksTokenId, folksChain.network);

switch (folksChain.chainType) {
case ChainType.EVM:
return await FolksEvmLoan.write.switchBorrowType(
FolksCore.getProvider<ChainType.EVM>(folksChain.folksChainId),
FolksCore.getSigner<ChainType.EVM>(),
accountId,
loanId,
poolId,
maxStableRate,
prepareCall,
);
default:
return exhaustiveCheck(folksChain.chainType);
}
},
};

export const read = {
Expand Down