-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bridge provider external url generator functions
- Loading branch information
1 parent
d59c065
commit 8f08f21
Showing
9 changed files
with
387 additions
and
22 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
packages/bridge/src/axelar/__tests__/external-urls.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { NativeEVMTokenConstantAddress } from "../../ethereum"; | ||
import { getAxelarExternalUrl } from "../external-urls"; | ||
|
||
describe("getAxelarExternalUrl", () => { | ||
it("should return the correct URL for Eth <> axlEth", async () => { | ||
const params = { | ||
fromChain: { chainId: 1, chainType: "evm" }, | ||
toChain: { chainId: "osmosis-1", chainType: "cosmos" }, | ||
fromAsset: { | ||
denom: "ETH", | ||
sourceDenom: "weth-wei", | ||
decimals: 18, | ||
address: NativeEVMTokenConstantAddress, | ||
}, | ||
toAsset: { | ||
address: | ||
"ibc/EA1D43981D5C9A1C4AAEA9C23BB1D4FA126BA9BC7020A25E0AE4AA841EA25DC5", | ||
decimals: 18, | ||
denom: "ETH", | ||
sourceDenom: "weth-wei", | ||
}, | ||
env: "mainnet", | ||
toAddress: "destination-address", | ||
} as Parameters<typeof getAxelarExternalUrl>[0]; | ||
|
||
const url = await getAxelarExternalUrl(params); | ||
|
||
expect(url).toBe( | ||
"https://satellite.money/?source=Ethereum&destination=osmosis&asset_denom=eth-wei&destination_address=destination-address" | ||
); | ||
}); | ||
|
||
it("should throw an error if fromChain is not found", async () => { | ||
const params = { | ||
fromChain: { chainId: "nonexistent", chainType: "evm" }, | ||
toChain: { chainId: "chain2", chainType: "cosmos" }, | ||
fromAsset: { | ||
address: "address1", | ||
denom: "denom1", | ||
sourceDenom: "sourceDenom1", | ||
decimals: 18, | ||
}, | ||
toAsset: { | ||
address: "address2", | ||
denom: "denom2", | ||
sourceDenom: "sourceDenom2", | ||
decimals: 18, | ||
}, | ||
env: "mainnet", | ||
toAddress: "destination-address", | ||
} as Parameters<typeof getAxelarExternalUrl>[0]; | ||
|
||
await expect(getAxelarExternalUrl(params)).rejects.toThrow( | ||
"Chain not found: nonexistent" | ||
); | ||
}); | ||
|
||
it("should throw an error if toChain is not found", async () => { | ||
const params = { | ||
fromChain: { chainId: "chain1", chainType: "evm" }, | ||
toChain: { chainId: "nonexistent", chainType: "cosmos" }, | ||
fromAsset: { | ||
address: "address1", | ||
denom: "denom1", | ||
sourceDenom: "sourceDenom1", | ||
decimals: 18, | ||
}, | ||
toAsset: { | ||
address: "address2", | ||
denom: "denom2", | ||
sourceDenom: "sourceDenom2", | ||
decimals: 18, | ||
}, | ||
env: "mainnet", | ||
toAddress: "destination-address", | ||
} as Parameters<typeof getAxelarExternalUrl>[0]; | ||
|
||
await expect(getAxelarExternalUrl(params)).rejects.toThrow( | ||
"Chain not found: chain1" | ||
); | ||
}); | ||
|
||
it("should throw an error if toAsset is not found", async () => { | ||
const params = { | ||
fromChain: { chainId: 1, chainType: "evm" }, | ||
toChain: { chainId: "osmosis-1", chainType: "cosmos" }, | ||
fromAsset: { | ||
address: "address1", | ||
denom: "denom1", | ||
sourceDenom: "sourceDenom1", | ||
decimals: 18, | ||
}, | ||
toAsset: { | ||
address: "nonexistent", | ||
denom: "denom2", | ||
sourceDenom: "sourceDenom2", | ||
decimals: 18, | ||
}, | ||
env: "mainnet", | ||
toAddress: "destination-address", | ||
} as Parameters<typeof getAxelarExternalUrl>[0]; | ||
|
||
await expect(getAxelarExternalUrl(params)).rejects.toThrow( | ||
"Asset not found: nonexistent" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { isNil } from "@osmosis-labs/utils"; | ||
|
||
import { | ||
BridgeProviderContext, | ||
GetBridgeExternalUrlParams, | ||
} from "../interface"; | ||
import { getAxelarAssets, getAxelarChains } from "./queries"; | ||
|
||
export async function getAxelarExternalUrl({ | ||
fromChain, | ||
toChain, | ||
toAsset, | ||
env, | ||
toAddress, | ||
}: GetBridgeExternalUrlParams & { | ||
env: BridgeProviderContext["env"]; | ||
}): Promise<string> { | ||
const [axelarChains, axelarAssets] = await Promise.all([ | ||
getAxelarChains({ env }), | ||
getAxelarAssets({ env }), | ||
]); | ||
|
||
const fromAxelarChain = axelarChains.find( | ||
(chain) => String(chain.chain_id) === String(fromChain.chainId) | ||
); | ||
|
||
if (!fromAxelarChain) { | ||
throw new Error(`Chain not found: ${fromChain.chainId}`); | ||
} | ||
|
||
const toAxelarChain = axelarChains.find( | ||
(chain) => String(chain.chain_id) === String(toChain.chainId) | ||
); | ||
|
||
if (!toAxelarChain) { | ||
throw new Error(`Chain not found: ${toChain.chainId}`); | ||
} | ||
|
||
const toAxelarAsset = axelarAssets.find((axelarAsset) => { | ||
return ( | ||
!isNil(axelarAsset.addresses[toAxelarChain.chain_name]) && | ||
(axelarAsset.addresses[toAxelarChain.chain_name].ibc_denom === | ||
toAsset.address || | ||
axelarAsset.addresses[toAxelarChain.chain_name].address === | ||
toAsset.address) | ||
); | ||
}); | ||
|
||
if (!toAxelarAsset) { | ||
throw new Error(`Asset not found: ${toAsset.address}`); | ||
} | ||
|
||
const url = new URL("https://satellite.money/"); | ||
url.searchParams.set("source", fromAxelarChain.chain_name); | ||
url.searchParams.set("destination", toAxelarChain.chain_name); | ||
url.searchParams.set("asset_denom", toAxelarAsset.id); | ||
url.searchParams.set("destination_address", toAddress); | ||
|
||
return url.toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { getSkipExternalUrl } from "../external-urls"; | ||
|
||
describe("getSkipExternalUrl", () => { | ||
it("should generate the correct URL for given parameters", () => { | ||
const params = { | ||
fromChain: { chainId: "cosmoshub-4" }, | ||
toChain: { chainId: "agoric-3" }, | ||
fromAsset: { address: "uatom" }, | ||
toAsset: { address: "ubld" }, | ||
} as Parameters<typeof getSkipExternalUrl>[0]; | ||
|
||
const expectedUrl = | ||
"https://ibc.fun/?src_chain=cosmoshub-4&src_asset=uatom&dest_chain=agoric-3&dest_asset=ubld"; | ||
const result = getSkipExternalUrl(params); | ||
|
||
expect(result).toBe(expectedUrl); | ||
}); | ||
|
||
it("should encode asset addresses correctly", () => { | ||
const params = { | ||
fromChain: { chainId: "akashnet-2" }, | ||
toChain: { chainId: "andromeda-1" }, | ||
fromAsset: { | ||
address: | ||
"ibc/2e5d0ac026ac1afa65a23023ba4f24bb8ddf94f118edc0bad6f625bfc557cded", | ||
}, | ||
toAsset: { | ||
address: | ||
"ibc/976c73350f6f48a69de740784c8a92931c696581a5c720d96ddf4afa860fff97", | ||
}, | ||
} as Parameters<typeof getSkipExternalUrl>[0]; | ||
|
||
const expectedUrl = | ||
"https://ibc.fun/?src_chain=akashnet-2&src_asset=ibc%2F2e5d0ac026ac1afa65a23023ba4f24bb8ddf94f118edc0bad6f625bfc557cded&dest_chain=andromeda-1&dest_asset=ibc%2F976c73350f6f48a69de740784c8a92931c696581a5c720d96ddf4afa860fff97"; | ||
const result = getSkipExternalUrl(params); | ||
|
||
expect(result).toBe(expectedUrl); | ||
}); | ||
|
||
it("should handle numeric chain IDs correctly", () => { | ||
const params = { | ||
fromChain: { chainId: 42161 }, | ||
toChain: { chainId: "andromeda-1" }, | ||
fromAsset: { address: "0xff970a61a04b1ca14834a43f5de4533ebddb5cc8" }, | ||
toAsset: { | ||
address: | ||
"ibc/976c73350f6f48a69de740784c8a92931c696581a5c720d96ddf4afa860fff97", | ||
}, | ||
} as Parameters<typeof getSkipExternalUrl>[0]; | ||
|
||
const expectedUrl = | ||
"https://ibc.fun/?src_chain=42161&src_asset=0xff970a61a04b1ca14834a43f5de4533ebddb5cc8&dest_chain=andromeda-1&dest_asset=ibc%2F976c73350f6f48a69de740784c8a92931c696581a5c720d96ddf4afa860fff97"; | ||
const result = getSkipExternalUrl(params); | ||
|
||
expect(result).toBe(expectedUrl); | ||
}); | ||
}); |
File renamed without changes.
Oops, something went wrong.