Skip to content

Commit

Permalink
feature: added dma-library wrapper for signing and ChargeFee
Browse files Browse the repository at this point in the history
  • Loading branch information
robercano committed Aug 21, 2023
1 parent ce4747b commit 92de461
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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],
Expand Down
54 changes: 54 additions & 0 deletions packages/dma-library/src/actions/common.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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,
},
],
)
}
18 changes: 18 additions & 0 deletions packages/dma-library/src/utils/signing/sign-message.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
const msgHash = ethers.utils.solidityKeccak256(calldataTypes, calldataValues)
return await signer.signMessage(ethers.utils.arrayify(msgHash))
}

0 comments on commit 92de461

Please sign in to comment.