Skip to content

Commit

Permalink
feat(GasService): using internal TX relay API (#242)
Browse files Browse the repository at this point in the history
* feat(GasService): using internal TX relay API

* refactor(GasService): reusing relaying logic

* Update evm/gas-service.js

Co-authored-by: Milap Sheth <[email protected]>

* Update evm/utils.js

Co-authored-by: Milap Sheth <[email protected]>

* refactor(GasService): relaying logic

* fix(GasService): relay flag for operators script

---------

Co-authored-by: Milap Sheth <[email protected]>
  • Loading branch information
re1ro and milapsheth authored May 30, 2024
1 parent 9136b77 commit aef9eda
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 35 deletions.
31 changes: 12 additions & 19 deletions evm/gas-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ const {
prompt,
getContractJSON,
getGasOptions,
wasEventEmitted,
isValidAddress,
validateParameters,
httpPost,
toBigNumberString,
timeout,
relayTransaction,
} = require('./utils');
const { addBaseOptions } = require('./cli-utils');
const { getWallet } = require('./sign-utils');
Expand Down Expand Up @@ -304,25 +304,16 @@ async function processCommand(config, chain, options) {
}

try {
const tx = await timeout(
gasService.updateGasInfo(chainsToUpdate, gasInfoUpdates, gasOptions),
chain.timeout || 60000,
new Error(`Timeout updating gas info for ${chain.name}`),
await relayTransaction(
options,
chain,
gasService,
'updateGasInfo',
[chainsToUpdate, gasInfoUpdates],
0,
gasOptions,
'GasInfoUpdated',
);

printInfo('TX', tx.hash);

const receipt = await timeout(
tx.wait(chain.confirmations),
chain.timeout || 60000,
new Error(`Timeout updating gas info for ${chain.name}`),
);

const eventEmitted = wasEventEmitted(receipt, gasService, 'GasInfoUpdated');

if (!eventEmitted) {
printWarn('Event not emitted in receipt.');
}
} catch (error) {
for (let i = 0; i < chainsToUpdate.length; i++) {
addFailedChainUpdate(chain.name, chainsToUpdate[i]);
Expand Down Expand Up @@ -368,6 +359,7 @@ if (require.main === module) {

// options for updateGasInfo
program.addOption(new Option('--chains <chains...>', 'Chain names'));
program.addOption(new Option('--relayerAPI <relayerAPI>', 'Relay the tx through an external relayer API').env('RELAYER_API'));

program.action((options) => {
main(options);
Expand All @@ -379,3 +371,4 @@ if (require.main === module) {
exports.getGasUpdates = getGasUpdates;
exports.addFailedChainUpdate = addFailedChainUpdate;
exports.printFailedChainUpdates = printFailedChainUpdates;
exports.relayTransaction = relayTransaction;
30 changes: 14 additions & 16 deletions evm/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const { ethers } = require('hardhat');
const {
Wallet,
getDefaultProvider,
utils: { isAddress, Interface },
Contract,
Expand All @@ -23,10 +22,10 @@ const {
validateParameters,
getContractJSON,
printWarn,
timeout,
} = require('./utils');
const { addBaseOptions } = require('./cli-utils');
const { getGasUpdates, printFailedChainUpdates, addFailedChainUpdate } = require('./gas-service');
const { getGasUpdates, printFailedChainUpdates, addFailedChainUpdate, relayTransaction } = require('./gas-service');
const { getWallet } = require('./sign-utils');

async function processCommand(config, chain, options) {
const {
Expand Down Expand Up @@ -62,8 +61,8 @@ async function processCommand(config, chain, options) {
const rpc = chain.rpc;
const provider = getDefaultProvider(rpc);

const wallet = new Wallet(privateKey, provider);
await printWalletInfo(wallet);
const wallet = await getWallet(privateKey, provider, options);
await printWalletInfo(wallet, options);

printInfo('Contract name', contractName);

Expand Down Expand Up @@ -268,17 +267,14 @@ async function processCommand(config, chain, options) {
const updateGasInfoCalldata = gasServiceInterface.encodeFunctionData('updateGasInfo', [chainsToUpdate, gasInfoUpdates]);

try {
const tx = await timeout(
operatorsContract.executeContract(target, updateGasInfoCalldata, 0, gasOptions),
chain.timeout || 60000,
new Error(`Timeout updating gas info for ${chain.name}`),
);
printInfo('TX', tx.hash);

await timeout(
tx.wait(chain.confirmations),
chain.timeout || 60000,
new Error(`Timeout updating gas info for ${chain.name}`),
await relayTransaction(
options,
chain,
operatorsContract,
'executeContract',
[target, updateGasInfoCalldata, 0],
0,
gasOptions,
);
} catch (error) {
for (let i = 0; i < chainsToUpdate.length; i++) {
Expand Down Expand Up @@ -321,10 +317,12 @@ if (require.main === module) {
'updateGasInfo',
]),
);
program.addOption(new Option('--offline', 'run script in offline mode'));
program.addOption(new Option('--args <args>', 'operator action arguments'));

// options for updateGasInfo
program.addOption(new Option('--chains <chains...>', 'Chain names'));
program.addOption(new Option('--relayerAPI <relayerAPI>', 'Relay the tx through an external relayer API').env('RELAYER_API'));

program.action((options) => {
main(options);
Expand Down
35 changes: 35 additions & 0 deletions evm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,40 @@ function timeout(prom, time, exception) {
);
}

async function relayTransaction(options, chain, contract, method, params, nativeValue = 0, gasOptions = {}, expectedEvent = null) {
if (options.relayerAPI) {
const result = await httpPost(options.relayerAPI, {
chain: chain.axelarId,
to: contract.address,
call_data: contract.interface.encodeFunctionData(method, params),
value: nativeValue,
});

printInfo('Relay ID', result.relay_id);
return;
}

await timeout(
(async () => {
const tx = await contract[method](...params, gasOptions);
printInfo('Tx hash', tx.hash);

const receipt = await tx.wait(chain.confirmations);

if (expectedEvent) {
const eventEmitted = wasEventEmitted(receipt, contract, expectedEvent);

if (!eventEmitted) {
printWarn('Event not emitted in receipt.');
}
}
})(),

chain.timeout || 60000,
new Error(`Timeout updating gas info for ${chain.name}`),
);
}

module.exports = {
deployCreate,
deployCreate2,
Expand Down Expand Up @@ -1258,4 +1292,5 @@ module.exports = {
timeout,
getAmplifierKeyAddresses,
getContractConfig,
relayTransaction,
};

0 comments on commit aef9eda

Please sign in to comment.