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 15 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
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 not have the `0x` prefix.
wormat marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 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.39.0",
"@swim-io/aptos": "workspace:^",
"@swim-io/core": "^0.39.0",
"@swim-io/evm": "^0.39.0",
"@swim-io/evm-contracts": "^0.39.0",
"@swim-io/pool-math": "^0.39.0",
"@swim-io/solana": "^0.39.0",
"@swim-io/solana-contracts": "^0.39.0",
"@swim-io/token-projects": "^0.39.0",
"@swim-io/token-projects": "workspace:^",
"@swim-io/utils": "^0.39.0",
"@swim-io/wormhole": "^0.39.0",
"bn.js": "^5.2.1",
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": "0x246bfb8da92a72f29d0441138058a43970551734d68958281d59e23a4f2b19a0::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": "0x246bfb8da92a72f29d0441138058a43970551734d68958281d59e23a4f2b19a0::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": "0x246bfb8da92a72f29d0441138058a43970551734d68958281d59e23a4f2b19a0::coin::T",
"decimals": 6,
},
"acala" => Object {
"address": "0x1111111111111111111111111111111111111111",
"decimals": 6,
Expand Down
3 changes: 3 additions & 0 deletions packages/aptos-contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/

.aptos
6 changes: 6 additions & 0 deletions packages/aptos-contracts/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = 'Swim.io'
version = '0.0.0'

[dependencies]
AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework/", rev = "main" }
135 changes: 135 additions & 0 deletions packages/aptos-contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Aptos contracts

### Install Aptos CLI

See https://aptos.dev/cli-tools/aptos-cli-tool/install-aptos-cli for installation instructions.

### Testnet account

Copy [Aptos testnet account](https://start.1password.com/open/i?a=PO5QNP2LDRCKVKP56IIAMR35JY&v=ag3qmycy4q3yrrwl2p457xu6oy&i=3acejkblqkxxefzwmk6v7q3gva&h=terok.1password.com) yaml file from 1password to `.aptos/config.yaml` in this directory.

If you need some gas, visit https://aptoslabs.com/testnet-faucet and import the testnet account into a wallet (try Martian).

```bash
$ aptos config show-profiles
{
"Result": {
"testnet": {
"has_private_key": true,
"public_key": "0xc3cdfb15b99a440d942d2a38d4ca568112e8740a404942792c13515647f6f5cf",
"account": "8c9d3a36ae2c7a765826c126fe625f39e9110ea329a5693d874e875227a889c2",
"rest_url": "https://fullnode.testnet.aptoslabs.com"
}
}
}
```

## Example of creating one coin (as a stablecoin placeholder) and the LiquidSwap pool

### Compile sources

```bash
$ aptos move compile --named-addresses account=testnet --save-metadata
Compiling, may take a little while to download git dependencies...
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING Swim.io
{
"Result": [
"8c9d3a36ae2c7a765826c126fe625f39e9110ea329a5693d874e875227a889c2::test_coin"
]
}
```

### Publish

```bash
$ aptos move publish --named-addresses account=testnet --profile testnet
Compiling, may take a little while to download git dependencies...
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING Swim.io
package size 1416 bytes
Do you want to submit a transaction for a range of [167200 - 250800] Octas at a gas unit price of 100 Octas? [yes/no] >
yes
{
"Result": {
"transaction_hash": "0x4c0bfa7ed404eaf041cd9875bea98de87cec89bf09041dc2cbb7f2a5dcbeb890",
"gas_used": 1672,
"gas_unit_price": 100,
"sender": "8c9d3a36ae2c7a765826c126fe625f39e9110ea329a5693d874e875227a889c2",
"sequence_number": 0,
"success": true,
"timestamp_us": 1666174938185145,
"version": 287152429,
"vm_status": "Executed successfully"
}
}
```

### Initialize and mint our stablecoin to use for liquidity on the pool later

```bash
$ aptos move run \
--function-id testnet::test_coin::initialize \
--profile testnet
Do you want to submit a transaction for a range of [118900 - 178300] Octas at a gas unit price of 100 Octas? [yes/no] >
yes
{
"Result": {
"transaction_hash": "0xa2988fea705fe1aa8992f4671f9804a0950168df1cab83bc77574d51149b667d",
"gas_used": 1198,
"gas_unit_price": 100,
"sender": "8c9d3a36ae2c7a765826c126fe625f39e9110ea329a5693d874e875227a889c2",
"sequence_number": 1,
"success": true,
"timestamp_us": 1666175155441650,
"version": 287159440,
"vm_status": "Executed successfully"
}
}
```

### Create the LiquidSwap pool

So our fake stablecoin has an address of 0x8c9d3a36ae2c7a765826c126fe625f39e9110ea329a5693d874e875227a889c2::test_coin::USDC and the attested SwimUSD has an address of 0x246bfb8da92a72f29d0441138058a43970551734d68958281d59e23a4f2b19a0::coin::T (see [attestation app](./../../apps/attestation/README.md)).

We get the LiquidSwap address from https://github.com/pontem-network/liquidswap/blob/main/Move.toml (0x190d44266241744264b964a37b8f09863167a12d3e70cda39376cfb4e3561e12) and we call their [entry function](https://docs.liquidswap.com/smart-contracts#scripts) named `register_pool`.

Before we do that, we need to sort the addresses of the two tokens in order to know which one to put first (see [coins sorting](https://docs.liquidswap.com/smart-contracts#coins-sorting)). So we [try](https://gist.github.com/borispovod/2809728c8959649d42c5cef15b4cedb7) their `is_sorted` function and it looks like the attested swimUSD needs to go first in the type arguments.

```
is_sorted("0x246bfb8da92a72f29d0441138058a43970551734d68958281d59e23a4f2b19a0::coin::T", "0x8c9d3a36ae2c7a765826c126fe625f39e9110ea329a5693d874e875227a889c2::test_coin::USDC")
true
```

At the time of this writing it seems there is a [bug](https://github.com/pontem-network/liquidswap/pull/118) on the `liquidswap::curves::Stable` so we will use the `liquidswap::curves::Uncorrelated` curve. See [Stable swaps](https://docs.liquidswap.com/protocol-overview#stable-swaps) for more info.

```
$ aptos move run \
--function-id 0x190d44266241744264b964a37b8f09863167a12d3e70cda39376cfb4e3561e12::scripts::register_pool \
--type-args 0x246bfb8da92a72f29d0441138058a43970551734d68958281d59e23a4f2b19a0::coin::T 0x8c9d3a36ae2c7a765826c126fe625f39e9110ea329a5693d874e875227a889c2::test_coin::USDC 0x190d44266241744264b964a37b8f09863167a12d3e70cda39376cfb4e3561e12::curves::Uncorrelated \
--profile testnet
Do you want to submit a transaction for a range of [287100 - 430600] Octas at a gas unit price of 100 Octas? [yes/no] >
yes
{
"Result": {
"transaction_hash": "0xe632a133dbc179afa9c0f80f61e64af9e63e46c9d582b577f11d39d154b1ad0a",
"gas_used": 2871,
"gas_unit_price": 100,
"sender": "8c9d3a36ae2c7a765826c126fe625f39e9110ea329a5693d874e875227a889c2",
"sequence_number": 2,
"success": true,
"timestamp_us": 1666181541637966,
"version": 287381898,
"vm_status": "Executed successfully"
}
}
```

So now if we check the `changes` tab in the explorer [https://explorer.aptoslabs.com/txn/0xe632a133dbc179afa9c0f80f61e64af9e63e46c9d582b577f11d39d154b1ad0a?network=testnet](https://explorer.aptoslabs.com/txn/0xe632a133dbc179afa9c0f80f61e64af9e63e46c9d582b577f11d39d154b1ad0a?network=testnet)

We see that our LP has this address `0x5a97986a9d031c4567e15b797be516910cfcb4156312482efc6a19c0a30c948::lp_coin::LP<0x246bfb8da92a72f29d0441138058a43970551734d68958281d59e23a4f2b19a0::coin::T, 0x8c9d3a36ae2c7a765826c126fe625f39e9110ea329a5693d874e875227a889c2::test_coin::USDC, 0x190d44266241744264b964a37b8f09863167a12d3e70cda39376cfb4e3561e12::curves::Uncorrelated>`
under the [liquidswap_pool_account](https://github.com/pontem-network/liquidswap/blob/5fc2625652c15369d0ffc52f9024c180d6e72fea/Move.toml#L15).
Loading