Skip to content

Commit

Permalink
Merge branch 'main' into fix/getfromamount-method
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdev3 authored Apr 19, 2024
2 parents d5cdafa + 400445d commit 59f231a
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 164 deletions.
68 changes: 68 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@


## [1.14.16-beta.0](https://github.com/0xsquid/api-sdk/compare/v1.14.1...v1.14.16-beta.0) (2024-03-27)


### Features

* add chainId property to evm balances response ([#239](https://github.com/0xsquid/api-sdk/issues/239)) ([e5166c0](https://github.com/0xsquid/api-sdk/commit/e5166c01790ad18f4517e83b20698d190fa60759))
* support injective network ([#279](https://github.com/0xsquid/api-sdk/issues/279)) ([42f09e6](https://github.com/0xsquid/api-sdk/commit/42f09e64b6dbff391278f27e4acd8172c1edae1a))


### Bug Fixes

* bignumber formatting ([#255](https://github.com/0xsquid/api-sdk/issues/255)) ([ea7a775](https://github.com/0xsquid/api-sdk/commit/ea7a7756767f4dd2c0a8b6ca23666e116b6fa0a6))
* handle ledger amino messages ([#215](https://github.com/0xsquid/api-sdk/issues/215)) ([2ef50f7](https://github.com/0xsquid/api-sdk/commit/2ef50f76d2429cca74c71a1fd1324beff9a90f0c)), closes [#216](https://github.com/0xsquid/api-sdk/issues/216) [#224](https://github.com/0xsquid/api-sdk/issues/224)
* handle possible undefined value that could throw bignumber error ([#261](https://github.com/0xsquid/api-sdk/issues/261)) ([5886f43](https://github.com/0xsquid/api-sdk/commit/5886f436faef9797d544a94d0200208fd716a363))
* parse undefined memo sent from api ([#237](https://github.com/0xsquid/api-sdk/issues/237)) ([6d42fdd](https://github.com/0xsquid/api-sdk/commit/6d42fdd45afc33bb81b192fa3e1c42694f1c6ca7))
* use a different rpc url for each chain ([#230](https://github.com/0xsquid/api-sdk/issues/230)) ([53bfa60](https://github.com/0xsquid/api-sdk/commit/53bfa60b9b314a7412b265fdea6e7de4454e49ea))
* use all chains when no chains are specified ([#233](https://github.com/0xsquid/api-sdk/issues/233)) ([ca638dd](https://github.com/0xsquid/api-sdk/commit/ca638dd1aaa3395272788880fd0a195be55db525))


### Reverts

* Revert "chore: branch cut release for sdk v1.14.9" ([7497aaa](https://github.com/0xsquid/api-sdk/commit/7497aaade74c5af75b12291d8be57f10aa022601))

## [1.14.15](https://github.com/0xsquid/api-sdk/compare/v1.14.1...v1.14.15) (2024-03-10)


### Features

* add chainId property to evm balances response ([#239](https://github.com/0xsquid/api-sdk/issues/239)) ([e5166c0](https://github.com/0xsquid/api-sdk/commit/e5166c01790ad18f4517e83b20698d190fa60759))
* support injective network ([#279](https://github.com/0xsquid/api-sdk/issues/279)) ([42f09e6](https://github.com/0xsquid/api-sdk/commit/42f09e64b6dbff391278f27e4acd8172c1edae1a))


### Bug Fixes

* bignumber formatting ([#255](https://github.com/0xsquid/api-sdk/issues/255)) ([ea7a775](https://github.com/0xsquid/api-sdk/commit/ea7a7756767f4dd2c0a8b6ca23666e116b6fa0a6))
* handle ledger amino messages ([#215](https://github.com/0xsquid/api-sdk/issues/215)) ([2ef50f7](https://github.com/0xsquid/api-sdk/commit/2ef50f76d2429cca74c71a1fd1324beff9a90f0c)), closes [#216](https://github.com/0xsquid/api-sdk/issues/216) [#224](https://github.com/0xsquid/api-sdk/issues/224)
* handle possible undefined value that could throw bignumber error ([#261](https://github.com/0xsquid/api-sdk/issues/261)) ([5886f43](https://github.com/0xsquid/api-sdk/commit/5886f436faef9797d544a94d0200208fd716a363))
* parse undefined memo sent from api ([#237](https://github.com/0xsquid/api-sdk/issues/237)) ([6d42fdd](https://github.com/0xsquid/api-sdk/commit/6d42fdd45afc33bb81b192fa3e1c42694f1c6ca7))
* use a different rpc url for each chain ([#230](https://github.com/0xsquid/api-sdk/issues/230)) ([53bfa60](https://github.com/0xsquid/api-sdk/commit/53bfa60b9b314a7412b265fdea6e7de4454e49ea))
* use all chains when no chains are specified ([#233](https://github.com/0xsquid/api-sdk/issues/233)) ([ca638dd](https://github.com/0xsquid/api-sdk/commit/ca638dd1aaa3395272788880fd0a195be55db525))


### Reverts

* Revert "chore: branch cut release for sdk v1.14.9" ([7497aaa](https://github.com/0xsquid/api-sdk/commit/7497aaade74c5af75b12291d8be57f10aa022601))

## [1.14.13](https://github.com/0xsquid/api-sdk/compare/v1.14.1...v1.14.13) (2024-01-17)


### Features

* add chainId property to evm balances response ([#239](https://github.com/0xsquid/api-sdk/issues/239)) ([e5166c0](https://github.com/0xsquid/api-sdk/commit/e5166c01790ad18f4517e83b20698d190fa60759))


### Bug Fixes

* bignumber formatting ([#255](https://github.com/0xsquid/api-sdk/issues/255)) ([ea7a775](https://github.com/0xsquid/api-sdk/commit/ea7a7756767f4dd2c0a8b6ca23666e116b6fa0a6))
* handle ledger amino messages ([#215](https://github.com/0xsquid/api-sdk/issues/215)) ([2ef50f7](https://github.com/0xsquid/api-sdk/commit/2ef50f76d2429cca74c71a1fd1324beff9a90f0c)), closes [#216](https://github.com/0xsquid/api-sdk/issues/216) [#224](https://github.com/0xsquid/api-sdk/issues/224)
* handle possible undefined value that could throw bignumber error ([#261](https://github.com/0xsquid/api-sdk/issues/261)) ([5886f43](https://github.com/0xsquid/api-sdk/commit/5886f436faef9797d544a94d0200208fd716a363))
* parse undefined memo sent from api ([#237](https://github.com/0xsquid/api-sdk/issues/237)) ([6d42fdd](https://github.com/0xsquid/api-sdk/commit/6d42fdd45afc33bb81b192fa3e1c42694f1c6ca7))
* use a different rpc url for each chain ([#230](https://github.com/0xsquid/api-sdk/issues/230)) ([53bfa60](https://github.com/0xsquid/api-sdk/commit/53bfa60b9b314a7412b265fdea6e7de4454e49ea))
* use all chains when no chains are specified ([#233](https://github.com/0xsquid/api-sdk/issues/233)) ([ca638dd](https://github.com/0xsquid/api-sdk/commit/ca638dd1aaa3395272788880fd0a195be55db525))


### Reverts

* Revert "chore: branch cut release for sdk v1.14.9" ([7497aaa](https://github.com/0xsquid/api-sdk/commit/7497aaade74c5af75b12291d8be57f10aa022601))

## [1.14.12](https://github.com/0xsquid/api-sdk/compare/v1.14.1...v1.14.12) (2023-12-06)


Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@0xsquid/sdk",
"version": "1.14.12",
"version": "1.14.16-beta.0",
"description": "🛠 An SDK for building applications on top of 0xsquid",
"repository": {
"type": "git",
Expand Down Expand Up @@ -33,13 +33,13 @@
"release:dry": "release-it --dry-run"
},
"dependencies": {
"ethereum-multicall": "2.21.0",
"ethereum-multicall": "2.23.0",
"axios": "^0.27.2",
"ethers": "^5.7.1",
"@cosmjs/encoding": "^0.31.0",
"@cosmjs/stargate": "^0.31.3",
"@cosmjs/cosmwasm-stargate": "^0.31.3",
"cosmjs-types": "^0.8.0"
"@cosmjs/encoding": "^0.32.2",
"@cosmjs/stargate": "^0.32.2",
"@cosmjs/cosmwasm-stargate": "^0.32.2",
"cosmjs-types": "^0.9.0"
},
"resolutions": {
"semver": "^7.5.4"
Expand Down
66 changes: 43 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { toUtf8 } from "@cosmjs/encoding";
import {
AminoTypes,
Coin,
DeliverTxResponse,
GasPrice,
SigningStargateClient,
calculateFee,
Expand Down Expand Up @@ -313,8 +314,11 @@ export class Squid {
signerAddress,
route,
executionSettings,
overrides
}: ExecuteRoute): Promise<ethers.providers.TransactionResponse | TxRaw> {
overrides,
useBroadcast = false
}: ExecuteRoute): Promise<
ethers.providers.TransactionResponse | TxRaw | DeliverTxResponse
> {
this.validateInit();

if (!route.transactionRequest) {
Expand All @@ -326,17 +330,15 @@ export class Squid {
});
}

const isEvmChainId = Number(route.params.fromChain) > 0;

// handle cosmos case
if (
signer instanceof SigningStargateClient ||
signer.constructor.name === "SigningStargateClient" ||
signer instanceof SigningCosmWasmClient ||
signer.constructor.name === "SigningCosmWasmClient"
) {
if (!isEvmChainId) {
return await this.executeRouteCosmos(
signer as SigningStargateClient,
signerAddress!,
route
route,
useBroadcast
);
}

Expand Down Expand Up @@ -382,7 +384,7 @@ export class Squid {
fromAmount: params.fromAmount,
fromChain,
infiniteApproval: executionSettings?.infiniteApproval,
signer,
signer: signer as ethers.Signer,
overrides: _overrides
});
}
Expand All @@ -402,7 +404,7 @@ export class Squid {
};
}

return await signer.sendTransaction(tx);
return await (signer as ethers.Signer).sendTransaction(tx);
}

public getRawTxHex({
Expand Down Expand Up @@ -463,8 +465,9 @@ export class Squid {
private async executeRouteCosmos(
signer: SigningStargateClient | SigningCosmWasmClient,
signerAddress: string,
route: RouteData
): Promise<TxRaw> {
route: RouteData,
useBroadcast = false
): Promise<TxRaw | DeliverTxResponse> {
const cosmosMsg: CosmosMsg = JSON.parse(route.transactionRequest!.data);
const msgs = [];
switch (cosmosMsg.msgTypeUrl) {
Expand Down Expand Up @@ -506,10 +509,6 @@ export class Squid {
amount: route.params.fromAmount
});

// simulate tx to estimate gas cost
const estimatedGas = await signer.simulate(signerAddress, msgs, "");
const gasMultiplier = Number(route.transactionRequest!.maxFeePerGas) || 1.3;

// This conversion is needed for Ledger, They only supports Amino messages
// TODO: At the moment there's a limit on Ledger Nano S models
// This limit prevents WASM_TYPE messages to be signed (because payload message is too big)
Expand All @@ -529,13 +528,31 @@ export class Squid {
const aminoMsg = aminoTypes.toAmino(formattedMsg);
const fromAminoMsg = aminoTypes.fromAmino(aminoMsg);

// simulate tx to estimate gas cost
const estimatedGas = await signer.simulate(
signerAddress,
[fromAminoMsg],
""
);
const gasMultiplier = Number(route.transactionRequest!.maxFeePerGas) || 1.3;
const fee = calculateFee(
Math.trunc(estimatedGas * gasMultiplier),
GasPrice.fromString(route.transactionRequest!.gasPrice)
);

if (useBroadcast) {
return (signer as SigningCosmWasmClient).signAndBroadcast(
signerAddress,
[fromAminoMsg],
fee,
""
);
}

return (signer as SigningCosmWasmClient).sign(
signerAddress,
[fromAminoMsg],
calculateFee(
Math.trunc(estimatedGas * gasMultiplier),
GasPrice.fromString(route.transactionRequest!.gasPrice)
),
fee,
""
);
}
Expand Down Expand Up @@ -868,11 +885,14 @@ export class Squid {
};
}

private getTimeoutTimestamp(): Long {
private getTimeoutTimestamp(): number {
const PACKET_LIFETIME_NANOS = 3600 * 1_000_000_000; // 1 Hour

const currentTimeNanos = Math.floor(Date.now() * 1_000_000);
return Long.fromNumber(currentTimeNanos + PACKET_LIFETIME_NANOS);
const timeoutTimestamp = Long.fromNumber(
currentTimeNanos + PACKET_LIFETIME_NANOS
);
return timeoutTimestamp.toNumber();
}

private getAminoTypeConverters(): AminoTypes {
Expand Down
56 changes: 38 additions & 18 deletions src/services/getEvmBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {

type ContractAddress = `0x${string}`;

const CHAINS_WITHOUT_MULTICALL = [314, 3141]; // Filecoin, & Filecoin testnet
const CHAINS_WITHOUT_MULTICALL = [
314, // Filecoin
3141, // Filecoin testnet
2222 // Kava
];

const getTokensBalanceSupportingMultiCall = async (
tokens: TokenData[],
Expand Down Expand Up @@ -50,7 +54,8 @@ const getTokensBalanceSupportingMultiCall = async (

const multicallInstance = new Multicall({
ethersProvider: provider,
tryAggregate: true
tryAggregate: true,
multicallCustomContractAddress: MULTICALL_ADDRESS
});

try {
Expand Down Expand Up @@ -94,23 +99,12 @@ const getTokensBalanceWithoutMultiCall = async (
): Promise<TokenBalance[]> => {
const balances: (TokenBalance | null)[] = await Promise.all(
tokens.map(async t => {
let balance: TokenBalance | null;
try {
if (t.address === NATIVE_EVM_TOKEN_ADDRESS) {
balance = await fetchBalance({
token: t,
userAddress,
rpcUrl: rpcUrlsPerChain[t.chainId]
});
} else {
balance = await fetchBalance({
token: t,
userAddress,
rpcUrl: rpcUrlsPerChain[t.chainId]
});
}

return balance;
return await fetchBalance({
token: t,
userAddress,
rpcUrl: rpcUrlsPerChain[t.chainId]
});
} catch (error) {
return null;
}
Expand Down Expand Up @@ -199,6 +193,10 @@ async function fetchBalance({
userAddress,
rpcUrl
}: FetchBalanceParams): Promise<TokenBalance | null> {
if (token.address.toLowerCase() === NATIVE_EVM_TOKEN_ADDRESS.toLowerCase()) {
return fetchNativeBalance({ token, userAddress, rpcUrl });
}

try {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);

Expand Down Expand Up @@ -228,3 +226,25 @@ async function fetchBalance({
return null;
}
}

async function fetchNativeBalance({
userAddress,
rpcUrl,
token
}: FetchBalanceParams) {
try {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const balance = await provider.getBalance(userAddress);

return {
address: token.address,
balance: balance.toString(),
decimals: token.decimals,
symbol: token.symbol,
chainId: token.chainId
};
} catch (error) {
console.error("Error fetching native token balance:", error);
return null;
}
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ export type ExecuteRoute = {
setGasPrice?: boolean;
};
overrides?: OverrideParams;
useBroadcast?: boolean;
};

export type Allowance = {
Expand Down
Loading

0 comments on commit 59f231a

Please sign in to comment.