Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aptos: Add to liquidswap pool (WIP) #451

Draft
wants to merge 39 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a567f0e
feat(aptos-contracts): Create testnet pool
nicomiicro Oct 19, 2022
e10a0ae
chore(token-projects): Add TokenProjectId.SwimAptosUsdcLp
nicomiicro Oct 20, 2022
3fb7e7d
chore(aptos): Add tokens and pools details
nicomiicro Oct 20, 2022
c985a41
wip: revert publish package
nicomiicro Oct 20, 2022
2b27e2c
chore(ui): update aptos tokens and pools config
nicomiicro Oct 20, 2022
cfec682
fix(aptos): handle zero balance on getTokenBalance
nicomiicro Oct 20, 2022
d295098
fix(aptos): use correct pool address
nicomiicro Oct 20, 2022
9736f22
chore(aptos-contracts): add a note for the initial minted coins amount
nicomiicro Oct 20, 2022
1fcec0f
docs(aptos-contracts): add prompt symbol to all commands
nicomiicro Oct 20, 2022
ffa0002
docs(aptos-contracts): link to attestation app
nicomiicro Oct 20, 2022
c0c09d8
docs(attestation): clarify format of APTOS_ACCOUNT_PRIVATE_KEY
nicomiicro Oct 20, 2022
0a0affd
chore(aptos-contracts): use a diff version
nicomiicro Oct 20, 2022
991a573
docs(aptos-contracts): grammar mistake
nicomiicro Oct 20, 2022
c59b8eb
Merge branch 'master' into aptos/contracts
nicomiicro Oct 20, 2022
fb10b42
test(ui): update swapTokenOptions snapshot
nicomiicro Oct 20, 2022
333c481
refactor(attestation): handle hex prefix in Aptos account private key
nicomiicro Oct 20, 2022
343fa35
feat(core): Add PoolState interface to core
nicomiicro Oct 21, 2022
a4d8dae
feat(core): Add getPoolState to client interface
nicomiicro Oct 21, 2022
cf1e61d
feat(aptos): implement client.getPoolState
nicomiicro Oct 24, 2022
bcb6fcd
feat(ui): add aptos pool state queries
nicomiicro Oct 24, 2022
634b35e
fix(aptos): use proper resource type in getTokenBalance
nicomiicro Oct 25, 2022
006370b
Merge branch 'master' into aptos/contracts
nicomiicro Oct 25, 2022
eedbc49
Merge branch 'aptos/contracts' into aptos/add-to-pool
nicomiicro Oct 25, 2022
6619a0f
chore(aptos): update contracts addresses
nicomiicro Oct 25, 2022
c7a38c6
Merge pull request #462 from swim-io/aptos/update-wormhole-and-lp
nicomiicro Oct 25, 2022
dfcba14
refactor(ui): define local AptosPoolState
nicomiicro Oct 25, 2022
cdf1422
refactor(aptos): remove AptosPoolState
nicomiicro Oct 25, 2022
8b3c262
refactor(aptos): tidy up utils and types
nicomiicro Oct 25, 2022
eb5f30b
test(ui): update swapTokenOptions.test.ts.snap
nicomiicro Oct 25, 2022
9d0af66
Merge branch 'aptos/contracts' into aptos/add-to-pool
nicomiicro Oct 25, 2022
4f3fd60
refactor(aptos): rename const to constants
nicomiicro Oct 25, 2022
c7156f1
refactor(aptos): rename getBalances to getPoolBalances
nicomiicro Oct 25, 2022
823318a
Merge pull request #460 from swim-io/aptos/add-to-pool
nicomiicro Oct 26, 2022
9ed167c
fix(ui): remove duplicate ecosystem array
nicomiicro Oct 26, 2022
d3a923a
fix(aptos): remove buggy liquidswap SDK
nicomiicro Oct 26, 2022
f1d424b
feat(aptos): Add getAddLiquidityTransactionPayload
nicomiicro Oct 26, 2022
4ea4f74
feat(ui): Add to Aptos pool
nicomiicro Oct 26, 2022
ca3a003
chore(aptos): fix typo
nicomiicro Oct 26, 2022
c706d96
Merge pull request #468 from swim-io/aptos/add-to-pool-2
nicomiicro Oct 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/attestation/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
APTOS_ACCOUNT_PRIVATE_KEY=0x674cd851321321...
WALLET_SOLANA_MNEMONIC_PHRASE=bla bla bla bla bla

