diff --git a/.eslintrc b/.eslintrc index 0b44142f..5943b6a7 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,5 +1,5 @@ { - "extends": "richardpringle", + "extends": ["richardpringle", "plugin:@typescript-eslint/recommended"], "env": { "es2020": true, "mocha": true diff --git a/.prettierrc b/.prettierrc index ee79893b..70ea16e0 100644 --- a/.prettierrc +++ b/.prettierrc @@ -11,6 +11,12 @@ "trailingComma": "all" } }, + { + "files": "*.ts", + "options": { + "trailingComma": "all" + } + }, { "files": "*.json", "options": { diff --git a/package-lock.json b/package-lock.json index 263cd923..35e1c859 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,9 @@ }, "devDependencies": { "@ianvs/prettier-plugin-sort-imports": "^4.2.1", + "@types/tmp": "^0.2.6", + "@typescript-eslint/eslint-plugin": "^7.13.1", + "@typescript-eslint/parser": "^7.13.1", "dotenv": "^16.3.1", "eslint": "^8.57.0", "eslint-config-richardpringle": "^2.0.0", @@ -1701,6 +1704,233 @@ "undici-types": "~5.26.4" } }, + "node_modules/@types/tmp": { + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/@types/tmp/-/tmp-0.2.6.tgz", + "integrity": "sha512-chhaNf2oKHlRkDGt+tiKE2Z5aJ6qalm7Z9rlLdBwmOiAAf09YQvvoLXjWK4HWPF1xU/fqvMgfNfpVoBscA/tKA==", + "dev": true + }, + "node_modules/@typescript-eslint/eslint-plugin": { + "version": "7.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-7.13.1.tgz", + "integrity": "sha512-kZqi+WZQaZfPKnsflLJQCz6Ze9FFSMfXrrIOcyargekQxG37ES7DJNpJUE9Q/X5n3yTIP/WPutVNzgknQ7biLg==", + "dev": true, + "dependencies": { + "@eslint-community/regexpp": "^4.10.0", + "@typescript-eslint/scope-manager": "7.13.1", + "@typescript-eslint/type-utils": "7.13.1", + "@typescript-eslint/utils": "7.13.1", + "@typescript-eslint/visitor-keys": "7.13.1", + "graphemer": "^1.4.0", + "ignore": "^5.3.1", + "natural-compare": "^1.4.0", + "ts-api-utils": "^1.3.0" + }, + "engines": { + "node": "^18.18.0 || >=20.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependencies": { + "@typescript-eslint/parser": "^7.0.0", + "eslint": "^8.56.0" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/@typescript-eslint/parser": { + "version": "7.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-7.13.1.tgz", + "integrity": "sha512-1ELDPlnLvDQ5ybTSrMhRTFDfOQEOXNM+eP+3HT/Yq7ruWpciQw+Avi73pdEbA4SooCawEWo3dtYbF68gN7Ed1A==", + "dev": true, + "dependencies": { + "@typescript-eslint/scope-manager": "7.13.1", + "@typescript-eslint/types": "7.13.1", + "@typescript-eslint/typescript-estree": "7.13.1", + "@typescript-eslint/visitor-keys": "7.13.1", + "debug": "^4.3.4" + }, + "engines": { + "node": "^18.18.0 || >=20.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependencies": { + "eslint": "^8.56.0" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/@typescript-eslint/scope-manager": { + "version": "7.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-7.13.1.tgz", + "integrity": "sha512-adbXNVEs6GmbzaCpymHQ0MB6E4TqoiVbC0iqG3uijR8ZYfpAXMGttouQzF4Oat3P2GxDVIrg7bMI/P65LiQZdg==", + "dev": true, + "dependencies": { + "@typescript-eslint/types": "7.13.1", + "@typescript-eslint/visitor-keys": "7.13.1" + }, + "engines": { + "node": "^18.18.0 || >=20.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "node_modules/@typescript-eslint/type-utils": { + "version": "7.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-7.13.1.tgz", + "integrity": "sha512-aWDbLu1s9bmgPGXSzNCxELu+0+HQOapV/y+60gPXafR8e2g1Bifxzevaa+4L2ytCWm+CHqpELq4CSoN9ELiwCg==", + "dev": true, + "dependencies": { + "@typescript-eslint/typescript-estree": "7.13.1", + "@typescript-eslint/utils": "7.13.1", + "debug": "^4.3.4", + "ts-api-utils": "^1.3.0" + }, + "engines": { + "node": "^18.18.0 || >=20.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependencies": { + "eslint": "^8.56.0" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/@typescript-eslint/types": { + "version": "7.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-7.13.1.tgz", + "integrity": "sha512-7K7HMcSQIAND6RBL4kDl24sG/xKM13cA85dc7JnmQXw2cBDngg7c19B++JzvJHRG3zG36n9j1i451GBzRuHchw==", + "dev": true, + "engines": { + "node": "^18.18.0 || >=20.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, + "node_modules/@typescript-eslint/typescript-estree": { + "version": "7.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-7.13.1.tgz", + "integrity": "sha512-uxNr51CMV7npU1BxZzYjoVz9iyjckBduFBP0S5sLlh1tXYzHzgZ3BR9SVsNed+LmwKrmnqN3Kdl5t7eZ5TS1Yw==", + "dev": true, + "dependencies": { + "@typescript-eslint/types": "7.13.1", + "@typescript-eslint/visitor-keys": "7.13.1", + "debug": "^4.3.4", + "globby": "^11.1.0", + "is-glob": "^4.0.3", + "minimatch": "^9.0.4", + "semver": "^7.6.0", + "ts-api-utils": "^1.3.0" + }, + "engines": { + "node": "^18.18.0 || >=20.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/@typescript-eslint/typescript-estree/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/@typescript-eslint/typescript-estree/node_modules/minimatch": { + "version": "9.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.4.tgz", + "integrity": "sha512-KqWh+VchfxcMNRAJjj2tnsSJdNbHsVgnkBhTNrW7AjVo6OvLtxw8zfT9oLw1JSohlFzJ8jCoTgaoXvJ+kHt6fw==", + "dev": true, + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=16 || 14 >=14.17" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/@typescript-eslint/typescript-estree/node_modules/semver": { + "version": "7.6.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.2.tgz", + "integrity": "sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==", + "dev": true, + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@typescript-eslint/utils": { + "version": "7.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-7.13.1.tgz", + "integrity": "sha512-h5MzFBD5a/Gh/fvNdp9pTfqJAbuQC4sCN2WzuXme71lqFJsZtLbjxfSk4r3p02WIArOF9N94pdsLiGutpDbrXQ==", + "dev": true, + "dependencies": { + "@eslint-community/eslint-utils": "^4.4.0", + "@typescript-eslint/scope-manager": "7.13.1", + "@typescript-eslint/types": "7.13.1", + "@typescript-eslint/typescript-estree": "7.13.1" + }, + "engines": { + "node": "^18.18.0 || >=20.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependencies": { + "eslint": "^8.56.0" + } + }, + "node_modules/@typescript-eslint/visitor-keys": { + "version": "7.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-7.13.1.tgz", + "integrity": "sha512-k/Bfne7lrP7hcb7m9zSsgcBmo+8eicqqfNAJ7uUY+jkTFpKeH2FSkWpFRtimBxgkyvqfu9jTPRbYOvud6isdXA==", + "dev": true, + "dependencies": { + "@typescript-eslint/types": "7.13.1", + "eslint-visitor-keys": "^3.4.3" + }, + "engines": { + "node": "^18.18.0 || >=20.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } + }, "node_modules/@ungap/structured-clone": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.2.0.tgz", @@ -1909,6 +2139,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/array-union": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz", + "integrity": "sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/array.prototype.findlastindex": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/array.prototype.findlastindex/-/array.prototype.findlastindex-1.2.5.tgz", @@ -2074,6 +2313,18 @@ "concat-map": "0.0.1" } }, + "node_modules/braces": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", + "dev": true, + "dependencies": { + "fill-range": "^7.1.1" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/brorand": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/brorand/-/brorand-1.1.0.tgz", @@ -2420,6 +2671,18 @@ "node": ">=6" } }, + "node_modules/dir-glob": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz", + "integrity": "sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA==", + "dev": true, + "dependencies": { + "path-type": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -3084,6 +3347,34 @@ "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", "dev": true }, + "node_modules/fast-glob": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.2.tgz", + "integrity": "sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==", + "dev": true, + "dependencies": { + "@nodelib/fs.stat": "^2.0.2", + "@nodelib/fs.walk": "^1.2.3", + "glob-parent": "^5.1.2", + "merge2": "^1.3.0", + "micromatch": "^4.0.4" + }, + "engines": { + "node": ">=8.6.0" + } + }, + "node_modules/fast-glob/node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fast-json-stable-stringify": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", @@ -3117,6 +3408,18 @@ "node": "^10.12.0 || >=12.0.0" } }, + "node_modules/fill-range": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", + "dev": true, + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/find-up": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", @@ -3352,6 +3655,26 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/globby": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/globby/-/globby-11.1.0.tgz", + "integrity": "sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==", + "dev": true, + "dependencies": { + "array-union": "^2.1.0", + "dir-glob": "^3.0.1", + "fast-glob": "^3.2.9", + "ignore": "^5.2.0", + "merge2": "^1.4.1", + "slash": "^3.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/gopd": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz", @@ -3691,6 +4014,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true, + "engines": { + "node": ">=0.12.0" + } + }, "node_modules/is-number-object": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/is-number-object/-/is-number-object-1.0.7.tgz", @@ -3996,6 +4328,28 @@ "integrity": "sha512-GaqWWShW4kv/G9IEucWScBx9G1/vsFZZJUO+tD26M8J8z3Kw5RDQjaoZe03YAClgeS/SWPOcb4nkFBTEi5DUEA==", "peer": true }, + "node_modules/merge2": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", + "integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==", + "dev": true, + "engines": { + "node": ">= 8" + } + }, + "node_modules/micromatch": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.7.tgz", + "integrity": "sha512-LPP/3KorzCwBxfeUuZmaR6bG2kdeHSbe0P2tY3FLRU4vYrjYz5hI4QZwV0njUx3jeuKe67YukQ1LSPZBKDqO/Q==", + "dev": true, + "dependencies": { + "braces": "^3.0.3", + "picomatch": "^2.3.1" + }, + "engines": { + "node": ">=8.6" + } + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", @@ -4294,6 +4648,15 @@ "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", "dev": true }, + "node_modules/path-type": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz", + "integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/periscopic": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/periscopic/-/periscopic-3.1.0.tgz", @@ -4320,6 +4683,18 @@ "integrity": "sha512-anP1Z8qwhkbmu7MFP5iTt+wQKXgwzf7zTyGlcdzabySa9vd0Xt392U0rVmz9poOaBj0uHJKyyo9/upk0HrEQew==", "dev": true }, + "node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/possible-typed-array-names": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz", @@ -4687,6 +5062,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/slash": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz", + "integrity": "sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/source-map-js": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.0.tgz", @@ -4890,6 +5274,30 @@ "node": ">=4" } }, + "node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + }, + "node_modules/ts-api-utils": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/ts-api-utils/-/ts-api-utils-1.3.0.tgz", + "integrity": "sha512-UQMIo7pb8WRomKR1/+MFVLTroIvDVtMX3K6OUir8ynLyzB8Jeriont2bTAtmNPa1ekAgN7YPDyf6V+ygrdU+eQ==", + "dev": true, + "engines": { + "node": ">=16" + }, + "peerDependencies": { + "typescript": ">=4.2.0" + } + }, "node_modules/tsconfig-paths": { "version": "3.15.0", "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.15.0.tgz", diff --git a/package.json b/package.json index 89000bff..1555acea 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ "build": "./scripts/run.sh build", "test": "./scripts/run.sh test", "coverage": "./scripts/coverage.sh", - "lint": "eslint --fix './scripts/*.js'", - "prettier": "prettier --write './scripts/*.js'", + "lint": "eslint --fix './src/*.ts'", + "prettier": "prettier --write './src/*.ts'", "compile": "npx tsc", "postinstall": "npm run compile" }, @@ -36,6 +36,9 @@ }, "devDependencies": { "@ianvs/prettier-plugin-sort-imports": "^4.2.1", + "@types/tmp": "^0.2.6", + "@typescript-eslint/eslint-plugin": "^7.13.1", + "@typescript-eslint/parser": "^7.13.1", "dotenv": "^16.3.1", "eslint": "^8.57.0", "eslint-config-richardpringle": "^2.0.0", diff --git a/src/index.ts b/src/index.ts index 92d28245..32d7443f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ export * from './bcs'; export * from './tx-builder'; -export * from './utils'; \ No newline at end of file +export * from './utils'; diff --git a/src/tx-builder.ts b/src/tx-builder.ts index 19dd1f51..27a8e278 100644 --- a/src/tx-builder.ts +++ b/src/tx-builder.ts @@ -1,20 +1,25 @@ -import { TransactionBlock, TransactionObjectInput } from '@mysten/sui.js/transactions'; -import { SuiObjectChange, SuiMoveNormalizedType, SuiClient, SuiTransactionBlockResponseOptions } from '@mysten/sui.js/client'; -import { bcs, BcsType } from '@mysten/bcs'; -import { utils as ethersUtils} from 'ethers'; -const { arrayify, hexlify } = ethersUtils; -const tmp = require('tmp'); -import path from 'path'; -import { updateMoveToml } from './utils'; import { execSync } from 'child_process'; +import path from 'path'; +import { bcs, BcsType } from '@mysten/bcs'; +import { SuiClient, SuiMoveNormalizedType, SuiObjectChange, SuiTransactionBlockResponseOptions } from '@mysten/sui.js/client'; import { Keypair } from '@mysten/sui.js/dist/cjs/cryptography'; +import { TransactionBlock, TransactionObjectInput } from '@mysten/sui.js/transactions'; +import { utils as ethersUtils } from 'ethers'; +import { updateMoveToml } from './utils'; +import tmp from 'tmp'; + +const { arrayify, hexlify } = ethersUtils; -const objectCache: {[id in string]: SuiObjectChange} = {}; +const objectCache = {} as { [id in string]: SuiObjectChange }; function updateCache(objectChanges: SuiObjectChange[]) { for (const change of objectChanges) { - if (!(change as any).objectId) continue; - objectCache[(change as any).objectId] = change; + if (!(change as { + objectId: string; + }).objectId) continue; + objectCache[(change as { + objectId: string; + }).objectId] = change; } } @@ -39,7 +44,9 @@ function getObject(tx: TransactionBlock, object: TransactionObjectInput) { } function getTypeName(type: SuiMoveNormalizedType): string { - function get(type: {address: string, module: string, name: string, typeArguments: string[]}) { + type Type = { address: string; module: string; name: string; typeArguments: string[] }; + + function get(type: Type) { let name = `${type.address}::${type.module}::${type.name}`; if (type.typeArguments.length > 0) { @@ -55,23 +62,24 @@ function getTypeName(type: SuiMoveNormalizedType): string { return name; } - if ((type as any).Struct) { - return get((type as any).Struct); - } else if ((type as any).Reference) { - return getTypeName((type as any).Reference); - } else if ((type as any).MutableReference) { - return getTypeName((type as any).MutableReference); - } else if ((type as any).Vector) { - return `vector<${getTypeName((type as any).Vector)}>`; + if ((type as {Struct: Type}).Struct) { + return get((type as {Struct: Type}).Struct); + } else if ((type as {Reference: SuiMoveNormalizedType}).Reference) { + return getTypeName((type as {Reference: SuiMoveNormalizedType}).Reference); + } else if ((type as {MutableReference: SuiMoveNormalizedType}).MutableReference) { + return getTypeName((type as {MutableReference: SuiMoveNormalizedType}).MutableReference); + } else if ((type as {Vector: SuiMoveNormalizedType}).Vector) { + return `vector<${getTypeName((type as {Vector: SuiMoveNormalizedType}).Vector)}>`; } return (type as string).toLowerCase(); } function getNestedStruct(tx: TransactionBlock, type: SuiMoveNormalizedType, arg: TransactionObjectInput) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any let inside = type as any; - while (inside.Vector) { + while ((inside as {Vector: SuiMoveNormalizedType}).Vector) { inside = inside.Vector; } @@ -83,20 +91,22 @@ function getNestedStruct(tx: TransactionBlock, type: SuiMoveNormalizedType, arg: return null; } + // eslint-disable-next-line @typescript-eslint/no-explicit-any if ((type as any).Struct || (type as any).Reference || (type as any).MutableReference) { return getObject(tx, arg); } - if (!(type as any).Vector) return null; + if (!(type as {Vector: SuiMoveNormalizedType}).Vector) return null; + // eslint-disable-next-line @typescript-eslint/no-explicit-any const nested = (arg as any).map((arg: any) => getNestedStruct(tx, (type as any).Vector, arg)); - const typeName = getTypeName((type as any).Vector); + const typeName = getTypeName((type as {Vector: SuiMoveNormalizedType}).Vector); return tx.makeMoveVec({ type: typeName, objects: nested, }); } -function serialize(tx: TransactionBlock, type: SuiMoveNormalizedType, arg: TransactionObjectInput ) { +function serialize(tx: TransactionBlock, type: SuiMoveNormalizedType, arg: TransactionObjectInput) { const struct = getNestedStruct(tx, type, arg); if (struct) { @@ -105,34 +115,36 @@ function serialize(tx: TransactionBlock, type: SuiMoveNormalizedType, arg: Trans const vectorU8 = () => bcs.vector(bcs.u8()).transform({ - input: (val: unknown) => { + input(val: unknown) { if (typeof val === 'string') val = arrayify(val); - return val as Iterable & { length: number; }; + return val as Iterable & { length: number }; }, - output: (value: number[]) => { + output(value: number[]) { return hexlify(value); - } + }, }); const serializer = (type: SuiMoveNormalizedType): BcsType => { if (isString(type)) { - return bcs.string() as any; + return bcs.string() as BcsType; } if (typeof type === 'string') { - if(type === 'Address') { + if (type === 'Address') { return bcs.fixedArray(32, bcs.u8()).transform({ - input: (id) => arrayify(id as any), + input: (id) => arrayify(id as number), output: (id) => hexlify(id), }); } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any return (bcs as any)[(type as string).toLowerCase()](); - } else if ((type as any).Vector) { - if ((type as any).Vector === 'U8') { - return vectorU8() as any; + } else if ((type as {Vector: SuiMoveNormalizedType}).Vector) { + if ((type as {Vector: SuiMoveNormalizedType}).Vector === 'U8') { + return vectorU8() as BcsType; } - return bcs.vector(serializer((type as any).Vector) as BcsType) as any; + return bcs.vector(serializer((type as {Vector: SuiMoveNormalizedType}).Vector)) as BcsType; } throw new Error(`Type ${JSON.stringify(type)} cannot be serialized`); @@ -142,8 +154,10 @@ function serialize(tx: TransactionBlock, type: SuiMoveNormalizedType, arg: Trans } function isTxContext(parameter: SuiMoveNormalizedType) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any let inside = parameter as any; - if(inside.MutableReference) { + + if (inside.MutableReference) { inside = inside.MutableReference.Struct; if (!inside) return false; } else if (inside.Reference) { @@ -152,10 +166,12 @@ function isTxContext(parameter: SuiMoveNormalizedType) { } else { return false; } + return inside.address === '0x2' && inside.module === 'tx_context' && inside.name === 'TxContext'; } function isString(parameter: SuiMoveNormalizedType) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any let asAny = parameter as any; if (asAny.MutableReference) parameter = asAny.MutableReference; if (asAny.Reference) asAny = asAny.Reference; @@ -177,9 +193,10 @@ class TxBuilder { async moveCall(moveCallInfo: { arguments?: TransactionObjectInput[]; typeArguments?: string[]; - target: `${string}::${string}::${string}` | {package: string, module: string, function: string}; + target: `${string}::${string}::${string}` | { package: string; module: string; function: string }; }) { let target = moveCallInfo.target; + // If target is string, convert to object that `getNormalizedMoveFunction` accepts. if (typeof target === 'string') { const first = target.indexOf(':'); @@ -198,7 +215,7 @@ class TxBuilder { let length = moveFn.parameters.length; if (isTxContext(moveFn.parameters[length - 1])) length = length - 1; - if(!moveCallInfo.arguments) moveCallInfo.arguments = []; + if (!moveCallInfo.arguments) moveCallInfo.arguments = []; if (length !== moveCallInfo.arguments.length) throw new Error( `Function ${target.package}::${target.module}::${target.function} takes ${moveFn.parameters.length} arguments but given ${moveCallInfo.arguments.length}`, @@ -208,6 +225,7 @@ class TxBuilder { return this.tx.moveCall({ target: `${target.package}::${target.module}::${target.function}`, + // eslint-disable-next-line @typescript-eslint/no-explicit-any arguments: convertedArgs as any, typeArguments: moveCallInfo.typeArguments, }); @@ -249,8 +267,9 @@ class TxBuilder { ...options, }, }); - if(!result.confirmedLocalExecution) { - while(true) { + + if (!result.confirmedLocalExecution) { + while (true) { try { result = await this.client.getTransactionBlock({ digest: result.digest, @@ -258,15 +277,16 @@ class TxBuilder { showEffects: true, showObjectChanges: true, ...options, - } + }, }); break; - } catch(e) { + } catch (e) { console.log(e); await new Promise((resolve) => setTimeout(resolve, 1000)); } } } + updateCache(result.objectChanges as SuiObjectChange[]); return result; } diff --git a/src/utils.ts b/src/utils.ts index 73c371dc..69bec017 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -82,7 +82,7 @@ export function parseEnv(arg: string) { case 'devnet': case 'testnet': case 'mainnet': - return { alias: arg, url: getFullnodeUrl(arg as "localnet" | "devnet" | "testnet" | "mainnet") }; + return { alias: arg, url: getFullnodeUrl(arg as 'localnet' | 'devnet' | 'testnet' | 'mainnet') }; default: return JSON.parse(arg); }