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

Add Swap example #112

Merged
merged 3 commits into from
Oct 26, 2023
Merged

Add Swap example #112

merged 3 commits into from
Oct 26, 2023

Conversation

0xmigo
Copy link
Contributor

@0xmigo 0xmigo commented Oct 24, 2023

Description

First version of the swap dapp or example. This version relies on the user to compile AND publish the facoin and swap package.

Test Plan

➜  typescript git:(main) ✗ pnpm swap 0x7f3d84369e337f51bf738ceac7bc661f6ca190989d92405495d716db40b1033f 0x88fef4956bd9155c217671ad14adb0ede4b19971a0cef82e4b94595575b96413

> [email protected] swap /Users/jinhou/Projects/aptos-ts-sdk/examples/typescript
> ts-node swap.ts "0x7f3d84369e337f51bf738ceac7bc661f6ca190989d92405495d716db40b1033f" "0x88fef4956bd9155c217671ad14adb0ede4b19971a0cef82e4b94595575b96413"

This example will create a main user account called 'Admin', it will be used to deploy Liquidity pool and two new fungible assets. 
After creating the Dog and Cat coin, and the liquidity pool, it will swap one token for another. 
Note: This example requires you to have the 'swap' module published before running. 
If you haven't published the 'swap' module, please publish the package using 
'aptos move create-resource-account-and-publish-package --seed 0 --address-name=swap --named-addresses deployer=[admin] --profile [admin]' first. 
[admin] is the account profile you will be using for this example. 

====== Account info ======

Admin's address is: 0xc0b7fa8173bb62556c1cd138c2d84fdabe86f5874ec6a3f36b3177c1c9b857e6
Admin's private key is: 0x88fef4956bd9155c217671ad14adb0ede4b19971a0cef82e4b94595575b96413
Swap address is: 0x7f3d84369e337f51bf738ceac7bc661f6ca190989d92405495d716db40b1033f

====== Create Fungible Asset -> (Dog and Cat coin) ======

Follow the steps to publish the Dog and Cat Coin module with Admin's address, and press enter. 
1. cd to /aptos-ts-sdk/examples/typescript/facoin folder 
2. run 'aptos move publish --named-address FACoin=[admin_address] --private-key=[private_key]
Fungible Asset:  [
  {
    icon_uri: 'http://example.com/favicon.ico',
    project_uri: 'http://example.com',
    supply_aggregator_table_handle_v1: null,
    supply_aggregator_table_key_v1: null,
    creator_address: '0xc0b7fa8173bb62556c1cd138c2d84fdabe86f5874ec6a3f36b3177c1c9b857e6',
    asset_type: '0x17c94be9519bcc24afc779041423b14b59c3af91f0a084fa9004a7e0a1434ef6',
    decimals: 8,
    last_transaction_timestamp: '2023-10-24T09:22:19',
    last_transaction_version: 6408698,
    name: 'CAT Coin',
    symbol: 'CAT Coin',
    token_standard: 'v2'
  },
  {
    icon_uri: 'http://example.com/favicon.ico',
    project_uri: 'http://example.com',
    supply_aggregator_table_handle_v1: null,
    supply_aggregator_table_key_v1: null,
    creator_address: '0xc0b7fa8173bb62556c1cd138c2d84fdabe86f5874ec6a3f36b3177c1c9b857e6',
    asset_type: '0x41d4a401c2e3ccab4576b138ecfdb83add0cd7d135e00a65e994433e1a2d1165',
    decimals: 8,
    last_transaction_timestamp: '2023-10-24T09:22:19',
    last_transaction_version: 6408698,
    name: 'DOG Coin',
    symbol: 'DOG Coin',
    token_standard: 'v2'
  }
]
Cat FACoin asset type: 0x17c94be9519bcc24afc779041423b14b59c3af91f0a084fa9004a7e0a1434ef6
Dog FACoin asset type: 0x41d4a401c2e3ccab4576b138ecfdb83add0cd7d135e00a65e994433e1a2d1165

====== Mint Dog and Cat Coin ======

minting 20_000_000 Dog coin...
Minting dog coin successful. -  0xd1e410127aa92d2f5f144b7c9aea52612473ea1c337a7a888b40f854768aea49
minting 30_000_000 Cat coin...
Minting cat coin successful. -  0xaf8c27a3ec8ed97bbc6a2b4373980022dfb0e67ef907ea33ac3083dc366e43c9

====== Current Balance ======

Admin's Dog coin balance: 20000000.
Admin's Cat coin balance: 30000000.

