Skip to content

Commit

Permalink
Add scalingFactor to Buffers (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoguerios committed Jan 20, 2025
1 parent 0b0f96b commit 28eb39b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
42 changes: 34 additions & 8 deletions typescript/src/buffer/bufferMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,45 @@ export function calculateBufferAmounts(
kind: SwapKind,
amountRaw: bigint,
rate: bigint,
scalingFactor: bigint,
): bigint {
if (direction === WrappingDirection.WRAP) {
// Amount in is underlying tokens, amount out is wrapped tokens
if (kind === SwapKind.GivenIn) {
// previewDeposit
return _convertToShares(amountRaw, rate, Rounding.DOWN);
return _convertToShares(
amountRaw,
rate,
scalingFactor,
Rounding.DOWN,
);
} else {
// previewMint
return _convertToAssets(amountRaw, rate, Rounding.UP);
return _convertToAssets(
amountRaw,
rate,
scalingFactor,
Rounding.UP,
);
}
} else {
// Amount in is wrapped tokens, amount out is underlying tokens
if (kind === SwapKind.GivenIn) {
// previewRedeem
return _convertToAssets(amountRaw, rate, Rounding.DOWN);
return _convertToAssets(
amountRaw,
rate,
scalingFactor,
Rounding.DOWN,
);
} else {
// previewWithdraw
return _convertToShares(amountRaw, rate, Rounding.UP);
return _convertToShares(
amountRaw,
rate,
scalingFactor,
Rounding.UP,
);
}
}
}
Expand All @@ -44,17 +65,22 @@ export function calculateBufferAmounts(
function _convertToShares(
assets: bigint,
rate: bigint,
scalingFactor: bigint,
rounding: Rounding,
): bigint {
if (rounding === Rounding.UP) return MathSol.divUpFixed(assets, rate);
return MathSol.divDownFixed(assets, rate);
const assetsScale18 = assets * scalingFactor;

Check failure on line 71 in typescript/src/buffer/bufferMath.ts

View workflow job for this annotation

GitHub Actions / checks

test/swaps.test.ts > swap tests > 'buffer-manual.json' +0 undefined

TypeError: Cannot mix BigInt and other types, use explicit conversions ❯ _convertToShares src/buffer/bufferMath.ts:71:27 ❯ Module.calculateBufferAmounts src/buffer/bufferMath.ts:27:20 ❯ Module.erc4626BufferWrapOrUnwrap src/buffer/erc4626BufferWrapOrUnwrap.ts:26:12 ❯ Vault.swap src/vault/vault.ts:146:20 ❯ test/swaps.test.ts:19:44

Check failure on line 71 in typescript/src/buffer/bufferMath.ts

View workflow job for this annotation

GitHub Actions / checks

test/swaps.test.ts > swap tests > 'buffer-manual.json' 1 undefined

TypeError: Cannot mix BigInt and other types, use explicit conversions ❯ _convertToShares src/buffer/bufferMath.ts:71:27 ❯ Module.calculateBufferAmounts src/buffer/bufferMath.ts:54:20 ❯ Module.erc4626BufferWrapOrUnwrap src/buffer/erc4626BufferWrapOrUnwrap.ts:26:12 ❯ Vault.swap src/vault/vault.ts:146:20 ❯ test/swaps.test.ts:19:44
if (rounding === Rounding.UP)
return MathSol.divUpFixed(assetsScale18, rate);
return MathSol.divDownFixed(assetsScale18, rate);
}

function _convertToAssets(
shares: bigint,
rate: bigint,
scalingFactor: bigint,
rounding: Rounding,
): bigint {
if (rounding === Rounding.UP) return MathSol.mulUpFixed(shares, rate);
return MathSol.mulDownFixed(shares, rate);
const sharesRaw = shares / scalingFactor; // TODO: think a bit about rounding direction

Check failure on line 83 in typescript/src/buffer/bufferMath.ts

View workflow job for this annotation

GitHub Actions / checks

test/swaps.test.ts > swap tests > 'buffer-manual.json' 1 undefined

TypeError: Cannot mix BigInt and other types, use explicit conversions ❯ _convertToAssets src/buffer/bufferMath.ts:83:23 ❯ Module.calculateBufferAmounts src/buffer/bufferMath.ts:35:20 ❯ Module.erc4626BufferWrapOrUnwrap src/buffer/erc4626BufferWrapOrUnwrap.ts:26:12 ❯ Vault.swap src/vault/vault.ts:146:20 ❯ test/swaps.test.ts:19:44

Check failure on line 83 in typescript/src/buffer/bufferMath.ts

View workflow job for this annotation

GitHub Actions / checks

test/swaps.test.ts > swap tests > 'buffer-manual.json' +0 undefined

TypeError: Cannot mix BigInt and other types, use explicit conversions ❯ _convertToAssets src/buffer/bufferMath.ts:83:23 ❯ Module.calculateBufferAmounts src/buffer/bufferMath.ts:46:20 ❯ Module.erc4626BufferWrapOrUnwrap src/buffer/erc4626BufferWrapOrUnwrap.ts:26:12 ❯ Vault.swap src/vault/vault.ts:146:20 ❯ test/swaps.test.ts:19:44
if (rounding === Rounding.UP) return MathSol.mulUpFixed(sharesRaw, rate);
return MathSol.mulDownFixed(sharesRaw, rate);
}
1 change: 1 addition & 0 deletions typescript/src/buffer/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type BufferMutable = {
export type BufferImmutable = {
poolAddress: string;
tokens: string[];
scalingFactor: bigint; // between wrapped/underlying
};

/**
Expand Down
1 change: 1 addition & 0 deletions typescript/src/buffer/erc4626BufferWrapOrUnwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ export function erc4626BufferWrapOrUnwrap(
input.swapKind,
input.amountRaw,
poolState.rate,
poolState.scalingFactor,
);
}

0 comments on commit 28eb39b

Please sign in to comment.