Skip to content

Commit b132300

Browse files
authored
fix overflow in grant instruction (#179)
1 parent 2aabc86 commit b132300

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

VoteStakeRegistry/actions/getGrantInstruction.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
TOKEN_PROGRAM_ID,
1414
} from '@solana/spl-token'
1515
import { VsrClient } from 'VoteStakeRegistry/sdk/client'
16+
import { fmtDecimalToBN } from '@utils/formatting'
1617

1718
export const getGrantInstruction = async ({
1819
fromPk,
@@ -21,6 +22,7 @@ export const getGrantInstruction = async ({
2122
grantMintPk,
2223
communityMintPk,
2324
amount,
25+
decimals,
2426
lockupPeriod,
2527
startTime,
2628
lockupKind,
@@ -36,6 +38,7 @@ export const getGrantInstruction = async ({
3638
realmPk: PublicKey
3739
tokenAuthority: PublicKey
3840
amount: number
41+
decimals: number
3942
//days or months in case of monthly vesting lockup type
4043
lockupPeriod: number
4144
lockupKind: LockupType
@@ -73,7 +76,7 @@ export const getGrantInstruction = async ({
7376
new BN(startTime),
7477
lockupPeriod,
7578
allowClawback,
76-
new BN(amount),
79+
fmtDecimalToBN(amount, decimals)
7780
)
7881
.accounts({
7982
registrar,

VoteStakeRegistry/components/instructions/Grant.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import Input from '@components/inputs/Input'
44
import useRealm from '@hooks/useRealm'
55
import {
66
getMintMinAmountAsDecimal,
7-
parseMintNaturalAmountFromDecimal,
87
} from '@tools/sdk/units'
98
import { PublicKey, TransactionInstruction } from '@solana/web3.js'
109
import { precision } from '@utils/formatting'
@@ -151,10 +150,6 @@ const Grant = ({
151150
const sourceAccount =
152151
form.governedTokenAccount.extensions.token?.account.address
153152
const destinationAccount = new PublicKey(form.destinationAccount)
154-
const mintAmount = parseMintNaturalAmountFromDecimal(
155-
form.amount!,
156-
form.governedTokenAccount.extensions.mint.account.decimals,
157-
)
158153

159154
const destinationTokenOwnerRecordPk = await getTokenOwnerRecordAddress(
160155
realm.owner,
@@ -194,7 +189,8 @@ const Grant = ({
194189
tokenAuthority:
195190
form.governedTokenAccount.extensions.token.account.owner,
196191
grantMintPk: form.governedTokenAccount.extensions.mint.publicKey,
197-
amount: mintAmount,
192+
amount: form.amount!,
193+
decimals: form.governedTokenAccount.extensions.mint.account.decimals,
198194
lockupPeriod: form.periods,
199195
startTime: form.startDateUnixSeconds,
200196
lockupKind: form.lockupKind.value,

utils/formatting.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,18 @@ export const fmtTimeToString = ({
8282
}
8383

8484
export { abbreviateAddress }
85+
86+
export const fmtDecimalToBN = (
87+
num: number,
88+
decimals: number,
89+
) => {
90+
const value = num.toString()
91+
const decimalIndex = value.indexOf('.')
92+
const decimalPlaces = decimalIndex !== -1 ? value.length - decimalIndex - 1 : 0
93+
let wholeNumber = value.replace('.', '')
94+
wholeNumber = decimals > decimalPlaces
95+
? wholeNumber + '0'.repeat(decimals - decimalPlaces)
96+
: wholeNumber.slice(0, decimalIndex) + wholeNumber.slice(decimalIndex + 1, decimals + decimalIndex + 1)
97+
98+
return new BN(wholeNumber)
99+
}

0 commit comments

Comments
 (0)