From 92de4616b2ba04fe25d409a33f862ef4663919ae Mon Sep 17 00:00:00 2001 From: Roberto Cano <3525807+robercano@users.noreply.github.com> Date: Mon, 21 Aug 2023 11:56:20 +0200 Subject: [PATCH] feature: added dma-library wrapper for signing and ChargeFee --- .../actions/common/charge-fee-mocked.test.ts | 6 +-- packages/dma-library/src/actions/common.ts | 54 +++++++++++++++++++ .../src/utils/signing/sign-message.ts | 18 +++++++ 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 packages/dma-library/src/utils/signing/sign-message.ts diff --git a/packages/dma-contracts/test/unit/actions/common/charge-fee-mocked.test.ts b/packages/dma-contracts/test/unit/actions/common/charge-fee-mocked.test.ts index 7c515f048..1dd4fa4e8 100644 --- a/packages/dma-contracts/test/unit/actions/common/charge-fee-mocked.test.ts +++ b/packages/dma-contracts/test/unit/actions/common/charge-fee-mocked.test.ts @@ -4,6 +4,7 @@ import { createDeploy } from '@dma-common/utils/deploy' import init from '@dma-common/utils/init' import { calldataTypes } from '@dma-library' import { toSolidityPercentage } from '@dma-library/utils/percentages/percentage-utils' +import { signSolidityCalldata } from '@dma-library/utils/signing/sign-message' import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' import { BigNumber, utils } from 'ethers' import { ethers } from 'hardhat' @@ -47,7 +48,8 @@ describe('ChargeFee Action | Unit', () => { debtAssetDecimals: number, ) => { // CREATE SIGNATURE - const msgHash = ethers.utils.solidityKeccak256( + const signature = await signSolidityCalldata( + backendSigner, ['uint256', 'uint256', 'uint256', 'address', 'uint8', 'uint256', 'address', 'uint8'], [ feeAmount, @@ -61,8 +63,6 @@ describe('ChargeFee Action | Unit', () => { ], ) - const signature = await backendSigner.signMessage(ethers.utils.arrayify(msgHash)) - // ENCODE THE CALL return utils.defaultAbiCoder.encode( [calldataTypes.common.ChargeFee], diff --git a/packages/dma-library/src/actions/common.ts b/packages/dma-library/src/actions/common.ts index d39c33580..e5c7c7111 100644 --- a/packages/dma-library/src/actions/common.ts +++ b/packages/dma-library/src/actions/common.ts @@ -1,7 +1,9 @@ import { CONTRACT_NAMES } from '@deploy-configurations/constants' import { getActionHash } from '@deploy-configurations/utils/action-hash' import { ActionCall, calldataTypes } from '@dma-library/types' +import { signSolidityCalldata } from '@dma-library/utils/signing/sign-message' import BigNumber from 'bignumber.js' +import { Signer } from 'ethers' import { ActionFactory } from './action-factory' @@ -167,3 +169,55 @@ export function positionCreated(args: { ], ) } + +export async function chargeFee(args: { + signer: Signer + feeAmount: BigNumber + maxFeePercentage: BigNumber + collateralAmount: BigNumber + collateralAsset: string + collateralAssetDecimals: number + debtAmount: BigNumber + debtAsset: string + debtAssetDecimals: number +}) { + const dataTypesWithoutSignature = [ + 'uint256', + 'uint256', + 'uint256', + 'address', + 'uint8', + 'uint256', + 'address', + 'uint8', + ] + + const signature: string = await signSolidityCalldata(args.signer, dataTypesWithoutSignature, [ + args.feeAmount, + args.maxFeePercentage, + args.collateralAmount, + args.collateralAsset, + args.collateralAssetDecimals, + args.debtAmount, + args.debtAsset, + args.debtAssetDecimals, + ]) + + return createAction( + getActionHash(CONTRACT_NAMES.common.CHARGE_FEE), + [calldataTypes.common.ChargeFee], + [ + { + feeAmount: args.feeAmount, + maxFeePercentage: args.maxFeePercentage, + collateralAmount: args.collateralAmount, + collateralAsset: args.collateralAsset, + collateralAssetDecimals: args.collateralAssetDecimals, + debtAmount: args.debtAmount, + debtAsset: args.debtAsset, + debtAssetDecimals: args.debtAssetDecimals, + signature, + }, + ], + ) +} diff --git a/packages/dma-library/src/utils/signing/sign-message.ts b/packages/dma-library/src/utils/signing/sign-message.ts new file mode 100644 index 000000000..023679631 --- /dev/null +++ b/packages/dma-library/src/utils/signing/sign-message.ts @@ -0,0 +1,18 @@ +import type { Signer } from 'ethers' +import { ethers } from 'ethers' + +/** + * + * @param signer Wallet with signing capabilities, i.e: with a private key + * @param calldataTypes Types of the calldata to be signed in solidity format + * @param calldataValues Values of the calldata to be signed + * @returns Signature of the calldata + */ +export async function signSolidityCalldata( + signer: Signer, + calldataTypes: string[], + calldataValues: any[], +): Promise { + const msgHash = ethers.utils.solidityKeccak256(calldataTypes, calldataValues) + return await signer.signMessage(ethers.utils.arrayify(msgHash)) +}