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: allow string type on prepareDepositTxs #143

Merged
merged 3 commits into from
Aug 24, 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
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
Loading