@scallop-io/sui-kit
Advanced tools
Comparing version 0.35.2 to 0.35.3
@@ -19,15 +19,2 @@ "use strict"; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __accessCheck = (obj, member, msg) => { | ||
if (!member.has(obj)) | ||
throw TypeError("Cannot " + msg); | ||
}; | ||
var __privateAdd = (obj, member, value) => { | ||
if (member.has(obj)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
member instanceof WeakSet ? member.add(obj) : member.set(obj, value); | ||
}; | ||
var __privateMethod = (obj, member, method) => { | ||
__accessCheck(obj, member, "access private method"); | ||
return method; | ||
}; | ||
@@ -37,4 +24,4 @@ // src/index.ts | ||
__export(src_exports, { | ||
SUI_CLOCK_OBJECT_ID: () => import_sui9.SUI_CLOCK_OBJECT_ID, | ||
SUI_SYSTEM_STATE_OBJECT_ID: () => import_sui9.SUI_SYSTEM_STATE_OBJECT_ID, | ||
SUI_CLOCK_OBJECT_ID: () => import_sui10.SUI_CLOCK_OBJECT_ID, | ||
SUI_SYSTEM_STATE_OBJECT_ID: () => import_sui10.SUI_SYSTEM_STATE_OBJECT_ID, | ||
SuiAccountManager: () => SuiAccountManager, | ||
@@ -44,9 +31,9 @@ SuiKit: () => SuiKit, | ||
SuiTxBlock: () => SuiTxBlock, | ||
TransactionBlock: () => import_sui9.TransactionBlock | ||
TransactionBlock: () => import_sui10.TransactionBlock | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var import_sui9 = require("@mysten/sui.js"); | ||
var import_sui10 = require("@mysten/sui.js"); | ||
// src/lib/sui-kit.ts | ||
var import_sui8 = require("@mysten/sui.js"); | ||
var import_sui9 = require("@mysten/sui.js"); | ||
@@ -293,5 +280,6 @@ // src/lib/sui-account-manager/sui-account-manager.ts | ||
// src/lib/sui-tx-builder/sui-tx-block.ts | ||
var import_sui7 = require("@mysten/sui.js"); | ||
var import_sui8 = require("@mysten/sui.js"); | ||
// src/lib/sui-tx-builder/util.ts | ||
var import_sui7 = require("@mysten/sui.js"); | ||
var getDefaultSuiInputType = (value) => { | ||
@@ -308,25 +296,40 @@ if (typeof value === "string" && value.startsWith("0x")) { | ||
}; | ||
function makeVecParam(txBlock, args, type) { | ||
if (args.length === 0) | ||
throw new Error("Transaction builder error: Empty array is not allowed"); | ||
const defaultSuiType = getDefaultSuiInputType(args[0]); | ||
if (type === "object" || !type && defaultSuiType === "object") { | ||
const objects = args.map( | ||
(arg) => typeof arg === "string" ? txBlock.object((0, import_sui7.normalizeSuiObjectId)(arg)) : arg | ||
); | ||
return txBlock.makeMoveVec({ objects }); | ||
} else { | ||
const vecType = type || defaultSuiType; | ||
return txBlock.pure(args, `vector<${vecType}>`); | ||
} | ||
} | ||
function isMoveVecArg(arg) { | ||
const isFullMoveVecArg = arg && arg.value && Array.isArray(arg.value) && arg.vecType; | ||
const isSimpleMoveVecArg = Array.isArray(arg); | ||
return isFullMoveVecArg || isSimpleMoveVecArg; | ||
} | ||
function convertArgs(txBlock, args) { | ||
return args.map((arg) => { | ||
if (typeof arg === "string" && arg.startsWith("0x")) { | ||
return txBlock.object((0, import_sui7.normalizeSuiObjectId)(arg)); | ||
} else if (isMoveVecArg(arg)) { | ||
const vecType = arg.vecType || void 0; | ||
return vecType ? makeVecParam(txBlock, arg.value, vecType) : makeVecParam(txBlock, arg); | ||
} else if (typeof arg !== "object") { | ||
return txBlock.pure(arg); | ||
} else { | ||
return arg; | ||
} | ||
}); | ||
} | ||
// src/lib/sui-tx-builder/sui-tx-block.ts | ||
var _makeVecParam, makeVecParam_fn, _isMoveVecArg, isMoveVecArg_fn, _convertArgs, convertArgs_fn; | ||
var SuiTxBlock = class { | ||
constructor(transaction) { | ||
/** | ||
* Since we know the elements in the array are the same type | ||
* If type is not provided, we will try to infer the type from the first element | ||
* By default, | ||
* | ||
* string starting with `0x` =====> object id | ||
* number, bigint ====> u64 | ||
* boolean =====> bool | ||
* | ||
* | ||
* If type is provided, we will use the type to convert the array | ||
* @param args | ||
* @param type 'address' | 'bool' | 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'object' | ||
*/ | ||
__privateAdd(this, _makeVecParam); | ||
__privateAdd(this, _isMoveVecArg); | ||
__privateAdd(this, _convertArgs); | ||
this.txBlock = new import_sui7.TransactionBlock(transaction); | ||
this.txBlock = new import_sui8.TransactionBlock(transaction); | ||
} | ||
@@ -338,3 +341,3 @@ //======== override methods of TransactionBlock ============ | ||
pure(value, type) { | ||
return this.txBlock.pure(value); | ||
return this.txBlock.pure(value, type); | ||
} | ||
@@ -391,3 +394,3 @@ object(value) { | ||
const tx = this.txBlock; | ||
tx.transferObjects(__privateMethod(this, _convertArgs, convertArgs_fn).call(this, objects), tx.pure(recipient)); | ||
tx.transferObjects(convertArgs(this.txBlock, objects), tx.pure(recipient)); | ||
return this; | ||
@@ -397,3 +400,3 @@ } | ||
const tx = this.txBlock; | ||
const coinObject = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, [coin])[0]; | ||
const coinObject = convertArgs(this.txBlock, [coin])[0]; | ||
const res = tx.splitCoins(coinObject, amounts.map((m) => tx.pure(m))); | ||
@@ -403,5 +406,5 @@ return amounts.map((_, i) => res[i]); | ||
mergeCoins(destination, sources) { | ||
const destionationObject = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, [destination])[0]; | ||
const sourceObjects = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, sources); | ||
return this.txBlock.mergeCoins(destionationObject, sourceObjects); | ||
const destinationObject = convertArgs(this.txBlock, [destination])[0]; | ||
const sourceObjects = convertArgs(this.txBlock, sources); | ||
return this.txBlock.mergeCoins(destinationObject, sourceObjects); | ||
} | ||
@@ -428,3 +431,3 @@ publish(...args) { | ||
throw new Error("Invalid target format. Expected `${string}::${string}::${string}`"); | ||
const convertedArgs = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, args); | ||
const convertedArgs = convertArgs(this.txBlock, args); | ||
const tx = this.txBlock; | ||
@@ -454,3 +457,3 @@ return tx.moveCall({ | ||
const tx = this.txBlock; | ||
const coinObjects = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, coins); | ||
const coinObjects = convertArgs(this.txBlock, coins); | ||
const mergedCoin = coinObjects[0]; | ||
@@ -469,3 +472,3 @@ if (coins.length > 1) { | ||
const tx = this.txBlock; | ||
const coinObjects = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, coins); | ||
const coinObjects = convertArgs(this.txBlock, coins); | ||
const mergedCoin = coinObjects[0]; | ||
@@ -498,3 +501,3 @@ if (coins.length > 1) { | ||
target: "0x3::sui_system::request_add_stake", | ||
arguments: [tx.object(import_sui7.SUI_SYSTEM_STATE_OBJECT_ID), stakeCoin, tx.pure(validatorAddr)] | ||
arguments: [tx.object(import_sui8.SUI_SYSTEM_STATE_OBJECT_ID), stakeCoin, tx.pure(validatorAddr)] | ||
}); | ||
@@ -504,38 +507,2 @@ return tx; | ||
}; | ||
_makeVecParam = new WeakSet(); | ||
makeVecParam_fn = function(args, type) { | ||
if (args.length === 0) | ||
throw new Error("Transaction builder error: Empty array is not allowed"); | ||
const defaultSuiType = getDefaultSuiInputType(args[0]); | ||
if (type === "object" || !type && defaultSuiType === "object") { | ||
const objects = args.map( | ||
(arg) => typeof arg === "string" ? this.txBlock.object((0, import_sui7.normalizeSuiObjectId)(arg)) : arg | ||
); | ||
return this.txBlock.makeMoveVec({ objects }); | ||
} else { | ||
const vecType = type || defaultSuiType; | ||
return this.txBlock.pure(args, `vector<${vecType}>`); | ||
} | ||
}; | ||
_isMoveVecArg = new WeakSet(); | ||
isMoveVecArg_fn = function(arg) { | ||
const isFullMoveVecArg = arg && arg.value && Array.isArray(arg.value) && arg.vecType; | ||
const isSimpleMoveVecArg = Array.isArray(arg); | ||
return isFullMoveVecArg || isSimpleMoveVecArg; | ||
}; | ||
_convertArgs = new WeakSet(); | ||
convertArgs_fn = function(args) { | ||
return args.map((arg) => { | ||
if (typeof arg === "string" && arg.startsWith("0x")) { | ||
return this.txBlock.object((0, import_sui7.normalizeSuiObjectId)(arg)); | ||
} else if (__privateMethod(this, _isMoveVecArg, isMoveVecArg_fn).call(this, arg)) { | ||
const vecType = arg.vecType || void 0; | ||
return vecType ? __privateMethod(this, _makeVecParam, makeVecParam_fn).call(this, arg.value, vecType) : __privateMethod(this, _makeVecParam, makeVecParam_fn).call(this, arg); | ||
} else if (typeof arg !== "object") { | ||
return this.txBlock.pure(arg); | ||
} else { | ||
return arg; | ||
} | ||
}); | ||
}; | ||
@@ -568,3 +535,3 @@ // src/lib/sui-kit.ts | ||
const keyPair = this.accountManager.getKeyPair(derivePathParams); | ||
return new import_sui8.RawSigner(keyPair, this.rpcProvider.provider); | ||
return new import_sui9.RawSigner(keyPair, this.rpcProvider.provider); | ||
} | ||
@@ -571,0 +538,0 @@ /** |
@@ -19,15 +19,2 @@ "use strict"; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __accessCheck = (obj, member, msg) => { | ||
if (!member.has(obj)) | ||
throw TypeError("Cannot " + msg); | ||
}; | ||
var __privateAdd = (obj, member, value) => { | ||
if (member.has(obj)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
member instanceof WeakSet ? member.add(obj) : member.set(obj, value); | ||
}; | ||
var __privateMethod = (obj, member, method) => { | ||
__accessCheck(obj, member, "access private method"); | ||
return method; | ||
}; | ||
@@ -37,4 +24,4 @@ // src/index.ts | ||
__export(src_exports, { | ||
SUI_CLOCK_OBJECT_ID: () => import_sui9.SUI_CLOCK_OBJECT_ID, | ||
SUI_SYSTEM_STATE_OBJECT_ID: () => import_sui9.SUI_SYSTEM_STATE_OBJECT_ID, | ||
SUI_CLOCK_OBJECT_ID: () => import_sui10.SUI_CLOCK_OBJECT_ID, | ||
SUI_SYSTEM_STATE_OBJECT_ID: () => import_sui10.SUI_SYSTEM_STATE_OBJECT_ID, | ||
SuiAccountManager: () => SuiAccountManager, | ||
@@ -44,9 +31,9 @@ SuiKit: () => SuiKit, | ||
SuiTxBlock: () => SuiTxBlock, | ||
TransactionBlock: () => import_sui9.TransactionBlock | ||
TransactionBlock: () => import_sui10.TransactionBlock | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var import_sui9 = require("@mysten/sui.js"); | ||
var import_sui10 = require("@mysten/sui.js"); | ||
// src/lib/sui-kit.ts | ||
var import_sui8 = require("@mysten/sui.js"); | ||
var import_sui9 = require("@mysten/sui.js"); | ||
@@ -293,5 +280,6 @@ // src/lib/sui-account-manager/sui-account-manager.ts | ||
// src/lib/sui-tx-builder/sui-tx-block.ts | ||
var import_sui7 = require("@mysten/sui.js"); | ||
var import_sui8 = require("@mysten/sui.js"); | ||
// src/lib/sui-tx-builder/util.ts | ||
var import_sui7 = require("@mysten/sui.js"); | ||
var getDefaultSuiInputType = (value) => { | ||
@@ -308,25 +296,40 @@ if (typeof value === "string" && value.startsWith("0x")) { | ||
}; | ||
function makeVecParam(txBlock, args, type) { | ||
if (args.length === 0) | ||
throw new Error("Transaction builder error: Empty array is not allowed"); | ||
const defaultSuiType = getDefaultSuiInputType(args[0]); | ||
if (type === "object" || !type && defaultSuiType === "object") { | ||
const objects = args.map( | ||
(arg) => typeof arg === "string" ? txBlock.object((0, import_sui7.normalizeSuiObjectId)(arg)) : arg | ||
); | ||
return txBlock.makeMoveVec({ objects }); | ||
} else { | ||
const vecType = type || defaultSuiType; | ||
return txBlock.pure(args, `vector<${vecType}>`); | ||
} | ||
} | ||
function isMoveVecArg(arg) { | ||
const isFullMoveVecArg = arg && arg.value && Array.isArray(arg.value) && arg.vecType; | ||
const isSimpleMoveVecArg = Array.isArray(arg); | ||
return isFullMoveVecArg || isSimpleMoveVecArg; | ||
} | ||
function convertArgs(txBlock, args) { | ||
return args.map((arg) => { | ||
if (typeof arg === "string" && arg.startsWith("0x")) { | ||
return txBlock.object((0, import_sui7.normalizeSuiObjectId)(arg)); | ||
} else if (isMoveVecArg(arg)) { | ||
const vecType = arg.vecType || void 0; | ||
return vecType ? makeVecParam(txBlock, arg.value, vecType) : makeVecParam(txBlock, arg); | ||
} else if (typeof arg !== "object") { | ||
return txBlock.pure(arg); | ||
} else { | ||
return arg; | ||
} | ||
}); | ||
} | ||
// src/lib/sui-tx-builder/sui-tx-block.ts | ||
var _makeVecParam, makeVecParam_fn, _isMoveVecArg, isMoveVecArg_fn, _convertArgs, convertArgs_fn; | ||
var SuiTxBlock = class { | ||
constructor(transaction) { | ||
/** | ||
* Since we know the elements in the array are the same type | ||
* If type is not provided, we will try to infer the type from the first element | ||
* By default, | ||
* | ||
* string starting with `0x` =====> object id | ||
* number, bigint ====> u64 | ||
* boolean =====> bool | ||
* | ||
* | ||
* If type is provided, we will use the type to convert the array | ||
* @param args | ||
* @param type 'address' | 'bool' | 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'object' | ||
*/ | ||
__privateAdd(this, _makeVecParam); | ||
__privateAdd(this, _isMoveVecArg); | ||
__privateAdd(this, _convertArgs); | ||
this.txBlock = new import_sui7.TransactionBlock(transaction); | ||
this.txBlock = new import_sui8.TransactionBlock(transaction); | ||
} | ||
@@ -338,3 +341,3 @@ //======== override methods of TransactionBlock ============ | ||
pure(value, type) { | ||
return this.txBlock.pure(value); | ||
return this.txBlock.pure(value, type); | ||
} | ||
@@ -391,3 +394,3 @@ object(value) { | ||
const tx = this.txBlock; | ||
tx.transferObjects(__privateMethod(this, _convertArgs, convertArgs_fn).call(this, objects), tx.pure(recipient)); | ||
tx.transferObjects(convertArgs(this.txBlock, objects), tx.pure(recipient)); | ||
return this; | ||
@@ -397,3 +400,3 @@ } | ||
const tx = this.txBlock; | ||
const coinObject = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, [coin])[0]; | ||
const coinObject = convertArgs(this.txBlock, [coin])[0]; | ||
const res = tx.splitCoins(coinObject, amounts.map((m) => tx.pure(m))); | ||
@@ -403,5 +406,5 @@ return amounts.map((_, i) => res[i]); | ||
mergeCoins(destination, sources) { | ||
const destionationObject = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, [destination])[0]; | ||
const sourceObjects = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, sources); | ||
return this.txBlock.mergeCoins(destionationObject, sourceObjects); | ||
const destinationObject = convertArgs(this.txBlock, [destination])[0]; | ||
const sourceObjects = convertArgs(this.txBlock, sources); | ||
return this.txBlock.mergeCoins(destinationObject, sourceObjects); | ||
} | ||
@@ -428,3 +431,3 @@ publish(...args) { | ||
throw new Error("Invalid target format. Expected `${string}::${string}::${string}`"); | ||
const convertedArgs = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, args); | ||
const convertedArgs = convertArgs(this.txBlock, args); | ||
const tx = this.txBlock; | ||
@@ -454,3 +457,3 @@ return tx.moveCall({ | ||
const tx = this.txBlock; | ||
const coinObjects = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, coins); | ||
const coinObjects = convertArgs(this.txBlock, coins); | ||
const mergedCoin = coinObjects[0]; | ||
@@ -469,3 +472,3 @@ if (coins.length > 1) { | ||
const tx = this.txBlock; | ||
const coinObjects = __privateMethod(this, _convertArgs, convertArgs_fn).call(this, coins); | ||
const coinObjects = convertArgs(this.txBlock, coins); | ||
const mergedCoin = coinObjects[0]; | ||
@@ -498,3 +501,3 @@ if (coins.length > 1) { | ||
target: "0x3::sui_system::request_add_stake", | ||
arguments: [tx.object(import_sui7.SUI_SYSTEM_STATE_OBJECT_ID), stakeCoin, tx.pure(validatorAddr)] | ||
arguments: [tx.object(import_sui8.SUI_SYSTEM_STATE_OBJECT_ID), stakeCoin, tx.pure(validatorAddr)] | ||
}); | ||
@@ -504,38 +507,2 @@ return tx; | ||
}; | ||
_makeVecParam = new WeakSet(); | ||
makeVecParam_fn = function(args, type) { | ||
if (args.length === 0) | ||
throw new Error("Transaction builder error: Empty array is not allowed"); | ||
const defaultSuiType = getDefaultSuiInputType(args[0]); | ||
if (type === "object" || !type && defaultSuiType === "object") { | ||
const objects = args.map( | ||
(arg) => typeof arg === "string" ? this.txBlock.object((0, import_sui7.normalizeSuiObjectId)(arg)) : arg | ||
); | ||
return this.txBlock.makeMoveVec({ objects }); | ||
} else { | ||
const vecType = type || defaultSuiType; | ||
return this.txBlock.pure(args, `vector<${vecType}>`); | ||
} | ||
}; | ||
_isMoveVecArg = new WeakSet(); | ||
isMoveVecArg_fn = function(arg) { | ||
const isFullMoveVecArg = arg && arg.value && Array.isArray(arg.value) && arg.vecType; | ||
const isSimpleMoveVecArg = Array.isArray(arg); | ||
return isFullMoveVecArg || isSimpleMoveVecArg; | ||
}; | ||
_convertArgs = new WeakSet(); | ||
convertArgs_fn = function(args) { | ||
return args.map((arg) => { | ||
if (typeof arg === "string" && arg.startsWith("0x")) { | ||
return this.txBlock.object((0, import_sui7.normalizeSuiObjectId)(arg)); | ||
} else if (__privateMethod(this, _isMoveVecArg, isMoveVecArg_fn).call(this, arg)) { | ||
const vecType = arg.vecType || void 0; | ||
return vecType ? __privateMethod(this, _makeVecParam, makeVecParam_fn).call(this, arg.value, vecType) : __privateMethod(this, _makeVecParam, makeVecParam_fn).call(this, arg); | ||
} else if (typeof arg !== "object") { | ||
return this.txBlock.pure(arg); | ||
} else { | ||
return arg; | ||
} | ||
}); | ||
}; | ||
@@ -568,3 +535,3 @@ // src/lib/sui-kit.ts | ||
const keyPair = this.accountManager.getKeyPair(derivePathParams); | ||
return new import_sui8.RawSigner(keyPair, this.rpcProvider.provider); | ||
return new import_sui9.RawSigner(keyPair, this.rpcProvider.provider); | ||
} | ||
@@ -571,0 +538,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
export type { SuiTxArg, SuiVecTxArg, SuiObjectArg } from './sui-tx-block'; | ||
export type { SuiTxArg, SuiVecTxArg, SuiObjectArg } from './types'; | ||
export { SuiTxBlock } from './sui-tx-block'; |
@@ -1,9 +0,3 @@ | ||
import { TransactionBlock, TransactionArgument, TransactionExpiration, SuiObjectRef, SharedObjectRef, JsonRpcProvider, TransactionType, Transactions, ObjectCallArg } from '@mysten/sui.js'; | ||
import { SuiInputTypes } from './util'; | ||
export type SuiTxArg = TransactionArgument | string | number | bigint | boolean; | ||
export type SuiObjectArg = SharedObjectRef | SuiObjectRef | string | TransactionArgument; | ||
export type SuiVecTxArg = { | ||
value: SuiTxArg[]; | ||
vecType: SuiInputTypes; | ||
} | SuiTxArg[]; | ||
import { TransactionBlock, TransactionExpiration, SuiObjectRef, SharedObjectRef, JsonRpcProvider, TransactionType, Transactions, ObjectCallArg } from '@mysten/sui.js'; | ||
import type { SuiTxArg, SuiObjectArg, SuiVecTxArg } from './types'; | ||
interface BuildOptions { | ||
@@ -14,3 +8,2 @@ provider?: JsonRpcProvider; | ||
export declare class SuiTxBlock { | ||
#private; | ||
txBlock: TransactionBlock; | ||
@@ -17,0 +10,0 @@ constructor(transaction?: TransactionBlock); |
@@ -0,1 +1,3 @@ | ||
import { TransactionArgument, TransactionBlock } from "@mysten/sui.js"; | ||
import { SuiTxArg } from "./types"; | ||
/** | ||
@@ -7,1 +9,74 @@ * These are the basics types that can be used in the SUI | ||
export declare const getDefaultSuiInputType: (value: any) => SuiInputTypes; | ||
/** | ||
* Since we know the elements in the array are the same type | ||
* If type is not provided, we will try to infer the type from the first element | ||
* By default, | ||
* | ||
* string starting with `0x` =====> object id | ||
* number, bigint ====> u64 | ||
* boolean =====> bool | ||
* | ||
* | ||
* If type is provided, we will use the type to convert the array | ||
* @param args | ||
* @param type 'address' | 'bool' | 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'object' | ||
*/ | ||
export declare function makeVecParam(txBlock: TransactionBlock, args: SuiTxArg[], type?: SuiInputTypes): ({ | ||
kind: "GasCoin"; | ||
} & ({ | ||
kind: "Input"; | ||
index: number; | ||
type?: "object" | "pure" | undefined; | ||
value?: any; | ||
} | { | ||
kind: "GasCoin"; | ||
} | { | ||
kind: "Result"; | ||
index: number; | ||
} | { | ||
kind: "NestedResult"; | ||
index: number; | ||
resultIndex: number; | ||
})[]) | ({ | ||
kind: "Result"; | ||
index: number; | ||
} & ({ | ||
kind: "Input"; | ||
index: number; | ||
type?: "object" | "pure" | undefined; | ||
value?: any; | ||
} | { | ||
kind: "GasCoin"; | ||
} | { | ||
kind: "Result"; | ||
index: number; | ||
} | { | ||
kind: "NestedResult"; | ||
index: number; | ||
resultIndex: number; | ||
})[]) | ({ | ||
kind: "NestedResult"; | ||
index: number; | ||
resultIndex: number; | ||
} & ({ | ||
kind: "Input"; | ||
index: number; | ||
type?: "object" | "pure" | undefined; | ||
value?: any; | ||
} | { | ||
kind: "GasCoin"; | ||
} | { | ||
kind: "Result"; | ||
index: number; | ||
} | { | ||
kind: "NestedResult"; | ||
index: number; | ||
resultIndex: number; | ||
})[]) | { | ||
kind: "Input"; | ||
index: number; | ||
type?: "object" | "pure" | undefined; | ||
value?: any; | ||
}; | ||
export declare function isMoveVecArg(arg: any): any; | ||
export declare function convertArgs(txBlock: TransactionBlock, args: any[]): TransactionArgument[]; |
{ | ||
"name": "@scallop-io/sui-kit", | ||
"version": "0.35.2", | ||
"version": "0.35.3", | ||
"description": "Tookit for interacting with SUI network", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1,2 +0,2 @@ | ||
export type { SuiTxArg, SuiVecTxArg, SuiObjectArg } from './sui-tx-block' | ||
export type { SuiTxArg, SuiVecTxArg, SuiObjectArg } from './types' | ||
export { SuiTxBlock } from './sui-tx-block' |
import { | ||
TransactionBlock, | ||
SUI_SYSTEM_STATE_OBJECT_ID, | ||
normalizeSuiObjectId, | ||
TransactionArgument, | ||
TransactionExpiration, | ||
@@ -13,8 +11,6 @@ SuiObjectRef, | ||
ObjectCallArg, | ||
} from '@mysten/sui.js' | ||
import { SuiInputTypes, getDefaultSuiInputType } from './util' | ||
} from '@mysten/sui.js'; | ||
import { convertArgs } from './util'; | ||
import type { SuiTxArg, SuiObjectArg, SuiVecTxArg } from './types'; | ||
export type SuiTxArg = TransactionArgument | string | number | bigint | boolean; | ||
export type SuiObjectArg = SharedObjectRef | SuiObjectRef | string | TransactionArgument; | ||
export type SuiVecTxArg = { value: SuiTxArg[], vecType: SuiInputTypes } | SuiTxArg[]; | ||
interface BuildOptions { | ||
@@ -37,3 +33,3 @@ provider?: JsonRpcProvider; | ||
pure(value: unknown, type?: string) { | ||
return this.txBlock.pure(value) | ||
return this.txBlock.pure(value, type) | ||
} | ||
@@ -93,3 +89,3 @@ object(value: string | ObjectCallArg) { | ||
const tx = this.txBlock; | ||
tx.transferObjects(this.#convertArgs(objects), tx.pure(recipient)); | ||
tx.transferObjects(convertArgs(this.txBlock, objects), tx.pure(recipient)); | ||
return this; | ||
@@ -99,3 +95,3 @@ } | ||
const tx = this.txBlock; | ||
const coinObject = this.#convertArgs([coin])[0]; | ||
const coinObject = convertArgs(this.txBlock,[coin])[0]; | ||
const res = tx.splitCoins(coinObject, amounts.map(m => tx.pure(m))); | ||
@@ -105,5 +101,5 @@ return amounts.map((_, i) => res[i]); | ||
mergeCoins(destination: SuiObjectArg, sources: SuiObjectArg[]) { | ||
const destionationObject = this.#convertArgs([destination])[0]; | ||
const sourceObjects = this.#convertArgs(sources); | ||
return this.txBlock.mergeCoins(destionationObject, sourceObjects); | ||
const destinationObject = convertArgs(this.txBlock, [destination])[0]; | ||
const sourceObjects = convertArgs(this.txBlock, sources); | ||
return this.txBlock.mergeCoins(destinationObject, sourceObjects); | ||
} | ||
@@ -131,3 +127,3 @@ publish(...args: Parameters<(typeof Transactions)['Publish']>) { | ||
if (match === null) throw new Error('Invalid target format. Expected `${string}::${string}::${string}`'); | ||
const convertedArgs = this.#convertArgs(args); | ||
const convertedArgs = convertArgs(this.txBlock, args); | ||
const tx = this.txBlock; | ||
@@ -163,3 +159,3 @@ return tx.moveCall({ | ||
const tx = this.txBlock; | ||
const coinObjects = this.#convertArgs(coins); | ||
const coinObjects = convertArgs(this.txBlock, coins); | ||
const mergedCoin = coinObjects[0]; | ||
@@ -181,3 +177,3 @@ if (coins.length > 1) { | ||
const tx = this.txBlock; | ||
const coinObjects = this.#convertArgs(coins); | ||
const coinObjects = convertArgs(this.txBlock, coins); | ||
const mergedCoin = coinObjects[0]; | ||
@@ -218,57 +214,2 @@ if (coins.length > 1) { | ||
} | ||
/** | ||
* Since we know the elements in the array are the same type | ||
* If type is not provided, we will try to infer the type from the first element | ||
* By default, | ||
* | ||
* string starting with `0x` =====> object id | ||
* number, bigint ====> u64 | ||
* boolean =====> bool | ||
* | ||
* | ||
* If type is provided, we will use the type to convert the array | ||
* @param args | ||
* @param type 'address' | 'bool' | 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'object' | ||
*/ | ||
#makeVecParam(args: SuiTxArg[], type?: SuiInputTypes) { | ||
if (args.length === 0) throw new Error('Transaction builder error: Empty array is not allowed'); | ||
const defaultSuiType = getDefaultSuiInputType(args[0]) | ||
if (type === 'object' || (!type && defaultSuiType === 'object')) { | ||
const objects = args.map(arg => | ||
typeof arg === 'string' ? this.txBlock.object(normalizeSuiObjectId(arg)) : (arg as any) | ||
); | ||
return this.txBlock.makeMoveVec({ objects }) | ||
} else { | ||
const vecType = type || defaultSuiType; | ||
return this.txBlock.pure(args, `vector<${vecType}>`) | ||
} | ||
} | ||
#isMoveVecArg(arg: any) { | ||
const isFullMoveVecArg = arg && arg.value && Array.isArray(arg.value) && arg.vecType; | ||
const isSimpleMoveVecArg = Array.isArray(arg); | ||
return isFullMoveVecArg || isSimpleMoveVecArg; | ||
} | ||
#convertArgs(args: any[]): TransactionArgument[] { | ||
return args.map(arg => { | ||
if (typeof arg === 'string' && arg.startsWith('0x')) { | ||
// We always treat string starting with `0x` as object id | ||
return this.txBlock.object(normalizeSuiObjectId(arg)) | ||
} else if (this.#isMoveVecArg(arg)) { | ||
// if it's an array arg, we will convert it to move vec | ||
const vecType = arg.vecType || undefined; | ||
return vecType | ||
? this.#makeVecParam(arg.value, vecType) | ||
: this.#makeVecParam(arg) | ||
} else if (typeof arg !== 'object') { | ||
// Other basic types such as string, number, boolean are converted to pure value | ||
return this.txBlock.pure(arg) | ||
} else { | ||
// We do nothing, because it's most likely already a move value | ||
return arg | ||
} | ||
}) | ||
} | ||
} |
@@ -0,1 +1,4 @@ | ||
import { normalizeSuiObjectId, TransactionArgument, TransactionBlock } from "@mysten/sui.js"; | ||
import { SuiTxArg } from "./types"; | ||
/** | ||
@@ -27,1 +30,56 @@ * These are the basics types that can be used in the SUI | ||
} | ||
/** | ||
* Since we know the elements in the array are the same type | ||
* If type is not provided, we will try to infer the type from the first element | ||
* By default, | ||
* | ||
* string starting with `0x` =====> object id | ||
* number, bigint ====> u64 | ||
* boolean =====> bool | ||
* | ||
* | ||
* If type is provided, we will use the type to convert the array | ||
* @param args | ||
* @param type 'address' | 'bool' | 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'object' | ||
*/ | ||
export function makeVecParam(txBlock: TransactionBlock, args: SuiTxArg[], type?: SuiInputTypes) { | ||
if (args.length === 0) throw new Error('Transaction builder error: Empty array is not allowed'); | ||
const defaultSuiType = getDefaultSuiInputType(args[0]) | ||
if (type === 'object' || (!type && defaultSuiType === 'object')) { | ||
const objects = args.map(arg => | ||
typeof arg === 'string' ? txBlock.object(normalizeSuiObjectId(arg)) : (arg as any) | ||
); | ||
return txBlock.makeMoveVec({ objects }) | ||
} else { | ||
const vecType = type || defaultSuiType; | ||
return txBlock.pure(args, `vector<${vecType}>`) | ||
} | ||
} | ||
export function isMoveVecArg(arg: any) { | ||
const isFullMoveVecArg = arg && arg.value && Array.isArray(arg.value) && arg.vecType; | ||
const isSimpleMoveVecArg = Array.isArray(arg); | ||
return isFullMoveVecArg || isSimpleMoveVecArg; | ||
} | ||
export function convertArgs(txBlock: TransactionBlock, args: any[]): TransactionArgument[] { | ||
return args.map(arg => { | ||
if (typeof arg === 'string' && arg.startsWith('0x')) { | ||
// We always treat string starting with `0x` as object id | ||
return txBlock.object(normalizeSuiObjectId(arg)) | ||
} else if (isMoveVecArg(arg)) { | ||
// if it's an array arg, we will convert it to move vec | ||
const vecType = arg.vecType || undefined; | ||
return vecType | ||
? makeVecParam(txBlock, arg.value, vecType) | ||
: makeVecParam(txBlock, arg) | ||
} else if (typeof arg !== 'object') { | ||
// Other basic types such as string, number, boolean are converted to pure value | ||
return txBlock.pure(arg) | ||
} else { | ||
// We do nothing, because it's most likely already a move value | ||
return arg | ||
} | ||
}) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38
340502
6094