1- import { array , boolean , Infer , integer , object , string , union } from 'superstruct' ;
2- import { normalizeSuiAddress , ObjectId , SharedObjectRef , SuiObjectRef } from '../types' ;
1+ import { array , boolean , define , Infer , integer , object , string , union } from 'superstruct' ;
2+ import { normalizeSuiAddress , ObjectId , SharedObjectRef , SuiObjectRef , TypeTag } from '../types' ;
33import { builder } from './bcs' ;
44
55const ObjectArg = union ( [
@@ -13,12 +13,36 @@ const ObjectArg = union([
1313 } ) ,
1414] ) ;
1515
16+ // @mysten /bcs v0.7 decodes U64 as string; bigint/number are used at build time.
17+ const bigintOrInteger = define < bigint | number | string > ( 'bigintOrInteger' , ( value ) => {
18+ if ( typeof value === 'bigint' ) return true ;
19+ if ( typeof value === 'number' ) return Number . isInteger ( value ) ;
20+ if ( typeof value === 'string' ) {
21+ try {
22+ BigInt ( value ) ;
23+ return true ;
24+ } catch {
25+ return false ;
26+ }
27+ }
28+ return false ;
29+ } ) ;
30+
1631export const PureCallArg = object ( { Pure : array ( integer ( ) ) } ) ;
1732export const ObjectCallArg = object ( { Object : ObjectArg } ) ;
33+ export const BalanceWithdrawalCallArg = object ( {
34+ BalanceWithdrawal : object ( {
35+ amount : bigintOrInteger ,
36+ // TypeTag is a recursive union; object() ensures the value is a non-null object
37+ // matching all TypeTag variants ({ bool: null }, { struct: StructTag }, etc.)
38+ type_ : object ( ) ,
39+ } ) ,
40+ } ) ;
1841export type PureCallArg = Infer < typeof PureCallArg > ;
1942export type ObjectCallArg = Infer < typeof ObjectCallArg > ;
43+ export type BalanceWithdrawalCallArg = Infer < typeof BalanceWithdrawalCallArg > ;
2044
21- export const BuilderCallArg = union ( [ PureCallArg , ObjectCallArg ] ) ;
45+ export const BuilderCallArg = union ( [ PureCallArg , ObjectCallArg , BalanceWithdrawalCallArg ] ) ;
2246export type BuilderCallArg = Infer < typeof BuilderCallArg > ;
2347
2448export const Inputs = {
@@ -33,12 +57,27 @@ export const Inputs = {
3357 SharedObjectRef ( ref : SharedObjectRef ) : ObjectCallArg {
3458 return { Object : { Shared : ref } } ;
3559 } ,
60+ /**
61+ * Create a BalanceWithdrawal CallArg that withdraws `amount` from the sender's
62+ * address balance at execution time. Use with `0x2::coin::redeem_funds` to
63+ * convert the withdrawal into a `Coin<T>` object.
64+ *
65+ * @param amount - amount in base units (MIST for SUI)
66+ * @param type_ - the TypeTag of the coin (defaults to SUI)
67+ */
68+ BalanceWithdrawal ( amount : bigint | number , type_ : TypeTag ) : BalanceWithdrawalCallArg {
69+ return { BalanceWithdrawal : { amount, type_ } } ;
70+ } ,
3671} ;
3772
38- export function getIdFromCallArg ( arg : ObjectId | ObjectCallArg ) : string {
73+ export function getIdFromCallArg ( arg : ObjectId | ObjectCallArg | BalanceWithdrawalCallArg ) : string {
3974 if ( typeof arg === 'string' ) {
4075 return normalizeSuiAddress ( arg ) ;
4176 }
77+ if ( 'BalanceWithdrawal' in arg ) {
78+ // BalanceWithdrawal inputs have no object ID; they cannot be deduplicated by ID
79+ return '' ;
80+ }
4281 if ( 'ImmOrOwned' in arg . Object ) {
4382 return arg . Object . ImmOrOwned . objectId ;
4483 }
0 commit comments