====== Create Liquidity Pool ======

initializing Lquidity Pool......
Init LP Pool success. -  0x66c43d41f8acc6a51e1151729fd776b614926a7443bd612e1b91e4818ac473af
Creating liquidity pool......
Creating liquidity pool successful. -  0x7399f7b15fb165dacb65de4b88307614d34faa0f17f4fec596640aaa10be4f51
Getting optimal LP amount......
Optimal LP amount:  [ '200000', '300000', '243948' ]
Adding liquidity......
Add liquidity succeed. -  0xf65c1c01d74c62aba7ed7dca397f2fbe452820a6bffd419d76bba8d646581257
Done.

====== Swap 100 Dog coins for Cat coins ======

Swaping 100 Dog coin to Cat coin......
Swap succeed. -  0x0f6683cc8e37d1ff929ecb2a0a51e5b153c3def9a05410765edec33c97522979
Swap finished.

====== Current Balance ======

Admin's Dog coin balance: 19799900.
Admin's Cat coin balance: 29700149.

Related Links

const payload: ViewRequestData = {
function: `0x${swap.toStringWithoutPrefix()}::router::optimal_liquidity_amounts`,
arguments: [
`0x${Hex.fromHexInput(token1Addr).toStringWithoutPrefix()}`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does just token1Addr.toString() here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not since its a HexInput. I will update the param type, then token1Addr.toString() will works here 😄

const rawTxn = await aptos.generateTransaction({
sender: deployer.accountAddress.toString(),
data: {
function: `0x${swap.toStringLongWithoutPrefix()}::router::add_liquidity_entry`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the 0 prefix should still work? toStringLongWithoutPrefix is a bit long instead of toString

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not about what will work or not. it is about the strict type we hold in the sdk. we ask the function to be of type 0x${string}::${string}::${string} but since swap.toString() is just a string (tho it does have 0x) typescript will complain that type dont match.
It is just something we need to change in the sdk

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed now.

Copy link
Contributor

@gregnazario gregnazario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some best practices and general use questions, but otherwise looks pretty good

authors = []

[addresses]
aptos_framework = "0x1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed FYI, it is inherited from AptosFramework.

Comment on lines 1 to 7
{
"function_id": "0x1::code::publish_package_txn",
"type_args": [],
"args": [
{
"type": "hex",
"value": "0x066661636f696e0100000000000000004044424644373036444337423244424346383030423741453236333539344541313937444532363537443844383942453244434138383642454437313330464439bd011f8b08000000000002ff3d8e410a83301045f7738a927da36e0b5d48c14b88c8988c1aac49c844dbe33769adbb799fff3ed37a540b4ed481c5952ef78b1851396305ec14d8389ba34a96b214805b9c5de094b41d408b5a076226ee007d74dc8f214dbc5c58b253be2b014dfd485b197b910c4d9eac26ab0cb1acb3d3fc950e261373718ed1f3ad2812cedb20955b8beffaf589031fa77281642a0808b4676945632d25e66dd026e4e8d75cdd4ec5f9d7a19f2ce0033e554261ff0000000203636174d40e1f8b08000000000002ffed59eb6edb3614fe9fa7605320930bd54e86b6ebd42468dab5db80a5019a0cc5500c0a2d51365789f4482a4e5ae4dd7778d385b6e274bda0c0961f814df25c78beef1c1ed293c9041da1efef53767f0f553caf4b82d41c2b94f16a4a1991a8c20ccf489e16359bd16949522c255108b31cd65096924b5c2d408a32c51167a4a76539270ce56451f22b92c73046b62660d18d08b4a46589a60465826045d90c61c4c8d2db44de26b2369754cdb50e34c722cf780e2b64bd589457e0092be82c460c572446f2aa9af232363ee624a3152ee5d8183e03619ce782488978617459d51966da0f3e5518769da30b8ad18ca8b4220ae758e16834464712dc93d4ecf6820849398b1155b069f0202760c47ab81060515c21a938181a6fb980bc3c7a0ef14a920ce2f2610bc15f2dc1fc42719916021c5f72f12e49fa714e920fa7a42c62740cf17d4de0c399c04c1644982fcf6ac1cc8763e7678c5e3af9232d7efd64d8109ffe45b2d6c089f97a9380db57cb04b3c15640aa3c4988105c046392ce1859195402004f925a158f8329be5010db275b6654c376a2231c90c12383f812941b002bfc0ea834c76ca6c3ae85811752a117af4eced29337af5ebc4e50fde8013a407b4eb99d3f3a3d7d71969efe71fcece4b704a0cd605ffbf5e3435838dd7e7e74b6ed56df7d0b78f25a64249d095e2f801dd59488c87c81c58321b6b1fd592f1bfdd9ecea175ee648904222c81cf044095e1a4a568035c406f2c5616d983c05ac758a006ffba1707b8580d69942c736777a34808491e81db972bcd37fda460ac6938659cd94b76aa7bb7c6b96685fecb467a099ba6e21fb9551457149df930e52260c6633863a66b33a00632786923bc9216d24cd28ec15e9a1d4a6518473703d413b9654a3ce964a9dc71a501d076edc0750763c08a6c6905497883cb5835659dc23c0e849a3703de11b4d7eda8ca684615812d6c9a851d6d0adf52eee4d5ad6270983221a8dfa733a4922c345a48bc8f62846937ba6daa17b93d595bdfd98a5b624868b1f9b395f22d7aa9a6ecf955a249389abf36338182605bea0b09731fc73aee8afb79677320bc10d1f3a622397698e0ecf4da00d57279a729326217cdae0b2e44b7b7c70a107ecd16198d5cf92718f279efe4090b0e0ce08f04ae3ebd744016c1d8668553e176e52e5d76c50d5cdbc9bd475d76d50e993cf313eb56903ca7d5a344aedcc0dea2a7e01a6799fd23beb0df4e9bbb6267d6840887b1b8fdb885eb7c40036ec4375088ac3b52fcc17942cdbcafa9a2850618ff7fe693fd059e866e53b697904b3a66d51732a7d33039f7c176379b4a8a725cd4c71eab709893b48f7fd897c1854286330f56eb538b8a2e2a2e8e6a39da7b66d18ac515edc09003e4e45eb40d43339ea576a537235104df0f479005e9a70d9b31562b7ae864344b884fe47a721920ba8220584046719af99ea8589c0d9660e70037950c2017d9e789ca069abb4b839a847a0ecef9ac2f8008356030b01ede31164c3fa66168eee5acdb9005ae5e994430bb3d4fc93fe7c30abc25ce5e912aa8f911e3a280893b55839282ea95432527cbdde02afc97c13b69df5de8fdb34b2b1eb280cf538c852dda79ae231a4b39f90cd5e6370cfa9bf761969ac2bde9efc674dd3b28143803ed73d203a2f047f4fd8392a2829f301e6788f56d803c255873f5f8f4c41b5deb90d877a810d80877d6ca6548f4b9196f97af40cd9d4eca5a1539f369d1df538d4a369a767d4bd64d8db6e62d1005bf411b289295f941c9daee076c4f0025f8a142178c69e5e1a79cb01604328bd148440570ff72d57ea91e4fa1a0c3730c6dbac405c776919a11764fd7d6505b3c2284e9dd615f4dc7803e0379fd09f947f6eb79b71d447bbad9f6951e25990824df6899aac42f93b2b86c1fc04246bf63f965f0ecb02ee6a6bc07c0345381778f959ce5d8de2d2295c45af2d9c415185f6b70fdc7fe6cc0da1f4c1bbd5d9e84a6d88e84faec1fe5c80baeeefe6febbc00186df7c6e7e8d5667a571be4583dc696cccae0c88b4aa6aa55f8af41306118465c4df4dcd9b067c3ef7713b1fb74db5be8c5ec0c517c689b48feb5ac65de7a96548400cb8d11ace58359495f0b46d883010f0f652a9a55a8234e336182b375cbb00727f672d416e4b1fad5da83b91bfd252991a47ecfd35767b6dafbabc88ccfc089e93ccb333642a111595fa653ecd09a3248fdab7df51075eb7eb59c9a7b8dc5fe7d761e3467823b75419f5df1f001415f967a803e46eedeeb1d75c5f60413ac5926650daf9b28db5130aa27dfba4ebbe8d3a5d41bab9d1ce9bc39a48ae17c55840285bc1a7bb9705ce48e795cedc4c9d701c9a8ad1deee6ea8724381f02c18ca64000c43d2442ba62c2ee8e0401b8dd1836e36f7bb9121773fc20b2add413de4478c1e76d43597d7e148f542ad03f72f6212e8e84624468fba8fab2b1dda47c4e4ce2704e5876e12ea3bda70407edced17d2a12473a16bd9e9b2eeee5b72098f524a3f6e605a42f98f30e4bd4af52f87b07af7f2e1eeeeee5e8c4a0e3fc951a341ff141626ed6a4dd990bb2656daa566fc5b4969fbfaa65dbb3155afb7aeb7fe01c6000b1d191e0000000003646f67d40e1f8b08000000000002ffed59eb6edb3614fe9fa7605b20930bd54e86b6ebd424687add80a5019a0cc5500c0a2d51365789f4482a4e5ae4dd7778d385b6e274bda0c0961f814df25c78beef1c1ed293c9041da21fef51766f17553caf4b82d41c2b94f16a4a1991a8c20ccf489e16359bd16949522c255108b31cd65096920b5c2d408a32c51167a4a76539270ce56451f24b92c73046b62660d18d08b4a46589a60465826045d90c61c4c8d2db44de26b2369754cdb50e34c722cf780e2b64bd589497e0092be82c460c572446f2b29af232363ee624a3152ee5d8183e05619ce782488978617459d51966da0f3e5518769da3738ad18ca8b4220ae758e16834468712dc93d4ecf69c0849398b1155b069f0202760c47ab81060515c22a938181a6fb980bc3c7c06f14a929ccfd0c72d047fb504f30bc5655a08707cc9c5fb24e9c739493e9e90b288d111c4f70d810fa70233591061be3cad05331f8e9c9f317ae9e40fb5f8d5e361437cfa17c95a03c7e6eb75026e5f2d13cc065b01a9f224214270118c493a6364655009003c496a553c0aa6f842416c1f6f99510ddbb18e7040068f0ce24b506e00acf07ba0d21cb3990ebb16065e48855ebc3e3e4d8fdfbe7ef12641f5c3fb681fed3ae576fef0e4e4c5697af2c7d1d3e3df128036837dedd58f0e60e1f4f6f3e357b7ddea3bef004f5e8b8ca433c1eb05b0a39a1211992fb07830c436b6aff4b2d19fcdae7ee1658e04292482cc014f94e0a5a1640558436c205f1cd686c953c05aa708f0b61f0ab75708689d29746473a74703481889de934bc73bfda76da4603c6998d54c79ab76bacbb76689f6c54e7b069aa9ab16b25f19551497f403e92065c2603663a86336ab0330766228b9951cd046d28cc25e911e4a6d1a453807d713b46d4935ea6ca9d479ac01d571e0c67d0065db83606a0c497589c8533b6895c53d028c1e370ad713bed1e4a7cd684a188625619d8c1a650ddd5aefe2dea4657d923028a2d1a83fa79324325c44ba88dc1ec56872d7543b7477b2bab2b71fb3d496c470f12333e74be45a55d3db73a516c964e2eafc180e864981cf29ec650cff9c2bfaeb8de59dcc4270c3878ed8c8659aa3c3331368c3d589a6dca449089f36b82cf9d21e1f5ce8017b741866f5b364dce389a73f10242cb83302bcd2f8fa3551005b87215a95cf85eb54f9351b547533ef3a75dd751b54fae4738c4f6dda80729f168d523b738dba8a9f8369dea7f4f67a037dfaaead491f1b10e2dec6e336a2572d31800d7b501d82e270e50bf33925cbb6b2be210a54d8e3bd7fda0f7416ba59f9415a1ec1ac695bd49c4adfccc027dfc5581e2dea694933539cfa6d42e20ed23d7f221f0415ca184cbd5b2d0eaea8b828baf968fb896d1b066b94177702808f53d13a10f54c8efa95da945c0d44133c7d1e8097265cf66c85d8adabe110112ea1ffd16988e402aa480121c159c66ba67a612270b69903dc401e9470409f271e2768da2a2d6e0eea1128fbbba6303ec0a0d5c04240fb7804d9b0be9985a3bb56732e8056793ae5d0c22c35ffa43f1fccaa305779ba84ea63a4870e0ac2642d560e8a0b2a958c145fafb7c06b32df846d7bbdf7e3368d6cec3a0a433d0eb254f7a9a6780ce9ec2764b3d718dc73eaaf5c461aeb8ab727ff69d3b46ce010a0cf750f88ce0ac13f1076860a4aca7c8039dea315f68070d5e1cfb7235350adb76fc2a15e6003e0611f9b29d5e352a465be1d3d4336357b69e8d4a74d67473d0ef568dae919752f19f6b69b5834c0167d846c62ca572547a72bb81931bcc0d72245089eb1a79746de7200d8104a2f0521d0d5c37dcb957a24b9be06c30d8cf1362b10d75d5a46e839597f5f59c1ac308a53a775053d37de00f8dd27f467e59fdbed661cf5d16eeb675a947816a460937da226ab50fece8a61303f03c99afd8fe5d7c3b280bbda1a30df4211ce055e7e917357a3b8740a57d16b0b675054a1fded03f79f397343287df06e7436ba521b22fadc35d85f0a50d7fd5ddf7f1738c0f0bbcfcd6fd1eaac34ce3768903b8d8dd99501915655adf44b917ec22082b08cf8bba979d380cf673e6e67e3b6a9d697d173b8f8c23891f6715dcbb8eb3cb50c098801375ac319ab86b2129eb60d110602de5e2ab5544b9066dc0663e5866b1740ee6faf25c84de9a3b50b752bf2575a2a53e388bdbfc66eafed55971791991fc173927976864c25a2a252bfcca7396194e451fbf63beac0eb763d2bf914977bebfc3a68dc086fe4962aa3fefb0380a222ff0cb58fdcaddd3df69aeb0b2c48a758d20c4a3b5fb6b1764241b46f9e74ddb751a72b483737da79735813c9f5a2180b08652bf864e7a2c019e9bcd2999ba9138e435331daddd909556e28109e0543990c8061489a68c594c505edef6ba331badfcde67e3732e4ee277841a53ba887fc88d1838ebae6f23a1ca95ea875e0fe454c021ddd88c4e861f77175a543fb8498dcfa8ca0fcd44d427d471b0ec8cf3bfd423a94642e742d3b5dd6dd79472ee0514ae9c70d4c4b28ff1186bc57a9fee51056ef5c3cd8d9d9d98d51c933ac9fde614cff141626ed6a4dd990bb2656daa566fc7b4969fbfaa65dbb3655afb6aeb6fe010ca47f45191e000000000300000000000000000000000000000000000000000000000000000000000000010e4170746f734672616d65776f726b00000000000000000000000000000000000000000000000000000000000000010b4170746f735374646c696200000000000000000000000000000000000000000000000000000000000000010a4d6f76655374646c696200"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, what is this file? A format file for deploying packages?

Perhaps we want to make a github issue to autogenerate this from the CLI for easier deployment via TS and others.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is output of aptos move build-publish-payload
--named-addresses abc=$abc
--json-output-file publication.json
--included-artifacts none

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, actually I don't need this file anymore. Will remove.

Originally I was thinking about reading the bytecode hex from this json file and port it into my swap example

examples/typescript/swap.ts Outdated Show resolved Hide resolved
token2Addr: HexInput,
): Promise<void> => {
const payload: ViewRequestData = {
function: `0x${swap.toStringWithoutPrefix()}::router::optimal_liquidity_amounts`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout here for improvements to the SDK to remove the 0x in the pattern

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure you mean. There's still a 0x at the start of the format string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's something we have to fix in the SDK, which adds the protection of some inputs as 0x${string}, however if you don't type the output of swap.toStringWithoutPrefix{} to 0x${string}, it won't accept it (even if it always outputs 0x12345)

We tried something fancy, and it made it kinda messy

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the API not accept module addresses without 0x prefix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, but we're trying to get rid of the variability

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually fixed now, so we can get rid of the 0x weird hack.

Comment on lines 45 to 47
"300000",
"200",
"300",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these U64s?

I suspect one of the things we should probably fix on the API side for View functions is the encoding, and allow BCS encoding of inputs, and use the ABI for translation.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

},
});

if (data.length !== 2) throw new Error("Expected two Fungible Asset.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (data.length !== 2) throw new Error("Expected two Fungible Asset.");
if (data.length !== 2) throw new Error("Expected two Fungible Assets.");

Did you also want to verify the fungible assets are different?

I don't think a liquidity pool with the same exact assets make any sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep correct. Hence I did created 2 different module (Cat and Dog) to separate out the two different fungible asset.

Good call here, will check and make sure these two assets are differents.

const data = await aptos.getCurrentFungibleAssetBalances({
options: {
where: {
owner_address: { _eq: owner.accountAddress.toString() },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should note here, I don't think that addresses are stored in shorter mode, making sure that the account addresses match?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or better we could provide an eq implementation that ignores 0x prefix (similar to equalIgnoreCase)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is GraphQL, so it's direct to the DB, without having a service that fronts the indexer and does conversions, it's going to have to match exactly

Copy link

@movekevin movekevin Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query can be modified to do an OR with the WHERE clause right? We can technically do a = without 0x or with 0x

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the owner_address that stored in DB are always prefixed with 0x

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .toString() here actually coming from Hex.toString() that dport implemented, so it should always contains the 0x prefix 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't just need the 0x, it actually needs the full 0's so you'll need to use toLongString(), double check with the indexer folks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh interesting. I thought toString() is already converting to long string which contains all of the leading zeros if there any.

Ok, we probably need to implement a toLongString() then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I just realized we already have one on AccountAddress, I was looking at the wrong class Hex lol all fixed now #134


console.log("====== Account info ======\n");
console.log(`Admin's address is: ${admin.accountAddress.toString()}`);
console.log(`Admin's private key is: ${admin.privateKey.toString()}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, we should not print out private keys in examples. Being as we should show proper best practices.

Instead, you can show the public key, or just the address.

examples/typescript/swap.ts Show resolved Hide resolved
examples/typescript/swap.ts Show resolved Hide resolved
/// A 2-in-1 module that combines managed_fungible_asset and coin_example into one module that when deployed, the
/// deployer will be creating a new managed fungible asset with the hardcoded supply config, name, symbol, and decimals.
/// The address of the asset can be obtained via get_metadata(). As a simple version, it only deals with primary stores.
module FACoin::cat {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a decent amount of duplication between this and dog module. I think one of @lightmark's examples can help?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, so I am not sure if creating two identical FA and doing swap between them works. Any idea? @movekevin

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That shouldn't matter :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting! Though I feel like the user might get confused, and cause some confusion.

@movekevin Shall we keep it as two separate module?

Copy link

@movekevin movekevin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would say are the highlights in terms of how v2 is easier or harder to write the same code as v1?

@gregnazario
Copy link
Contributor

I would also note, that private key you just used for this example (since it's now logged here), you don't use again @0xjinn

@movekevin
Copy link

This is very insightful so far. Amazing job @0xjinn !

): Promise<void> => {
const payload: ViewRequestData = {
function: `0x${swap.toStringWithoutPrefix()}::router::optimal_liquidity_amounts`,
arguments: [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was renamed to functionArguments, make sure you are working against the latest local sdk

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not arguments to make it shorter? 😜

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user feedback

Copy link
Contributor

@xbtmatt xbtmatt Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@movekevin it was because arguments is a reserved keyword in strict mode, so you couldn't easily deconstruct an args struct without having to rename. You had to do this:

const { ..., arguments: functionArguments, ... } = args;
// use functionArguments...

if it leads to issues down the road we can change it back :p

const rawTxn = await aptos.generateTransaction({
sender: deployer.accountAddress.toString(),
data: {
function: `0x${swap.toStringLongWithoutPrefix()}::router::add_liquidity_entry`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not about what will work or not. it is about the strict type we hold in the sdk. we ask the function to be of type 0x${string}::${string}::${string} but since swap.toString() is just a string (tho it does have 0x) typescript will complain that type dont match.
It is just something we need to change in the sdk

@0xmigo
Copy link
Contributor Author

0xmigo commented Oct 25, 2023

Address comments

  1. Rebase to main to update arguments to functionArugments
  2. Remove the private key console.log
  3. update FA token from HexInput to AccountAddress
  4. others.

recipient: AccountAddress,
): Promise<string> => {
const rawTxn = await aptos.generateTransaction({
sender: deployer.accountAddress.toString(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required even with the signAndSubmitTransaction call on line 79?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep this is required. I think this is basically generating raw txn in bytecode, and we need the address to specify where the module gonna live in.

@0xmigo 0xmigo enabled auto-merge (squash) October 26, 2023 17:08
Copy link
Contributor

@gregnazario gregnazario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the long string check

token2Addr: HexInput,
): Promise<void> => {
const payload: ViewRequestData = {
function: `0x${swap.toStringWithoutPrefix()}::router::optimal_liquidity_amounts`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually fixed now, so we can get rid of the 0x weird hack.

const rawTxn = await aptos.generateTransaction({
sender: deployer.accountAddress.toString(),
data: {
function: `0x${swap.toStringLongWithoutPrefix()}::router::add_liquidity_entry`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed now.

const data = await aptos.getCurrentFungibleAssetBalances({
options: {
where: {
owner_address: { _eq: owner.accountAddress.toString() },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

owner_address is stored as a full hex, with a 0x in front. So, this likely needs to be .toLongString()

@0xmigo 0xmigo merged commit 142adda into main Oct 26, 2023
8 checks passed
@0xmigo 0xmigo deleted the jin/swap_example branch October 26, 2023 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants