-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathInputs.ts
More file actions
80 lines (72 loc) · 2.75 KB
/
Inputs.ts
File metadata and controls
80 lines (72 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { any, array, boolean, Infer, integer, object, string, union } from 'superstruct';
import { normalizeSuiAddress, ObjectId, SharedObjectRef, SuiObjectRef, TypeTag } from '../types';
import { builder } from './bcs';
const ObjectArg = union([
object({ ImmOrOwned: SuiObjectRef }),
object({
Shared: object({
objectId: string(),
initialSharedVersion: union([integer(), string()]),
mutable: boolean(),
}),
}),
]);
export const PureCallArg = object({ Pure: array(integer()) });
export const ObjectCallArg = object({ Object: ObjectArg });
export const BalanceWithdrawalCallArg = object({
BalanceWithdrawal: object({
amount: any(),
type_: any(),
}),
});
export type PureCallArg = Infer<typeof PureCallArg>;
export type ObjectCallArg = Infer<typeof ObjectCallArg>;
export type BalanceWithdrawalCallArg = Infer<typeof BalanceWithdrawalCallArg>;
export const BuilderCallArg = union([PureCallArg, ObjectCallArg, BalanceWithdrawalCallArg]);
export type BuilderCallArg = Infer<typeof BuilderCallArg>;
export const Inputs = {
Pure(data: unknown, type?: string): PureCallArg {
return {
Pure: Array.from(data instanceof Uint8Array ? data : builder.ser(type!, data).toBytes()),
};
},
ObjectRef(ref: SuiObjectRef): ObjectCallArg {
return { Object: { ImmOrOwned: ref } };
},
SharedObjectRef(ref: SharedObjectRef): ObjectCallArg {
return { Object: { Shared: ref } };
},
/**
* Create a BalanceWithdrawal CallArg that withdraws `amount` from the sender's
* address balance at execution time. Use with `0x2::coin::redeem_funds` to
* convert the withdrawal into a `Coin<T>` object.
*
* @param amount - amount in base units (MIST for SUI)
* @param type_ - the TypeTag of the coin (defaults to SUI)
*/
BalanceWithdrawal(amount: bigint | number, type_: TypeTag): BalanceWithdrawalCallArg {
return { BalanceWithdrawal: { amount, type_ } };
},
};
export function getIdFromCallArg(arg: ObjectId | ObjectCallArg | BalanceWithdrawalCallArg): string {
if (typeof arg === 'string') {
return normalizeSuiAddress(arg);
}
if ('BalanceWithdrawal' in arg) {
// BalanceWithdrawal inputs have no object ID; they cannot be deduplicated by ID
return '';
}
if ('ImmOrOwned' in arg.Object) {
return arg.Object.ImmOrOwned.objectId;
}
return arg.Object.Shared.objectId;
}
export function getSharedObjectInput(arg: BuilderCallArg): SharedObjectRef | undefined {
return typeof arg === 'object' && 'Object' in arg && 'Shared' in arg.Object ? arg.Object.Shared : undefined;
}
export function isSharedObjectInput(arg: BuilderCallArg): boolean {
return !!getSharedObjectInput(arg);
}
export function isMutableSharedObjectInput(arg: BuilderCallArg): boolean {
return getSharedObjectInput(arg)?.mutable ?? false;
}