2 changes: 2 additions & 0 deletions apps/attestation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ See [wormhole token registration page](https://book.wormhole.com/technical/types
```bash
WALLET_SOLANA_MNEMONIC_PHRASE='...' APTOS_ACCOUNT_PRIVATE_KEY=... yarn attestFromSolanaToAptos --mintAddress '3ngTtoyP9GFybFifX1dr7gCFXFiM2Wr6NfXn6EuU7k6C'
```

Note: APTOS_ACCOUNT_PRIVATE_KEY should be hex-encoded.
2 changes: 1 addition & 1 deletion apps/attestation/src/aptos/attestFromSolanaToAptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async function main() {
console.info(`Fetched signed VAA. vaaBytes: ${vaaBytes.toString()}`);

const sender = new AptosAccount(
new Uint8Array(Buffer.from(aptosPrivateKey, "hex")),
new Uint8Array(Buffer.from(aptosPrivateKey.replace(/^0x/, ""), "hex")),
);

const client = new AptosClient(aptosRpcUrl);
Expand Down
6 changes: 3 additions & 3 deletions apps/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
"@sentry/types": "^7.9.0",
"@solana/spl-token": "^0.3.5",
"@solana/web3.js": "^1.62.0",
"@swim-io/aptos": "^0.40.0",
"@swim-io/core": "^0.40.0",
"@swim-io/aptos": "workspace:^",
"@swim-io/core": "workspace:^",
"@swim-io/evm": "^0.40.0",
"@swim-io/evm-contracts": "^0.40.0",
"@swim-io/pool-math": "^0.40.0",
"@swim-io/solana": "^0.40.0",
"@swim-io/solana-contracts": "^0.40.0",
"@swim-io/token-projects": "^0.40.0",
"@swim-io/token-projects": "workspace:^",
"@swim-io/utils": "^0.40.0",
"@swim-io/wormhole": "^0.40.0",
"bn.js": "^5.2.1",
Expand Down
10 changes: 8 additions & 2 deletions apps/ui/src/components/AddForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import {
EuiSpacer,
EuiText,
} from "@elastic/eui";
import { APTOS_ECOSYSTEM_ID } from "@swim-io/aptos";
import { SOLANA_ECOSYSTEM_ID } from "@swim-io/solana";
import { TOKEN_PROJECTS_BY_ID } from "@swim-io/token-projects";
import { filterMap, isEachNotNull, isNotNull } from "@swim-io/utils";
import type Decimal from "decimal.js";
import Decimal from "decimal.js";
import type { FormEvent, ReactElement } from "react";
import { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
Expand Down Expand Up @@ -264,6 +265,11 @@ export const AddForm = ({
);

const estimatedLpOutput = useMemo(() => {
// TODO aptos pool math
if (poolSpec.ecosystem === APTOS_ECOSYSTEM_ID && !poolMath) {
return Amount.fromHuman(lpToken, new Decimal("1"));
}

if (!poolMath || !isEachNotNull(inputAmounts)) {
return null;
}
Expand Down Expand Up @@ -466,7 +472,7 @@ export const AddForm = ({
minimumMintAmount === null ||
!inputAmounts.every(isNotNull) ||
maxSlippageFraction === null ||
poolMath === null
(poolMath === null && !nativeEcosystems.includes(APTOS_ECOSYSTEM_ID)) // TODO aptos remove exclusion
) {
notify(
t("notify.unexpected_form_error_title"),
Expand Down
3 changes: 3 additions & 0 deletions apps/ui/src/components/molecules/TxListItem/TxListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EuiListGroupItem } from "@elastic/eui";
import { APTOS_ECOSYSTEM_ID } from "@swim-io/aptos";
import { EvmEcosystemId } from "@swim-io/evm";
import { SOLANA_ECOSYSTEM_ID } from "@swim-io/solana";
import type { FC } from "react";
Expand All @@ -13,6 +14,8 @@ interface Props {
const getHref = (ecosystemId: EcosystemId, txId: string): string => {
// TODO: Support different environments (ie testnets).
switch (ecosystemId) {
case APTOS_ECOSYSTEM_ID:
return `https://explorer.aptoslabs.com/txn/${txId}`;
case SOLANA_ECOSYSTEM_ID:
return `https://solana.fm/tx/${txId}`;
case EvmEcosystemId.Ethereum:
Expand Down
15 changes: 5 additions & 10 deletions apps/ui/src/config/ecosystem.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { AptosEcosystemId } from "@swim-io/aptos";
import { APTOS_ECOSYSTEM_ID } from "@swim-io/aptos";
import { EVM_ECOSYSTEMS, EvmEcosystemId, isEvmEcosystemId } from "@swim-io/evm";
import { EvmEcosystemId, isEvmEcosystemId } from "@swim-io/evm";
import type { SolanaEcosystemId } from "@swim-io/solana";
import { SOLANA_ECOSYSTEM_ID } from "@swim-io/solana";
import type { ReadonlyRecord } from "@swim-io/utils";
import { filterMap, getRecordKeys } from "@swim-io/utils";
import { filterMap } from "@swim-io/utils";
import { WormholeChainId } from "@swim-io/wormhole";

import ACALA_SVG from "../images/ecosystems/acala.svg";
Expand Down Expand Up @@ -40,24 +40,19 @@ const isEcosystemId = (ecosystemId: string): ecosystemId is EcosystemId => {
);
};

const ALL_ECOSYSTEMS: readonly EcosystemId[] = [
export const ECOSYSTEM_IDS: readonly EcosystemId[] = [
APTOS_ECOSYSTEM_ID,
SOLANA_ECOSYSTEM_ID,
...getRecordKeys(EVM_ECOSYSTEMS),
...Object.values(EvmEcosystemId),
];

export const DISABLED_ECOSYSTEMS: readonly EcosystemId[] =
process.env.REACT_APP_DISABLE_ECOSYSTEMS === "*"
? ALL_ECOSYSTEMS
? ECOSYSTEM_IDS
: (process.env.REACT_APP_DISABLE_ECOSYSTEMS ?? "")
.split(",")
.filter(isEcosystemId);

export const ECOSYSTEM_IDS: readonly EcosystemId[] = [
SOLANA_ECOSYSTEM_ID,
...Object.values(EvmEcosystemId),
];

export const isEcosystemEnabled = (ecosystemId: EcosystemId): boolean => {
switch (ecosystemId) {
case SOLANA_ECOSYSTEM_ID:
Expand Down
10 changes: 10 additions & 0 deletions apps/ui/src/config/pools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AptosEcosystemId } from "@swim-io/aptos";
import { APTOS_ECOSYSTEM_ID, aptos } from "@swim-io/aptos";
import { Env } from "@swim-io/core";
import { EvmEcosystemId } from "@swim-io/evm";
import type { SolanaEcosystemId } from "@swim-io/solana";
Expand Down Expand Up @@ -536,6 +537,15 @@ export const TESTNET_POOLS_FOR_RESTRUCTURE: readonly PoolSpec[] = [
lpToken: "testnet-acala-lp-ausd",
tokens: ["testnet-swimusd", "testnet-acala-ausd"],
},
...aptos.chains[Env.Testnet].pools.map((pool) => ({
wormat marked this conversation as resolved.
Show resolved Hide resolved
isDisabled:
!isEcosystemEnabled(APTOS_ECOSYSTEM_ID) || !isPoolRestructureEnabled(),
ecosystem: pool.ecosystemId,
tokens: pool.tokenIds,
lpToken: pool.lpTokenId,
isLegacyPool: pool.isLegacyPool ?? false,
...pool,
})),
];

export const TESTNET_POOLS: readonly PoolSpec[] = [
Expand Down
130 changes: 72 additions & 58 deletions apps/ui/src/config/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { APTOS_ECOSYSTEM_ID, aptos } from "@swim-io/aptos";
import { Env } from "@swim-io/core";
import type {
TokenConfig as CoreTokenConfig,
Expand Down Expand Up @@ -585,6 +586,69 @@ const MAINNET_TOKENS: readonly TokenConfig[] = [
},
].filter((spec) => !spec.isDisabled);

const TESTNET_SWIMUSD_WRAPPED_DETAILS_ARRAY: readonly (readonly [
EcosystemId,
TokenDetails,
])[] = [
[APTOS_ECOSYSTEM_ID, aptos.chains[Env.Testnet].swimUsdDetails],
wormat marked this conversation as resolved.
Show resolved Hide resolved
[
EvmEcosystemId.Acala,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
wormat marked this conversation as resolved.
Show resolved Hide resolved
decimals: 6,
},
],
[
EvmEcosystemId.Aurora,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Avalanche,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Bnb,
{
address: "0x4c15919a4354b4416e7afcb9a27a118bc45818c0", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Ethereum,
{
address: "0x4873edbb0B4b5b48A6FBe50CacB85e58D0b62ab5", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Fantom,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Karura,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Polygon,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
];

export const TESTNET_SWIMUSD: TokenConfig = {
isDisabled: !isPoolRestructureEnabled(),
id: "testnet-swimusd",
Expand All @@ -594,64 +658,7 @@ export const TESTNET_SWIMUSD: TokenConfig = {
address: "3ngTtoyP9GFybFifX1dr7gCFXFiM2Wr6NfXn6EuU7k6C", // TODO: Update
decimals: 6,
},
wrappedDetails: new Map([
[
EvmEcosystemId.Acala,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Aurora,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Avalanche,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Bnb,
{
address: "0x4c15919a4354b4416e7afcb9a27a118bc45818c0", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Ethereum,
{
address: "0x4873edbb0B4b5b48A6FBe50CacB85e58D0b62ab5", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Fantom,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Karura,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
[
EvmEcosystemId.Polygon,
{
address: "0x1111111111111111111111111111111111111111", // TODO: Update
decimals: 6,
},
],
]),
wrappedDetails: new Map(TESTNET_SWIMUSD_WRAPPED_DETAILS_ARRAY),
};

export const TESTNET_TOKENS_FOR_RESTRUCTURE: readonly TokenConfig[] = [
Expand Down Expand Up @@ -767,6 +774,13 @@ export const TESTNET_TOKENS_FOR_RESTRUCTURE: readonly TokenConfig[] = [
},
wrappedDetails: EMPTY_MAP,
},
...aptos.chains[Env.Testnet].tokens.map((token) => ({
isDisabled:
!isEcosystemEnabled(APTOS_ECOSYSTEM_ID) || !isPoolRestructureEnabled(),
nativeEcosystemId: APTOS_ECOSYSTEM_ID,
...token,
wrappedDetails: EMPTY_MAP,
})),
];

export const TESTNET_TOKENS: readonly TokenConfig[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ Array [
"nativeEcosystemId": "solana",
"projectId": "swim-lp-solana-usdc-usdt",
"wrappedDetails": Map {
"aptos" => Object {
"address": "0x30ab37efc691ea7202540b50f3f0f6b090adb143c0746fd49a7e4b7c5870ce8b::coin::T",
"decimals": 6,
},
"acala" => Object {
"address": "0x1111111111111111111111111111111111111111",
"decimals": 6,
Expand Down Expand Up @@ -159,6 +163,10 @@ Array [
"nativeEcosystemId": "solana",
"projectId": "swim-lp-solana-usdc-usdt",
"wrappedDetails": Map {
"aptos" => Object {
"address": "0x30ab37efc691ea7202540b50f3f0f6b090adb143c0746fd49a7e4b7c5870ce8b::coin::T",
"decimals": 6,
},
"acala" => Object {
"address": "0x1111111111111111111111111111111111111111",
"decimals": 6,
Expand Down Expand Up @@ -206,6 +214,10 @@ Array [
"nativeEcosystemId": "solana",
"projectId": "swim-lp-solana-usdc-usdt",
"wrappedDetails": Map {
"aptos" => Object {
"address": "0x30ab37efc691ea7202540b50f3f0f6b090adb143c0746fd49a7e4b7c5870ce8b::coin::T",
"decimals": 6,
},
"acala" => Object {
"address": "0x1111111111111111111111111111111111111111",
"decimals": 6,
Expand Down
33 changes: 32 additions & 1 deletion apps/ui/src/hooks/interaction/useAddInteractionMutation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getAddLiquidityTransactionPayload } from "@swim-io/aptos";
import { isEvmEcosystemId } from "@swim-io/evm";
import { Pool__factory } from "@swim-io/evm-contracts";
import { SOLANA_ECOSYSTEM_ID } from "@swim-io/solana";
Expand All @@ -12,6 +13,7 @@ import {
createOperationSpecs,
doSingleSolanaPoolOperationV2,
findOrCreateSplTokenAccount,
getSortedAmounts,
getTokensByPool,
} from "../../models";
import type { AddInteractionState } from "../../models";
Expand Down Expand Up @@ -165,7 +167,36 @@ export const useAddInteractionMutation = () => {
draft.addTxId = tx.id;
});
} else {
throw new Error("Unexpected ecosystem for add interaction");
const { inputAmounts } = interaction.params;

const { wallet } = wallets[ecosystem];
if (wallet === null) {
throw new Error(`${ecosystem} wallet not found`);
}
const { address } = wallet;
if (address === null) {
throw new Error(`${ecosystem} wallet not connected`);
}

const atomicAmounts = getSortedAmounts([
inputAmounts[0],
inputAmounts[1],
]).map((amount) => amount.toAtomicString(ecosystem));

const txId = await wallet.signAndSubmitTransaction(
getAddLiquidityTransactionPayload({
poolAddress: poolSpec.address,
coinXAmountAtomic: atomicAmounts[0],
coinYAmountAtomic: atomicAmounts[1],
}),
);

updateInteractionState(interaction.id, (draft) => {
if (draft.interactionType !== interaction.type) {
throw new Error("Interaction type mismatch");
}
draft.addTxId = txId;
});
}
},
{
Expand Down
Loading