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

[tokenvm] Fix Demo #940

Merged
merged 6 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
83 changes: 41 additions & 42 deletions examples/tokenvm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,16 @@ command from this location:
When you are done, the output should look something like this:
```
database: .token-cli
address: token1rvzhmceq997zntgvravfagsks6w0ryud3rylh4cdvayry0dl97nsjzf3yp
chainID: Em2pZtHr7rDCzii43an2bBi1M2mTFyLN33QP1Xfjy7BcWtaH9
metadata (can be changed later): MarioCoin
address: token1qrzvk4zlwj9zsacqgtufx7zvapd3quufqpxk5rsdd4633m4wz2fdj73w34s
chainID: 2btpGNEt7pGXxYUMk7Pp3TTiq8ij4JsrDxTv9oNB46wpwqLVQ9
symbol: MARIO
decimals: 2
metadata: its a me, mario
continue (y/n): y
✅ txID: 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug
✅ txID: o7bJT3aRwgLR19c2avPSKBNjdrCsPgxBXA3oc1MeLvvEKKNP7
assetID: 2r9rd3oUGfir4pnmkyy7aBUcScQrkuqbYjdmxaJDAe1pb2Je8u
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is hash(txID, 0)

```

_`txID` is the `assetID` of your new asset._

The "loaded address" here is the address of the default private key (`demo.pk`). We
use this key to authenticate all interactions with the `tokenvm`.

Expand All @@ -170,14 +171,13 @@ When you are done, the output should look something like this (usually easiest
just to mint to yourself).
```
database: .token-cli
address: token1rvzhmceq997zntgvravfagsks6w0ryud3rylh4cdvayry0dl97nsjzf3yp
chainID: Em2pZtHr7rDCzii43an2bBi1M2mTFyLN33QP1Xfjy7BcWtaH9
assetID: 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug
metadata: MarioCoin supply: 0
recipient: token1rvzhmceq997zntgvravfagsks6w0ryud3rylh4cdvayry0dl97nsjzf3yp
amount: 10000
address: token1qrzvk4zlwj9zsacqgtufx7zvapd3quufqpxk5rsdd4633m4wz2fdj73w34s
chainID: 2btpGNEt7pGXxYUMk7Pp3TTiq8ij4JsrDxTv9oNB46wpwqLVQ9
assetID: 2r9rd3oUGfir4pnmkyy7aBUcScQrkuqbYjdmxaJDAe1pb2Je8u
symbol: MARIO decimals: 2 metadata: its a me, mario supply: 0
amount: 100
continue (y/n): y
✅ txID: X1E5CVFgFFgniFyWcj5wweGg66TyzjK2bMWWTzFwJcwFYkF72
✅ txID: Zrq8CUXeXQqjX97QnNAd4RRi2SiWz14cHyMh16NjQ15vFakPg
```

#### Step 3: View Your Balance
Expand All @@ -190,15 +190,16 @@ so by running the following command from this location:
When you are done, the output should look something like this:
```
database: .token-cli
address: token1rvzhmceq997zntgvravfagsks6w0ryud3rylh4cdvayry0dl97nsjzf3yp
chainID: Em2pZtHr7rDCzii43an2bBi1M2mTFyLN33QP1Xfjy7BcWtaH9
assetID (use TKN for native token): 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug
metadata: MarioCoin supply: 10000
balance: 10000 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug
address: token1qrzvk4zlwj9zsacqgtufx7zvapd3quufqpxk5rsdd4633m4wz2fdj73w34s
chainID: 2btpGNEt7pGXxYUMk7Pp3TTiq8ij4JsrDxTv9oNB46wpwqLVQ9
assetID (use TKN for native token): 2r9rd3oUGfir4pnmkyy7aBUcScQrkuqbYjdmxaJDAe1pb2Je8u
uri: http://127.0.0.1:9652/ext/bc/2btpGNEt7pGXxYUMk7Pp3TTiq8ij4JsrDxTv9oNB46wpwqLVQ9
symbol: MARIO decimals: 2 metadata: its a me, mario supply: 10000
balance: 100.00 MARIO
```

