@interest-protocol/vortex-sdk 0.0.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin.d.ts +17 -0
- package/dist/admin.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6101 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +6092 -0
- package/dist/index.mjs.map +1 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/vortex.d.ts +24 -0
- package/dist/vortex.d.ts.map +1 -0
- package/dist/vortex.types.d.ts +69 -0
- package/dist/vortex.types.d.ts.map +1 -0
- package/package.json +49 -0
- package/rollup.config.js +58 -0
- package/src/admin.ts +124 -0
- package/src/index.ts +4 -0
- package/src/utils.ts +66 -0
- package/src/vortex.ts +162 -0
- package/src/vortex.types.ts +84 -0
- package/tsconfig.json +18 -0
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const BN254_FIELD_MODULUS = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
2
|
+
export declare const generateRandomNote: () => {
|
|
3
|
+
nullifier: bigint;
|
|
4
|
+
secret: bigint;
|
|
5
|
+
commitment: bigint;
|
|
6
|
+
};
|
|
7
|
+
export declare function bigIntToFieldElement(value: bigint): bigint;
|
|
8
|
+
export declare function addressToFieldElement(address: string): bigint;
|
|
9
|
+
export declare const stringToField: (s: string) => bigint;
|
|
10
|
+
export declare function zeros(treeLevels: number): bigint[];
|
|
11
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,mBAAmB,iFACgD,CAAC;AAYjF,eAAO,MAAM,kBAAkB;;;;CAS9B,CAAC;AAEF,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,UAEjD;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,UAMpD;AAED,eAAO,MAAM,aAAa,GAAI,GAAG,MAAM,WAKtC,CAAC;AAEF,wBAAgB,KAAK,CAAC,UAAU,EAAE,MAAM,YAiBvC"}
|
package/dist/vortex.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { SdkConstructorArgs, DepositArgs, Pool, WithdrawArgs } from './vortex.types';
|
|
2
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
3
|
+
export declare class VortexSdk {
|
|
4
|
+
private client;
|
|
5
|
+
private packageId;
|
|
6
|
+
private pools;
|
|
7
|
+
TREE_HEIGHT: number;
|
|
8
|
+
BN254_FIELD_MODULUS: bigint;
|
|
9
|
+
constructor(args: SdkConstructorArgs);
|
|
10
|
+
generateRandomNote(): {
|
|
11
|
+
nullifier: bigint;
|
|
12
|
+
secret: bigint;
|
|
13
|
+
commitment: bigint;
|
|
14
|
+
};
|
|
15
|
+
poseidon1(a: bigint | number | string): bigint;
|
|
16
|
+
poseidon2(a: bigint | number | string, b: bigint | number | string): bigint;
|
|
17
|
+
addressToFieldElement(address: string): bigint;
|
|
18
|
+
bigIntToFieldElement(value: bigint): bigint;
|
|
19
|
+
zeros(): bigint[];
|
|
20
|
+
deposit({ commitment, pool, tx }: DepositArgs): Transaction;
|
|
21
|
+
withdraw({ proofPointsHex, root, nullifier, recipient, relayer, relayerFee, pool, tx, }: WithdrawArgs): Transaction;
|
|
22
|
+
root(pool: Pool): Promise<any>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=vortex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vortex.d.ts","sourceRoot":"","sources":["../src/vortex.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAElB,WAAW,EAEX,IAAI,EACJ,YAAY,EACb,MAAM,gBAAgB,CAAC;AAWxB,OAAO,EAAE,WAAW,EAAmB,MAAM,0BAA0B,CAAC;AAKxE,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,KAAK,CAAc;IAEpB,WAAW,SAAM;IACjB,mBAAmB,SAAuB;gBAErC,IAAI,EAAE,kBAAkB;IAMpC,kBAAkB;;;;;IAIlB,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAIrC,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAIlE,qBAAqB,CAAC,OAAO,EAAE,MAAM;IAIrC,oBAAoB,CAAC,KAAK,EAAE,MAAM;IAIlC,KAAK;IAIL,OAAO,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,EAAsB,EAAE,EAAE,WAAW;IA0BjE,QAAQ,CAAC,EACP,cAAc,EACd,IAAI,EACJ,SAAS,EACT,SAAS,EACT,OAAO,EACP,UAAU,EACV,IAAI,EACJ,EAAsB,GACvB,EAAE,YAAY;IAqCT,IAAI,CAAC,IAAI,EAAE,IAAI;CA4BtB"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { SuiClient } from '@mysten/sui/client';
|
|
2
|
+
import { Transaction, TransactionResult } from '@mysten/sui/transactions';
|
|
3
|
+
export declare enum Pool {
|
|
4
|
+
shrimp = 100000000,
|
|
5
|
+
dolphin = 1000000000,
|
|
6
|
+
whale = 10000000000
|
|
7
|
+
}
|
|
8
|
+
export declare enum Modules {
|
|
9
|
+
vortex = "vortex",
|
|
10
|
+
proof = "vortex_proof",
|
|
11
|
+
merkleTree = "vortex_merkle_tree"
|
|
12
|
+
}
|
|
13
|
+
export interface SharedObject {
|
|
14
|
+
objectId: string;
|
|
15
|
+
initialSharedVersion: string;
|
|
16
|
+
}
|
|
17
|
+
export interface VortexPools {
|
|
18
|
+
[Pool.shrimp]: SharedObject;
|
|
19
|
+
[Pool.dolphin]: SharedObject;
|
|
20
|
+
[Pool.whale]: SharedObject;
|
|
21
|
+
}
|
|
22
|
+
export interface SdkConstructorArgs {
|
|
23
|
+
client: SuiClient;
|
|
24
|
+
packageId: string;
|
|
25
|
+
pools: VortexPools;
|
|
26
|
+
}
|
|
27
|
+
export interface AdminSdkConstructorArgs {
|
|
28
|
+
packageId: string;
|
|
29
|
+
adminCap: string;
|
|
30
|
+
}
|
|
31
|
+
export interface DepositArgs {
|
|
32
|
+
tx?: Transaction;
|
|
33
|
+
commitment: bigint;
|
|
34
|
+
pool: Pool;
|
|
35
|
+
}
|
|
36
|
+
export interface WithdrawArgs {
|
|
37
|
+
tx?: Transaction;
|
|
38
|
+
pool: Pool;
|
|
39
|
+
proofPointsHex: string;
|
|
40
|
+
root: bigint;
|
|
41
|
+
nullifier: bigint;
|
|
42
|
+
recipient: string;
|
|
43
|
+
relayer: string;
|
|
44
|
+
relayerFee: bigint;
|
|
45
|
+
}
|
|
46
|
+
export interface NewPoolArgs {
|
|
47
|
+
tx?: Transaction;
|
|
48
|
+
pool: Pool;
|
|
49
|
+
}
|
|
50
|
+
export interface SharePoolArgs {
|
|
51
|
+
tx: Transaction;
|
|
52
|
+
pool: TransactionResult;
|
|
53
|
+
}
|
|
54
|
+
export interface SetDepositFeeArgs {
|
|
55
|
+
tx?: Transaction;
|
|
56
|
+
pool: TransactionResult | string;
|
|
57
|
+
fee: bigint;
|
|
58
|
+
}
|
|
59
|
+
export interface SetWithdrawFeeArgs {
|
|
60
|
+
tx?: Transaction;
|
|
61
|
+
pool: TransactionResult | string;
|
|
62
|
+
fee: bigint;
|
|
63
|
+
}
|
|
64
|
+
export interface SetGroth16VkArgs {
|
|
65
|
+
tx?: Transaction;
|
|
66
|
+
pool: TransactionResult | string;
|
|
67
|
+
vk: string;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=vortex.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vortex.types.d.ts","sourceRoot":"","sources":["../src/vortex.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE1E,oBAAY,IAAI;IAEd,MAAM,YAAc;IAEpB,OAAO,aAAgB;IAEvB,KAAK,cAAiB;CACvB;AAED,oBAAY,OAAO;IACjB,MAAM,WAAW;IACjB,KAAK,iBAAiB;IACtB,UAAU,uBAAuB;CAClC;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,oBAAoB,EAAE,MAAM,CAAC;CAC9B;AAED,MAAM,WAAW,WAAW;IAC1B,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC;IAC5B,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,YAAY,CAAC;IAC7B,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,YAAY,CAAC;CAC5B;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,SAAS,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,WAAW,CAAC;CACpB;AAED,MAAM,WAAW,uBAAuB;IACtC,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B,EAAE,CAAC,EAAE,WAAW,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,IAAI,CAAC;CACZ;AAED,MAAM,WAAW,YAAY;IAC3B,EAAE,CAAC,EAAE,WAAW,CAAC;IACjB,IAAI,EAAE,IAAI,CAAC;IACX,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,WAAW;IAC1B,EAAE,CAAC,EAAE,WAAW,CAAC;IACjB,IAAI,EAAE,IAAI,CAAC;CACZ;AAED,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,WAAW,CAAC;IAChB,IAAI,EAAE,iBAAiB,CAAC;CACzB;AAED,MAAM,WAAW,iBAAiB;IAChC,EAAE,CAAC,EAAE,WAAW,CAAC;IACjB,IAAI,EAAE,iBAAiB,GAAG,MAAM,CAAC;IACjC,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,kBAAkB;IACjC,EAAE,CAAC,EAAE,WAAW,CAAC;IACjB,IAAI,EAAE,iBAAiB,GAAG,MAAM,CAAC;IACjC,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,gBAAgB;IAC/B,EAAE,CAAC,EAAE,WAAW,CAAC;IACjB,IAAI,EAAE,iBAAiB,GAAG,MAAM,CAAC;IACjC,EAAE,EAAE,MAAM,CAAC;CACZ"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@interest-protocol/vortex-sdk",
|
|
3
|
+
"version": "0.0.1-alpha.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"source": "./src/index.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@mysten/sui": "^1.36.2",
|
|
21
|
+
"@polymedia/suitcase-core": "^0.0.67",
|
|
22
|
+
"poseidon-hash": "^1.0.1",
|
|
23
|
+
"poseidon-lite": "^0.3.0",
|
|
24
|
+
"ramda": "^0.30.1",
|
|
25
|
+
"tiny-invariant": "^1.3.3"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"rimraf": "^6.0.1",
|
|
29
|
+
"tsup": "^8.3.5",
|
|
30
|
+
"@interest-protocol/prettier-config": "1.0.0",
|
|
31
|
+
"@interest-protocol/typescript-config": "1.0.0"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public",
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@mysten/sui": "^1.36.2",
|
|
37
|
+
"@polymedia/suitcase-core": "^0.0.67",
|
|
38
|
+
"poseidon-hash": "^1.0.1",
|
|
39
|
+
"poseidon-lite": "^0.3.0",
|
|
40
|
+
"ramda": "^0.30.1",
|
|
41
|
+
"tiny-invariant": "^1.3.3"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"prebuild": "rimraf dist",
|
|
46
|
+
"build": "rollup -c rollup.config.js",
|
|
47
|
+
"build:types": "tsc --emitDeclarationOnly"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const resolve = require('@rollup/plugin-node-resolve');
|
|
2
|
+
const commonjs = require('@rollup/plugin-commonjs');
|
|
3
|
+
const typescript = require('@rollup/plugin-typescript');
|
|
4
|
+
const pkg = require('./package.json');
|
|
5
|
+
|
|
6
|
+
// Get workspace dependencies
|
|
7
|
+
const workspaceDeps = Object.keys(pkg.dependencies || {}).filter((dep) =>
|
|
8
|
+
pkg.dependencies[dep].startsWith('workspace:')
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
// Get publishConfig dependencies (which will remain external)
|
|
12
|
+
const publishConfigDeps = pkg.publishConfig?.dependencies
|
|
13
|
+
? Object.keys(pkg.publishConfig.dependencies)
|
|
14
|
+
: [];
|
|
15
|
+
|
|
16
|
+
// Workspace dependencies to bundle (not in publishConfig)
|
|
17
|
+
const depsToBundled = workspaceDeps.filter(
|
|
18
|
+
(dep) => !publishConfigDeps.includes(dep)
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
// All external dependencies
|
|
22
|
+
const allExternalDeps = [
|
|
23
|
+
...Object.keys(pkg.dependencies || {}).filter(
|
|
24
|
+
(dep) => !depsToBundled.includes(dep)
|
|
25
|
+
),
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
console.log('Workspace dependencies to bundle:', depsToBundled);
|
|
29
|
+
console.log('External dependencies:', allExternalDeps);
|
|
30
|
+
|
|
31
|
+
module.exports = {
|
|
32
|
+
input: 'src/index.ts',
|
|
33
|
+
output: [
|
|
34
|
+
{
|
|
35
|
+
file: 'dist/index.js',
|
|
36
|
+
format: 'cjs',
|
|
37
|
+
sourcemap: true,
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
file: 'dist/index.mjs',
|
|
41
|
+
format: 'esm',
|
|
42
|
+
sourcemap: true,
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
external: allExternalDeps,
|
|
46
|
+
plugins: [
|
|
47
|
+
resolve({
|
|
48
|
+
extensions: ['.ts', '.tsx', '.js', '.jsx'],
|
|
49
|
+
// Critical for bundling workspace dependencies
|
|
50
|
+
preserveSymlinks: false,
|
|
51
|
+
}),
|
|
52
|
+
commonjs(),
|
|
53
|
+
typescript({
|
|
54
|
+
tsconfig: './tsconfig.json',
|
|
55
|
+
declaration: true,
|
|
56
|
+
}),
|
|
57
|
+
],
|
|
58
|
+
};
|
package/src/admin.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { AdminSdkConstructorArgs } from './vortex.types';
|
|
2
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
3
|
+
import { fromHex } from '@mysten/sui/utils';
|
|
4
|
+
import {
|
|
5
|
+
NewPoolArgs,
|
|
6
|
+
Modules,
|
|
7
|
+
SharePoolArgs,
|
|
8
|
+
SetDepositFeeArgs,
|
|
9
|
+
SetWithdrawFeeArgs,
|
|
10
|
+
SetGroth16VkArgs,
|
|
11
|
+
} from './vortex.types';
|
|
12
|
+
|
|
13
|
+
export class VortexAdminSdk {
|
|
14
|
+
private packageId: string;
|
|
15
|
+
private adminCap: string;
|
|
16
|
+
|
|
17
|
+
constructor(args: AdminSdkConstructorArgs) {
|
|
18
|
+
this.packageId = args.packageId;
|
|
19
|
+
this.adminCap = args.adminCap;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
newPool({ pool, tx = new Transaction() }: NewPoolArgs) {
|
|
23
|
+
const vortex = tx.moveCall({
|
|
24
|
+
package: this.packageId,
|
|
25
|
+
module: Modules.vortex,
|
|
26
|
+
function: 'new',
|
|
27
|
+
arguments: [tx.object(this.adminCap), tx.pure.u64(pool)],
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
vortex,
|
|
32
|
+
tx,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
sharePool({ tx, pool }: SharePoolArgs) {
|
|
37
|
+
tx.moveCall({
|
|
38
|
+
package: this.packageId,
|
|
39
|
+
module: Modules.vortex,
|
|
40
|
+
function: 'share',
|
|
41
|
+
arguments: [pool],
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return tx;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setDepositFee({ tx = new Transaction(), pool, fee }: SetDepositFeeArgs) {
|
|
48
|
+
if (typeof pool === 'object') {
|
|
49
|
+
tx.moveCall({
|
|
50
|
+
package: this.packageId,
|
|
51
|
+
module: Modules.vortex,
|
|
52
|
+
function: 'set_deposit_fee',
|
|
53
|
+
arguments: [pool, tx.object(this.adminCap), tx.pure.u64(fee)],
|
|
54
|
+
});
|
|
55
|
+
} else {
|
|
56
|
+
tx.moveCall({
|
|
57
|
+
package: this.packageId,
|
|
58
|
+
module: Modules.vortex,
|
|
59
|
+
function: 'set_deposit_fee',
|
|
60
|
+
arguments: [
|
|
61
|
+
tx.object(pool),
|
|
62
|
+
tx.object(this.adminCap),
|
|
63
|
+
tx.pure.u64(fee),
|
|
64
|
+
],
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return tx;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
setWithdrawFee({ tx = new Transaction(), pool, fee }: SetWithdrawFeeArgs) {
|
|
72
|
+
if (typeof pool === 'object') {
|
|
73
|
+
tx.moveCall({
|
|
74
|
+
package: this.packageId,
|
|
75
|
+
module: Modules.vortex,
|
|
76
|
+
function: 'set_withdraw_fee',
|
|
77
|
+
arguments: [pool, tx.object(this.adminCap), tx.pure.u64(fee)],
|
|
78
|
+
});
|
|
79
|
+
} else {
|
|
80
|
+
tx.moveCall({
|
|
81
|
+
package: this.packageId,
|
|
82
|
+
module: Modules.vortex,
|
|
83
|
+
function: 'set_withdraw_fee',
|
|
84
|
+
arguments: [
|
|
85
|
+
tx.object(pool),
|
|
86
|
+
tx.object(this.adminCap),
|
|
87
|
+
tx.pure.u64(fee),
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return tx;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
setGroth16Vk({ tx = new Transaction(), pool, vk }: SetGroth16VkArgs) {
|
|
96
|
+
const vkBytes = fromHex(vk);
|
|
97
|
+
|
|
98
|
+
if (typeof pool === 'object') {
|
|
99
|
+
tx.moveCall({
|
|
100
|
+
package: this.packageId,
|
|
101
|
+
module: Modules.vortex,
|
|
102
|
+
function: 'set_groth16_vk',
|
|
103
|
+
arguments: [
|
|
104
|
+
pool,
|
|
105
|
+
tx.object(this.adminCap),
|
|
106
|
+
tx.pure.vector('u8', vkBytes),
|
|
107
|
+
],
|
|
108
|
+
});
|
|
109
|
+
} else {
|
|
110
|
+
tx.moveCall({
|
|
111
|
+
package: this.packageId,
|
|
112
|
+
module: Modules.vortex,
|
|
113
|
+
function: 'set_groth16_vk',
|
|
114
|
+
arguments: [
|
|
115
|
+
tx.object(pool),
|
|
116
|
+
tx.object(this.adminCap),
|
|
117
|
+
tx.pure.vector('u8', vkBytes),
|
|
118
|
+
],
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return tx;
|
|
123
|
+
}
|
|
124
|
+
}
|
package/src/index.ts
ADDED
package/src/utils.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { poseidon2, poseidon1 } from 'poseidon-lite';
|
|
2
|
+
import invariant from 'tiny-invariant';
|
|
3
|
+
|
|
4
|
+
import { isValidSuiAddress, normalizeSuiAddress } from '@mysten/sui/utils';
|
|
5
|
+
|
|
6
|
+
export const BN254_FIELD_MODULUS =
|
|
7
|
+
21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
8
|
+
|
|
9
|
+
function randomBigIntHex(byteLength: number): bigint {
|
|
10
|
+
const hexString = Array.from(
|
|
11
|
+
crypto.getRandomValues(new Uint8Array(byteLength))
|
|
12
|
+
)
|
|
13
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
14
|
+
.join('');
|
|
15
|
+
|
|
16
|
+
return BigInt('0x' + hexString);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const generateRandomNote = () => {
|
|
20
|
+
const nullifier = randomBigIntHex(31);
|
|
21
|
+
const secret = randomBigIntHex(31);
|
|
22
|
+
const commitment = poseidon2([nullifier, secret]);
|
|
23
|
+
return {
|
|
24
|
+
nullifier,
|
|
25
|
+
secret,
|
|
26
|
+
commitment,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export function bigIntToFieldElement(value: bigint) {
|
|
31
|
+
return value % BN254_FIELD_MODULUS;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function addressToFieldElement(address: string) {
|
|
35
|
+
invariant(isValidSuiAddress(address), 'Invalid Sui address');
|
|
36
|
+
|
|
37
|
+
const cleanAddress = normalizeSuiAddress(address, true);
|
|
38
|
+
|
|
39
|
+
return bigIntToFieldElement(BigInt(cleanAddress));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const stringToField = (s: string) => {
|
|
43
|
+
const bytes = new TextEncoder().encode(s);
|
|
44
|
+
let acc = 0n;
|
|
45
|
+
for (const b of bytes) acc = (acc << 8n) | BigInt(b);
|
|
46
|
+
return acc % BN254_FIELD_MODULUS;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export function zeros(treeLevels: number) {
|
|
50
|
+
if (treeLevels < 1) throw new Error('treeLevels must be >= 1');
|
|
51
|
+
|
|
52
|
+
// ZERO_VALUE := Poseidon("vortex")
|
|
53
|
+
const ZERO_VALUE = poseidon1([stringToField('vortex')]);
|
|
54
|
+
|
|
55
|
+
const zeros: bigint[] = [];
|
|
56
|
+
|
|
57
|
+
let currentZero = ZERO_VALUE;
|
|
58
|
+
zeros.push(currentZero);
|
|
59
|
+
|
|
60
|
+
for (let i = 1; i < treeLevels; i++) {
|
|
61
|
+
currentZero = poseidon2([currentZero, currentZero]);
|
|
62
|
+
zeros.push(currentZero);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return zeros;
|
|
66
|
+
}
|
package/src/vortex.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SdkConstructorArgs,
|
|
3
|
+
VortexPools,
|
|
4
|
+
DepositArgs,
|
|
5
|
+
Modules,
|
|
6
|
+
Pool,
|
|
7
|
+
WithdrawArgs,
|
|
8
|
+
} from './vortex.types';
|
|
9
|
+
import { SuiClient } from '@mysten/sui/client';
|
|
10
|
+
import {
|
|
11
|
+
generateRandomNote,
|
|
12
|
+
zeros,
|
|
13
|
+
BN254_FIELD_MODULUS,
|
|
14
|
+
addressToFieldElement,
|
|
15
|
+
bigIntToFieldElement,
|
|
16
|
+
} from './utils';
|
|
17
|
+
import { poseidon2, poseidon1 } from 'poseidon-lite';
|
|
18
|
+
import { devInspectAndGetReturnValues } from '@polymedia/suitcase-core';
|
|
19
|
+
import { Transaction, coinWithBalance } from '@mysten/sui/transactions';
|
|
20
|
+
import { fromHex, SUI_TYPE_ARG } from '@mysten/sui/utils';
|
|
21
|
+
import { bcs } from '@mysten/sui/bcs';
|
|
22
|
+
import invariant from 'tiny-invariant';
|
|
23
|
+
|
|
24
|
+
export class VortexSdk {
|
|
25
|
+
private client: SuiClient;
|
|
26
|
+
private packageId: string;
|
|
27
|
+
private pools: VortexPools;
|
|
28
|
+
|
|
29
|
+
public TREE_HEIGHT = 26;
|
|
30
|
+
public BN254_FIELD_MODULUS = BN254_FIELD_MODULUS;
|
|
31
|
+
|
|
32
|
+
constructor(args: SdkConstructorArgs) {
|
|
33
|
+
this.client = args.client;
|
|
34
|
+
this.packageId = args.packageId;
|
|
35
|
+
this.pools = args.pools;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
generateRandomNote() {
|
|
39
|
+
return generateRandomNote();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
poseidon1(a: bigint | number | string) {
|
|
43
|
+
return poseidon1([a]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
poseidon2(a: bigint | number | string, b: bigint | number | string) {
|
|
47
|
+
return poseidon2([a, b]);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
addressToFieldElement(address: string) {
|
|
51
|
+
return addressToFieldElement(address);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
bigIntToFieldElement(value: bigint) {
|
|
55
|
+
return bigIntToFieldElement(value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
zeros() {
|
|
59
|
+
return zeros(this.TREE_HEIGHT);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
deposit({ commitment, pool, tx = new Transaction() }: DepositArgs) {
|
|
63
|
+
const poolObject = this.pools[pool];
|
|
64
|
+
|
|
65
|
+
const suiCoin = coinWithBalance({
|
|
66
|
+
type: SUI_TYPE_ARG,
|
|
67
|
+
balance: pool,
|
|
68
|
+
})(tx);
|
|
69
|
+
|
|
70
|
+
tx.moveCall({
|
|
71
|
+
package: this.packageId,
|
|
72
|
+
module: Modules.vortex,
|
|
73
|
+
function: 'deposit',
|
|
74
|
+
arguments: [
|
|
75
|
+
tx.sharedObjectRef({
|
|
76
|
+
objectId: poolObject.objectId,
|
|
77
|
+
mutable: true,
|
|
78
|
+
initialSharedVersion: poolObject.initialSharedVersion,
|
|
79
|
+
}),
|
|
80
|
+
suiCoin,
|
|
81
|
+
tx.pure.u256(commitment),
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return tx;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
withdraw({
|
|
89
|
+
proofPointsHex,
|
|
90
|
+
root,
|
|
91
|
+
nullifier,
|
|
92
|
+
recipient,
|
|
93
|
+
relayer,
|
|
94
|
+
relayerFee,
|
|
95
|
+
pool,
|
|
96
|
+
tx = new Transaction(),
|
|
97
|
+
}: WithdrawArgs) {
|
|
98
|
+
const poolObject = this.pools[pool];
|
|
99
|
+
|
|
100
|
+
const proof = tx.moveCall({
|
|
101
|
+
package: this.packageId,
|
|
102
|
+
module: Modules.proof,
|
|
103
|
+
function: 'new',
|
|
104
|
+
arguments: [
|
|
105
|
+
tx.pure.vector('u8', fromHex(proofPointsHex)),
|
|
106
|
+
tx.pure.vector('u8', []),
|
|
107
|
+
tx.pure.vector('u8', []),
|
|
108
|
+
tx.pure.u256(root),
|
|
109
|
+
tx.pure.u256(nullifier),
|
|
110
|
+
tx.pure.address(recipient),
|
|
111
|
+
tx.pure.u64(pool),
|
|
112
|
+
tx.pure.address(relayer),
|
|
113
|
+
tx.pure.u64(relayerFee),
|
|
114
|
+
],
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
tx.moveCall({
|
|
118
|
+
package: this.packageId,
|
|
119
|
+
module: Modules.vortex,
|
|
120
|
+
function: 'withdraw',
|
|
121
|
+
arguments: [
|
|
122
|
+
tx.sharedObjectRef({
|
|
123
|
+
objectId: poolObject.objectId,
|
|
124
|
+
mutable: true,
|
|
125
|
+
initialSharedVersion: poolObject.initialSharedVersion,
|
|
126
|
+
}),
|
|
127
|
+
proof,
|
|
128
|
+
],
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return tx;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async root(pool: Pool) {
|
|
135
|
+
const tx = new Transaction();
|
|
136
|
+
|
|
137
|
+
const poolObject = this.pools[pool];
|
|
138
|
+
|
|
139
|
+
tx.moveCall({
|
|
140
|
+
package: this.packageId,
|
|
141
|
+
module: Modules.merkleTree,
|
|
142
|
+
function: 'root',
|
|
143
|
+
arguments: [
|
|
144
|
+
tx.sharedObjectRef({
|
|
145
|
+
objectId: poolObject.objectId,
|
|
146
|
+
mutable: false,
|
|
147
|
+
initialSharedVersion: poolObject.initialSharedVersion,
|
|
148
|
+
}),
|
|
149
|
+
],
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const result = await devInspectAndGetReturnValues(this.client, tx, [
|
|
153
|
+
[bcs.u256()],
|
|
154
|
+
]);
|
|
155
|
+
|
|
156
|
+
invariant(result[0], 'Root devInspectAndGetReturnValues failed');
|
|
157
|
+
|
|
158
|
+
const [root] = result[0][0].map((value: string) => BigInt(value));
|
|
159
|
+
|
|
160
|
+
return root;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { SuiClient } from '@mysten/sui/client';
|
|
2
|
+
import { Transaction, TransactionResult } from '@mysten/sui/transactions';
|
|
3
|
+
|
|
4
|
+
export enum Pool {
|
|
5
|
+
// 0.1 Sui
|
|
6
|
+
shrimp = 100_000_000,
|
|
7
|
+
// 1 Sui
|
|
8
|
+
dolphin = 1_000_000_000,
|
|
9
|
+
// 10 Sui
|
|
10
|
+
whale = 10_000_000_000,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export enum Modules {
|
|
14
|
+
vortex = 'vortex',
|
|
15
|
+
proof = 'vortex_proof',
|
|
16
|
+
merkleTree = 'vortex_merkle_tree',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SharedObject {
|
|
20
|
+
objectId: string;
|
|
21
|
+
initialSharedVersion: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface VortexPools {
|
|
25
|
+
[Pool.shrimp]: SharedObject;
|
|
26
|
+
[Pool.dolphin]: SharedObject;
|
|
27
|
+
[Pool.whale]: SharedObject;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SdkConstructorArgs {
|
|
31
|
+
client: SuiClient;
|
|
32
|
+
packageId: string;
|
|
33
|
+
pools: VortexPools;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface AdminSdkConstructorArgs {
|
|
37
|
+
packageId: string;
|
|
38
|
+
adminCap: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface DepositArgs {
|
|
42
|
+
tx?: Transaction;
|
|
43
|
+
commitment: bigint;
|
|
44
|
+
pool: Pool;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface WithdrawArgs {
|
|
48
|
+
tx?: Transaction;
|
|
49
|
+
pool: Pool;
|
|
50
|
+
proofPointsHex: string;
|
|
51
|
+
root: bigint;
|
|
52
|
+
nullifier: bigint;
|
|
53
|
+
recipient: string;
|
|
54
|
+
relayer: string;
|
|
55
|
+
relayerFee: bigint;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface NewPoolArgs {
|
|
59
|
+
tx?: Transaction;
|
|
60
|
+
pool: Pool;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface SharePoolArgs {
|
|
64
|
+
tx: Transaction;
|
|
65
|
+
pool: TransactionResult;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface SetDepositFeeArgs {
|
|
69
|
+
tx?: Transaction;
|
|
70
|
+
pool: TransactionResult | string;
|
|
71
|
+
fee: bigint;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface SetWithdrawFeeArgs {
|
|
75
|
+
tx?: Transaction;
|
|
76
|
+
pool: TransactionResult | string;
|
|
77
|
+
fee: bigint;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface SetGroth16VkArgs {
|
|
81
|
+
tx?: Transaction;
|
|
82
|
+
pool: TransactionResult | string;
|
|
83
|
+
vk: string;
|
|
84
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2020",
|
|
4
|
+
"module": "esnext",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"sourceMap": true,
|
|
10
|
+
"outDir": "dist",
|
|
11
|
+
"rootDir": "src",
|
|
12
|
+
"strict": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true
|
|
15
|
+
},
|
|
16
|
+
"@interest-protocol/*": ["../*/src"],
|
|
17
|
+
"exclude": ["node_modules", "dist"]
|
|
18
|
+
}
|