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

fix: better number formatting #25

Merged
merged 7 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 3 additions & 2 deletions generator/features/__snapshots__/assetListing.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ contract AaveV3Ethereum_Test_20231023_Test is ProtocolV3TestBase {
AaveV3Ethereum_Test_20231023 internal proposal;

function setUp() public {
vm.createSelectFork(vm.rpcUrl('mainnet'), 18461057);
vm.createSelectFork(vm.rpcUrl('mainnet'), 18479488);
proposal = new AaveV3Ethereum_Test_20231023();
}

Expand Down Expand Up @@ -219,7 +219,7 @@ contract DeployEthereum is EthereumScript {
* command: make deploy-ledger contract=src/20231023_AaveV3Ethereum_Test/Test_20231023.s.sol:CreateProposal chain=mainnet
*/
contract CreateProposal is EthereumScript {
function run() external broadcast {
function run() external {
// create payloads
PayloadsControllerUtils.Payload[] memory payloads = new PayloadsControllerUtils.Payload[](1);

Expand All @@ -230,6 +230,7 @@ contract CreateProposal is EthereumScript {
payloads[0] = GovV3Helpers.buildMainnetPayload(vm, actionsEthereum);

// create proposal
vm.startBroadcast();
GovV3Helpers.createProposal2_5(
payloads,
GovV3Helpers.ipfsHashFile(vm, 'src/20231023_AaveV3Ethereum_Test/Test.md')
Expand Down
2 changes: 1 addition & 1 deletion generator/features/mocks/configs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Options} from '../../types';
import {Listing} from '../types';
import {CapsUpdate, Listing} from '../types';

export const MOCK_OPTIONS: Options = {
pools: ['AaveV3Ethereum'],
Expand Down
47 changes: 47 additions & 0 deletions generator/prompts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// sum.test.js
import {expect, describe, it} from 'vitest';
import {
transformNumberToHumanReadable,
transformNumberToPercent,
translateJsNumberToSol,
translateJsPercentToSol,
} from './prompts';

describe('prompts', () => {
/**
* Transformers are here to format the input based on a users input
* They do not change the users input value though, the effect is purely visual
*/
describe('transforms', () => {
it('transformNumberToHumanReadable: should return a human readable full number', () => {
expect(transformNumberToHumanReadable('1000')).toBe('1,000');
expect(transformNumberToHumanReadable('1000000')).toBe('1,000,000');
});

it('transformNumberToPercent: should return a human readable % number', () => {
expect(transformNumberToPercent('100')).toBe('100 %');
expect(transformNumberToPercent('3333.33')).toBe('3,333.33 %');
expect(transformNumberToPercent('0.33')).toBe('0.33 %');
expect(transformNumberToPercent('0.3')).toBe('0.3 %');
});
});

/**
* Translates, translate the js input value to solidity
*/
describe('translate', () => {
it('translateJsNumberToSol: should properly translate values', () => {
expect(translateJsNumberToSol('0')).toBe('0');
expect(translateJsNumberToSol('1000')).toBe('1_000');
expect(translateJsNumberToSol('1000000')).toBe('1_000_000');
});

it('translateJsPercentToSol: should properly translate % values', () => {
expect(translateJsPercentToSol('0')).toBe('0');
expect(translateJsPercentToSol('100')).toBe('100_00');
expect(translateJsPercentToSol('3333.33')).toBe('3_333_33');
expect(translateJsPercentToSol('0.33')).toBe('33');
expect(translateJsPercentToSol('0.3')).toBe('30');
});
});
});
32 changes: 21 additions & 11 deletions generator/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,39 @@ function isAddressOrKeepCurrent(value: string) {
}

// TRANSFORMS
function transformNumberToPercent(value: string) {
export function transformNumberToPercent(value: string) {
if (value && isNumber(value)) {
if (Number(value) <= 9) value = value.padStart(2, '0');
return value.replace(/(?=(\d{2}$)+(?!\d))/g, '.') + ' %';
return (
new Intl.NumberFormat('en-us', {
maximumFractionDigits: 2,
}).format(value as unknown as number) + ' %'
);
}
return value;
}

function transformNumberToHumanReadable(value: string) {
export function transformNumberToHumanReadable(value: string) {
if (value && isNumber(value)) {
return value.replace(/(?=(\d{3}$)+(?!\d))/g, '.');
return new Intl.NumberFormat('en-us').format(BigInt(value));
brotherlymite marked this conversation as resolved.
Show resolved Hide resolved
}
return value;
}

// TRANSLATIONS
function translateJsPercentToSol(value: string, bpsToRay?: boolean) {
export function translateJsPercentToSol(value: string, bpsToRay?: boolean) {
if (value === ENGINE_FLAGS.KEEP_CURRENT) return `EngineFlags.KEEP_CURRENT`;
if (bpsToRay) return `_bpsToRay(${value.replace(/(?=(\d{2}$))/g, '_')})`;
return value.replace(/(?=(\d{2}$)+(?!\d))/g, '_');
}

function translateJsNumberToSol(value: string) {
const formattedValue = new Intl.NumberFormat('en-us', {
maximumFractionDigits: 2,
minimumFractionDigits: 2,
}).format(value as unknown as number);
const _value = (
Number(value) >= 1 ? formattedValue : formattedValue.replace(/^0\.0*(?=[0-9])/, '')
).replace(/[\.,]/g, '_');
if (bpsToRay) return `_bpsToRay(${_value})`;
return _value;
}

export function translateJsNumberToSol(value: string) {
if (value === ENGINE_FLAGS.KEEP_CURRENT) return `EngineFlags.KEEP_CURRENT`;
return String(value).replace(/\B(?=(\d{3})+(?!\d))/g, '_');
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"homepage": "https://github.com/bgd-labs/aave-proposals-v3#readme",
"devDependencies": {
"@types/node": "^20.8.10",
"prettier": "2.8.7",
"prettier-plugin-solidity": "1.1.3",
"vitest": "^0.34.6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ contract DeployEthereum is EthereumScript {
IPayloadsControllerCore.ExecutionAction[]
memory actions = new IPayloadsControllerCore.ExecutionAction[](1);
actions[0] = GovV3Helpers.buildAction(address(payload0));

// register action at payloadsController
GovV3Helpers.createPayload(actions);
}
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,13 @@
dependencies:
undici-types "~5.25.1"

"@types/node@^20.8.10":
version "20.8.10"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.8.10.tgz#a5448b895c753ae929c26ce85cab557c6d4a365e"
integrity sha512-TlgT8JntpcbmKUFzjhsyhGfP2fsiz1Mv56im6enJ905xG1DAYesxJaeSbGqQmAw8OWPdhyJGhGSQGKRNJ45u9w==
dependencies:
undici-types "~5.26.4"

"@types/normalize-package-data@^2.4.0":
version "2.4.2"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.2.tgz#9b0e3e8533fe5024ad32d6637eb9589988b6fdca"
Expand Down Expand Up @@ -1789,6 +1796,11 @@ undici-types@~5.25.1:
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.25.3.tgz#e044115914c85f0bcbb229f346ab739f064998c3"
integrity sha512-Ga1jfYwRn7+cP9v8auvEXN1rX3sWqlayd4HP7OKk4mZWylEmu3KzXDUGrQUN6Ol7qo1gPvB2e5gX6udnyEPgdA==

undici-types@~5.26.4:
version "5.26.5"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

util-deprecate@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
Expand Down