hearn-oracle-sdk 1.0.1 → 1.0.3
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 +40 -26
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
- package/src/example.ts +0 -57
- package/src/index.ts +0 -159
- package/tsconfig.json +0 -18
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
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,CAA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,CAAC,MAAM,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
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hearn-oracle-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "SDK for interacting with Hearn Oracle on Sui blockchain",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
7
10
|
"scripts": {
|
|
8
11
|
"build": "tsc",
|
|
9
12
|
"dev": "tsc --watch"
|
package/src/example.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
// Example usage of Hearn Oracle SDK
|
|
2
|
-
// This file demonstrates how to use the OracleClient in a frontend application
|
|
3
|
-
|
|
4
|
-
import { SuiClient } from '@mysten/sui/client';
|
|
5
|
-
import { OracleClient } from './index'; // or 'hearn-oracle-sdk' if installed
|
|
6
|
-
|
|
7
|
-
async function main() {
|
|
8
|
-
// Initialize Sui client
|
|
9
|
-
const provider = new SuiClient({
|
|
10
|
-
url: 'https://fullnode.mainnet.sui.io:443'
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
// These IDs should come from your deployment
|
|
14
|
-
const packageId = '0x...'; // Replace with actual deployed package ID
|
|
15
|
-
const oracleId = '0x...'; // Replace with actual oracle object ID
|
|
16
|
-
|
|
17
|
-
// Create OracleClient instance
|
|
18
|
-
const oracleClient = new OracleClient(provider, packageId, oracleId);
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
// Example 1: Get price for SUI
|
|
22
|
-
console.log('Fetching SUI price...');
|
|
23
|
-
const suiPrice = await oracleClient.getPriceByType('0x2::sui::SUI');
|
|
24
|
-
console.log('SUI Price:', suiPrice);
|
|
25
|
-
|
|
26
|
-
// Example 2: Get prices for multiple assets
|
|
27
|
-
console.log('Fetching multiple prices...');
|
|
28
|
-
const assetTypes = [
|
|
29
|
-
'0x2::sui::SUI',
|
|
30
|
-
// Add more asset types as needed
|
|
31
|
-
// '0x...::btc::BTC',
|
|
32
|
-
// '0x...::eth::ETH'
|
|
33
|
-
];
|
|
34
|
-
const prices = await oracleClient.getPricesByTypes(assetTypes);
|
|
35
|
-
console.log('Prices:', prices);
|
|
36
|
-
|
|
37
|
-
// Example 3: Update prices for multiple assets
|
|
38
|
-
console.log('Building transaction to update prices...');
|
|
39
|
-
const clockId = '0x6'; // Standard Clock object ID in Sui
|
|
40
|
-
const updateTx = await oracleClient.updatePrices(assetTypes, clockId);
|
|
41
|
-
console.log('Update transaction built:', updateTx);
|
|
42
|
-
|
|
43
|
-
// To execute the transaction, you need a signer:
|
|
44
|
-
// const signer = ...; // Your wallet signer
|
|
45
|
-
// const result = await signer.signAndExecuteTransactionBlock({
|
|
46
|
-
// transactionBlock: updateTx,
|
|
47
|
-
// });
|
|
48
|
-
// console.log('Update result:', result);
|
|
49
|
-
|
|
50
|
-
// Example 4: Handle errors
|
|
51
|
-
} catch (error) {
|
|
52
|
-
console.error('Error:', error);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Run the example
|
|
57
|
-
main();
|
package/src/index.ts
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { SuiClient } from '@mysten/sui/client';
|
|
2
|
-
import { Transaction } from '@mysten/sui/transactions';
|
|
3
|
-
import { bcs } from '@mysten/bcs';
|
|
4
|
-
|
|
5
|
-
// BCS schema for `TypeName` used by the Move contract.
|
|
6
|
-
// NOTE: This matches the struct shape expected on-chain.
|
|
7
|
-
const TypeNameBcs = bcs.struct('TypeName', {
|
|
8
|
-
// Move `address` is BCS-encoded as 32 bytes in Sui.
|
|
9
|
-
addr: bcs.bytes(32),
|
|
10
|
-
module: bcs.string(),
|
|
11
|
-
name: bcs.string(),
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
function suiAddressToBytes(address: string): Uint8Array {
|
|
15
|
-
const hex = address.startsWith('0x') ? address.slice(2) : address;
|
|
16
|
-
// 32 bytes = 64 hex chars
|
|
17
|
-
const padded = hex.padStart(64, '0');
|
|
18
|
-
const buf = Buffer.from(padded, 'hex');
|
|
19
|
-
if (buf.length !== 32) {
|
|
20
|
-
throw new Error(`Invalid Sui address length: expected 32 bytes, got ${buf.length}`);
|
|
21
|
-
}
|
|
22
|
-
return new Uint8Array(buf);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class OracleClient {
|
|
26
|
-
private provider: SuiClient;
|
|
27
|
-
private packageId: string;
|
|
28
|
-
private oracleId: string;
|
|
29
|
-
|
|
30
|
-
constructor(provider: SuiClient, packageId: string, oracleId: string) {
|
|
31
|
-
this.provider = provider;
|
|
32
|
-
this.packageId = packageId;
|
|
33
|
-
this.oracleId = oracleId;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Get the price of a single asset by its type.
|
|
38
|
-
* @param assetType The type of the asset, e.g., '0x2::sui::SUI'
|
|
39
|
-
* @returns The price as a number (assuming wad format, divided by 1e18)
|
|
40
|
-
*/
|
|
41
|
-
async getPriceByType(assetType: string): Promise<number> {
|
|
42
|
-
const tx = new Transaction();
|
|
43
|
-
tx.moveCall({
|
|
44
|
-
target: `${this.packageId}::pyth_oracle::price_by_type`,
|
|
45
|
-
typeArguments: [assetType],
|
|
46
|
-
arguments: [tx.object(this.oracleId)],
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
const result = await this.provider.devInspectTransactionBlock({
|
|
50
|
-
transactionBlock: tx,
|
|
51
|
-
sender: '0x0000000000000000000000000000000000000000000000000000000000000000', // dummy sender for read-only
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
if (!result.results || result.results.length === 0) {
|
|
55
|
-
throw new Error('No results from transaction inspection');
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const returnValue = result.results[0].returnValues?.[0];
|
|
59
|
-
if (!returnValue) {
|
|
60
|
-
throw new Error('No return value from price_by_type');
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Assuming return is u128 as bytes, convert to bigint
|
|
64
|
-
const priceBigInt = BigInt('0x' + Buffer.from(returnValue[0]).toString('hex'));
|
|
65
|
-
// Assuming wad format, divide by 1e18
|
|
66
|
-
return Number(priceBigInt) / 1e18;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Batch query feed object ids for multiple asset types in a single devInspect.
|
|
71
|
-
* Returns a map from assetType -> { priceObjectId, aggregatorId }.
|
|
72
|
-
*/
|
|
73
|
-
private async getFeedObjectIdsByTypes(
|
|
74
|
-
assetTypes: string[],
|
|
75
|
-
): Promise<Record<string, { priceObjectId: string; aggregatorId: string }>> {
|
|
76
|
-
const queryTx = new Transaction();
|
|
77
|
-
const typeNames = assetTypes.map((assetType) => {
|
|
78
|
-
const [address, module, name] = assetType.split('::');
|
|
79
|
-
return { addr: suiAddressToBytes(address), module, name };
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
queryTx.moveCall({
|
|
83
|
-
target: `${this.packageId}::pyth_oracle::get_feed_object_addresses_batch`,
|
|
84
|
-
arguments: [queryTx.object(this.oracleId), queryTx.pure(bcs.vector(TypeNameBcs).serialize(typeNames))],
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
const queryResult = await this.provider.devInspectTransactionBlock({
|
|
88
|
-
transactionBlock: queryTx,
|
|
89
|
-
sender: '0x0000000000000000000000000000000000000000000000000000000000000000',
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
if (!queryResult.results || queryResult.results.length !== 1) {
|
|
93
|
-
throw new Error(`Unexpected results length from get_feed_object_addresses_batch: ${queryResult.results?.length ?? 0}`);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const returnValues = queryResult.results[0]?.returnValues;
|
|
97
|
-
if (!returnValues || returnValues.length < 2) {
|
|
98
|
-
throw new Error('Invalid return values from get_feed_object_addresses_batch');
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Each return is a BCS-encoded `vector<address>`.
|
|
102
|
-
const pythAddrBytesVec = bcs.vector(bcs.bytes(32)).parse(new Uint8Array(returnValues[0]![0] as any));
|
|
103
|
-
const sbAddrBytesVec = bcs.vector(bcs.bytes(32)).parse(new Uint8Array(returnValues[1]![0] as any));
|
|
104
|
-
|
|
105
|
-
if (pythAddrBytesVec.length !== assetTypes.length || sbAddrBytesVec.length !== assetTypes.length) {
|
|
106
|
-
throw new Error(
|
|
107
|
-
`Unexpected address vector lengths from get_feed_object_addresses_batch: pyth=${pythAddrBytesVec.length}, sb=${sbAddrBytesVec.length}, expected=${assetTypes.length}`,
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const out: Record<string, { priceObjectId: string; aggregatorId: string }> = {};
|
|
112
|
-
for (let i = 0; i < assetTypes.length; i++) {
|
|
113
|
-
const assetType = assetTypes[i]!;
|
|
114
|
-
out[assetType] = {
|
|
115
|
-
priceObjectId: '0x' + Buffer.from(pythAddrBytesVec[i]!).toString('hex'),
|
|
116
|
-
aggregatorId: '0x' + Buffer.from(sbAddrBytesVec[i]!).toString('hex'),
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
return out;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Get the prices of multiple assets by their types.
|
|
124
|
-
* @param assetTypes Array of asset types
|
|
125
|
-
* @returns Array of prices
|
|
126
|
-
*/
|
|
127
|
-
async getPricesByTypes(assetTypes: string[]): Promise<number[]> {
|
|
128
|
-
const promises = assetTypes.map(assetType => this.getPriceByType(assetType));
|
|
129
|
-
return Promise.all(promises);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Update prices for multiple assets by calling adaptor_oracle::update_price in a loop.
|
|
134
|
-
* This builds a transaction that updates multiple assets' prices.
|
|
135
|
-
* @param updateDatas Array of update data for each asset
|
|
136
|
-
* @param clockId The ID of the Clock object
|
|
137
|
-
* @returns A Promise that resolves to a TransactionBlock that can be signed and executed
|
|
138
|
-
*/
|
|
139
|
-
async updatePrices(assetTypes: string[], clockId: string): Promise<Transaction> {
|
|
140
|
-
const tx = new Transaction();
|
|
141
|
-
// 1) Batch query feed object ids (single devInspect).
|
|
142
|
-
const feedObjectIdsByType = await this.getFeedObjectIdsByTypes(assetTypes);
|
|
143
|
-
// 2) Add update calls using the queried ids.
|
|
144
|
-
// NOTE: caller guarantees `assetTypes` has no duplicates and order does not matter.
|
|
145
|
-
for (const [assetType, ids] of Object.entries(feedObjectIdsByType)) {
|
|
146
|
-
tx.moveCall({
|
|
147
|
-
target: `${this.packageId}::adaptor_oracle::update_price`,
|
|
148
|
-
typeArguments: [assetType],
|
|
149
|
-
arguments: [
|
|
150
|
-
tx.object(this.oracleId),
|
|
151
|
-
tx.object(ids.priceObjectId),
|
|
152
|
-
tx.object(ids.aggregatorId),
|
|
153
|
-
tx.object(clockId),
|
|
154
|
-
],
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
return tx;
|
|
158
|
-
}
|
|
159
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"lib": ["ES2020"],
|
|
6
|
-
"outDir": "./dist",
|
|
7
|
-
"rootDir": "./src",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"forceConsistentCasingInFileNames": true,
|
|
12
|
-
"declaration": true,
|
|
13
|
-
"declarationMap": true,
|
|
14
|
-
"sourceMap": true
|
|
15
|
-
},
|
|
16
|
-
"include": ["src/**/*"],
|
|
17
|
-
"exclude": ["node_modules", "dist"]
|
|
18
|
-
}
|