Skip to content

Commit 85f914f

Browse files
committed
query rate limit duration
1 parent 3b9de96 commit 85f914f

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

wormhole-connect/src/routes/ntt/consts.ts

-2
This file was deleted.

wormhole-connect/src/routes/ntt/nttBase.ts

+7
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@ export abstract class NttBase extends BaseRoute {
294294
).getCurrentInboundCapacity(fromChain);
295295
}
296296

297+
async getRateLimitDuration(
298+
chain: ChainId | ChainName,
299+
nttManagerAddress: string,
300+
): Promise<number> {
301+
return await getNttManager(chain, nttManagerAddress).getRateLimitDuration();
302+
}
303+
297304
async getInboundQueuedTransfer(
298305
chain: ChainName | ChainId,
299306
nttManagerAddress: string,

wormhole-connect/src/routes/ntt/platforms/evm/nttManager.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
NotEnoughCapacityError,
1717
ContractIsPausedError,
1818
} from '../../errors';
19-
import { RATE_LIMIT_DURATION } from 'routes/ntt/consts';
2019
import {
2120
encodeTransceiverInstructions,
2221
encodeWormholeTransceiverInstruction,
@@ -137,6 +136,10 @@ export class NttManagerEvm {
137136
).toString();
138137
}
139138

139+
async getRateLimitDuration(): Promise<number> {
140+
return (await this.nttManager.rateLimitDuration()).toNumber();
141+
}
142+
140143
async getInboundQueuedTransfer(
141144
emitterChain: ChainName | ChainId,
142145
nttManagerMessage: NttManagerMessage<NativeTokenTransfer>,
@@ -147,12 +150,11 @@ export class NttManagerEvm {
147150
);
148151
if (queuedTransfer.txTimestamp.gt(0)) {
149152
const { recipient, amount, txTimestamp } = queuedTransfer;
153+
const duration = await this.getRateLimitDuration();
150154
return {
151155
recipient: wh.parseAddress(recipient, this.chain),
152156
amount: amount.toString(),
153-
rateLimitExpiryTimestamp: txTimestamp
154-
.add(RATE_LIMIT_DURATION)
155-
.toNumber(),
157+
rateLimitExpiryTimestamp: txTimestamp.add(duration).toNumber(),
156158
};
157159
}
158160
return undefined;

wormhole-connect/src/routes/ntt/platforms/solana/nttManager.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import {
1414
createAssociatedTokenAccountInstruction,
1515
} from '@solana/spl-token';
1616
import { BN } from '@coral-xyz/anchor';
17-
import { RATE_LIMIT_DURATION } from 'routes/ntt/consts';
1817
import { parseVaa } from '@certusone/wormhole-sdk/lib/esm';
1918
import { utils } from 'ethers';
2019
import { WormholeTransceiverMessage } from '../../payloads/wormhole';
2120
import { NttManagerMessage } from '../../payloads/common';
2221
import { NativeTokenTransfer } from '../../payloads/transfers';
2322

23+
// TODO: make sure this is in sync with the contract
24+
const RATE_LIMIT_DURATION = 24 * 60 * 60;
25+
2426
export class NttManagerSolana {
2527
readonly ntt: NTT;
2628
readonly connection: Connection;
@@ -198,7 +200,11 @@ export class NttManagerSolana {
198200
const {
199201
rateLimit: { limit, capacityAtLastTx, lastTxTimestamp },
200202
} = await this.ntt.getOutboxRateLimit();
201-
return this.getCurrentCapacity(limit, capacityAtLastTx, lastTxTimestamp);
203+
return await this.getCurrentCapacity(
204+
limit,
205+
capacityAtLastTx,
206+
lastTxTimestamp,
207+
);
202208
}
203209

204210
async getCurrentInboundCapacity(
@@ -207,22 +213,38 @@ export class NttManagerSolana {
207213
const {
208214
rateLimit: { limit, capacityAtLastTx, lastTxTimestamp },
209215
} = await this.ntt.getInboxRateLimit(fromChain);
210-
return this.getCurrentCapacity(limit, capacityAtLastTx, lastTxTimestamp);
216+
return await this.getCurrentCapacity(
217+
limit,
218+
capacityAtLastTx,
219+
lastTxTimestamp,
220+
);
211221
}
212222

213-
getCurrentCapacity(limit: BN, capacityAtLastTx: BN, lastTxTimestamp: BN) {
223+
async getCurrentCapacity(
224+
limit: BN,
225+
capacityAtLastTx: BN,
226+
lastTxTimestamp: BN,
227+
) {
214228
const timePassed = BN.max(
215229
new BN(Date.now() / 1000).sub(lastTxTimestamp),
216230
new BN(0),
217231
);
232+
const duration = await this.getRateLimitDuration();
218233
const calculatedCapacity = capacityAtLastTx.add(
219-
timePassed.mul(limit).div(new BN(RATE_LIMIT_DURATION)),
234+
timePassed.mul(limit).div(new BN(duration)),
220235
);
221236
return calculatedCapacity.lt(limit)
222237
? calculatedCapacity.toString()
223238
: limit.toString();
224239
}
225240

241+
async getRateLimitDuration(): Promise<number> {
242+
// TODO: how will solana implement this?
243+
// const config = await this.ntt.getConfig();
244+
// return config.rateLimitDuration.toNumber();
245+
return RATE_LIMIT_DURATION;
246+
}
247+
226248
async getInboundQueuedTransfer(
227249
emitterChain: ChainName | ChainId,
228250
managerMessage: NttManagerMessage<NativeTokenTransfer>,

wormhole-connect/src/views/Bridge/NttInboundCapacityWarning.tsx

+21-5
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,52 @@ const NttInboundCapacityWarning = ({
2323
fromChain,
2424
}: Props) => {
2525
const [capacity, setCapacity] = useState<BigNumber | undefined>(undefined);
26-
const managerAddress = TOKENS[token]?.ntt?.nttManager;
26+
const [duration, setDuration] = useState<number>(0);
27+
const nttManagerAddress = TOKENS[token]?.ntt?.nttManager;
2728

2829
useEffect(() => {
29-
if (!chain || !managerAddress || !fromChain) return;
30+
if (!chain || !nttManagerAddress || !fromChain) return;
3031
let active = true;
3132
const fetchCapacity = async () => {
3233
try {
3334
const ntt = new NttManual();
35+
const duration = await ntt.getRateLimitDuration(
36+
chain,
37+
nttManagerAddress,
38+
);
39+
// if the rate limit duration 0, then rate limiting is disabled
40+
if (duration === 0) {
41+
if (active) {
42+
setCapacity(undefined);
43+
setDuration(0);
44+
}
45+
return;
46+
}
3447
const capacity = await ntt.getCurrentInboundCapacity(
3548
chain,
36-
managerAddress,
49+
nttManagerAddress,
3750
fromChain,
3851
);
3952
if (active) {
4053
setCapacity(capacity ? BigNumber.from(capacity) : undefined);
54+
setDuration(duration);
4155
}
4256
} catch (error) {
4357
console.error('Failed to fetch capacity:', error);
4458
if (active) {
4559
setCapacity(undefined);
60+
setDuration(0);
4661
}
4762
}
4863
};
4964
fetchCapacity();
5065
return () => {
5166
active = false;
5267
};
53-
}, [chain, managerAddress, fromChain]);
68+
}, [chain, nttManagerAddress, fromChain]);
5469

5570
const showWarning = useMemo(() => {
56-
if (!token || !amount || !chain || !capacity) return;
71+
if (!token || !amount || !chain || !capacity || !duration) return;
5772
const destTokenKey = getNativeVersionOfToken(TOKENS[token].symbol, chain);
5873
const destToken = TOKENS[destTokenKey];
5974
if (!destToken) return;
@@ -68,6 +83,7 @@ const NttInboundCapacityWarning = ({
6883

6984
if (!showWarning) return null;
7085

86+
// TODO: change 24 hours?
7187
const content = (
7288
<>
7389
{`Your transfer to ${

0 commit comments

Comments
 (0)