Skip to content

Commit

Permalink
Merge pull request #143 from getclave/feat/allow-string-amount
Browse files Browse the repository at this point in the history
feat: allow string type on `prepareDepositTxs`
  • Loading branch information
Hugo0 authored Aug 24, 2024
2 parents c0db3fd + 89d8969 commit 0e27966
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/consts/interfaces.consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export enum EPeanutLinkType {

export interface IPeanutLinkDetails {
chainId: string
tokenAmount: number
tokenAmount: number | string
tokenType?: EPeanutLinkType
tokenAddress?: string
tokenId?: number
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,14 @@ function trim_decimal_overflow(_n: number, decimals: number) {
return arr[0] + '.' + fraction
}

function getStringAmount(amount: interfaces.IPeanutLinkDetails['tokenAmount'], decimals: number) {
if (typeof amount === 'string') {
return amount
} else {
return trim_decimal_overflow(amount, decimals)
}
}

/**
* Returns an array of transactions necessary to create a link (e.g. 1. approve, 2. makeDeposit)
* all values obligatory.
Expand Down Expand Up @@ -827,7 +835,7 @@ async function prepareDepositTxs({
'Error validating link details: please make sure all required fields are provided and valid'
)
}
const tokenAmountString = trim_decimal_overflow(linkDetails.tokenAmount, linkDetails.tokenDecimals!)
const tokenAmountString = getStringAmount(linkDetails.tokenAmount, linkDetails.tokenDecimals!)
const tokenAmountBigNum = ethers.utils.parseUnits(tokenAmountString, linkDetails.tokenDecimals)
const totalTokenAmount = tokenAmountBigNum.mul(numberOfLinks)

Expand Down Expand Up @@ -1267,7 +1275,7 @@ async function validateLinkDetails(
throw new Error('need to provide tokenAddress if tokenType is not 0')
}

const tokenAmountString = trim_decimal_overflow(linkDetails.tokenAmount, linkDetails.tokenDecimals!)
const tokenAmountString = getStringAmount(linkDetails.tokenAmount, linkDetails.tokenDecimals!)
const tokenAmountBigNum = ethers.utils.parseUnits(tokenAmountString, linkDetails.tokenDecimals) // v5
assert(tokenAmountBigNum.gt(0), 'tokenAmount must be greater than 0')

Expand Down Expand Up @@ -3217,4 +3225,5 @@ export {
getTxReceiptFromHash,
getTokenBalance,
compareVersions,
getStringAmount,
}
4 changes: 2 additions & 2 deletions src/raffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getLinksFromTx,
interfaces,
prepareApproveERC20Tx,
trim_decimal_overflow,
getStringAmount,
} from '.'
import { getRawParamsFromLink, validateUserName, compareVersions } from './util'
import {
Expand Down Expand Up @@ -174,7 +174,7 @@ export async function prepareRaffleDepositTxs({

// Convert linkDetails.tokenAmount to BigNumber.
// Used only for ETH and ERC20 raffles.
const tokenAmountString = trim_decimal_overflow(linkDetails.tokenAmount, linkDetails.tokenDecimals)
const tokenAmountString = getStringAmount(linkDetails.tokenAmount, linkDetails.tokenDecimals)
const tokenAmountBigNum = ethers.utils.parseUnits(tokenAmountString, linkDetails.tokenDecimals)

const peanutVaultAddress = getContractAddress(linkDetails.chainId, peanutContractVersion)
Expand Down

0 comments on commit 0e27966

Please sign in to comment.