Skip to content

Commit

Permalink
refactor: approve with batch (#238)
Browse files Browse the repository at this point in the history
* refactor: use api directly from config, use try catch while fetching data

* refactor: update axelar schema to check lcd url

* chore: add more checks on reponse data

* chore: move response checks outside below try catch

* chore: renamed response var
  • Loading branch information
blockchainguyy authored May 23, 2024
1 parent d3012df commit 3c1c79c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
3 changes: 2 additions & 1 deletion axelar-chains-config/tests/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const axelarSchema = {
id: { type: 'string' },
axelarId: { type: 'string' },
rpc: { type: 'string' },
lcd: { type: 'string' },
// Matches for "" "http://example.com:443" "https://example.com:443" "https://example.com" "http://example.com"
lcd: { type: 'string', pattern: '^$|^(https?:\\/\\/[^\\/\\:]+(:\\d+)?)$' },
grpc: { type: 'string' },
tokenSymbol: { type: 'string' },
},
Expand Down
37 changes: 20 additions & 17 deletions evm/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,6 @@ const getSignedWeightedExecuteInput = async (data, operators, weights, threshold
);
};

const fetchBatchData = async (apiUrl, batchId) => {
try {
const response = await httpGet(`${apiUrl}/${batchId}`);
const data = response?.execute_data;

return '0x' + data;
} catch (error) {
throw new Error(`Failed to fetch batch data: ${error.message}`);
}
};

async function processCommand(config, chain, options) {
const { privateKey, address, action, yes } = options;

Expand Down Expand Up @@ -199,26 +188,40 @@ async function processCommand(config, chain, options) {
}

case 'approveWithBatch': {
const { batchID, api } = options;
const { batchID } = options;

if (!batchID) {
throw new Error('Batch ID is required for the approve action');
}

const batchId = batchID.startsWith('0x') ? batchID.substring(2) : batchID;
let apiUrl = api || `${config.axelar.lcd}/axelar/evm/v1beta1/batched_commands/${chain.name.toLowerCase()}`;
apiUrl = apiUrl.endsWith('/') ? apiUrl.slice(0, -1) : apiUrl;
const apiUrl = `${config.axelar.lcd}/axelar/evm/v1beta1/batched_commands/${chain.axelarId}/${batchId}`;

const executeData = await fetchBatchData(apiUrl, batchId);
let executeData, response;

try {
response = await httpGet(`${apiUrl}`);
executeData = '0x' + response.execute_data;
} catch (error) {
throw new Error(`Failed to fetch batch data: ${error.message}`);
}

if (response == null || !response.execute_data) {
throw new Error('Response does not contain execute_data');
}

if (response.status !== 'BATCHED_COMMANDS_STATUS_SIGNED') {
throw new Error('Data is not yet signed by operators');
}

const tx = {
to: gatewayAddress,
data: executeData,
...gasOptions,
};

const response = await wallet.sendTransaction(tx);
printInfo('Approve tx', response.hash);
const txResponse = await wallet.sendTransaction(tx);
printInfo('Approve tx', txResponse.hash);

const receipt = await response.wait(chain.confirmations);
const eventEmitted = wasEventEmitted(receipt, gateway, 'ContractCallApproved');
Expand Down

0 comments on commit 3c1c79c

Please sign in to comment.