Skip to content

Commit

Permalink
Add tag to constant manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Sep 25, 2024
1 parent f062ab0 commit 212fda2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
26 changes: 26 additions & 0 deletions assembly/helpers/__tests__/constantKeyManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { resetStorage } from '../../vm-mock';
import { u256 } from 'as-bignum/assembly';
import { ConstantManager } from '../constantKeyManager';
import { KeyIncrementer } from '../keyIncrementer';
import { stringToBytes } from '@massalabs/as-types';

beforeEach(() => {
resetStorage();
Expand Down Expand Up @@ -36,6 +37,31 @@ describe('ConstantManager - use cases', () => {
expect(fee.mustValue()).toBe(100);
expect(usdc.mustValue().toString()).toBe('1000000');
});

test('executes a basic scenario - with a tag', () => {
const tagOwner = stringToBytes('owner');
const tagFee = stringToBytes('fee');
const tagUsdc = stringToBytes('usdc');

// a key manager instance is needed to generate unique keys
const keyManager = new KeyIncrementer<u8>();

const owner = new ConstantManager<Address>(keyManager, tagOwner);
const fee = new ConstantManager<u64>(keyManager, tagFee);
const usdc = new ConstantManager<u256>(keyManager, tagUsdc);

owner.set(
new Address('AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq'),
);
fee.set(100);
usdc.set(u256.fromU64(1000000));

expect(owner.mustValue().toString()).toBe(
'AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq',
);
expect(fee.mustValue()).toBe(100);
expect(usdc.mustValue().toString()).toBe('1000000');
});
});

describe('ConstantManager - unit tests', () => {
Expand Down
7 changes: 5 additions & 2 deletions assembly/helpers/constantKeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import { Storage } from '../std';
export class ConstantManager<TValue, TKey = u8, TArray = void> {
public key: StaticArray<u8>;

constructor(manager: KeySequenceManager = new KeyIncrementer<TKey>(0)) {
this.key = manager.nextKey();
constructor(
manager: KeySequenceManager = new KeyIncrementer<TKey>(0),
tag: StaticArray<u8> = new StaticArray<u8>(0),
) {
this.key = manager.nextKey().concat(tag);
}

/**
Expand Down

0 comments on commit 212fda2

Please sign in to comment.