perps-sdk-ts 1.0.1
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/.claude/settings.local.json +11 -0
- package/CONTRACT_METHOD_FIXES.md +189 -0
- package/INTEGRATION_SUMMARY.md +219 -0
- package/OPTIMIZATION_GUIDE.md +238 -0
- package/README.md +384 -0
- package/SNAPSHOT_FIX_SUMMARY.md +161 -0
- package/SNAPSHOT_OPTIMIZATION_SUMMARY.md +199 -0
- package/dist/abis/Referral.d.ts +36 -0
- package/dist/abis/Referral.js +4 -0
- package/dist/abis/Trading.d.ts +57 -0
- package/dist/abis/Trading.js +742 -0
- package/dist/abis/erc20.d.ts +51 -0
- package/dist/abis/erc20.js +4 -0
- package/dist/abis/index.d.ts +8 -0
- package/dist/abis/index.js +24 -0
- package/dist/abis/multicall.d.ts +85 -0
- package/dist/abis/multicall.js +4 -0
- package/dist/abis/pairInfos.d.ts +77 -0
- package/dist/abis/pairInfos.js +4 -0
- package/dist/abis/pairStorage.d.ts +124 -0
- package/dist/abis/pairStorage.js +4 -0
- package/dist/abis/priceAggregator.d.ts +77 -0
- package/dist/abis/priceAggregator.js +4 -0
- package/dist/abis/tardingStorage.d.ts +97 -0
- package/dist/abis/tardingStorage.js +1295 -0
- package/dist/abis.d.ts +623 -0
- package/dist/abis.js +49 -0
- package/dist/client.d.ts +118 -0
- package/dist/client.js +224 -0
- package/dist/config.d.ts +43 -0
- package/dist/config.js +42 -0
- package/dist/crypto/spki.d.ts +55 -0
- package/dist/crypto/spki.js +160 -0
- package/dist/feed/feed_client.d.ts +68 -0
- package/dist/feed/feed_client.js +239 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +87 -0
- package/dist/rpc/asset_parameters.d.ts +62 -0
- package/dist/rpc/asset_parameters.js +169 -0
- package/dist/rpc/blended.d.ts +23 -0
- package/dist/rpc/blended.js +55 -0
- package/dist/rpc/category_parameters.d.ts +34 -0
- package/dist/rpc/category_parameters.js +105 -0
- package/dist/rpc/delegation.d.ts +81 -0
- package/dist/rpc/delegation.js +180 -0
- package/dist/rpc/fee_parameters.d.ts +46 -0
- package/dist/rpc/fee_parameters.js +113 -0
- package/dist/rpc/multicall.d.ts +83 -0
- package/dist/rpc/multicall.js +117 -0
- package/dist/rpc/pair_info_queries.d.ts +101 -0
- package/dist/rpc/pair_info_queries.js +161 -0
- package/dist/rpc/pairs_cache.d.ts +62 -0
- package/dist/rpc/pairs_cache.js +240 -0
- package/dist/rpc/referral_operations.d.ts +67 -0
- package/dist/rpc/referral_operations.js +143 -0
- package/dist/rpc/snapshot.d.ts +49 -0
- package/dist/rpc/snapshot.js +162 -0
- package/dist/rpc/trade.d.ts +84 -0
- package/dist/rpc/trade.js +249 -0
- package/dist/rpc/trading_operations.d.ts +103 -0
- package/dist/rpc/trading_operations.js +295 -0
- package/dist/rpc/trading_parameters.d.ts +49 -0
- package/dist/rpc/trading_parameters.js +94 -0
- package/dist/signers/base.d.ts +24 -0
- package/dist/signers/base.js +10 -0
- package/dist/signers/kms.d.ts +47 -0
- package/dist/signers/kms.js +172 -0
- package/dist/signers/local.d.ts +43 -0
- package/dist/signers/local.js +64 -0
- package/dist/types.d.ts +1419 -0
- package/dist/types.js +245 -0
- package/dist/utils.d.ts +52 -0
- package/dist/utils.js +134 -0
- package/examples/advanced-queries.ts +181 -0
- package/examples/basic-usage.ts +78 -0
- package/examples/delegation-and-referrals.ts +130 -0
- package/examples/get-pyth-ids.ts +61 -0
- package/examples/kms-signer.ts +31 -0
- package/examples/optimized-snapshot.ts +153 -0
- package/examples/price-feed-with-sdk-ids.ts +97 -0
- package/examples/price-feed.ts +36 -0
- package/examples/trading-operations.ts +149 -0
- package/package.json +41 -0
- package/src/abis/Referral.ts +3 -0
- package/src/abis/Trading.ts +741 -0
- package/src/abis/erc20.ts +3 -0
- package/src/abis/index.ts +8 -0
- package/src/abis/multicall.ts +3 -0
- package/src/abis/pairInfos.ts +3 -0
- package/src/abis/pairStorage.ts +3 -0
- package/src/abis/priceAggregator.ts +3 -0
- package/src/abis/tardingStorage.ts +1294 -0
- package/src/abis.ts +56 -0
- package/src/client.ts +373 -0
- package/src/config.ts +62 -0
- package/src/crypto/spki.ts +197 -0
- package/src/feed/feed_client.ts +288 -0
- package/src/index.ts +114 -0
- package/src/rpc/asset_parameters.ts +217 -0
- package/src/rpc/blended.ts +77 -0
- package/src/rpc/category_parameters.ts +128 -0
- package/src/rpc/delegation.ts +225 -0
- package/src/rpc/fee_parameters.ts +150 -0
- package/src/rpc/multicall.ts +164 -0
- package/src/rpc/pair_info_queries.ts +208 -0
- package/src/rpc/pairs_cache.ts +268 -0
- package/src/rpc/referral_operations.ts +164 -0
- package/src/rpc/snapshot.ts +210 -0
- package/src/rpc/trade.ts +306 -0
- package/src/rpc/trading_operations.ts +378 -0
- package/src/rpc/trading_parameters.ts +127 -0
- package/src/signers/base.ts +27 -0
- package/src/signers/kms.ts +212 -0
- package/src/signers/local.ts +70 -0
- package/src/types.ts +410 -0
- package/src/utils.ts +155 -0
- package/tsconfig.json +18 -0
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { JsonRpcProvider, TransactionReceipt, TransactionRequest } from 'ethers';
|
|
2
|
+
import { BaseSigner } from './signers/base';
|
|
3
|
+
import { FeedClient } from './feed/feed_client';
|
|
4
|
+
import { PairsCache } from './rpc/pairs_cache';
|
|
5
|
+
import { AssetParametersRPC } from './rpc/asset_parameters';
|
|
6
|
+
import { CategoryParametersRPC } from './rpc/category_parameters';
|
|
7
|
+
import { FeeParametersRPC } from './rpc/fee_parameters';
|
|
8
|
+
import { TradingParametersRPC } from './rpc/trading_parameters';
|
|
9
|
+
import { BlendedRPC } from './rpc/blended';
|
|
10
|
+
import { TradeRPC } from './rpc/trade';
|
|
11
|
+
import { SnapshotRPC } from './rpc/snapshot';
|
|
12
|
+
import { TradingOperationsRPC } from './rpc/trading_operations';
|
|
13
|
+
import { DelegationRPC } from './rpc/delegation';
|
|
14
|
+
import { PairInfoQueriesRPC } from './rpc/pair_info_queries';
|
|
15
|
+
import { ReferralOperationsRPC } from './rpc/referral_operations';
|
|
16
|
+
import { MulticallRPC } from './rpc/multicall';
|
|
17
|
+
/**
|
|
18
|
+
* Main client for interacting with Avantis trading platform
|
|
19
|
+
*/
|
|
20
|
+
export declare class TraderClient {
|
|
21
|
+
provider: JsonRpcProvider;
|
|
22
|
+
signer?: BaseSigner;
|
|
23
|
+
feedClient?: FeedClient;
|
|
24
|
+
private contracts;
|
|
25
|
+
pairsCache: PairsCache;
|
|
26
|
+
assetParams: AssetParametersRPC;
|
|
27
|
+
categoryParams: CategoryParametersRPC;
|
|
28
|
+
feeParams: FeeParametersRPC;
|
|
29
|
+
tradingParams: TradingParametersRPC;
|
|
30
|
+
blendedParams: BlendedRPC;
|
|
31
|
+
tradeRPC: TradeRPC;
|
|
32
|
+
snapshotRPC: SnapshotRPC;
|
|
33
|
+
tradingOps: TradingOperationsRPC;
|
|
34
|
+
delegation: DelegationRPC;
|
|
35
|
+
pairInfoQueries: PairInfoQueriesRPC;
|
|
36
|
+
referral: ReferralOperationsRPC;
|
|
37
|
+
multicall: MulticallRPC;
|
|
38
|
+
/**
|
|
39
|
+
* Create a new TraderClient
|
|
40
|
+
* @param providerUrl - Ethereum RPC endpoint
|
|
41
|
+
* @param signer - Transaction signer (optional)
|
|
42
|
+
* @param feedClient - Feed client for price updates (optional)
|
|
43
|
+
*/
|
|
44
|
+
constructor(providerUrl: string, signer?: BaseSigner, feedClient?: FeedClient);
|
|
45
|
+
/**
|
|
46
|
+
* Initialize contract instances
|
|
47
|
+
*/
|
|
48
|
+
private initializeContracts;
|
|
49
|
+
/**
|
|
50
|
+
* Get a contract instance
|
|
51
|
+
* @param name - Contract name
|
|
52
|
+
* @returns Contract instance
|
|
53
|
+
*/
|
|
54
|
+
private getContract;
|
|
55
|
+
/**
|
|
56
|
+
* Set signer for transaction signing
|
|
57
|
+
* @param signer - Signer instance
|
|
58
|
+
*/
|
|
59
|
+
setSigner(signer: BaseSigner): void;
|
|
60
|
+
/**
|
|
61
|
+
* Set local signer using private key
|
|
62
|
+
* @param privateKey - Private key
|
|
63
|
+
*/
|
|
64
|
+
setLocalSigner(privateKey: string): void;
|
|
65
|
+
/**
|
|
66
|
+
* Set AWS KMS signer
|
|
67
|
+
* @param kmsKeyId - KMS key ID
|
|
68
|
+
* @param region - AWS region
|
|
69
|
+
*/
|
|
70
|
+
setAwsKmsSigner(kmsKeyId: string, region?: string): void;
|
|
71
|
+
/**
|
|
72
|
+
* Get native token balance
|
|
73
|
+
* @param address - Address to check
|
|
74
|
+
* @returns Balance in native token
|
|
75
|
+
*/
|
|
76
|
+
getBalance(address: string): Promise<bigint>;
|
|
77
|
+
/**
|
|
78
|
+
* Get USDC balance
|
|
79
|
+
* @param address - Address to check
|
|
80
|
+
* @returns USDC balance
|
|
81
|
+
*/
|
|
82
|
+
getUsdcBalance(address: string): Promise<number>;
|
|
83
|
+
/**
|
|
84
|
+
* Get USDC allowance for trading
|
|
85
|
+
* @param address - Address to check
|
|
86
|
+
* @returns Allowance amount
|
|
87
|
+
*/
|
|
88
|
+
getUsdcAllowanceForTrading(address: string): Promise<number>;
|
|
89
|
+
/**
|
|
90
|
+
* Approve USDC for trading
|
|
91
|
+
* @param amount - Amount to approve
|
|
92
|
+
* @returns Transaction receipt
|
|
93
|
+
*/
|
|
94
|
+
approveUsdcForTrading(amount: number): Promise<TransactionReceipt | null>;
|
|
95
|
+
/**
|
|
96
|
+
* Sign transaction and wait for receipt
|
|
97
|
+
* @param tx - Transaction to sign
|
|
98
|
+
* @returns Transaction receipt
|
|
99
|
+
*/
|
|
100
|
+
signAndGetReceipt(tx: TransactionRequest): Promise<TransactionReceipt | null>;
|
|
101
|
+
/**
|
|
102
|
+
* Estimate gas for a transaction
|
|
103
|
+
* @param tx - Transaction to estimate
|
|
104
|
+
* @returns Estimated gas
|
|
105
|
+
*/
|
|
106
|
+
estimateGas(tx: TransactionRequest): Promise<bigint>;
|
|
107
|
+
/**
|
|
108
|
+
* Get transaction count (nonce)
|
|
109
|
+
* @param address - Address to check
|
|
110
|
+
* @returns Transaction count
|
|
111
|
+
*/
|
|
112
|
+
getTransactionCount(address: string): Promise<number>;
|
|
113
|
+
/**
|
|
114
|
+
* Get chain ID
|
|
115
|
+
* @returns Chain ID
|
|
116
|
+
*/
|
|
117
|
+
getChainId(): Promise<bigint>;
|
|
118
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TraderClient = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const local_1 = require("./signers/local");
|
|
6
|
+
const kms_1 = require("./signers/kms");
|
|
7
|
+
const config_1 = require("./config");
|
|
8
|
+
const pairs_cache_1 = require("./rpc/pairs_cache");
|
|
9
|
+
const asset_parameters_1 = require("./rpc/asset_parameters");
|
|
10
|
+
const category_parameters_1 = require("./rpc/category_parameters");
|
|
11
|
+
const fee_parameters_1 = require("./rpc/fee_parameters");
|
|
12
|
+
const trading_parameters_1 = require("./rpc/trading_parameters");
|
|
13
|
+
const blended_1 = require("./rpc/blended");
|
|
14
|
+
const trade_1 = require("./rpc/trade");
|
|
15
|
+
const snapshot_1 = require("./rpc/snapshot");
|
|
16
|
+
const trading_operations_1 = require("./rpc/trading_operations");
|
|
17
|
+
const delegation_1 = require("./rpc/delegation");
|
|
18
|
+
const pair_info_queries_1 = require("./rpc/pair_info_queries");
|
|
19
|
+
const referral_operations_1 = require("./rpc/referral_operations");
|
|
20
|
+
const multicall_1 = require("./rpc/multicall");
|
|
21
|
+
const types_1 = require("./types");
|
|
22
|
+
const abis_1 = require("./abis");
|
|
23
|
+
/**
|
|
24
|
+
* Main client for interacting with Avantis trading platform
|
|
25
|
+
*/
|
|
26
|
+
class TraderClient {
|
|
27
|
+
/**
|
|
28
|
+
* Create a new TraderClient
|
|
29
|
+
* @param providerUrl - Ethereum RPC endpoint
|
|
30
|
+
* @param signer - Transaction signer (optional)
|
|
31
|
+
* @param feedClient - Feed client for price updates (optional)
|
|
32
|
+
*/
|
|
33
|
+
constructor(providerUrl, signer, feedClient) {
|
|
34
|
+
// Contracts
|
|
35
|
+
this.contracts = new Map();
|
|
36
|
+
this.provider = new ethers_1.JsonRpcProvider(providerUrl);
|
|
37
|
+
this.signer = signer;
|
|
38
|
+
this.feedClient = feedClient;
|
|
39
|
+
// Initialize RPC modules
|
|
40
|
+
this.initializeContracts();
|
|
41
|
+
this.pairsCache = new pairs_cache_1.PairsCache(this.provider, this.getContract('PairStorage'));
|
|
42
|
+
const pairStorage = this.getContract('PairStorage');
|
|
43
|
+
const pairInfos = this.getContract('PairInfos');
|
|
44
|
+
const trading = this.getContract('Trading');
|
|
45
|
+
const tradingStorage = this.getContract('TradingStorage');
|
|
46
|
+
const referral = this.getContract('Referral');
|
|
47
|
+
this.assetParams = new asset_parameters_1.AssetParametersRPC(this.provider, pairStorage, pairInfos, this.pairsCache, tradingStorage);
|
|
48
|
+
this.categoryParams = new category_parameters_1.CategoryParametersRPC(this.provider, pairStorage, this.pairsCache);
|
|
49
|
+
this.feeParams = new fee_parameters_1.FeeParametersRPC(this.provider, pairInfos, this.pairsCache, referral);
|
|
50
|
+
this.tradingParams = new trading_parameters_1.TradingParametersRPC(this.provider, pairInfos, this.pairsCache);
|
|
51
|
+
this.blendedParams = new blended_1.BlendedRPC(this.assetParams, this.categoryParams, this.pairsCache);
|
|
52
|
+
this.tradeRPC = new trade_1.TradeRPC(this.provider, trading, tradingStorage, this.pairsCache);
|
|
53
|
+
this.snapshotRPC = new snapshot_1.SnapshotRPC(this.pairsCache, this.assetParams, this.categoryParams, this.feeParams, this.blendedParams);
|
|
54
|
+
// Initialize new trading modules
|
|
55
|
+
this.tradingOps = new trading_operations_1.TradingOperationsRPC(trading, tradingStorage, this.signer);
|
|
56
|
+
this.delegation = new delegation_1.DelegationRPC(trading, this.signer);
|
|
57
|
+
this.pairInfoQueries = new pair_info_queries_1.PairInfoQueriesRPC(pairInfos, this.getContract('PriceAggregator'));
|
|
58
|
+
this.referral = new referral_operations_1.ReferralOperationsRPC(referral, this.signer);
|
|
59
|
+
this.multicall = new multicall_1.MulticallRPC(this.getContract('Multicall'));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Initialize contract instances
|
|
63
|
+
*/
|
|
64
|
+
initializeContracts() {
|
|
65
|
+
this.contracts.set('TradingStorage', new ethers_1.Contract(config_1.CONTRACTS.TradingStorage, abis_1.TRADING_STORAGE_ABI, this.provider));
|
|
66
|
+
this.contracts.set('PairStorage', new ethers_1.Contract(config_1.CONTRACTS.PairStorage, abis_1.PAIR_STORAGE_ABI, this.provider));
|
|
67
|
+
this.contracts.set('PairInfos', new ethers_1.Contract(config_1.CONTRACTS.PairInfos, abis_1.PAIR_INFOS_ABI, this.provider));
|
|
68
|
+
this.contracts.set('PriceAggregator', new ethers_1.Contract(config_1.CONTRACTS.PriceAggregator, abis_1.PRICE_AGGREGATOR_ABI, this.provider));
|
|
69
|
+
this.contracts.set('USDC', new ethers_1.Contract(config_1.CONTRACTS.USDC, abis_1.ERC20_ABI, this.provider));
|
|
70
|
+
this.contracts.set('Trading', new ethers_1.Contract(config_1.CONTRACTS.Trading, abis_1.TRADING_ABI, this.provider));
|
|
71
|
+
this.contracts.set('Referral', new ethers_1.Contract(config_1.CONTRACTS.Referral, abis_1.REFERRAL_ABI, this.provider));
|
|
72
|
+
this.contracts.set('Multicall', new ethers_1.Contract(config_1.CONTRACTS.Multicall, abis_1.MULTICALL_ABI, this.provider));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get a contract instance
|
|
76
|
+
* @param name - Contract name
|
|
77
|
+
* @returns Contract instance
|
|
78
|
+
*/
|
|
79
|
+
getContract(name) {
|
|
80
|
+
const contract = this.contracts.get(name);
|
|
81
|
+
if (!contract) {
|
|
82
|
+
throw new Error(`Contract ${name} not initialized`);
|
|
83
|
+
}
|
|
84
|
+
return contract;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Set signer for transaction signing
|
|
88
|
+
* @param signer - Signer instance
|
|
89
|
+
*/
|
|
90
|
+
setSigner(signer) {
|
|
91
|
+
this.signer = signer;
|
|
92
|
+
this.tradingOps.setSigner(signer);
|
|
93
|
+
this.delegation.setSigner(signer);
|
|
94
|
+
this.referral.setSigner(signer);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Set local signer using private key
|
|
98
|
+
* @param privateKey - Private key
|
|
99
|
+
*/
|
|
100
|
+
setLocalSigner(privateKey) {
|
|
101
|
+
this.signer = new local_1.LocalSigner(privateKey, this.provider);
|
|
102
|
+
this.setSigner(this.signer);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Set AWS KMS signer
|
|
106
|
+
* @param kmsKeyId - KMS key ID
|
|
107
|
+
* @param region - AWS region
|
|
108
|
+
*/
|
|
109
|
+
setAwsKmsSigner(kmsKeyId, region = 'us-east-1') {
|
|
110
|
+
this.signer = new kms_1.KMSSigner(kmsKeyId, this.provider, region);
|
|
111
|
+
this.setSigner(this.signer);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get native token balance
|
|
115
|
+
* @param address - Address to check
|
|
116
|
+
* @returns Balance in native token
|
|
117
|
+
*/
|
|
118
|
+
async getBalance(address) {
|
|
119
|
+
return await this.provider.getBalance(address);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get USDC balance
|
|
123
|
+
* @param address - Address to check
|
|
124
|
+
* @returns USDC balance
|
|
125
|
+
*/
|
|
126
|
+
async getUsdcBalance(address) {
|
|
127
|
+
const balance = await this.getContract('USDC').balanceOf(address);
|
|
128
|
+
return (0, types_1.fromBlockchain6)(balance);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Get USDC allowance for trading
|
|
132
|
+
* @param address - Address to check
|
|
133
|
+
* @returns Allowance amount
|
|
134
|
+
*/
|
|
135
|
+
async getUsdcAllowanceForTrading(address) {
|
|
136
|
+
const tradingStorageAddress = await this.getContract('TradingStorage').getAddress();
|
|
137
|
+
const allowance = await this.getContract('USDC').allowance(address, tradingStorageAddress);
|
|
138
|
+
return (0, types_1.fromBlockchain6)(allowance);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Approve USDC for trading
|
|
142
|
+
* @param amount - Amount to approve
|
|
143
|
+
* @returns Transaction receipt
|
|
144
|
+
*/
|
|
145
|
+
async approveUsdcForTrading(amount) {
|
|
146
|
+
if (!this.signer) {
|
|
147
|
+
throw new Error('Signer not set');
|
|
148
|
+
}
|
|
149
|
+
const tradingStorageAddress = await this.getContract('TradingStorage').getAddress();
|
|
150
|
+
const amountWei = BigInt(Math.floor(amount * 1e6));
|
|
151
|
+
const tx = {
|
|
152
|
+
to: config_1.CONTRACTS.USDC,
|
|
153
|
+
data: this.getContract('USDC').interface.encodeFunctionData('approve', [
|
|
154
|
+
tradingStorageAddress,
|
|
155
|
+
amountWei,
|
|
156
|
+
]),
|
|
157
|
+
};
|
|
158
|
+
return await this.signAndGetReceipt(tx);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Sign transaction and wait for receipt
|
|
162
|
+
* @param tx - Transaction to sign
|
|
163
|
+
* @returns Transaction receipt
|
|
164
|
+
*/
|
|
165
|
+
async signAndGetReceipt(tx) {
|
|
166
|
+
if (!this.signer) {
|
|
167
|
+
throw new Error('Signer not set');
|
|
168
|
+
}
|
|
169
|
+
// Fill in missing transaction fields
|
|
170
|
+
const address = await this.signer.getAddress();
|
|
171
|
+
tx.from = address;
|
|
172
|
+
if (!tx.chainId) {
|
|
173
|
+
const network = await this.provider.getNetwork();
|
|
174
|
+
tx.chainId = network.chainId;
|
|
175
|
+
}
|
|
176
|
+
if (tx.nonce === undefined) {
|
|
177
|
+
tx.nonce = await this.provider.getTransactionCount(address);
|
|
178
|
+
}
|
|
179
|
+
if (!tx.gasLimit) {
|
|
180
|
+
tx.gasLimit = await this.provider.estimateGas(tx);
|
|
181
|
+
}
|
|
182
|
+
if (!tx.maxFeePerGas && !tx.gasPrice) {
|
|
183
|
+
const feeData = await this.provider.getFeeData();
|
|
184
|
+
if (feeData.maxFeePerGas) {
|
|
185
|
+
tx.maxFeePerGas = feeData.maxFeePerGas;
|
|
186
|
+
tx.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas || feeData.maxFeePerGas;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
tx.gasPrice = feeData.gasPrice || undefined;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// Sign the transaction
|
|
193
|
+
const signedTx = await this.signer.signTransaction(tx);
|
|
194
|
+
// Send the transaction
|
|
195
|
+
const txResponse = await this.provider.broadcastTransaction(signedTx);
|
|
196
|
+
// Wait for confirmation
|
|
197
|
+
return await txResponse.wait();
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Estimate gas for a transaction
|
|
201
|
+
* @param tx - Transaction to estimate
|
|
202
|
+
* @returns Estimated gas
|
|
203
|
+
*/
|
|
204
|
+
async estimateGas(tx) {
|
|
205
|
+
return await this.provider.estimateGas(tx);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Get transaction count (nonce)
|
|
209
|
+
* @param address - Address to check
|
|
210
|
+
* @returns Transaction count
|
|
211
|
+
*/
|
|
212
|
+
async getTransactionCount(address) {
|
|
213
|
+
return await this.provider.getTransactionCount(address);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Get chain ID
|
|
217
|
+
* @returns Chain ID
|
|
218
|
+
*/
|
|
219
|
+
async getChainId() {
|
|
220
|
+
const network = await this.provider.getNetwork();
|
|
221
|
+
return network.chainId;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
exports.TraderClient = TraderClient;
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration file containing contract addresses and API endpoints
|
|
3
|
+
* for Avantis trading platform
|
|
4
|
+
*/
|
|
5
|
+
export interface ContractAddresses {
|
|
6
|
+
TradingStorage: string;
|
|
7
|
+
PairStorage: string;
|
|
8
|
+
PairInfos: string;
|
|
9
|
+
PriceAggregator: string;
|
|
10
|
+
USDC: string;
|
|
11
|
+
Trading: string;
|
|
12
|
+
Multicall: string;
|
|
13
|
+
Referral: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Mainnet contract addresses for Avantis (Base Network)
|
|
17
|
+
*/
|
|
18
|
+
export declare const CONTRACTS: ContractAddresses;
|
|
19
|
+
/**
|
|
20
|
+
* API endpoints for Avantis services
|
|
21
|
+
*/
|
|
22
|
+
export declare const API_ENDPOINTS: {
|
|
23
|
+
SOCKET_API: string;
|
|
24
|
+
PYTH_WS: string;
|
|
25
|
+
PYTH_HTTP: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Network configuration
|
|
29
|
+
*/
|
|
30
|
+
export interface NetworkConfig {
|
|
31
|
+
chainId: number;
|
|
32
|
+
name: string;
|
|
33
|
+
rpcUrl: string;
|
|
34
|
+
contracts: ContractAddresses;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get contract address by name
|
|
38
|
+
*/
|
|
39
|
+
export declare function getContractAddress(contractName: keyof ContractAddresses): string;
|
|
40
|
+
/**
|
|
41
|
+
* Update contract addresses (useful for testing or different networks)
|
|
42
|
+
*/
|
|
43
|
+
export declare function setContractAddresses(addresses: Partial<ContractAddresses>): void;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration file containing contract addresses and API endpoints
|
|
4
|
+
* for Avantis trading platform
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.API_ENDPOINTS = exports.CONTRACTS = void 0;
|
|
8
|
+
exports.getContractAddress = getContractAddress;
|
|
9
|
+
exports.setContractAddresses = setContractAddresses;
|
|
10
|
+
/**
|
|
11
|
+
* Mainnet contract addresses for Avantis (Base Network)
|
|
12
|
+
*/
|
|
13
|
+
exports.CONTRACTS = {
|
|
14
|
+
TradingStorage: '0x8a311D7048c35985aa31C131B9A13e03a5f7422d',
|
|
15
|
+
PairStorage: '0x5db3772136e5557EFE028Db05EE95C84D76faEC4',
|
|
16
|
+
PairInfos: '0x81F22d0Cc22977c91bEfE648C9fddf1f2bd977e5',
|
|
17
|
+
PriceAggregator: '0x64e2625621970F8cfA17B294670d61CB883dA511',
|
|
18
|
+
USDC: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
|
|
19
|
+
Trading: '0x44914408af82bC9983bbb330e3578E1105e11d4e',
|
|
20
|
+
Multicall: '0xb7125506Ff25211c4C51DFD8DdED00BE6Fa8Cbf7',
|
|
21
|
+
Referral: '0x1A110bBA13A1f16cCa4b79758BD39290f29De82D',
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* API endpoints for Avantis services
|
|
25
|
+
*/
|
|
26
|
+
exports.API_ENDPOINTS = {
|
|
27
|
+
SOCKET_API: 'https://socket-api-pub.avantisfi.com/socket-api/v1/data',
|
|
28
|
+
PYTH_WS: 'wss://hermes.pyth.network/ws',
|
|
29
|
+
PYTH_HTTP: 'https://hermes.pyth.network/v2/updates/price/latest',
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Get contract address by name
|
|
33
|
+
*/
|
|
34
|
+
function getContractAddress(contractName) {
|
|
35
|
+
return exports.CONTRACTS[contractName];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Update contract addresses (useful for testing or different networks)
|
|
39
|
+
*/
|
|
40
|
+
function setContractAddresses(addresses) {
|
|
41
|
+
Object.assign(exports.CONTRACTS, addresses);
|
|
42
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cryptographic utilities for KMS signature handling
|
|
3
|
+
* Handles ECDSA signature conversion and Ethereum address derivation
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Convert a public key (as big integer coordinates) to an Ethereum address
|
|
7
|
+
* @param publicKeyX - X coordinate of public key
|
|
8
|
+
* @param publicKeyY - Y coordinate of public key
|
|
9
|
+
* @returns Ethereum address
|
|
10
|
+
*/
|
|
11
|
+
export declare function publicKeyIntToEthAddress(publicKeyX: bigint, publicKeyY: bigint): string;
|
|
12
|
+
/**
|
|
13
|
+
* Parse DER-encoded public key and convert to Ethereum address
|
|
14
|
+
* @param derPublicKey - DER-encoded public key bytes
|
|
15
|
+
* @returns Ethereum address
|
|
16
|
+
*/
|
|
17
|
+
export declare function derEncodedPublicKeyToEthAddress(derPublicKey: Uint8Array): string;
|
|
18
|
+
/**
|
|
19
|
+
* Parse DER-encoded signature to extract r and s values
|
|
20
|
+
* @param derSignature - DER-encoded signature
|
|
21
|
+
* @returns Object with r and s as hex strings
|
|
22
|
+
*/
|
|
23
|
+
export declare function getSigRS(derSignature: Uint8Array): {
|
|
24
|
+
r: string;
|
|
25
|
+
s: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Recover the v value for an ECDSA signature
|
|
29
|
+
* @param msgHash - Message hash that was signed
|
|
30
|
+
* @param r - r component of signature
|
|
31
|
+
* @param s - s component of signature
|
|
32
|
+
* @param expectedAddress - Expected Ethereum address
|
|
33
|
+
* @returns v value (27 or 28)
|
|
34
|
+
*/
|
|
35
|
+
export declare function getSigV(msgHash: string, r: string, s: string, expectedAddress: string): number;
|
|
36
|
+
/**
|
|
37
|
+
* Get complete signature (r, s, v) from DER-encoded KMS signature
|
|
38
|
+
* @param kmsSignature - DER-encoded signature from KMS
|
|
39
|
+
* @param msgHash - Message hash that was signed
|
|
40
|
+
* @param ethAddress - Expected Ethereum address
|
|
41
|
+
* @returns Complete signature object
|
|
42
|
+
*/
|
|
43
|
+
export declare function getSigRSV(kmsSignature: Uint8Array, msgHash: string, ethAddress: string): {
|
|
44
|
+
r: string;
|
|
45
|
+
s: string;
|
|
46
|
+
v: number;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Convert signature components to a single hex string
|
|
50
|
+
* @param r - r component
|
|
51
|
+
* @param s - s component
|
|
52
|
+
* @param v - v component
|
|
53
|
+
* @returns Signature as hex string
|
|
54
|
+
*/
|
|
55
|
+
export declare function signatureToHex(r: string, s: string, v: number): string;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.publicKeyIntToEthAddress = publicKeyIntToEthAddress;
|
|
4
|
+
exports.derEncodedPublicKeyToEthAddress = derEncodedPublicKeyToEthAddress;
|
|
5
|
+
exports.getSigRS = getSigRS;
|
|
6
|
+
exports.getSigV = getSigV;
|
|
7
|
+
exports.getSigRSV = getSigRSV;
|
|
8
|
+
exports.signatureToHex = signatureToHex;
|
|
9
|
+
const ethers_1 = require("ethers");
|
|
10
|
+
/**
|
|
11
|
+
* Cryptographic utilities for KMS signature handling
|
|
12
|
+
* Handles ECDSA signature conversion and Ethereum address derivation
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Convert a public key (as big integer coordinates) to an Ethereum address
|
|
16
|
+
* @param publicKeyX - X coordinate of public key
|
|
17
|
+
* @param publicKeyY - Y coordinate of public key
|
|
18
|
+
* @returns Ethereum address
|
|
19
|
+
*/
|
|
20
|
+
function publicKeyIntToEthAddress(publicKeyX, publicKeyY) {
|
|
21
|
+
// Concatenate 0x04 (uncompressed key prefix) + x + y
|
|
22
|
+
const xBytes = publicKeyX.toString(16).padStart(64, '0');
|
|
23
|
+
const yBytes = publicKeyY.toString(16).padStart(64, '0');
|
|
24
|
+
const publicKeyHex = '0x04' + xBytes + yBytes;
|
|
25
|
+
// Keccak256 hash
|
|
26
|
+
const hash = (0, ethers_1.keccak256)(publicKeyHex);
|
|
27
|
+
// Take last 20 bytes as address
|
|
28
|
+
return '0x' + hash.slice(-40);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Parse DER-encoded public key and convert to Ethereum address
|
|
32
|
+
* @param derPublicKey - DER-encoded public key bytes
|
|
33
|
+
* @returns Ethereum address
|
|
34
|
+
*/
|
|
35
|
+
function derEncodedPublicKeyToEthAddress(derPublicKey) {
|
|
36
|
+
// Parse the DER structure to extract the public key
|
|
37
|
+
// DER structure for ECDSA public key (simplified parsing)
|
|
38
|
+
// Find the public key bytes (starts with 0x04 for uncompressed key)
|
|
39
|
+
let publicKeyStart = -1;
|
|
40
|
+
for (let i = 0; i < derPublicKey.length - 64; i++) {
|
|
41
|
+
if (derPublicKey[i] === 0x04) {
|
|
42
|
+
publicKeyStart = i;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (publicKeyStart === -1) {
|
|
47
|
+
throw new Error('Could not find uncompressed public key in DER structure');
|
|
48
|
+
}
|
|
49
|
+
// Extract 65 bytes: 0x04 + 32 bytes X + 32 bytes Y
|
|
50
|
+
const publicKeyBytes = derPublicKey.slice(publicKeyStart, publicKeyStart + 65);
|
|
51
|
+
// Convert to hex
|
|
52
|
+
const publicKeyHex = '0x' + Buffer.from(publicKeyBytes).toString('hex');
|
|
53
|
+
// Keccak256 hash (skip the 0x04 prefix)
|
|
54
|
+
const hash = (0, ethers_1.keccak256)(publicKeyHex);
|
|
55
|
+
// Take last 20 bytes as address
|
|
56
|
+
return '0x' + hash.slice(-40);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Parse DER-encoded signature to extract r and s values
|
|
60
|
+
* @param derSignature - DER-encoded signature
|
|
61
|
+
* @returns Object with r and s as hex strings
|
|
62
|
+
*/
|
|
63
|
+
function getSigRS(derSignature) {
|
|
64
|
+
// DER signature structure:
|
|
65
|
+
// 0x30 [total-length] 0x02 [r-length] [r-bytes] 0x02 [s-length] [s-bytes]
|
|
66
|
+
let offset = 0;
|
|
67
|
+
// Check sequence tag
|
|
68
|
+
if (derSignature[offset++] !== 0x30) {
|
|
69
|
+
throw new Error('Invalid DER signature: missing sequence tag');
|
|
70
|
+
}
|
|
71
|
+
// Skip total length
|
|
72
|
+
offset++;
|
|
73
|
+
// Read r
|
|
74
|
+
if (derSignature[offset++] !== 0x02) {
|
|
75
|
+
throw new Error('Invalid DER signature: missing r integer tag');
|
|
76
|
+
}
|
|
77
|
+
const rLength = derSignature[offset++];
|
|
78
|
+
let rBytes = derSignature.slice(offset, offset + rLength);
|
|
79
|
+
offset += rLength;
|
|
80
|
+
// Remove leading zero if present (DER encoding adds it for positive numbers)
|
|
81
|
+
if (rBytes[0] === 0x00) {
|
|
82
|
+
rBytes = rBytes.slice(1);
|
|
83
|
+
}
|
|
84
|
+
const r = '0x' + Buffer.from(rBytes).toString('hex');
|
|
85
|
+
// Read s
|
|
86
|
+
if (derSignature[offset++] !== 0x02) {
|
|
87
|
+
throw new Error('Invalid DER signature: missing s integer tag');
|
|
88
|
+
}
|
|
89
|
+
const sLength = derSignature[offset++];
|
|
90
|
+
let sBytes = derSignature.slice(offset, offset + sLength);
|
|
91
|
+
// Remove leading zero if present
|
|
92
|
+
if (sBytes[0] === 0x00) {
|
|
93
|
+
sBytes = sBytes.slice(1);
|
|
94
|
+
}
|
|
95
|
+
const s = '0x' + Buffer.from(sBytes).toString('hex');
|
|
96
|
+
return { r, s };
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Recover the v value for an ECDSA signature
|
|
100
|
+
* @param msgHash - Message hash that was signed
|
|
101
|
+
* @param r - r component of signature
|
|
102
|
+
* @param s - s component of signature
|
|
103
|
+
* @param expectedAddress - Expected Ethereum address
|
|
104
|
+
* @returns v value (27 or 28)
|
|
105
|
+
*/
|
|
106
|
+
function getSigV(msgHash, r, s, expectedAddress) {
|
|
107
|
+
// Try both possible v values (27 and 28)
|
|
108
|
+
for (const v of [27, 28]) {
|
|
109
|
+
try {
|
|
110
|
+
const signature = {
|
|
111
|
+
r,
|
|
112
|
+
s,
|
|
113
|
+
v,
|
|
114
|
+
};
|
|
115
|
+
// Construct the signature string
|
|
116
|
+
const sigString = (0, ethers_1.concat)([
|
|
117
|
+
signature.r,
|
|
118
|
+
signature.s,
|
|
119
|
+
new Uint8Array([signature.v]),
|
|
120
|
+
]);
|
|
121
|
+
// Try to recover the address
|
|
122
|
+
const recoveredAddress = ethers_1.SigningKey.recoverPublicKey((0, ethers_1.getBytes)(msgHash), sigString);
|
|
123
|
+
const recoveredAddr = (0, ethers_1.keccak256)('0x' + recoveredAddress.slice(4));
|
|
124
|
+
const addr = '0x' + recoveredAddr.slice(-40);
|
|
125
|
+
if (addr.toLowerCase() === expectedAddress.toLowerCase()) {
|
|
126
|
+
return v;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
130
|
+
// Continue to next v value
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
throw new Error('Could not recover v value from signature');
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get complete signature (r, s, v) from DER-encoded KMS signature
|
|
138
|
+
* @param kmsSignature - DER-encoded signature from KMS
|
|
139
|
+
* @param msgHash - Message hash that was signed
|
|
140
|
+
* @param ethAddress - Expected Ethereum address
|
|
141
|
+
* @returns Complete signature object
|
|
142
|
+
*/
|
|
143
|
+
function getSigRSV(kmsSignature, msgHash, ethAddress) {
|
|
144
|
+
const { r, s } = getSigRS(kmsSignature);
|
|
145
|
+
const v = getSigV(msgHash, r, s, ethAddress);
|
|
146
|
+
return { r, s, v };
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Convert signature components to a single hex string
|
|
150
|
+
* @param r - r component
|
|
151
|
+
* @param s - s component
|
|
152
|
+
* @param v - v component
|
|
153
|
+
* @returns Signature as hex string
|
|
154
|
+
*/
|
|
155
|
+
function signatureToHex(r, s, v) {
|
|
156
|
+
const rHex = r.startsWith('0x') ? r.slice(2) : r;
|
|
157
|
+
const sHex = s.startsWith('0x') ? s.slice(2) : s;
|
|
158
|
+
const vHex = v.toString(16).padStart(2, '0');
|
|
159
|
+
return '0x' + rHex.padStart(64, '0') + sHex.padStart(64, '0') + vHex;
|
|
160
|
+
}
|