#### Step 4: Create an Order
So, we have some of our token (`MarioCoin`)...now what? Let's put an order
So, we have some of our token (`MARIO`)...now what? Let's put an order
on-chain that will allow someone to trade the native token (`TKN`) for some.
You can do so by running the following command from this location:
```bash
Expand All @@ -208,21 +209,20 @@ You can do so by running the following command from this location:
When you are done, the output should look something like this:
```
database: .token-cli
address: token1rvzhmceq997zntgvravfagsks6w0ryud3rylh4cdvayry0dl97nsjzf3yp
chainID: Em2pZtHr7rDCzii43an2bBi1M2mTFyLN33QP1Xfjy7BcWtaH9
address: token1qrzvk4zlwj9zsacqgtufx7zvapd3quufqpxk5rsdd4633m4wz2fdj73w34s
chainID: 2btpGNEt7pGXxYUMk7Pp3TTiq8ij4JsrDxTv9oNB46wpwqLVQ9
in assetID (use TKN for native token): TKN
in tick: 1
out assetID (use TKN for native token): 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug
metadata: MarioCoin supply: 10000
balance: 10000 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug
in tick: 1
out assetID (use TKN for native token): 2r9rd3oUGfir4pnmkyy7aBUcScQrkuqbYjdmxaJDAe1pb2Je8u
symbol: MARIO decimals: 2 metadata: its a me, mario supply: 10000
balance: 100.00 MARIO
out tick: 10
supply (must be multiple of out tick): 100
continue (y/n): y
✅ txID: 2TdeT2ZsQtJhbWJuhLZ3eexuCY4UP6W7q5ZiAHMYtVfSSp1ids
✅ txID: 23xyvTKHZUy9ym2VC8ysdsaRz9xbLyfuUoRgGXn6zmotuvxw2e
orderID: 2hJdJV2JisA1ESQ2SbGapjHn9ZetouU6TyptMNLsRgufQK1hVc
```

_`txID` is the `orderID` of your new order._

The "in tick" is how much of the "in assetID" that someone must trade to get
"out tick" of the "out assetID". Any fill of this order must send a multiple of
"in tick" to be considered valid (this avoids ANY sort of precision issues with
Expand All @@ -238,19 +238,19 @@ following command from this location:
When you are done, the output should look something like this:
```
database: .token-cli
address: token1rvzhmceq997zntgvravfagsks6w0ryud3rylh4cdvayry0dl97nsjzf3yp
chainID: Em2pZtHr7rDCzii43an2bBi1M2mTFyLN33QP1Xfjy7BcWtaH9
address: token1qrzvk4zlwj9zsacqgtufx7zvapd3quufqpxk5rsdd4633m4wz2fdj73w34s
chainID: 2btpGNEt7pGXxYUMk7Pp3TTiq8ij4JsrDxTv9oNB46wpwqLVQ9
in assetID (use TKN for native token): TKN
balance: 997.999993843 TKN
out assetID (use TKN for native token): 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug
metadata: MarioCoin supply: 10000
balance: 9999999999.999856949 TKN
out assetID (use TKN for native token): 2r9rd3oUGfir4pnmkyy7aBUcScQrkuqbYjdmxaJDAe1pb2Je8u
symbol: MARIO decimals: 2 metadata: its a me, mario supply: 10000
available orders: 1
0) Rate(in/out): 100000000.0000 InTick: 1.000000000 TKN OutTick: 10 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug Remaining: 100 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug
select order: 0
0) Rate(in/out): 1000000.0000 InTick: 1.000000000 TKN OutTick: 10.00 MARIO Remaining: 100.00 MARIO
select order: 0 [auto-selected]
value (must be multiple of in tick): 2
in: 2.000000000 TKN out: 20 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug
in: 2.000000000 TKN out: 20.00 MARIO
continue (y/n): y
✅ txID: uw9YrZcs4QQTEBSR3guVnzQTFyKKm5QFGVTvuGyntSTrx3aGm
✅ txID: B2MCLPTHouQMzd2dfxod2qiJC8p58vSahSDGVhZEJCCvQpzXB
```

Note how all available orders for this pair are listed by the CLI (these come
Expand All @@ -267,12 +267,11 @@ location:
When you are done, the output should look something like this:
```
database: .token-cli
address: token1rvzhmceq997zntgvravfagsks6w0ryud3rylh4cdvayry0dl97nsjzf3yp
chainID: Em2pZtHr7rDCzii43an2bBi1M2mTFyLN33QP1Xfjy7BcWtaH9
orderID: 2TdeT2ZsQtJhbWJuhLZ3eexuCY4UP6W7q5ZiAHMYtVfSSp1ids
out assetID (use TKN for native token): 27grFs9vE2YP9kwLM5hQJGLDvqEY9ii71zzdoRHNGC4Appavug
address: token1qrzvk4zlwj9zsacqgtufx7zvapd3quufqpxk5rsdd4633m4wz2fdj73w34s
orderID: 2hJdJV2JisA1ESQ2SbGapjHn9ZetouU6TyptMNLsRgufQK1hVc
out assetID (use TKN for native token): 2r9rd3oUGfir4pnmkyy7aBUcScQrkuqbYjdmxaJDAe1pb2Je8u
continue (y/n): y
✅ txID: poGnxYiLZAruurNjugTPfN1JjwSZzGZdZnBEezp5HB98PhKcn
✅ txID: KuPqXinaS2H7uDLh6LriPCMyn8DzNQP9XZ8Lv7wWjY88sDK4A
```

Any funds that were locked up in the order will be returned to the creator's
Expand Down
74 changes: 44 additions & 30 deletions examples/tokenvm/cmd/token-cli/cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/examples/tokenvm/actions"
"github.com/ava-labs/hypersdk/utils"

frpc "github.com/ava-labs/hypersdk/examples/tokenvm/cmd/token-faucet/rpc"
tconsts "github.com/ava-labs/hypersdk/examples/tokenvm/consts"
hutils "github.com/ava-labs/hypersdk/utils"
)

var actionCmd = &cobra.Command{
Expand Down Expand Up @@ -72,14 +72,14 @@ var fundFaucetCmd = &cobra.Command{
if err != nil {
return err
}
if err = sendAndWait(ctx, []chain.Action{&actions.Transfer{
if _, err = sendAndWait(ctx, []chain.Action{&actions.Transfer{
To: addr,
Asset: ids.Empty,
Value: amount,
}}, cli, scli, tcli, factory, true); err != nil {
return err
}
hutils.Outf("{{green}}funded faucet:{{/}} %s\n", faucetAddress)
utils.Outf("{{green}}funded faucet:{{/}} %s\n", faucetAddress)
return nil
},
}
Expand Down Expand Up @@ -122,7 +122,7 @@ var transferCmd = &cobra.Command{
}

// Generate transaction
err = sendAndWait(ctx, []chain.Action{&actions.Transfer{
_, err = sendAndWait(ctx, []chain.Action{&actions.Transfer{
To: recipient,
Asset: assetID,
Value: amount,
Expand Down Expand Up @@ -165,12 +165,19 @@ var createAssetCmd = &cobra.Command{
}

// Generate transaction
err = sendAndWait(ctx, []chain.Action{&actions.CreateAsset{
txID, err := sendAndWait(ctx, []chain.Action{&actions.CreateAsset{
Symbol: []byte(symbol),
Decimals: uint8(decimals), // already constrain above to prevent overflow
Metadata: []byte(metadata),
}}, cli, scli, tcli, factory, true)
return err
if err != nil {
return err
}

// Print assetID
assetID := chain.CreateActionID(txID, 0)
utils.Outf("{{green}}assetID:{{/}} %s\n", assetID)
return nil
},
}

Expand All @@ -193,17 +200,17 @@ var mintAssetCmd = &cobra.Command{
return err
}
if !exists {
hutils.Outf("{{red}}%s does not exist{{/}}\n", assetID)
hutils.Outf("{{red}}exiting...{{/}}\n")
utils.Outf("{{red}}%s does not exist{{/}}\n", assetID)
utils.Outf("{{red}}exiting...{{/}}\n")
return nil
}
if owner != codec.MustAddressBech32(tconsts.HRP, priv.Address) {
hutils.Outf("{{red}}%s is the owner of %s, you are not{{/}}\n", owner, assetID)
hutils.Outf("{{red}}exiting...{{/}}\n")
utils.Outf("{{red}}%s is the owner of %s, you are not{{/}}\n", owner, assetID)
utils.Outf("{{red}}exiting...{{/}}\n")
return nil
}
hutils.Outf(
"{{yellow}}symbol:{{/}} %s {{yellow}}decimals:{{/}} %s {{yellow}}metadata:{{/}} %s {{yellow}}supply:{{/}} %d\n",
utils.Outf(
"{{yellow}}symbol:{{/}} %s {{yellow}}decimals:{{/}} %d {{yellow}}metadata:{{/}} %s {{yellow}}supply:{{/}} %d\n",
string(symbol),
decimals,
string(metadata),
Expand All @@ -229,7 +236,7 @@ var mintAssetCmd = &cobra.Command{
}

// Generate transaction
err = sendAndWait(ctx, []chain.Action{&actions.MintAsset{
_, err = sendAndWait(ctx, []chain.Action{&actions.MintAsset{
Asset: assetID,
To: recipient,
Value: amount,
Expand Down Expand Up @@ -266,7 +273,7 @@ var closeOrderCmd = &cobra.Command{
}

// Generate transaction
err = sendAndWait(ctx, []chain.Action{&actions.CloseOrder{
_, err = sendAndWait(ctx, []chain.Action{&actions.CloseOrder{
Order: orderID,
Out: outAssetID,
}}, cli, scli, tcli, factory, true)
Expand Down Expand Up @@ -294,11 +301,11 @@ var createOrderCmd = &cobra.Command{
}
if inAssetID != ids.Empty {
if !exists {
hutils.Outf("{{red}}%s does not exist{{/}}\n", inAssetID)
hutils.Outf("{{red}}exiting...{{/}}\n")
utils.Outf("{{red}}%s does not exist{{/}}\n", inAssetID)
utils.Outf("{{red}}exiting...{{/}}\n")
return nil
}
hutils.Outf(
utils.Outf(
"{{yellow}}symbol:{{/}} %s {{yellow}}decimals:{{/}} %d {{yellow}}metadata:{{/}} %s {{yellow}}supply:{{/}} %d\n",
string(symbol),
decimals,
Expand Down Expand Up @@ -352,14 +359,21 @@ var createOrderCmd = &cobra.Command{
}

// Generate transaction
err = sendAndWait(ctx, []chain.Action{&actions.CreateOrder{
txID, err := sendAndWait(ctx, []chain.Action{&actions.CreateOrder{
In: inAssetID,
InTick: inTick,
Out: outAssetID,
OutTick: outTick,
Supply: supply,
}}, cli, scli, tcli, factory, true)
return err
if err != nil {
return err
}

// Print orderID
orderID := chain.CreateActionID(txID, 0)
utils.Outf("{{green}}orderID:{{/}} %s\n", orderID)
return nil
},
}

Expand Down Expand Up @@ -398,26 +412,26 @@ var fillOrderCmd = &cobra.Command{
return err
}
if len(orders) == 0 {
hutils.Outf("{{red}}no available orders{{/}}\n")
hutils.Outf("{{red}}exiting...{{/}}\n")
utils.Outf("{{red}}no available orders{{/}}\n")
utils.Outf("{{red}}exiting...{{/}}\n")
return nil
}
hutils.Outf("{{cyan}}available orders:{{/}} %d\n", len(orders))
utils.Outf("{{cyan}}available orders:{{/}} %d\n", len(orders))
max := 20
if len(orders) < max {
max = len(orders)
}
for i := 0; i < max; i++ {
order := orders[i]
hutils.Outf(
utils.Outf(
"%d) {{cyan}}Rate(in/out):{{/}} %.4f {{cyan}}InTick:{{/}} %s %s {{cyan}}OutTick:{{/}} %s %s {{cyan}}Remaining:{{/}} %s %s\n", //nolint:lll
i,
float64(order.InTick)/float64(order.OutTick),
hutils.FormatBalance(order.InTick, inDecimals),
utils.FormatBalance(order.InTick, inDecimals),
inSymbol,
hutils.FormatBalance(order.OutTick, outDecimals),
utils.FormatBalance(order.OutTick, outDecimals),
outSymbol,
hutils.FormatBalance(order.Remaining, outDecimals),
utils.FormatBalance(order.Remaining, outDecimals),
outSymbol,
)
}
Expand Down Expand Up @@ -451,11 +465,11 @@ var fillOrderCmd = &cobra.Command{
}
multiples := value / order.InTick
outAmount := multiples * order.OutTick
hutils.Outf(
utils.Outf(
"{{orange}}in:{{/}} %s %s {{orange}}out:{{/}} %s %s\n",
hutils.FormatBalance(value, inDecimals),
utils.FormatBalance(value, inDecimals),
inSymbol,
hutils.FormatBalance(outAmount, outDecimals),
utils.FormatBalance(outAmount, outDecimals),
outSymbol,
)

Expand All @@ -469,7 +483,7 @@ var fillOrderCmd = &cobra.Command{
if err != nil {
return err
}
err = sendAndWait(ctx, []chain.Action{&actions.FillOrder{
_, err = sendAndWait(ctx, []chain.Action{&actions.FillOrder{
Order: order.ID,
Owner: owner,
In: inAssetID,
Expand Down
17 changes: 10 additions & 7 deletions examples/tokenvm/cmd/token-cli/cmd/resolutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"reflect"

"github.com/ava-labs/avalanchego/ids"

"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/cli"
"github.com/ava-labs/hypersdk/codec"
Expand All @@ -23,38 +25,39 @@ import (
func sendAndWait(
ctx context.Context, actions []chain.Action, cli *rpc.JSONRPCClient,
scli *rpc.WebSocketClient, tcli *trpc.JSONRPCClient, factory chain.AuthFactory, printStatus bool,
) error {
) (ids.ID, error) {
parser, err := tcli.Parser(ctx)
if err != nil {
return err
return ids.Empty, err
}
_, tx, _, err := cli.GenerateTransaction(ctx, parser, actions, factory)
if err != nil {
return err
return ids.Empty, err
}

if err := scli.RegisterTx(tx); err != nil {
return err
return ids.Empty, err
}
var res *chain.Result
for {
txID, dErr, result, err := scli.ListenTx(ctx)
if dErr != nil {
return dErr
return ids.Empty, dErr
}
if err != nil {
return err
return ids.Empty, err
}
if txID == tx.ID() {
res = result
break
}
// TODO: don't drop these results (may be needed by a different connection)
utils.Outf("{{yellow}}skipping unexpected transaction:{{/}} %s\n", tx.ID())
}
if printStatus {
handler.Root().PrintStatus(tx.ID(), res.Success)
}
return nil
return tx.ID(), nil
}

func handleTx(c *trpc.JSONRPCClient, tx *chain.Transaction, result *chain.Result) {
Expand Down
Loading
Loading