hearn-oracle-sdk 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +7 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +41 -27
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +43 -27
package/dist/index.d.ts
CHANGED
|
@@ -4,13 +4,15 @@ export declare class OracleClient {
|
|
|
4
4
|
private provider;
|
|
5
5
|
private packageId;
|
|
6
6
|
private oracleId;
|
|
7
|
+
private static readonly DEFAULT_CLOCK_ID;
|
|
7
8
|
constructor(provider: SuiClient, packageId: string, oracleId: string);
|
|
8
9
|
/**
|
|
9
10
|
* Get the price of a single asset by its type.
|
|
10
11
|
* @param assetType The type of the asset, e.g., '0x2::sui::SUI'
|
|
11
|
-
* @
|
|
12
|
+
* @param clockId The ID of the Clock object (defaults to 0x6)
|
|
13
|
+
* @returns The price as a bigint in WAD (1e18) precision.
|
|
12
14
|
*/
|
|
13
|
-
getPriceByType(assetType: string): Promise<
|
|
15
|
+
getPriceByType(assetType: string, clockId?: string): Promise<bigint>;
|
|
14
16
|
/**
|
|
15
17
|
* Batch query feed object ids for multiple asset types in a single devInspect.
|
|
16
18
|
* Returns a map from assetType -> { priceObjectId, aggregatorId }.
|
|
@@ -19,13 +21,14 @@ export declare class OracleClient {
|
|
|
19
21
|
/**
|
|
20
22
|
* Get the prices of multiple assets by their types.
|
|
21
23
|
* @param assetTypes Array of asset types
|
|
24
|
+
* @param clockId The ID of the Clock object (defaults to 0x6)
|
|
22
25
|
* @returns Array of prices
|
|
23
26
|
*/
|
|
24
|
-
getPricesByTypes(assetTypes: string[]): Promise<
|
|
27
|
+
getPricesByTypes(assetTypes: string[], clockId?: string): Promise<bigint[]>;
|
|
25
28
|
/**
|
|
26
29
|
* Update prices for multiple assets by calling adaptor_oracle::update_price in a loop.
|
|
27
30
|
* This builds a transaction that updates multiple assets' prices.
|
|
28
|
-
* @param
|
|
31
|
+
* @param assetTypes Array of asset types (e.g. '0x2::sui::SUI')
|
|
29
32
|
* @param clockId The ID of the Clock object
|
|
30
33
|
* @returns A Promise that resolves to a TransactionBlock that can be signed and executed
|
|
31
34
|
*/
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAgCvD,qBAAa,YAAY;IACrB,OAAO,CAAC,QAAQ,CAAY;IAC5B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,QAAQ,CAAS;IAGzB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAS;gBAErC,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAMpE;;;;;OAKG;IACG,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,GAAE,MAAsC,GAAG,OAAO,CAAC,MAAM,CAAC;IA2BzG;;;OAGG;YACW,uBAAuB;IAoDrC;;;;;OAKG;IACG,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,OAAO,GAAE,MAAsC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAKhH;;;;;;OAMG;IACG,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;CAoBlF"}
|
package/dist/index.js
CHANGED
|
@@ -3,23 +3,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.OracleClient = void 0;
|
|
4
4
|
const transactions_1 = require("@mysten/sui/transactions");
|
|
5
5
|
const bcs_1 = require("@mysten/bcs");
|
|
6
|
-
// BCS schema for `TypeName` used by the Move contract.
|
|
7
|
-
//
|
|
6
|
+
// BCS schema for `std::type_name::TypeName` used by the Move contract.
|
|
7
|
+
// In Sui Move stdlib, `TypeName` is:
|
|
8
|
+
// struct TypeName { name: std::ascii::String }
|
|
9
|
+
// and `std::ascii::String` is:
|
|
10
|
+
// struct String { bytes: vector<u8> }
|
|
11
|
+
const AsciiStringBcs = bcs_1.bcs.struct('AsciiString', {
|
|
12
|
+
bytes: bcs_1.bcs.vector(bcs_1.bcs.u8()),
|
|
13
|
+
});
|
|
8
14
|
const TypeNameBcs = bcs_1.bcs.struct('TypeName', {
|
|
9
|
-
|
|
10
|
-
addr: bcs_1.bcs.bytes(32),
|
|
11
|
-
module: bcs_1.bcs.string(),
|
|
12
|
-
name: bcs_1.bcs.string(),
|
|
15
|
+
name: AsciiStringBcs,
|
|
13
16
|
});
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
20
|
-
throw new Error(`Invalid
|
|
17
|
+
// Convert a Move type tag string into the exact `std::type_name::TypeName.name` format.
|
|
18
|
+
// Move stdlib encodes addresses as lowercase hex WITHOUT 0x and padded to 32 bytes (64 hex chars).
|
|
19
|
+
function normalizeTypeNameString(typeTag) {
|
|
20
|
+
const sep = '::';
|
|
21
|
+
const idx = typeTag.indexOf(sep);
|
|
22
|
+
if (idx < 0) {
|
|
23
|
+
throw new Error(`Invalid type tag (missing '::'): ${typeTag}`);
|
|
21
24
|
}
|
|
22
|
-
|
|
25
|
+
const addrPart = typeTag.slice(0, idx);
|
|
26
|
+
const rest = typeTag.slice(idx + sep.length);
|
|
27
|
+
const hex = (addrPart.startsWith('0x') ? addrPart.slice(2) : addrPart).toLowerCase();
|
|
28
|
+
const padded = hex.padStart(64, '0');
|
|
29
|
+
return `${padded}${sep}${rest}`;
|
|
23
30
|
}
|
|
24
31
|
class OracleClient {
|
|
25
32
|
constructor(provider, packageId, oracleId) {
|
|
@@ -30,14 +37,15 @@ class OracleClient {
|
|
|
30
37
|
/**
|
|
31
38
|
* Get the price of a single asset by its type.
|
|
32
39
|
* @param assetType The type of the asset, e.g., '0x2::sui::SUI'
|
|
33
|
-
* @
|
|
40
|
+
* @param clockId The ID of the Clock object (defaults to 0x6)
|
|
41
|
+
* @returns The price as a bigint in WAD (1e18) precision.
|
|
34
42
|
*/
|
|
35
|
-
async getPriceByType(assetType) {
|
|
43
|
+
async getPriceByType(assetType, clockId = OracleClient.DEFAULT_CLOCK_ID) {
|
|
36
44
|
const tx = new transactions_1.Transaction();
|
|
37
45
|
tx.moveCall({
|
|
38
|
-
target: `${this.packageId}::
|
|
46
|
+
target: `${this.packageId}::pyth_oracle::price_by_type`,
|
|
39
47
|
typeArguments: [assetType],
|
|
40
|
-
arguments: [tx.object(this.oracleId)],
|
|
48
|
+
arguments: [tx.object(this.oracleId), tx.object(clockId)],
|
|
41
49
|
});
|
|
42
50
|
const result = await this.provider.devInspectTransactionBlock({
|
|
43
51
|
transactionBlock: tx,
|
|
@@ -50,10 +58,9 @@ class OracleClient {
|
|
|
50
58
|
if (!returnValue) {
|
|
51
59
|
throw new Error('No return value from price_by_type');
|
|
52
60
|
}
|
|
53
|
-
//
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
return Number(priceBigInt) / 1e18;
|
|
61
|
+
// Return is BCS-encoded u128 (little-endian). Parse via BCS.
|
|
62
|
+
const priceU128Str = bcs_1.bcs.u128().parse(new Uint8Array(returnValue[0]));
|
|
63
|
+
return BigInt(priceU128Str);
|
|
57
64
|
}
|
|
58
65
|
/**
|
|
59
66
|
* Batch query feed object ids for multiple asset types in a single devInspect.
|
|
@@ -62,8 +69,11 @@ class OracleClient {
|
|
|
62
69
|
async getFeedObjectIdsByTypes(assetTypes) {
|
|
63
70
|
const queryTx = new transactions_1.Transaction();
|
|
64
71
|
const typeNames = assetTypes.map((assetType) => {
|
|
65
|
-
|
|
66
|
-
|
|
72
|
+
// `get_feed_object_addresses_batch` expects `vector<std::type_name::TypeName>`.
|
|
73
|
+
// We encode TypeName as { name: { bytes } } where bytes are ASCII bytes of the normalized type string.
|
|
74
|
+
const normalized = normalizeTypeNameString(assetType);
|
|
75
|
+
const bytes = Array.from(Buffer.from(normalized, 'ascii'));
|
|
76
|
+
return { name: { bytes } };
|
|
67
77
|
});
|
|
68
78
|
queryTx.moveCall({
|
|
69
79
|
target: `${this.packageId}::pyth_oracle::get_feed_object_addresses_batch`,
|
|
@@ -99,16 +109,17 @@ class OracleClient {
|
|
|
99
109
|
/**
|
|
100
110
|
* Get the prices of multiple assets by their types.
|
|
101
111
|
* @param assetTypes Array of asset types
|
|
112
|
+
* @param clockId The ID of the Clock object (defaults to 0x6)
|
|
102
113
|
* @returns Array of prices
|
|
103
114
|
*/
|
|
104
|
-
async getPricesByTypes(assetTypes) {
|
|
105
|
-
const promises = assetTypes.map(assetType => this.getPriceByType(assetType));
|
|
115
|
+
async getPricesByTypes(assetTypes, clockId = OracleClient.DEFAULT_CLOCK_ID) {
|
|
116
|
+
const promises = assetTypes.map(assetType => this.getPriceByType(assetType, clockId));
|
|
106
117
|
return Promise.all(promises);
|
|
107
118
|
}
|
|
108
119
|
/**
|
|
109
120
|
* Update prices for multiple assets by calling adaptor_oracle::update_price in a loop.
|
|
110
121
|
* This builds a transaction that updates multiple assets' prices.
|
|
111
|
-
* @param
|
|
122
|
+
* @param assetTypes Array of asset types (e.g. '0x2::sui::SUI')
|
|
112
123
|
* @param clockId The ID of the Clock object
|
|
113
124
|
* @returns A Promise that resolves to a TransactionBlock that can be signed and executed
|
|
114
125
|
*/
|
|
@@ -134,4 +145,7 @@ class OracleClient {
|
|
|
134
145
|
}
|
|
135
146
|
}
|
|
136
147
|
exports.OracleClient = OracleClient;
|
|
148
|
+
// Sui system Clock shared object id.
|
|
149
|
+
// See: `0x2::clock::Clock` (typically `0x6` on Sui networks).
|
|
150
|
+
OracleClient.DEFAULT_CLOCK_ID = '0x6';
|
|
137
151
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AACA,2DAAuD;AACvD,qCAAkC;AAElC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AACA,2DAAuD;AACvD,qCAAkC;AAElC,uEAAuE;AACvE,qCAAqC;AACrC,iDAAiD;AACjD,+BAA+B;AAC/B,wCAAwC;AACxC,MAAM,cAAc,GAAG,SAAG,CAAC,MAAM,CAAC,aAAa,EAAE;IAC7C,KAAK,EAAE,SAAG,CAAC,MAAM,CAAC,SAAG,CAAC,EAAE,EAAE,CAAC;CAC9B,CAAC,CAAC;AAEH,MAAM,WAAW,GAAG,SAAG,CAAC,MAAM,CAAC,UAAU,EAAE;IACvC,IAAI,EAAE,cAAc;CACvB,CAAC,CAAC;AAEH,wFAAwF;AACxF,mGAAmG;AACnG,SAAS,uBAAuB,CAAC,OAAe;IAC5C,MAAM,GAAG,GAAG,IAAI,CAAC;IACjB,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACjC,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CAAC,oCAAoC,OAAO,EAAE,CAAC,CAAC;IACnE,CAAC;IAED,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACvC,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC;IAC7C,MAAM,GAAG,GAAG,CAAC,QAAQ,C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M,CAAC,GAAG,CAAC,aAAa,CAAC;oBAC5B,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC;oBAC3B,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC;iBACrB;aACJ,CAAC,CAAC;QACP,CAAC;QACD,OAAO,EAAE,CAAC;IACd,CAAC;;AA5IL,oCA6IC;AAzIG,qCAAqC;AACrC,8DAA8D;AACtC,6BAAgB,GAAG,KAAK,CAAC"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -2,30 +2,42 @@ import { SuiClient } from '@mysten/sui/client';
|
|
|
2
2
|
import { Transaction } from '@mysten/sui/transactions';
|
|
3
3
|
import { bcs } from '@mysten/bcs';
|
|
4
4
|
|
|
5
|
-
// BCS schema for `TypeName` used by the Move contract.
|
|
6
|
-
//
|
|
5
|
+
// BCS schema for `std::type_name::TypeName` used by the Move contract.
|
|
6
|
+
// In Sui Move stdlib, `TypeName` is:
|
|
7
|
+
// struct TypeName { name: std::ascii::String }
|
|
8
|
+
// and `std::ascii::String` is:
|
|
9
|
+
// struct String { bytes: vector<u8> }
|
|
10
|
+
const AsciiStringBcs = bcs.struct('AsciiString', {
|
|
11
|
+
bytes: bcs.vector(bcs.u8()),
|
|
12
|
+
});
|
|
13
|
+
|
|
7
14
|
const TypeNameBcs = bcs.struct('TypeName', {
|
|
8
|
-
|
|
9
|
-
addr: bcs.bytes(32),
|
|
10
|
-
module: bcs.string(),
|
|
11
|
-
name: bcs.string(),
|
|
15
|
+
name: AsciiStringBcs,
|
|
12
16
|
});
|
|
13
17
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
20
|
-
throw new Error(`Invalid
|
|
18
|
+
// Convert a Move type tag string into the exact `std::type_name::TypeName.name` format.
|
|
19
|
+
// Move stdlib encodes addresses as lowercase hex WITHOUT 0x and padded to 32 bytes (64 hex chars).
|
|
20
|
+
function normalizeTypeNameString(typeTag: string): string {
|
|
21
|
+
const sep = '::';
|
|
22
|
+
const idx = typeTag.indexOf(sep);
|
|
23
|
+
if (idx < 0) {
|
|
24
|
+
throw new Error(`Invalid type tag (missing '::'): ${typeTag}`);
|
|
21
25
|
}
|
|
22
|
-
|
|
26
|
+
|
|
27
|
+
const addrPart = typeTag.slice(0, idx);
|
|
28
|
+
const rest = typeTag.slice(idx + sep.length);
|
|
29
|
+
const hex = (addrPart.startsWith('0x') ? addrPart.slice(2) : addrPart).toLowerCase();
|
|
30
|
+
const padded = hex.padStart(64, '0');
|
|
31
|
+
return `${padded}${sep}${rest}`;
|
|
23
32
|
}
|
|
24
33
|
|
|
25
34
|
export class OracleClient {
|
|
26
35
|
private provider: SuiClient;
|
|
27
36
|
private packageId: string;
|
|
28
37
|
private oracleId: string;
|
|
38
|
+
// Sui system Clock shared object id.
|
|
39
|
+
// See: `0x2::clock::Clock` (typically `0x6` on Sui networks).
|
|
40
|
+
private static readonly DEFAULT_CLOCK_ID = '0x6';
|
|
29
41
|
|
|
30
42
|
constructor(provider: SuiClient, packageId: string, oracleId: string) {
|
|
31
43
|
this.provider = provider;
|
|
@@ -36,14 +48,15 @@ export class OracleClient {
|
|
|
36
48
|
/**
|
|
37
49
|
* Get the price of a single asset by its type.
|
|
38
50
|
* @param assetType The type of the asset, e.g., '0x2::sui::SUI'
|
|
39
|
-
* @
|
|
51
|
+
* @param clockId The ID of the Clock object (defaults to 0x6)
|
|
52
|
+
* @returns The price as a bigint in WAD (1e18) precision.
|
|
40
53
|
*/
|
|
41
|
-
async getPriceByType(assetType: string): Promise<
|
|
54
|
+
async getPriceByType(assetType: string, clockId: string = OracleClient.DEFAULT_CLOCK_ID): Promise<bigint> {
|
|
42
55
|
const tx = new Transaction();
|
|
43
56
|
tx.moveCall({
|
|
44
|
-
target: `${this.packageId}::
|
|
57
|
+
target: `${this.packageId}::pyth_oracle::price_by_type`,
|
|
45
58
|
typeArguments: [assetType],
|
|
46
|
-
arguments: [tx.object(this.oracleId)],
|
|
59
|
+
arguments: [tx.object(this.oracleId), tx.object(clockId)],
|
|
47
60
|
});
|
|
48
61
|
|
|
49
62
|
const result = await this.provider.devInspectTransactionBlock({
|
|
@@ -60,10 +73,9 @@ export class OracleClient {
|
|
|
60
73
|
throw new Error('No return value from price_by_type');
|
|
61
74
|
}
|
|
62
75
|
|
|
63
|
-
//
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
return Number(priceBigInt) / 1e18;
|
|
76
|
+
// Return is BCS-encoded u128 (little-endian). Parse via BCS.
|
|
77
|
+
const priceU128Str = bcs.u128().parse(new Uint8Array(returnValue[0] as any));
|
|
78
|
+
return BigInt(priceU128Str);
|
|
67
79
|
}
|
|
68
80
|
|
|
69
81
|
/**
|
|
@@ -75,8 +87,11 @@ export class OracleClient {
|
|
|
75
87
|
): Promise<Record<string, { priceObjectId: string; aggregatorId: string }>> {
|
|
76
88
|
const queryTx = new Transaction();
|
|
77
89
|
const typeNames = assetTypes.map((assetType) => {
|
|
78
|
-
|
|
79
|
-
|
|
90
|
+
// `get_feed_object_addresses_batch` expects `vector<std::type_name::TypeName>`.
|
|
91
|
+
// We encode TypeName as { name: { bytes } } where bytes are ASCII bytes of the normalized type string.
|
|
92
|
+
const normalized = normalizeTypeNameString(assetType);
|
|
93
|
+
const bytes = Array.from(Buffer.from(normalized, 'ascii'));
|
|
94
|
+
return { name: { bytes } };
|
|
80
95
|
});
|
|
81
96
|
|
|
82
97
|
queryTx.moveCall({
|
|
@@ -122,17 +137,18 @@ export class OracleClient {
|
|
|
122
137
|
/**
|
|
123
138
|
* Get the prices of multiple assets by their types.
|
|
124
139
|
* @param assetTypes Array of asset types
|
|
140
|
+
* @param clockId The ID of the Clock object (defaults to 0x6)
|
|
125
141
|
* @returns Array of prices
|
|
126
142
|
*/
|
|
127
|
-
async getPricesByTypes(assetTypes: string[]): Promise<
|
|
128
|
-
const promises = assetTypes.map(assetType => this.getPriceByType(assetType));
|
|
143
|
+
async getPricesByTypes(assetTypes: string[], clockId: string = OracleClient.DEFAULT_CLOCK_ID): Promise<bigint[]> {
|
|
144
|
+
const promises = assetTypes.map(assetType => this.getPriceByType(assetType, clockId));
|
|
129
145
|
return Promise.all(promises);
|
|
130
146
|
}
|
|
131
147
|
|
|
132
148
|
/**
|
|
133
149
|
* Update prices for multiple assets by calling adaptor_oracle::update_price in a loop.
|
|
134
150
|
* This builds a transaction that updates multiple assets' prices.
|
|
135
|
-
* @param
|
|
151
|
+
* @param assetTypes Array of asset types (e.g. '0x2::sui::SUI')
|
|
136
152
|
* @param clockId The ID of the Clock object
|
|
137
153
|
* @returns A Promise that resolves to a TransactionBlock that can be signed and executed
|
|
138
154
|
*/
|