From edeeb38d390d2762457498f62d618f94330cdc74 Mon Sep 17 00:00:00 2001 From: Foivos Date: Thu, 6 Jun 2024 19:23:03 +0300 Subject: [PATCH] lint and prettier --- scripts/publish-package.js | 5 +- scripts/tx-builder.js | 109 ++++++++++++++++++++----------------- scripts/utils.js | 16 +++--- 3 files changed, 70 insertions(+), 60 deletions(-) diff --git a/scripts/publish-package.js b/scripts/publish-package.js index 297ae424..767d0e28 100644 --- a/scripts/publish-package.js +++ b/scripts/publish-package.js @@ -1,12 +1,11 @@ require('dotenv').config(); -const { setConfig, getFullObject, requestSuiFromFaucet, updateMoveToml } = require('./utils'); +const { requestSuiFromFaucet, updateMoveToml } = require('./utils'); const { Ed25519Keypair } = require('@mysten/sui.js/keypairs/ed25519'); const { TransactionBlock } = require('@mysten/sui.js/transactions'); const { SuiClient } = require('@mysten/sui.js/client'); const { execSync } = require('child_process'); const { parseEnv } = require('./utils'); const tmp = require('tmp'); -const fs = require('fs'); const path = require('path'); async function publishPackage(packageName, client, keypair) { @@ -46,7 +45,7 @@ async function publishPackage(packageName, client, keypair) { showEffects: true, showObjectChanges: true, showContent: true, - } + }, }); if (publishTxn.effects?.status.status !== 'success') throw new Error('Publish Tx failed'); diff --git a/scripts/tx-builder.js b/scripts/tx-builder.js index 12200c68..d1f827c4 100644 --- a/scripts/tx-builder.js +++ b/scripts/tx-builder.js @@ -5,90 +5,99 @@ const { } = require('ethers'); function getObject(tx, object) { - if( typeof(object) === 'string' || Array.isArray(object) ) - return tx.object(object); + if (typeof object === 'string' || Array.isArray(object)) return tx.object(object); return object; } function getTypeName(type) { function get(type) { - const name = `${type.address}::${type.module}::${type.name}`; - if(type.typeArguments.length > 0) { + let name = `${type.address}::${type.module}::${type.name}`; + + if (type.typeArguments.length > 0) { name += `<${type.typeArguments[0]}`; - for(let i = 1; i < type.typeArguments.length; i++) { + + for (let i = 1; i < type.typeArguments.length; i++) { name += `,${type.typeArguments[i]}`; } + name += '>'; } + return name; } - if ( type.Struct ) { + + if (type.Struct) { return get(type.Struct); - } else if ( type.Reference ) { + } else if (type.Reference) { return getTypeName(type.Reference); - } else if ( type.MutableReference ) { + } else if (type.MutableReference) { return getTypeName(type.MutableReference); } else if (type.Vector) { - return `vector<${getTypeName(type.Vector)}>` - } else { - return type.toLowerCase(); + return `vector<${getTypeName(type.Vector)}>`; } + + return type.toLowerCase(); } function getNestedStruct(tx, type, arg) { - if ( type.Struct || type.Reference || type.MutableReference ) { + if (type.Struct || type.Reference || type.MutableReference) { return getObject(tx, arg); } - if(!type.Vector) return null; - nested = arg.map(arg => getNestedStruct(tx, type.Vector, arg)); - if(!nested) return null; + if (!type.Vector) return null; + const nested = arg.map((arg) => getNestedStruct(tx, type.Vector, arg)); + if (!nested) return null; const typeName = getTypeName(type.Vector); return tx.makeMoveVec({ - type: typeName, + type: typeName, objects: nested, }); } -function serialize (tx, type, arg) { - let struct = getNestedStruct(tx, type, arg) - if(struct) { +function serialize(tx, type, arg) { + const struct = getNestedStruct(tx, type, arg); + + if (struct) { return struct; } - bcs.address = () => bcs.fixedArray(32, bcs.u8()).transform({ - input: (id) => arrayify(id), - output: (id) => hexlify(id), - }); + bcs.address = () => + bcs.fixedArray(32, bcs.u8()).transform({ + input: (id) => arrayify(id), + output: (id) => hexlify(id), + }); - const vectorU8 = () => bcs.vector(bcs.u8()).transform({ - input: (input) => { - if(typeof(input) === 'string') input = arrayify(input); - return bcs.vector(bcs.u8()).serialize(input).toBytes(); - } - }) + const vectorU8 = () => + bcs.vector(bcs.u8()).transform({ + input(input) { + if (typeof input === 'string') input = arrayify(input); + return bcs.vector(bcs.u8()).serialize(input).toBytes(); + }, + }); const serializer = (type) => { - if (typeof(type) === 'string') { + if (typeof type === 'string') { return bcs[type.toLowerCase()](); } else if (type.Vector) { - if(type.Vector === 'U8') { + if (type.Vector === 'U8') { return vectorU8(); } + return bcs.vector(serializer(type.Vector)); - } else { - return null; } - } + + return null; + }; + return tx.pure(serializer(type).serialize(arg).toBytes()); } function isTxContext(parameter) { parameter = parameter.MutableReference; - if(!parameter) return false; + if (!parameter) return false; parameter = parameter.Struct; - if(!parameter) return false; + if (!parameter) return false; return parameter.address === '0x2' && parameter.module === 'tx_context' && parameter.name === 'TxContext'; } @@ -97,12 +106,12 @@ class TxBuilder { this.client = client; this.keypair = keypair; this.tx = new TransactionBlock(); - this.tx.object + this.tx.object; } async moveCall(target, args, typeArguments = []) { // If target is string, convert to object that `getNormalizedMoveFunction` accepts. - if(typeof(target) === 'string') { + if (typeof target === 'string') { const first = target.indexOf(':'); const last = target.indexOf(':', first + 2); const packageId = target.slice(0, first); @@ -112,21 +121,25 @@ class TxBuilder { package: packageId, module, function: functionName, - } + }; } + const moveFn = await this.client.getNormalizedMoveFunction(target); let length = moveFn.parameters.length; - if(isTxContext(moveFn.parameters[length - 1])) length = length - 1; - if(length != args.length) throw new Error(`Function ${target.package}::${target.module}::${target.function} takes ${moveFn.parameters.length} arguments but given ${args.length}`); + if (isTxContext(moveFn.parameters[length - 1])) length = length - 1; + if (length !== args.length) + throw new Error( + `Function ${target.package}::${target.module}::${target.function} takes ${moveFn.parameters.length} arguments but given ${args.length}`, + ); const convertedArgs = args.map((arg, index) => serialize(this.tx, moveFn.parameters[index], arg)); return this.tx.moveCall({ target: `${target.package}::${target.module}::${target.function}`, arguments: convertedArgs, - typeArguments: typeArguments, - }) + typeArguments, + }); } async signAndExecute(options) { @@ -137,15 +150,13 @@ class TxBuilder { showEffects: true, showObjectChanges: true, showContent: true, - ...options - } + ...options, + }, }); return result; } } - - module.exports = { - TxBuilder -} + TxBuilder, +}; diff --git a/scripts/utils.js b/scripts/utils.js index 5c4239bd..9a7ec6c2 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -1,7 +1,4 @@ -const { arrayify } = require('ethers/lib/utils'); const fs = require('fs'); - -const configs = {}; const { requestSuiFromFaucetV0, getFaucetHost } = require('@mysten/sui.js/faucet'); const { getFullnodeUrl } = require('@mysten/sui.js/client'); @@ -57,9 +54,8 @@ async function requestSuiFromFaucet(env, address) { } function updateMoveToml(packageName, packageId, moveDir = `${__dirname}/../move`) { - const path = `${moveDir}/${packageName}/Move.toml`; - + let toml = fs.readFileSync(path, 'utf8'); const lines = toml.split('\n'); @@ -77,8 +73,12 @@ function updateMoveToml(packageName, packageId, moveDir = `${__dirname}/../move` for (let i = addressesIndex + 1; i < lines.length; i++) { const line = lines[i]; const eqIndex = line.indexOf('='); - - if (eqIndex < 0 || line.slice(0, packageName.length) !== packageName || line.slice(packageName.length, eqIndex) !== Array(eqIndex - packageName.length + 1).join(' ') ) { + + if ( + eqIndex < 0 || + line.slice(0, packageName.length) !== packageName || + line.slice(packageName.length, eqIndex) !== Array(eqIndex - packageName.length + 1).join(' ') + ) { continue; } @@ -86,7 +86,7 @@ function updateMoveToml(packageName, packageId, moveDir = `${__dirname}/../move` } toml = lines.join('\n'); - + fs.writeFileSync(path, toml); }