Skip to content

Commit 7dd7769

Browse files
committed
fixup! chore: added gas estimation api to planner
1 parent b3a35eb commit 7dd7769

File tree

5 files changed

+103
-2056
lines changed

5 files changed

+103
-2056
lines changed

packages/portfolio-contract/tools/portfolio-actors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ export const planTransfer = (
348348
dest: PoolKey,
349349
amount: NatAmount,
350350
feeBrand: Brand<'nat'>,
351-
gmpFees: { acct: bigint; wallet: bigint; return: bigint},
351+
gmpFees: { acct: bigint; wallet: bigint; return: bigint },
352352
): MovementDesc[] => {
353353
const { protocol: p, chainName: evm } = PoolPlaces[dest];
354354
const steps: MovementDesc[] = [];
@@ -368,7 +368,7 @@ export const planTransfer = (
368368
dest: `@${evm}`,
369369
amount,
370370
fee: make(feeBrand, gmpFees.acct),
371-
detail: {evmFee: gmpFees.return}
371+
detail: { evmGas: gmpFees.return },
372372
});
373373
steps.push({
374374
src: `@${evm}`,

services/ymax-planner/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"author": "Agoric",
2727
"license": "Apache-2.0",
2828
"devDependencies": {
29-
"@types/keccak": "^3",
3029
"@types/node": "^22.10.2",
3130
"@types/ws": "^8",
3231
"ava": "^6.3.0",
@@ -42,7 +41,6 @@
4241
"@agoric/ertp": "workspace:*",
4342
"@agoric/internal": "workspace:*",
4443
"@agoric/orchestration": "workspace:*",
45-
"@axelar-network/axelarjs-sdk": "^0.17.7",
4644
"@cosmjs/crypto": "^0.36.0",
4745
"@cosmjs/encoding": "^0.36.0",
4846
"@cosmjs/proto-signing": "^0.36.0",
@@ -55,12 +53,10 @@
5553
"@endo/patterns": "^1.7.0",
5654
"@endo/promise-kit": "^1.1.13",
5755
"@google-cloud/secret-manager": "^6.1.0",
58-
"@metamask/abi-utils": "^3.0.0",
5956
"chain-registry": "^2.0.44",
6057
"dotenv": "^17.2.1",
6158
"ethers": "^6.13.0",
6259
"json-rpc-2.0": "^1.7.1",
63-
"keccak": "^3.0.4",
6460
"ky": "^1.4.0",
6561
"ts-blank-space": "^0.6.1",
6662
"ws": "^8.18.3"

services/ymax-planner/src/gas-estimation.ts

Lines changed: 57 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,49 @@
1-
import { AxelarQueryAPI, Environment } from '@axelar-network/axelarjs-sdk';
1+
import { buildGMPPayload } from '@agoric/orchestration/src/utils/gmp.js';
22
import { ethers } from 'ethers';
3-
import { fromHex } from '@cosmjs/encoding';
4-
import { encodeAbiParameters } from 'viem';
5-
import createKeccakHash from 'keccak';
6-
import { encode } from '@metamask/abi-utils';
73

84
const GAS_ESTIMATOR_CONTRACT = '0x0010F196F5CD0314f68FF665b8c8eD93531112Fe';
5+
const COMMAND_ID =
6+
'0xddea323337dfb73c82df7393d76b2f38835d5c2bda0123c47d1a2fc06527d19f';
97

10-
const hexToBytes = (hex: string) => fromHex(hex.slice(2));
8+
const bytesToHex = (bytes: number[]): string =>
9+
'0x' + bytes.map(n => n.toString(16).padStart(2, '0')).join('');
1110

12-
const uint8ArrayToHex = (uint8Array: Uint8Array): string => {
13-
return `0x${Array.from(uint8Array)
14-
.map(byte => byte.toString(16).padStart(2, '0'))
15-
.join('')}`;
11+
const vars = {
12+
mainnet: {
13+
AXELAR_GAS_API: 'https://api.axelarscan.io/gmp/estimateGasFee',
14+
RPC: {
15+
Avalanche: 'https://api.avax.network/ext/bc/C/rpc',
16+
},
17+
},
18+
testnet: {
19+
AXELAR_GAS_API: 'https://testnet.api.axelarscan.io/gmp/estimateGasFee',
20+
RPC: {
21+
Avalanche: 'https://api.avax-test.network/ext/bc/C/rpc',
22+
},
23+
},
1624
};
1725

18-
const pack = (
19-
functionSignature: string,
20-
paramTypes: Array<string>,
21-
params: Array<string>,
22-
): `0x${string}` => {
23-
const functionHash = createKeccakHash('keccak256')
24-
.update(functionSignature)
25-
.digest();
26-
return uint8ArrayToHex(
27-
Uint8Array.from([
28-
...Uint8Array.from(functionHash.subarray(0, 4)),
29-
...encode(paramTypes, params),
30-
]),
31-
) as `0x${string}`;
32-
};
26+
const params = vars['testnet'];
3327

34-
const generatePayload = (
35-
id: string,
36-
contractAddress: string,
37-
payment: string,
38-
) => {
39-
const abiEncodedContractCalls = [
28+
const generatePayload = (contractAddress: string, payment: string) => {
29+
const contractCalls = [
4030
{
4131
target: '0x5425890298aed601595a70AB815c96711a31Bc65' as `0x${string}`,
42-
data: pack(
43-
'approve(address,uint256)',
44-
['address', 'uint256'],
45-
['0x8B9b2AF4afB389b4a70A474dfD4AdCD4a302bb40', payment],
46-
),
32+
functionSignature: 'approve(address,uint256)',
33+
args: ['0x8B9b2AF4afB389b4a70A474dfD4AdCD4a302bb40', payment],
4734
},
4835
{
4936
target: '0x8B9b2AF4afB389b4a70A474dfD4AdCD4a302bb40' as `0x${string}`,
50-
data: pack(
51-
'supply(address,uint256,address,uint16)',
52-
['address', 'uint256', 'address', 'uint16'],
53-
[
54-
'0x5425890298aed601595a70AB815c96711a31Bc65',
55-
payment,
56-
contractAddress,
57-
'0',
58-
],
59-
),
37+
functionSignature: 'supply(address,uint256,address,uint16)',
38+
args: [
39+
'0x5425890298aed601595a70AB815c96711a31Bc65',
40+
payment,
41+
contractAddress,
42+
'0',
43+
],
6044
},
6145
];
62-
const payload = encodeAbiParameters(
63-
[
64-
{
65-
type: 'tuple',
66-
name: 'callMessage',
67-
components: [
68-
{ name: 'id', type: 'string' },
69-
{
70-
name: 'calls',
71-
type: 'tuple[]',
72-
components: [
73-
{ name: 'target', type: 'address' },
74-
{ name: 'data', type: 'bytes' },
75-
],
76-
},
77-
],
78-
},
79-
],
80-
[{ id, calls: abiEncodedContractCalls }],
81-
);
82-
return payload;
46+
return bytesToHex(buildGMPPayload(contractCalls));
8347
};
8448

8549
const queryAxelarGasAPI = async (
@@ -88,18 +52,26 @@ const queryAxelarGasAPI = async (
8852
gasLimit: bigint,
8953
gasToken?: string,
9054
) => {
91-
const api = new AxelarQueryAPI({ environment: Environment.TESTNET });
92-
93-
const GAS_MULTIPLIER = 1;
94-
95-
const feeQuote = await api.estimateGasFee(
96-
sourceChain,
97-
destinationChain, // mayber lowercase?
98-
gasLimit,
99-
GAS_MULTIPLIER,
100-
gasToken,
101-
);
102-
return feeQuote;
55+
const response = await fetch(params.AXELAR_GAS_API, {
56+
method: 'POST',
57+
headers: {
58+
'Content-Type': 'application/json',
59+
},
60+
body: JSON.stringify({
61+
sourceChain,
62+
destinationChain,
63+
gasLimit: gasLimit.toString(),
64+
sourceTokenSymbol: gasToken,
65+
gasMultiplier: '1',
66+
}),
67+
});
68+
69+
if (!response.ok) {
70+
throw new Error(`HTTP error! status: ${response.status}`);
71+
}
72+
73+
const data = await response.json();
74+
return data;
10375
};
10476

10577
export const getReturnFeeEstimate = async (chainName: string) => {
@@ -151,22 +123,15 @@ const createGasEstimator = (rpcUrl: string, contractAddress: string) => {
151123
};
152124

153125
const estimateContracts = {
154-
Avalanche: createGasEstimator(
155-
'https://api.avax-test.network/ext/bc/C/rpc',
156-
GAS_ESTIMATOR_CONTRACT,
157-
),
126+
Avalanche: createGasEstimator(params.RPC.Avalanche, GAS_ESTIMATOR_CONTRACT),
158127
};
159128

160129
export const getWalletEstimate = async (chainName: string) => {
161130
const estimateContract = estimateContracts[chainName];
162-
const txId = 'tx0';
163-
const payloadString = generatePayload(txId, GAS_ESTIMATOR_CONTRACT, '5');
164-
const payload = Array.from(hexToBytes(payloadString));
165-
const commandId =
166-
'0xddea323337dfb73c82df7393d76b2f38835d5c2bda0123c47d1a2fc06527d19f';
131+
const payload = generatePayload(GAS_ESTIMATOR_CONTRACT, '5');
167132

168-
const gasLimit = await estimateContract('executeFactory', [
169-
commandId,
133+
const gasLimit = await estimateContract('executeWallet', [
134+
COMMAND_ID,
170135
'agoric',
171136
'agoric1owner',
172137
payload,
@@ -180,15 +145,14 @@ export const getWalletEstimate = async (chainName: string) => {
180145
)) as string;
181146
return BigInt(estimate);
182147
};
148+
183149
export const getFactoryContractEstimate = async (chainName: string) => {
184150
const estimateContract = estimateContracts[chainName];
185151

186152
const payload = ethers.AbiCoder.defaultAbiCoder().encode(['uint256'], [0]);
187-
const commandId =
188-
'0xddea323337dfb73c82df7393d76b2f38835d5c2bda0123c47d1a2fc06527d19f';
189153

190154
const gasLimit = await estimateContract('executeFactory', [
191-
commandId,
155+
COMMAND_ID,
192156
'agoric',
193157
'agoric1owner',
194158
payload,

services/ymax-planner/src/plan-deposit.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,6 @@ export const handleDeposit = async (
119119
}),
120120
),
121121
];
122-
return steps;
122+
console.log(steps);
123+
// return steps;
123124
};

0 commit comments

Comments
 (0)