From 889c16a479d58b04a37599a43dd4a7804908d237 Mon Sep 17 00:00:00 2001 From: Asa Oines Date: Mon, 25 Jul 2022 11:57:46 -0400 Subject: [PATCH] Integrate the InterchainGasCalculator into Kathy (#818) --- typescript/helloworld/src/app/app.ts | 4 +- typescript/infra/scripts/helloworld/kathy.ts | 39 +++++++++++++++----- typescript/sdk/src/gas/calculator.ts | 15 +++++++- typescript/sdk/src/index.ts | 2 +- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/typescript/helloworld/src/app/app.ts b/typescript/helloworld/src/app/app.ts index b6c8a4a1d..be7e03782 100644 --- a/typescript/helloworld/src/app/app.ts +++ b/typescript/helloworld/src/app/app.ts @@ -1,4 +1,4 @@ -import { ethers } from 'ethers'; +import { BigNumber, ethers } from 'ethers'; import { TypedListener } from '@abacus-network/core/dist/common'; import { @@ -19,6 +19,7 @@ export class HelloWorldApp< from: From, to: Remotes, message: string, + value: BigNumber, receiveHandler?: TypedListener, ): Promise { const sender = this.getContracts(from).router; @@ -36,6 +37,7 @@ export class HelloWorldApp< const tx = await sender.sendHelloWorld(toDomain, message, { ...chainConnection.overrides, gasLimit, + value, }); const receipt = await tx.wait(chainConnection.confirmations); diff --git a/typescript/infra/scripts/helloworld/kathy.ts b/typescript/infra/scripts/helloworld/kathy.ts index 411dbd560..91393efc2 100644 --- a/typescript/infra/scripts/helloworld/kathy.ts +++ b/typescript/infra/scripts/helloworld/kathy.ts @@ -1,7 +1,11 @@ import { Gauge, Registry } from 'prom-client'; import { HelloWorldApp } from '@abacus-network/helloworld'; -import { ChainName, Chains } from '@abacus-network/sdk'; +import { + ChainName, + Chains, + InterchainGasCalculator, +} from '@abacus-network/sdk'; import { submitMetrics } from '../../src/utils/metrics'; import { sleep } from '../../src/utils/utils'; @@ -33,6 +37,11 @@ async function main() { constMetricLabels.abacus_deployment = environment; const coreConfig = getCoreEnvironmentConfig(environment); const app = await getApp(coreConfig); + const multiProvider = await coreConfig.getMultiProvider(); + const gasCalc = InterchainGasCalculator.fromEnvironment( + environment, + multiProvider as any, + ); const chains = app.chains() as Chains[]; const skip = process.env.CHAINS_TO_SKIP?.split(',').filter( (skipChain) => skipChain.length > 0, @@ -47,7 +56,7 @@ async function main() { let failureOccurred = false; - const sources = chains.filter((chain) => !skip || !skip.includes(chain)); + const origins = chains.filter((chain) => !skip || !skip.includes(chain)); // submit frequently so we don't have to wait a super long time for info to get into the metrics const metricsInterval = setInterval(() => { @@ -56,19 +65,19 @@ async function main() { ); }, 1000 * 30); - for (const source of sources) { - for (const destination of sources.filter((d) => d !== source)) { + for (const origin of origins) { + for (const destination of origins.filter((d) => d !== origin)) { const labels = { - origin: source, + origin, remote: destination, ...constMetricLabels, }; try { - await sendMessage(app, source, destination); + await sendMessage(app, origin, destination, gasCalc); messagesSendStatus.labels({ ...labels }).set(1); } catch (err) { console.error( - `Error sending message from ${source} to ${destination}, continuing...`, + `Error sending message from ${origin} to ${destination}, continuing...`, `${err}`.replaceAll('\n', ' ## '), ); failureOccurred = true; @@ -92,11 +101,21 @@ async function main() { async function sendMessage( app: HelloWorldApp, - source: ChainName, + origin: ChainName, destination: ChainName, + gasCalc: InterchainGasCalculator, ) { - console.log(`Sending message from ${source} to ${destination}`); - const receipt = await app.sendHelloWorld(source, destination, `Hello!`); + const msg = 'Hello!'; + const expected = { + origin, + destination, + sender: app.getContracts(origin).router.address, + recipient: app.getContracts(destination).router.address, + body: msg, + }; + const value = await gasCalc.estimatePaymentForMessage(expected); + console.log(`Sending message from ${origin} to ${destination}`); + const receipt = await app.sendHelloWorld(origin, destination, msg, value); console.log(JSON.stringify(receipt.events || receipt.logs)); } diff --git a/typescript/sdk/src/gas/calculator.ts b/typescript/sdk/src/gas/calculator.ts index 66a51f40d..a0136e774 100644 --- a/typescript/sdk/src/gas/calculator.ts +++ b/typescript/sdk/src/gas/calculator.ts @@ -4,7 +4,11 @@ import { BigNumber, FixedNumber, ethers } from 'ethers'; import { utils } from '@abacus-network/utils'; import { chainMetadata } from '../consts/chainMetadata'; -import { AbacusCore } from '../core/AbacusCore'; +import { + AbacusCore, + CoreEnvironment, + CoreEnvironmentChain, +} from '../core/AbacusCore'; import { MultiProvider } from '../providers/MultiProvider'; import { ChainName, Remotes } from '../types'; import { convertDecimalValue, mulBigAndFixed } from '../utils/number'; @@ -86,6 +90,15 @@ export class InterchainGasCalculator { private paymentEstimateMultiplier: ethers.FixedNumber; private messageGasEstimateBuffer: ethers.BigNumber; + static fromEnvironment( + env: Env, + multiProvider: MultiProvider>, + config?: InterchainGasCalculatorConfig, + ): InterchainGasCalculator> { + const core = AbacusCore.fromEnvironment(env, multiProvider); + return new InterchainGasCalculator(multiProvider, core, config); + } + constructor( multiProvider: MultiProvider, core: AbacusCore, diff --git a/typescript/sdk/src/index.ts b/typescript/sdk/src/index.ts index 7b9a176b9..2209f4240 100644 --- a/typescript/sdk/src/index.ts +++ b/typescript/sdk/src/index.ts @@ -71,7 +71,7 @@ export { } from './core/TestCoreApp'; export { TestCoreDeployer } from './core/TestCoreDeployer'; -export { InterchainGasCalculator } from './gas/calculator'; +export { InterchainGasCalculator, ParsedMessage } from './gas/calculator'; export { CoinGeckoTokenPriceGetter, TokenPriceGetter,