Skip to content

Commit

Permalink
lint and prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Foivos committed Jun 6, 2024
1 parent 296eece commit edeeb38
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 60 deletions.
5 changes: 2 additions & 3 deletions scripts/publish-package.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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');

Expand Down
109 changes: 60 additions & 49 deletions scripts/tx-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -137,15 +150,13 @@ class TxBuilder {
showEffects: true,
showObjectChanges: true,
showContent: true,
...options
}
...options,
},
});
return result;
}
}



module.exports = {
TxBuilder
}
TxBuilder,
};
16 changes: 8 additions & 8 deletions scripts/utils.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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');
Expand All @@ -77,16 +73,20 @@ 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;
}

lines[i] = line.slice(0, eqIndex + 1) + ` "${packageId}"`;
}

toml = lines.join('\n');

fs.writeFileSync(path, toml);
}

Expand Down

0 comments on commit edeeb38

Please sign in to comment.