-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add calldata compression to mint function * prettier * fix * fix * fix * fix test cmd
- Loading branch information
Showing
14 changed files
with
194 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@soundxyz/sdk': patch | ||
--- | ||
|
||
Add calldata compression to mint function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { cdCompress, cdDecompress } from './calldata' | ||
|
||
// minimal tests ported to sanity check typescript adaptation | ||
// see full test suite on https://github.com/Vectorized/solady/blob/main/js/solady.test.js | ||
describe('cdCompress', () => { | ||
test('LibZip: Calldata compress', () => { | ||
const data = | ||
'0xac9650d80000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000a40c49ccbe000000000000000000000000000000000000000000000000000000000005b70e00000000000000000000000000000000000000000000000000000dfc79825feb0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000645c48a7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084fc6f7865000000000000000000000000000000000000000000000000000000000005b70e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffff00000000000000000000000000000000ffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004449404b7c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000001f1cdf1a632eaaab40d1c263edf49faf749010a1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000064df2ab5bb0000000000000000000000007f5c764cbc14f9669b88837ca1490cca17c3160700000000000000000000000000000000000000000000000000000000000000000000000000000000000000001f1cdf1a632eaaab40d1c263edf49faf749010a100000000000000000000000000000000000000000000000000000000' | ||
const expected = | ||
'0x5369af27001e20001e04001e80001d0160001d0220001d02a0001ea40c49ccbe001c05b70e00190dfc79825feb005b645c48a7003a84fc6f7865001c05b70e002f008f000f008f003a4449404b7c002b1f1cdf1a632eaaab40d1c263edf49faf749010a1003a64df2ab5bb000b7f5c764cbc14f9669b88837ca1490cca17c31607002b1f1cdf1a632eaaab40d1c263edf49faf749010a1001b' | ||
expect(cdCompress(data)).toEqual(expected) | ||
}) | ||
}) | ||
|
||
describe('cdDecompress', () => { | ||
test('LibZip: Calldata decompress on invalid input', () => { | ||
const data = '0xffffffff00ff' | ||
const expected = | ||
'0x0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
expect(cdDecompress(data)).toEqual(expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* eslint-disable no-bitwise */ | ||
|
||
import type { Hex } from 'viem' | ||
|
||
/** | ||
* typescript adaptations of cdCompress and cdDecompress from | ||
* https://github.com/Vectorized/solady/blob/main/js/solady.js | ||
* | ||
* these are custom calldata compression and decompression functions | ||
* that are supported by Sound contracts (sound creator v2, sound edition v2, and super minter) | ||
* and can reduce calldata size (biggest influence on L2 gas costs) when there are lots of zeros or lots of 0xffs | ||
*/ | ||
export function cdCompress(original: Hex): Hex { | ||
const data = hexString(original) | ||
let output: Hex = '0x' | ||
let zeroCount: number = 0 | ||
let ffCount: number = 0 | ||
|
||
const pushByte = (b: number): void => { | ||
output += byteToString((+(output.length < 4 * 2 + 2) * 0xff) ^ b) | ||
} | ||
|
||
const rle = (value: number, distance: number): void => { | ||
pushByte(0x00) | ||
pushByte(distance - 1 + value * 0x80) | ||
} | ||
|
||
for (let i = 0; i < data.length; i += 2) { | ||
const currentByte: number = parseByte(data, i) | ||
if (currentByte === 0) { | ||
if (ffCount) { | ||
rle(1, ffCount) | ||
ffCount = 0 | ||
} | ||
if (++zeroCount === 0x80) { | ||
rle(0, 0x80) | ||
zeroCount = 0 | ||
} | ||
continue | ||
} | ||
if (currentByte === 0xff) { | ||
if (zeroCount) { | ||
rle(0, zeroCount) | ||
zeroCount = 0 | ||
} | ||
if (++ffCount === 0x20) { | ||
rle(1, 0x20) | ||
ffCount = 0 | ||
} | ||
continue | ||
} | ||
if (ffCount) { | ||
rle(1, ffCount) | ||
ffCount = 0 | ||
} | ||
if (zeroCount) { | ||
rle(0, zeroCount) | ||
zeroCount = 0 | ||
} | ||
pushByte(currentByte) | ||
} | ||
if (ffCount) { | ||
rle(1, ffCount) | ||
} | ||
if (zeroCount) { | ||
rle(0, zeroCount) | ||
} | ||
return output | ||
} | ||
|
||
export function cdDecompress(compressed: Hex): Hex { | ||
const data = hexString(compressed) | ||
let output: Hex = '0x' | ||
|
||
for (let i = 0; i < data.length; ) { | ||
let c: number = (+(i < 4 * 2) * 0xff) ^ parseByte(data, i) | ||
i += 2 | ||
if (!c) { | ||
c = (+(i < 4 * 2) * 0xff) ^ parseByte(data, i) | ||
const size: number = (c & 0x7f) + 1 | ||
i += 2 | ||
for (let j = 0; j < size; ++j) { | ||
output += byteToString((c >> 7 !== 0 && j < 32 ? 1 : 0) * 0xff) | ||
} | ||
continue | ||
} | ||
output += byteToString(c) | ||
} | ||
return output | ||
} | ||
|
||
function hexString(data: Hex): string { | ||
const match = data.trim().match(/^(0x)?([0-9A-Fa-f]*)$/) | ||
if (match && match[2] && match[2].length % 2 === 0) { | ||
return match[2] | ||
} | ||
throw new Error('Data must be a valid hex string with even length.') | ||
} | ||
|
||
function byteToString(b: number): string { | ||
return (b | 0x100).toString(16).substr(1) | ||
} | ||
|
||
function parseByte(data: string, i: number): number { | ||
return parseInt(data.substr(i, 2), 16) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
benchmark: { | ||
outputFile: './bench/report.json', | ||
reporters: process.env['CI'] ? ['json'] : ['verbose'], | ||
}, | ||
coverage: { | ||
reporter: process.env['CI'] ? ['lcov'] : ['text', 'json', 'html'], | ||
exclude: ['**/errors/utils.ts', '**/dist/**', '**/*.test.ts', '**/_test/**'], | ||
}, | ||
environment: 'node', | ||
// setupFiles: ['./test/setup.ts'], | ||
// globalSetup: ['./test/globalSetup.ts'], | ||
testTimeout: 30_000, | ||
}, | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters