Skip to content

Commit 181a10e

Browse files
committed
feat: add v11 voter params functions
1 parent 8833a05 commit 181a10e

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

src/context-helpers/internal-context.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Account, BaseContract, internal } from '@algorandfoundation/algorand-ty
22
import { AccountData } from '../impl/account'
33
import { ApplicationData } from '../impl/application'
44
import { AssetData } from '../impl/asset'
5+
import { VoterData } from '../impl/voter-params'
56
import { TransactionGroup } from '../subcontexts/transaction-context'
67
import { TestExecutionContext } from '../test-execution-context'
78

@@ -69,6 +70,14 @@ class InternalContext {
6970
}
7071
return data
7172
}
73+
74+
getVoterData(account: Account): VoterData {
75+
const data = this.ledger.voterDataMap.get(account)
76+
if (!data) {
77+
throw internal.errors.internalError('Unknown voter, check correct testing context is active')
78+
}
79+
return data
80+
}
7281
}
7382

7483
export const lazyContext = new InternalContext()

src/impl/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export { arg } from './logicSigArg'
1414
export * from './pure'
1515
export { gloadBytes, gloadUint64, Scratch } from './scratch'
1616
export { gaid, Txn } from './txn'
17+
export { VoterParams } from './voter-params'

src/impl/voter-params.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Account, internal, uint64 } from '@algorandfoundation/algorand-typescript'
2+
import { lazyContext } from '../context-helpers/internal-context'
3+
import { getAccount } from './acct-params'
4+
5+
export class VoterData {
6+
balance: uint64
7+
incentiveEligible: boolean
8+
9+
constructor() {
10+
this.balance = 0
11+
this.incentiveEligible = false
12+
}
13+
}
14+
15+
const getVoterData = (a: Account | internal.primitives.StubUint64Compat): VoterData => {
16+
const acct = getAccount(a)
17+
return lazyContext.getVoterData(acct)
18+
}
19+
20+
export const VoterParams: internal.opTypes.VoterParamsType = {
21+
voterBalance: function (a: Account | internal.primitives.StubUint64Compat): readonly [uint64, boolean] {
22+
const data = getVoterData(a)
23+
return [data.balance, data.balance !== 0]
24+
},
25+
voterIncentiveEligible: function (a: Account | internal.primitives.StubUint64Compat): readonly [boolean, boolean] {
26+
const data = getVoterData(a)
27+
return [data.incentiveEligible, data.balance !== 0]
28+
},
29+
}

src/subcontexts/ledger-context.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { AssetData } from '../impl/asset'
77
import { BlockData } from '../impl/block'
88
import { GlobalData } from '../impl/global'
99
import { GlobalStateCls } from '../impl/state'
10+
import { VoterData } from '../impl/voter-params'
1011
import { asBigInt, asMaybeBytesCls, asMaybeUint64Cls, asUint64, asUint64Cls, iterBigInt } from '../util'
1112

1213
export class LedgerContext {
@@ -16,6 +17,7 @@ export class LedgerContext {
1617
appIdContractMap = new Uint64Map<BaseContract>()
1718
accountDataMap = new AccountMap<AccountData>()
1819
assetDataMap = new Uint64Map<AssetData>()
20+
voterDataMap = new AccountMap<VoterData>()
1921
blocks = new Uint64Map<BlockData>()
2022
globalData = new GlobalData()
2123

@@ -120,6 +122,14 @@ export class LedgerContext {
120122
})
121123
}
122124

125+
patchVoterData(account: Account, data: Partial<VoterData>) {
126+
const voterData = this.voterDataMap.get(account) ?? new VoterData()
127+
this.voterDataMap.set(account, {
128+
...voterData,
129+
...data,
130+
})
131+
}
132+
123133
patchBlockData(index: internal.primitives.StubUint64Compat, data: Partial<BlockData>): void {
124134
const i = asUint64(index)
125135
const blockData = this.blocks.get(i) ?? new BlockData()

tests/state-op-codes.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,17 @@ describe('State op codes', async () => {
287287
})
288288
})
289289

290+
describe('VoterParams', async () => {
291+
it('should return the configured balance and incentive eligibility', async () => {
292+
const mockAccount = ctx.any.account()
293+
ctx.ledger.patchVoterData(mockAccount, { balance: 100, incentiveEligible: true })
294+
const balance = op.VoterParams.voterBalance(mockAccount)
295+
const incentiveEligible = op.VoterParams.voterIncentiveEligible(mockAccount)
296+
expect(balance).toEqual([100, true])
297+
expect(incentiveEligible).toEqual([true, true])
298+
})
299+
})
300+
290301
describe('Global', async () => {
291302
it('should return the correct global field value', async () => {
292303
const creator = ctx.any.account()

0 commit comments

Comments
 (0)