Skip to content

Commit

Permalink
fixup! tools: makeIBCTransferMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Jul 17, 2024
1 parent 36f2cf6 commit 5226842
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
31 changes: 31 additions & 0 deletions multichain-testing/test/tools/ibc-transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import anyTest from '@endo/ses-ava/prepare-endo.js';
import type { TestFn } from 'ava';
import { getTimeout } from '../../tools/ibc-transfer.js';
import {
NANOSECONDS_PER_MILLISECOND,
SECONDS_PER_MINUTE,
MILLISECONDS_PER_SECOND,
} from '@agoric/orchestration/src/utils/time.js';

const test = anyTest as TestFn<Record<string, never>>;

const minutesInFuture = (now: bigint, minutes = 5n) =>
now + minutes * SECONDS_PER_MINUTE * MILLISECONDS_PER_SECOND;

test('getTimeout returns nanoseconds 5 minutes in the future', async t => {
const now = Date.now();
const fiveMinutesInFuture = minutesInFuture(BigInt(now));

const timeout = getTimeout(now);
const timeoutInMS = timeout / NANOSECONDS_PER_MILLISECOND;
t.is(fiveMinutesInFuture, timeoutInMS);
});

test('getTimeout accepts minutes in future for 2nd arg', async t => {
const now = Date.now();
const twoMinutesInFuture = minutesInFuture(BigInt(now), 2n);

const timeout = getTimeout(now, 2n);
const timeoutInMS = timeout / NANOSECONDS_PER_MILLISECOND;
t.is(twoMinutesInFuture, timeoutInMS);
});
32 changes: 17 additions & 15 deletions multichain-testing/tools/ibc-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import type {
IBCMsgTransferOptions,
} from '@agoric/orchestration';
import {
MILLISECONDS_PER_SECOND,
NANOSECONDS_PER_MILLISECOND,
NANOSECONDS_PER_SECOND,
SECONDS_PER_MINUTE,
} from '@agoric/orchestration/src/utils/time.js';
import { MsgTransfer } from '@agoric/cosmic-proto/ibc/applications/transfer/v1/tx.js';
import { createWallet } from './wallet.js';
Expand All @@ -36,6 +37,21 @@ type SimpleChainAddress = {
chainName: string;
};

/**
* @param {number} ms current time in ms (e.g. Date.now())
* @param {bigint} [minutes=5n] number of minutes in the future
* @returns {bigint} nanosecond timestamp 5 mins in the future */
export const getTimeout = (ms: number, minutes = 5n) => {
console.debug('Current Time (ms):', ms);
console.debug('Current Time (ISO):', new Date(ms).toISOString());
const timeoutMS =
BigInt(ms) + MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE * minutes;
console.debug('Timeout Time (ms):', timeoutMS);
const timeoutNS = timeoutMS * NANOSECONDS_PER_MILLISECOND;
console.debug('Timeout Time (ns):', timeoutNS);
return timeoutNS;
};

export const makeIBCTransferMsg = (
amount: DenomAmount,
destination: SimpleChainAddress,
Expand All @@ -59,20 +75,6 @@ export const makeIBCTransferMsg = (
const { counterPartyPortId, counterPartyChannelId } =
connection.transferChannel;

/**
* @param {number} ms current time in ms (e.g. Date.now())
* @param {bigint} [minutes=5n] number of minutes in the future
* @returns {bigint} nanosecond timestamp 5 mins in the future */
const getTimeout = (ms: number, minutes = 5n) => {
console.debug('Current Time (ms):', ms);
console.debug('Current Time (ISO):', new Date(ms).toISOString());
const timeoutMS = BigInt(ms) + NANOSECONDS_PER_SECOND * minutes;
console.debug('Timeout Time (ms):', timeoutMS);
const timeoutNS = timeoutMS * NANOSECONDS_PER_MILLISECOND;
console.debug('Timeout Time (ns):', timeoutNS);
return timeoutNS;
};

const msgTransfer = MsgTransfer.fromPartial({
sender: sender.address,
receiver: destination.address,
Expand Down

0 comments on commit 5226842

Please sign in to comment.