diff --git a/chain/mock_action.go b/chain/mock_action.go index 7ccb1a4604..0d28be63e5 100644 --- a/chain/mock_action.go +++ b/chain/mock_action.go @@ -46,12 +46,12 @@ func (m *MockAction) EXPECT() *MockActionMockRecorder { } // Execute mocks base method. -func (m *MockAction) Execute(arg0 context.Context, arg1 Rules, arg2 state.Mutable, arg3 int64, arg4 codec.Address, arg5 ids.ID) (bool, uint64, []byte, error) { +func (m *MockAction) Execute(arg0 context.Context, arg1 Rules, arg2 state.Mutable, arg3 int64, arg4 codec.Address, arg5 codec.LID) (bool, uint64, [][]byte, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Execute", arg0, arg1, arg2, arg3, arg4, arg5) ret0, _ := ret[0].(bool) ret1, _ := ret[1].(uint64) - ret2, _ := ret[2].([]byte) + ret2, _ := ret[2].([][]byte) ret3, _ := ret[3].(error) return ret0, ret1, ret2, ret3 } @@ -63,10 +63,10 @@ func (mr *MockActionMockRecorder) Execute(arg0, arg1, arg2, arg3, arg4, arg5 any } // GetActionID mocks base method. -func (m *MockAction) GetActionID(arg0 byte, arg1 ids.ID) codec.Address { +func (m *MockAction) GetActionID(arg0 byte, arg1 ids.ID) codec.LID { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetActionID", arg0, arg1) - ret0, _ := ret[0].(codec.Address) + ret0, _ := ret[0].(codec.LID) return ret0 } @@ -131,7 +131,7 @@ func (mr *MockActionMockRecorder) Size() *gomock.Call { } // StateKeys mocks base method. -func (m *MockAction) StateKeys(arg0, arg1 codec.Address) state.Keys { +func (m *MockAction) StateKeys(arg0 codec.Address, arg1 codec.LID) state.Keys { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "StateKeys", arg0, arg1) ret0, _ := ret[0].(state.Keys) diff --git a/cli/spam.go b/cli/spam.go index 41a5b3a1b9..be0e29a334 100644 --- a/cli/spam.go +++ b/cli/spam.go @@ -186,7 +186,7 @@ func (h *Handler) Spam( } if !result.Success { // Should never happen - return fmt.Errorf("%w: %s", ErrTxFailed, result.Output) + return fmt.Errorf("%w: %s", ErrTxFailed, result.Outputs) } } var recipientFunc func() (*PrivateKey, error) @@ -446,7 +446,7 @@ func (h *Handler) Spam( } if !result.Success { // Should never happen - return fmt.Errorf("%w: %s", ErrTxFailed, result.Output) + return fmt.Errorf("%w: %s", ErrTxFailed, result.Outputs) } } utils.Outf( @@ -492,7 +492,11 @@ func startIssuer(cctx context.Context, issuer *txIssuer) { if result.Success { confirmedTxs++ } else { - utils.Outf("{{orange}}on-chain tx failure:{{/}} %s %t\n", string(result.Output), result.Success) + for i := 0; i < len(result.Outputs); i++ { + for j := 0; j < len(result.Outputs[i]); j++ { + utils.Outf("{{orange}}on-chain tx failure:{{/}} %s %t\n", string(result.Outputs[i][j]), result.Success) + } + } } } else { // We can't error match here because we receive it over the wire. diff --git a/examples/morpheusvm/cmd/morpheus-cli/cmd/key.go b/examples/morpheusvm/cmd/morpheus-cli/cmd/key.go index 43f2b99b8d..c876fa707e 100644 --- a/examples/morpheusvm/cmd/morpheus-cli/cmd/key.go +++ b/examples/morpheusvm/cmd/morpheus-cli/cmd/key.go @@ -217,7 +217,7 @@ var setKeyCmd = &cobra.Command{ }, } -func lookupKeyBalance(addr codec.Address, uri string, networkID uint32, chainID ids.ID, _ ids.ID) error { +func lookupKeyBalance(addr codec.Address, uri string, networkID uint32, chainID ids.ID, _ codec.LID) error { _, err := handler.GetBalance(context.TODO(), brpc.NewJSONRPCClient(uri, networkID, chainID), addr) return err } diff --git a/examples/morpheusvm/cmd/morpheus-cli/cmd/resolutions.go b/examples/morpheusvm/cmd/morpheus-cli/cmd/resolutions.go index 215a419c99..dba02cee15 100644 --- a/examples/morpheusvm/cmd/morpheus-cli/cmd/resolutions.go +++ b/examples/morpheusvm/cmd/morpheus-cli/cmd/resolutions.go @@ -57,28 +57,32 @@ func sendAndWait( } func handleTx(tx *chain.Transaction, result *chain.Result) { - summaryStr := string(result.Output) - actor := tx.Auth.Actor() - status := "❌" - if result.Success { - status = "✅" - for _, action := range tx.Actions { - switch act := action.(type) { //nolint:gocritic - case *actions.Transfer: - summaryStr = fmt.Sprintf("%s %s -> %s", utils.FormatBalance(act.Value, consts.Decimals), consts.Symbol, codec.MustAddressBech32(consts.HRP, act.To)) + for i := 0; i < len(result.Outputs); i++ { + for j := 0; j < len(result.Outputs[i]); j++ { + summaryStr := string(result.Outputs[i][j]) + actor := tx.Auth.Actor() + status := "❌" + if result.Success { + status = "✅" + for _, action := range tx.Actions { + switch act := action.(type) { //nolint:gocritic + case *actions.Transfer: + summaryStr = fmt.Sprintf("%s %s -> %s", utils.FormatBalance(act.Value, consts.Decimals), consts.Symbol, codec.MustAddressBech32(consts.HRP, act.To)) + } + } } + utils.Outf( + "%s {{yellow}}%s{{/}} {{yellow}}actor:{{/}} %s {{yellow}}summary (%s):{{/}} [%s] {{yellow}}fee (max %.2f%%):{{/}} %s %s {{yellow}}consumed:{{/}} [%s]\n", + status, + tx.ID(), + codec.MustAddressBech32(consts.HRP, actor), + reflect.TypeOf(tx.Actions), + summaryStr, + float64(result.Fee)/float64(tx.Base.MaxFee)*100, + utils.FormatBalance(result.Fee, consts.Decimals), + consts.Symbol, + cli.ParseDimensions(result.Consumed), + ) } } - utils.Outf( - "%s {{yellow}}%s{{/}} {{yellow}}actor:{{/}} %s {{yellow}}summary (%s):{{/}} [%s] {{yellow}}fee (max %.2f%%):{{/}} %s %s {{yellow}}consumed:{{/}} [%s]\n", - status, - tx.ID(), - codec.MustAddressBech32(consts.HRP, actor), - reflect.TypeOf(tx.Actions), - summaryStr, - float64(result.Fee)/float64(tx.Base.MaxFee)*100, - utils.FormatBalance(result.Fee, consts.Decimals), - consts.Symbol, - cli.ParseDimensions(result.Consumed), - ) } diff --git a/examples/morpheusvm/tests/load/load_test.go b/examples/morpheusvm/tests/load/load_test.go index 3fef5700cc..629d579b57 100644 --- a/examples/morpheusvm/tests/load/load_test.go +++ b/examples/morpheusvm/tests/load/load_test.go @@ -411,7 +411,11 @@ var _ = ginkgo.Describe("load tests vm", func() { for _, result := range blk.Results() { if !result.Success { unitPrices, _ := instances[0].cli.UnitPrices(context.Background(), false) - fmt.Println("tx failed", "unit prices:", unitPrices, "consumed:", result.Consumed, "fee:", result.Fee, "output:", string(result.Output)) + for i := 0; i < len(result.Outputs); i++ { + for j := 0; j < len(result.Outputs[i]); j++ { + fmt.Println("tx failed", "unit prices:", unitPrices, "consumed:", result.Consumed, "fee:", result.Fee, "output:", string(result.Outputs[i][j])) + } + } } gomega.Ω(result.Success).Should(gomega.BeTrue()) } diff --git a/examples/tokenvm/cmd/token-cli/cmd/resolutions.go b/examples/tokenvm/cmd/token-cli/cmd/resolutions.go index b798ad6197..3c36404c34 100644 --- a/examples/tokenvm/cmd/token-cli/cmd/resolutions.go +++ b/examples/tokenvm/cmd/token-cli/cmd/resolutions.go @@ -57,88 +57,92 @@ func sendAndWait( } func handleTx(c *trpc.JSONRPCClient, tx *chain.Transaction, result *chain.Result) { - summaryStr := string(result.Output) - actor := tx.Auth.Actor() - status := "❌" - if result.Success { - status = "✅" - for i, act := range tx.Actions { - switch action := act.(type) { - case *actions.CreateAsset: - assetID := action.GetActionID(uint8(i), tx.ID()) - summaryStr = fmt.Sprintf("assetID: %s symbol: %s decimals: %d metadata: %s", assetID, action.Symbol, action.Decimals, action.Metadata) - case *actions.MintAsset: - _, symbol, decimals, _, _, _, err := c.Asset(context.TODO(), action.Asset, true) - if err != nil { - utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) - return - } - amountStr := utils.FormatBalance(action.Value, decimals) - summaryStr = fmt.Sprintf("%s %s -> %s", amountStr, symbol, codec.MustAddressBech32(tconsts.HRP, action.To)) - case *actions.BurnAsset: - summaryStr = fmt.Sprintf("%d %s -> 🔥", action.Value, action.Asset) + for i := 0; i < len(result.Outputs); i++ { + for j := 0; j < len(result.Outputs[i]); j++ { + summaryStr := string(result.Outputs[i][j]) + actor := tx.Auth.Actor() + status := "❌" + if result.Success { + status = "✅" + for i, act := range tx.Actions { + switch action := act.(type) { + case *actions.CreateAsset: + assetID := action.GetActionID(uint8(i), tx.ID()) + summaryStr = fmt.Sprintf("assetID: %s symbol: %s decimals: %d metadata: %s", assetID, action.Symbol, action.Decimals, action.Metadata) + case *actions.MintAsset: + _, symbol, decimals, _, _, _, err := c.Asset(context.TODO(), action.Asset, true) + if err != nil { + utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) + return + } + amountStr := utils.FormatBalance(action.Value, decimals) + summaryStr = fmt.Sprintf("%s %s -> %s", amountStr, symbol, codec.MustAddressBech32(tconsts.HRP, action.To)) + case *actions.BurnAsset: + summaryStr = fmt.Sprintf("%d %s -> 🔥", action.Value, action.Asset) - case *actions.Transfer: - _, symbol, decimals, _, _, _, err := c.Asset(context.TODO(), action.Asset, true) - if err != nil { - utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) - return - } - amountStr := utils.FormatBalance(action.Value, decimals) - summaryStr = fmt.Sprintf("%s %s -> %s", amountStr, symbol, codec.MustAddressBech32(tconsts.HRP, action.To)) - if len(action.Memo) > 0 { - summaryStr += fmt.Sprintf(" (memo: %s)", action.Memo) - } + case *actions.Transfer: + _, symbol, decimals, _, _, _, err := c.Asset(context.TODO(), action.Asset, true) + if err != nil { + utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) + return + } + amountStr := utils.FormatBalance(action.Value, decimals) + summaryStr = fmt.Sprintf("%s %s -> %s", amountStr, symbol, codec.MustAddressBech32(tconsts.HRP, action.To)) + if len(action.Memo) > 0 { + summaryStr += fmt.Sprintf(" (memo: %s)", action.Memo) + } - case *actions.CreateOrder: - _, inSymbol, inDecimals, _, _, _, err := c.Asset(context.TODO(), action.In, true) - if err != nil { - utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) - return - } - inTickStr := utils.FormatBalance(action.InTick, inDecimals) - _, outSymbol, outDecimals, _, _, _, err := c.Asset(context.TODO(), action.Out, true) - if err != nil { - utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) - return - } - outTickStr := utils.FormatBalance(action.OutTick, outDecimals) - supplyStr := utils.FormatBalance(action.Supply, outDecimals) - summaryStr = fmt.Sprintf("%s %s -> %s %s (supply: %s %s)", inTickStr, inSymbol, outTickStr, outSymbol, supplyStr, outSymbol) - case *actions.FillOrder: - or, _ := actions.UnmarshalOrderResult(result.Output) - _, inSymbol, inDecimals, _, _, _, err := c.Asset(context.TODO(), action.In, true) - if err != nil { - utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) - return - } - inAmtStr := utils.FormatBalance(or.In, inDecimals) - _, outSymbol, outDecimals, _, _, _, err := c.Asset(context.TODO(), action.Out, true) - if err != nil { - utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) - return + case *actions.CreateOrder: + _, inSymbol, inDecimals, _, _, _, err := c.Asset(context.TODO(), action.In, true) + if err != nil { + utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) + return + } + inTickStr := utils.FormatBalance(action.InTick, inDecimals) + _, outSymbol, outDecimals, _, _, _, err := c.Asset(context.TODO(), action.Out, true) + if err != nil { + utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) + return + } + outTickStr := utils.FormatBalance(action.OutTick, outDecimals) + supplyStr := utils.FormatBalance(action.Supply, outDecimals) + summaryStr = fmt.Sprintf("%s %s -> %s %s (supply: %s %s)", inTickStr, inSymbol, outTickStr, outSymbol, supplyStr, outSymbol) + case *actions.FillOrder: + or, _ := actions.UnmarshalOrderResult(result.Outputs[i][j]) + _, inSymbol, inDecimals, _, _, _, err := c.Asset(context.TODO(), action.In, true) + if err != nil { + utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) + return + } + inAmtStr := utils.FormatBalance(or.In, inDecimals) + _, outSymbol, outDecimals, _, _, _, err := c.Asset(context.TODO(), action.Out, true) + if err != nil { + utils.Outf("{{red}}could not fetch asset info:{{/}} %v", err) + return + } + outAmtStr := utils.FormatBalance(or.Out, outDecimals) + remainingStr := utils.FormatBalance(or.Remaining, outDecimals) + summaryStr = fmt.Sprintf( + "%s %s -> %s %s (remaining: %s %s)", + inAmtStr, inSymbol, outAmtStr, outSymbol, remainingStr, outSymbol, + ) + case *actions.CloseOrder: + summaryStr = fmt.Sprintf("orderID: %s", action.Order) + } } - outAmtStr := utils.FormatBalance(or.Out, outDecimals) - remainingStr := utils.FormatBalance(or.Remaining, outDecimals) - summaryStr = fmt.Sprintf( - "%s %s -> %s %s (remaining: %s %s)", - inAmtStr, inSymbol, outAmtStr, outSymbol, remainingStr, outSymbol, - ) - case *actions.CloseOrder: - summaryStr = fmt.Sprintf("orderID: %s", action.Order) } + utils.Outf( + "%s {{yellow}}%s{{/}} {{yellow}}actor:{{/}} %s {{yellow}}summary (%s):{{/}} [%s] {{yellow}}fee (max %.2f%%):{{/}} %s %s {{yellow}}consumed:{{/}} [%s]\n", + status, + tx.ID(), + codec.MustAddressBech32(tconsts.HRP, actor), + reflect.TypeOf(tx.Actions), + summaryStr, + float64(result.Fee)/float64(tx.Base.MaxFee)*100, + utils.FormatBalance(result.Fee, tconsts.Decimals), + tconsts.Symbol, + cli.ParseDimensions(result.Consumed), + ) } } - utils.Outf( - "%s {{yellow}}%s{{/}} {{yellow}}actor:{{/}} %s {{yellow}}summary (%s):{{/}} [%s] {{yellow}}fee (max %.2f%%):{{/}} %s %s {{yellow}}consumed:{{/}} [%s]\n", - status, - tx.ID(), - codec.MustAddressBech32(tconsts.HRP, actor), - reflect.TypeOf(tx.Actions), - summaryStr, - float64(result.Fee)/float64(tx.Base.MaxFee)*100, - utils.FormatBalance(result.Fee, tconsts.Decimals), - tconsts.Symbol, - cli.ParseDimensions(result.Consumed), - ) } diff --git a/examples/tokenvm/cmd/token-wallet/backend/backend.go b/examples/tokenvm/cmd/token-wallet/backend/backend.go index 61c1fa3608..cecc080331 100644 --- a/examples/tokenvm/cmd/token-wallet/backend/backend.go +++ b/examples/tokenvm/cmd/token-wallet/backend/backend.go @@ -136,7 +136,7 @@ func (b *Backend) Start(ctx context.Context) error { if err := b.AddAddressBook("Me", b.addrStr); err != nil { return err } - if err := b.s.StoreAsset(ids.Empty, false); err != nil { + if err := b.s.StoreAsset(codec.EmptyAddress, false); err != nil { return err } @@ -220,257 +220,286 @@ func (b *Backend) collectBlocks() { } // We should exit action parsing as soon as possible - switch action := tx.Action.(type) { - case *actions.Transfer: - if actor != b.addr && action.To != b.addr { - continue - } - - _, symbol, decimals, _, _, owner, err := b.tcli.Asset(b.ctx, action.Asset, true) - if err != nil { - b.fatal(err) - return - } - txInfo := &TransactionInfo{ - ID: tx.ID().String(), - Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), - Success: result.Success, - Timestamp: blk.Tmstmp, - Actor: codec.MustAddressBech32(tconsts.HRP, actor), - Type: "Transfer", - Units: hcli.ParseDimensions(result.Consumed), - Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), - } - if result.Success { - txInfo.Summary = fmt.Sprintf("%s %s -> %s", hutils.FormatBalance(action.Value, decimals), symbol, codec.MustAddressBech32(tconsts.HRP, action.To)) - if len(action.Memo) > 0 { - txInfo.Summary += fmt.Sprintf(" (memo: %s)", action.Memo) + for i, act := range tx.Actions { + actionID := act.GetActionID(uint8(i), tx.ID()) + switch action := act.(type) { + case *actions.Transfer: + if actor != b.addr && action.To != b.addr { + continue } - } else { - txInfo.Summary = string(result.Output) - } - if action.To == b.addr { - if actor != b.addr && result.Success { - b.txAlertLock.Lock() - b.transactionAlerts = append(b.transactionAlerts, &Alert{"info", fmt.Sprintf("Received %s %s from Transfer", hutils.FormatBalance(action.Value, decimals), symbol)}) - b.txAlertLock.Unlock() - } - hasAsset, err := b.s.HasAsset(action.Asset) + + _, symbol, decimals, _, _, owner, err := b.tcli.Asset(b.ctx, action.Asset, true) if err != nil { b.fatal(err) return } - if !hasAsset { - if err := b.s.StoreAsset(action.Asset, b.addrStr == owner); err != nil { + txInfo := &TransactionInfo{ + ID: tx.ID().String(), + Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), + Success: result.Success, + Timestamp: blk.Tmstmp, + Actor: codec.MustAddressBech32(tconsts.HRP, actor), + Type: "Transfer", + Units: hcli.ParseDimensions(result.Consumed), + Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), + } + if result.Success { + txInfo.Summary = fmt.Sprintf("%s %s -> %s", hutils.FormatBalance(action.Value, decimals), symbol, codec.MustAddressBech32(tconsts.HRP, action.To)) + if len(action.Memo) > 0 { + txInfo.Summary += fmt.Sprintf(" (memo: %s)", action.Memo) + } + } else { + for j := 0; j < len(result.Outputs); j++ { + for k := 0; k < len(result.Outputs[j]); k++ { + txInfo.Summary += fmt.Sprintf(" %s", string(result.Outputs[j][k])) + } + } + } + if action.To == b.addr { + if actor != b.addr && result.Success { + b.txAlertLock.Lock() + b.transactionAlerts = append(b.transactionAlerts, &Alert{"info", fmt.Sprintf("Received %s %s from Transfer", hutils.FormatBalance(action.Value, decimals), symbol)}) + b.txAlertLock.Unlock() + } + hasAsset, err := b.s.HasAsset(action.Asset) + if err != nil { + b.fatal(err) + return + } + if !hasAsset { + if err := b.s.StoreAsset(action.Asset, b.addrStr == owner); err != nil { + b.fatal(err) + return + } + } + if err := b.s.StoreTransaction(txInfo); err != nil { + b.fatal(err) + return + } + } else if actor == b.addr { + if err := b.s.StoreTransaction(txInfo); err != nil { b.fatal(err) return } } - if err := b.s.StoreTransaction(txInfo); err != nil { + case *actions.CreateAsset: + if actor != b.addr { + continue + } + + if err := b.s.StoreAsset(actionID, true); err != nil { b.fatal(err) return } - } else if actor == b.addr { + txInfo := &TransactionInfo{ + ID: tx.ID().String(), + Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), + Success: result.Success, + Timestamp: blk.Tmstmp, + Actor: codec.MustAddressBech32(tconsts.HRP, actor), + Type: "CreateAsset", + Units: hcli.ParseDimensions(result.Consumed), + Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), + } + if result.Success { + txInfo.Summary = fmt.Sprintf("assetID: %s symbol: %s decimals: %d metadata: %s", actionID, action.Symbol, action.Decimals, action.Metadata) + } else { + for j := 0; j < len(result.Outputs); j++ { + for k := 0; k < len(result.Outputs[j]); k++ { + txInfo.Summary += fmt.Sprintf(" %s", string(result.Outputs[j][k])) + } + } + } if err := b.s.StoreTransaction(txInfo); err != nil { b.fatal(err) return } - } - case *actions.CreateAsset: - if actor != b.addr { - continue - } - - if err := b.s.StoreAsset(tx.ID(), true); err != nil { - b.fatal(err) - return - } - txInfo := &TransactionInfo{ - ID: tx.ID().String(), - Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), - Success: result.Success, - Timestamp: blk.Tmstmp, - Actor: codec.MustAddressBech32(tconsts.HRP, actor), - Type: "CreateAsset", - Units: hcli.ParseDimensions(result.Consumed), - Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), - } - if result.Success { - txInfo.Summary = fmt.Sprintf("assetID: %s symbol: %s decimals: %d metadata: %s", tx.ID(), action.Symbol, action.Decimals, action.Metadata) - } else { - txInfo.Summary = string(result.Output) - } - if err := b.s.StoreTransaction(txInfo); err != nil { - b.fatal(err) - return - } - case *actions.MintAsset: - if actor != b.addr && action.To != b.addr { - continue - } - - _, symbol, decimals, _, _, owner, err := b.tcli.Asset(b.ctx, action.Asset, true) - if err != nil { - b.fatal(err) - return - } - txInfo := &TransactionInfo{ - ID: tx.ID().String(), - Timestamp: blk.Tmstmp, - Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), - Success: result.Success, - Actor: codec.MustAddressBech32(tconsts.HRP, actor), - Type: "Mint", - Units: hcli.ParseDimensions(result.Consumed), - Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), - } - if result.Success { - txInfo.Summary = fmt.Sprintf("%s %s -> %s", hutils.FormatBalance(action.Value, decimals), symbol, codec.MustAddressBech32(tconsts.HRP, action.To)) - } else { - txInfo.Summary = string(result.Output) - } - if action.To == b.addr { - if actor != b.addr && result.Success { - b.txAlertLock.Lock() - b.transactionAlerts = append(b.transactionAlerts, &Alert{"info", fmt.Sprintf("Received %s %s from Mint", hutils.FormatBalance(action.Value, decimals), symbol)}) - b.txAlertLock.Unlock() + case *actions.MintAsset: + if actor != b.addr && action.To != b.addr { + continue } - hasAsset, err := b.s.HasAsset(action.Asset) + + _, symbol, decimals, _, _, owner, err := b.tcli.Asset(b.ctx, action.Asset, true) if err != nil { b.fatal(err) return } - if !hasAsset { - if err := b.s.StoreAsset(action.Asset, b.addrStr == owner); err != nil { + txInfo := &TransactionInfo{ + ID: tx.ID().String(), + Timestamp: blk.Tmstmp, + Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), + Success: result.Success, + Actor: codec.MustAddressBech32(tconsts.HRP, actor), + Type: "Mint", + Units: hcli.ParseDimensions(result.Consumed), + Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), + } + if result.Success { + txInfo.Summary = fmt.Sprintf("%s %s -> %s", hutils.FormatBalance(action.Value, decimals), symbol, codec.MustAddressBech32(tconsts.HRP, action.To)) + } else { + for j := 0; j < len(result.Outputs); j++ { + for k := 0; k < len(result.Outputs[j]); k++ { + txInfo.Summary += fmt.Sprintf(" %s", string(result.Outputs[j][k])) + } + } + } + if action.To == b.addr { + if actor != b.addr && result.Success { + b.txAlertLock.Lock() + b.transactionAlerts = append(b.transactionAlerts, &Alert{"info", fmt.Sprintf("Received %s %s from Mint", hutils.FormatBalance(action.Value, decimals), symbol)}) + b.txAlertLock.Unlock() + } + hasAsset, err := b.s.HasAsset(action.Asset) + if err != nil { + b.fatal(err) + return + } + if !hasAsset { + if err := b.s.StoreAsset(action.Asset, b.addrStr == owner); err != nil { + b.fatal(err) + return + } + } + if err := b.s.StoreTransaction(txInfo); err != nil { + b.fatal(err) + return + } + } else if actor == b.addr { + if err := b.s.StoreTransaction(txInfo); err != nil { b.fatal(err) return } } - if err := b.s.StoreTransaction(txInfo); err != nil { + case *actions.CreateOrder: + if actor != b.addr { + continue + } + + _, inSymbol, inDecimals, _, _, _, err := b.tcli.Asset(b.ctx, action.In, true) + if err != nil { + b.fatal(err) + return + } + _, outSymbol, outDecimals, _, _, _, err := b.tcli.Asset(b.ctx, action.Out, true) + if err != nil { b.fatal(err) return } - } else if actor == b.addr { + txInfo := &TransactionInfo{ + ID: tx.ID().String(), + Timestamp: blk.Tmstmp, + Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), + Success: result.Success, + Actor: codec.MustAddressBech32(tconsts.HRP, actor), + Type: "CreateOrder", + Units: hcli.ParseDimensions(result.Consumed), + Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), + } + if result.Success { + txInfo.Summary = fmt.Sprintf("%s %s -> %s %s (supply: %s %s)", + hutils.FormatBalance(action.InTick, inDecimals), + inSymbol, + hutils.FormatBalance(action.OutTick, outDecimals), + outSymbol, + hutils.FormatBalance(action.Supply, outDecimals), + outSymbol, + ) + } else { + for j := 0; j < len(result.Outputs); j++ { + for k := 0; k < len(result.Outputs[j]); k++ { + txInfo.Summary += fmt.Sprintf(" %s", string(result.Outputs[j][k])) + } + } + } if err := b.s.StoreTransaction(txInfo); err != nil { b.fatal(err) return } - } - case *actions.CreateOrder: - if actor != b.addr { - continue - } + case *actions.FillOrder: + if actor != b.addr && action.Owner != b.addr { + continue + } - _, inSymbol, inDecimals, _, _, _, err := b.tcli.Asset(b.ctx, action.In, true) - if err != nil { - b.fatal(err) - return - } - _, outSymbol, outDecimals, _, _, _, err := b.tcli.Asset(b.ctx, action.Out, true) - if err != nil { - b.fatal(err) - return - } - txInfo := &TransactionInfo{ - ID: tx.ID().String(), - Timestamp: blk.Tmstmp, - Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), - Success: result.Success, - Actor: codec.MustAddressBech32(tconsts.HRP, actor), - Type: "CreateOrder", - Units: hcli.ParseDimensions(result.Consumed), - Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), - } - if result.Success { - txInfo.Summary = fmt.Sprintf("%s %s -> %s %s (supply: %s %s)", - hutils.FormatBalance(action.InTick, inDecimals), - inSymbol, - hutils.FormatBalance(action.OutTick, outDecimals), - outSymbol, - hutils.FormatBalance(action.Supply, outDecimals), - outSymbol, - ) - } else { - txInfo.Summary = string(result.Output) - } - if err := b.s.StoreTransaction(txInfo); err != nil { - b.fatal(err) - return - } - case *actions.FillOrder: - if actor != b.addr && action.Owner != b.addr { - continue - } + _, inSymbol, inDecimals, _, _, _, err := b.tcli.Asset(b.ctx, action.In, true) + if err != nil { + b.fatal(err) + return + } + _, outSymbol, outDecimals, _, _, _, err := b.tcli.Asset(b.ctx, action.Out, true) + if err != nil { + b.fatal(err) + return + } + txInfo := &TransactionInfo{ + ID: tx.ID().String(), + Timestamp: blk.Tmstmp, + Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), + Success: result.Success, + Actor: codec.MustAddressBech32(tconsts.HRP, actor), + Type: "FillOrder", + Units: hcli.ParseDimensions(result.Consumed), + Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), + } + if result.Success { + for j := 0; j < len(result.Outputs[i]); j++ { + or, _ := actions.UnmarshalOrderResult(result.Outputs[i][j]) + txInfo.Summary = fmt.Sprintf("%s %s -> %s %s (remaining: %s %s)", + hutils.FormatBalance(or.In, inDecimals), + inSymbol, + hutils.FormatBalance(or.Out, outDecimals), + outSymbol, + hutils.FormatBalance(or.Remaining, outDecimals), + outSymbol, + ) + + if action.Owner == b.addr && actor != b.addr { + b.txAlertLock.Lock() + b.transactionAlerts = append(b.transactionAlerts, &Alert{"info", fmt.Sprintf("Received %s %s from FillOrder", hutils.FormatBalance(or.In, inDecimals), inSymbol)}) + b.txAlertLock.Unlock() + } + } + } else { + for j := 0; j < len(result.Outputs); j++ { + for k := 0; k < len(result.Outputs[j]); k++ { + txInfo.Summary += fmt.Sprintf(" %s", string(result.Outputs[j][k])) + } + } + } + if actor == b.addr { + if err := b.s.StoreTransaction(txInfo); err != nil { + b.fatal(err) + return + } + } + case *actions.CloseOrder: + if actor != b.addr { + continue + } - _, inSymbol, inDecimals, _, _, _, err := b.tcli.Asset(b.ctx, action.In, true) - if err != nil { - b.fatal(err) - return - } - _, outSymbol, outDecimals, _, _, _, err := b.tcli.Asset(b.ctx, action.Out, true) - if err != nil { - b.fatal(err) - return - } - txInfo := &TransactionInfo{ - ID: tx.ID().String(), - Timestamp: blk.Tmstmp, - Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), - Success: result.Success, - Actor: codec.MustAddressBech32(tconsts.HRP, actor), - Type: "FillOrder", - Units: hcli.ParseDimensions(result.Consumed), - Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), - } - if result.Success { - or, _ := actions.UnmarshalOrderResult(result.Output) - txInfo.Summary = fmt.Sprintf("%s %s -> %s %s (remaining: %s %s)", - hutils.FormatBalance(or.In, inDecimals), - inSymbol, - hutils.FormatBalance(or.Out, outDecimals), - outSymbol, - hutils.FormatBalance(or.Remaining, outDecimals), - outSymbol, - ) - - if action.Owner == b.addr && actor != b.addr { - b.txAlertLock.Lock() - b.transactionAlerts = append(b.transactionAlerts, &Alert{"info", fmt.Sprintf("Received %s %s from FillOrder", hutils.FormatBalance(or.In, inDecimals), inSymbol)}) - b.txAlertLock.Unlock() + txInfo := &TransactionInfo{ + ID: tx.ID().String(), + Timestamp: blk.Tmstmp, + Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), + Success: result.Success, + Actor: codec.MustAddressBech32(tconsts.HRP, actor), + Type: "CloseOrder", + Units: hcli.ParseDimensions(result.Consumed), + Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), + } + if result.Success { + txInfo.Summary = fmt.Sprintf("OrderID: %s", action.Order) + } else { + for j := 0; j < len(result.Outputs); j++ { + for k := 0; k < len(result.Outputs[j]); k++ { + txInfo.Summary += fmt.Sprintf(" %s", string(result.Outputs[j][k])) + } + } } - } else { - txInfo.Summary = string(result.Output) - } - if actor == b.addr { if err := b.s.StoreTransaction(txInfo); err != nil { b.fatal(err) return } } - case *actions.CloseOrder: - if actor != b.addr { - continue - } - - txInfo := &TransactionInfo{ - ID: tx.ID().String(), - Timestamp: blk.Tmstmp, - Size: fmt.Sprintf("%.2fKB", float64(tx.Size())/units.KiB), - Success: result.Success, - Actor: codec.MustAddressBech32(tconsts.HRP, actor), - Type: "CloseOrder", - Units: hcli.ParseDimensions(result.Consumed), - Fee: fmt.Sprintf("%s %s", hutils.FormatBalance(result.Fee, tconsts.Decimals), tconsts.Symbol), - } - if result.Success { - txInfo.Summary = fmt.Sprintf("OrderID: %s", action.Order) - } else { - txInfo.Summary = string(result.Output) - } - if err := b.s.StoreTransaction(txInfo); err != nil { - b.fatal(err) - return - } } } now := time.Now() @@ -613,9 +642,9 @@ func (b *Backend) GetMyAssets() []*AssetInfo { b.fatal(err) return nil } - strAsset := asset.String() + strAsset := codec.LIDToString(tconsts.HRP, asset) assets = append(assets, &AssetInfo{ - ID: asset.String(), + ID: codec.LIDToString(tconsts.HRP, asset), Symbol: string(symbol), Decimals: int(decimals), Metadata: string(metadata), @@ -629,7 +658,7 @@ func (b *Backend) GetMyAssets() []*AssetInfo { func (b *Backend) CreateAsset(symbol string, decimals string, metadata string) error { // Ensure have sufficient balance - bal, err := b.tcli.Balance(b.ctx, b.addrStr, ids.Empty) + bal, err := b.tcli.Balance(b.ctx, b.addrStr, codec.EmptyAddress) if err != nil { return err } @@ -639,11 +668,11 @@ func (b *Backend) CreateAsset(symbol string, decimals string, metadata string) e if err != nil { return err } - _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, &actions.CreateAsset{ + _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, []chain.Action{&actions.CreateAsset{ Symbol: []byte(symbol), Decimals: uint8(udecimals), Metadata: []byte(metadata), - }, b.factory) + }}, b.factory) if err != nil { return fmt.Errorf("%w: unable to generate transaction", err) } @@ -663,17 +692,14 @@ func (b *Backend) CreateAsset(symbol string, decimals string, metadata string) e return err } if !result.Success { - return fmt.Errorf("transaction failed on-chain: %s", result.Output) + return fmt.Errorf("transaction failed on-chain: %s", result.Outputs) } return nil } func (b *Backend) MintAsset(asset string, address string, amount string) error { // Input validation - assetID, err := ids.FromString(asset) - if err != nil { - return err - } + assetID := codec.LIDFromString(asset) _, _, decimals, _, _, _, err := b.tcli.Asset(b.ctx, assetID, true) if err != nil { return err @@ -688,17 +714,17 @@ func (b *Backend) MintAsset(asset string, address string, amount string) error { } // Ensure have sufficient balance - bal, err := b.tcli.Balance(b.ctx, b.addrStr, ids.Empty) + bal, err := b.tcli.Balance(b.ctx, b.addrStr, codec.EmptyAddress) if err != nil { return err } // Generate transaction - _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, &actions.MintAsset{ + _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, []chain.Action{&actions.MintAsset{ To: to, Asset: assetID, Value: value, - }, b.factory) + }}, b.factory) if err != nil { return fmt.Errorf("%w: unable to generate transaction", err) } @@ -718,17 +744,14 @@ func (b *Backend) MintAsset(asset string, address string, amount string) error { return err } if !result.Success { - return fmt.Errorf("transaction failed on-chain: %s", result.Output) + return fmt.Errorf("transaction failed on-chain: %s", result.Outputs) } return nil } func (b *Backend) Transfer(asset string, address string, amount string, memo string) error { // Input validation - assetID, err := ids.FromString(asset) - if err != nil { - return err - } + assetID := codec.LIDFromString(asset) _, symbol, decimals, _, _, _, err := b.tcli.Asset(b.ctx, assetID, true) if err != nil { return err @@ -752,22 +775,22 @@ func (b *Backend) Transfer(asset string, address string, amount string, memo str } // Ensure have sufficient balance for fees - bal, err := b.tcli.Balance(b.ctx, b.addrStr, ids.Empty) + bal, err := b.tcli.Balance(b.ctx, b.addrStr, codec.EmptyAddress) if err != nil { return err } // Generate transaction - _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, &actions.Transfer{ + _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, []chain.Action{&actions.Transfer{ To: to, Asset: assetID, Value: value, Memo: []byte(memo), - }, b.factory) + }}, b.factory) if err != nil { return fmt.Errorf("%w: unable to generate transaction", err) } - if assetID != ids.Empty { + if assetID != codec.EmptyAddress { if maxFee > bal { return fmt.Errorf("insufficient balance (have: %s %s, want: %s %s)", hutils.FormatBalance(bal, tconsts.Decimals), tconsts.Symbol, hutils.FormatBalance(maxFee, tconsts.Decimals), tconsts.Symbol) } @@ -789,7 +812,7 @@ func (b *Backend) Transfer(asset string, address string, amount string, memo str return err } if !result.Success { - return fmt.Errorf("transaction failed on-chain: %s", result.Output) + return fmt.Errorf("transaction failed on-chain: %s", result.Outputs) } return nil } @@ -813,11 +836,11 @@ func (b *Backend) GetBalance() ([]*BalanceInfo, error) { if err != nil { return nil, err } - strAsset := asset.String() - if asset == ids.Empty { - balances = append(balances, &BalanceInfo{ID: asset.String(), Str: fmt.Sprintf("%s %s", hutils.FormatBalance(bal, decimals), symbol), Bal: fmt.Sprintf("%s (Balance: %s)", symbol, hutils.FormatBalance(bal, decimals)), Has: bal > 0}) + strAsset := codec.LIDToString(tconsts.HRP, asset) + if asset == codec.EmptyAddress { + balances = append(balances, &BalanceInfo{ID: strAsset, Str: fmt.Sprintf("%s %s", hutils.FormatBalance(bal, decimals), symbol), Bal: fmt.Sprintf("%s (Balance: %s)", symbol, hutils.FormatBalance(bal, decimals)), Has: bal > 0}) } else { - balances = append(balances, &BalanceInfo{ID: asset.String(), Str: fmt.Sprintf("%s %s [%s]", hutils.FormatBalance(bal, decimals), symbol, asset), Bal: fmt.Sprintf("%s [%s..%s] (Balance: %s)", symbol, strAsset[:3], strAsset[len(strAsset)-3:], hutils.FormatBalance(bal, decimals)), Has: bal > 0}) + balances = append(balances, &BalanceInfo{ID: strAsset, Str: fmt.Sprintf("%s %s [%s]", hutils.FormatBalance(bal, decimals), symbol, asset), Bal: fmt.Sprintf("%s [%s..%s] (Balance: %s)", symbol, strAsset[:3], strAsset[len(strAsset)-3:], hutils.FormatBalance(bal, decimals)), Has: bal > 0}) } } return balances, nil @@ -942,9 +965,9 @@ func (b *Backend) GetAllAssets() []*AssetInfo { b.fatal(err) return nil } - strAsset := asset.String() + strAsset := codec.LIDToString(tconsts.HRP, asset) assets = append(assets, &AssetInfo{ - ID: asset.String(), + ID: codec.LIDToString(tconsts.HRP, asset), Symbol: string(symbol), Decimals: int(decimals), Metadata: string(metadata), @@ -957,10 +980,7 @@ func (b *Backend) GetAllAssets() []*AssetInfo { } func (b *Backend) AddAsset(asset string) error { - assetID, err := ids.FromString(asset) - if err != nil { - return err - } + assetID := codec.LIDFromString(asset) hasAsset, err := b.s.HasAsset(assetID) if err != nil { return err @@ -1003,10 +1023,10 @@ func (b *Backend) GetMyOrders() ([]*Order, error) { return nil, err } orders = append(orders, &Order{ - ID: orderID.String(), - InID: inID.String(), + ID: codec.LIDToString(tconsts.HRP, orderID), + InID: codec.LIDToString(tconsts.HRP, inID), InSymbol: string(inSymbol), - OutID: outID.String(), + OutID: codec.LIDToString(tconsts.HRP, outID), OutSymbol: string(outSymbol), Price: fmt.Sprintf("%s %s / %s %s", hutils.FormatBalance(order.InTick, inDecimals), inSymbol, hutils.FormatBalance(order.OutTick, outDecimals), outSymbol), InTick: fmt.Sprintf("%s %s", hutils.FormatBalance(order.InTick, inDecimals), inSymbol), @@ -1031,7 +1051,7 @@ func (b *Backend) GetOrders(pair string) ([]*Order, error) { } assetIDs := strings.Split(pair, "-") in := assetIDs[0] - inID, err := ids.FromString(in) + inID, err := codec.LIDFromString(tconsts.HRP, in) if err != nil { return nil, err } @@ -1040,7 +1060,7 @@ func (b *Backend) GetOrders(pair string) ([]*Order, error) { return nil, err } out := assetIDs[1] - outID, err := ids.FromString(out) + outID, err := codec.LIDFromString(tconsts.HRP, out) if err != nil { return nil, err } @@ -1053,7 +1073,7 @@ func (b *Backend) GetOrders(pair string) ([]*Order, error) { for i := 0; i < len(rawOrders); i++ { order := rawOrders[i] orders[i] = &Order{ - ID: order.ID.String(), + ID: codec.LIDToString(tconsts.HRP, order.ID), InID: in, InSymbol: string(inSymbol), OutID: out, @@ -1072,14 +1092,8 @@ func (b *Backend) GetOrders(pair string) ([]*Order, error) { } func (b *Backend) CreateOrder(assetIn string, inTick string, assetOut string, outTick string, supply string) error { - inID, err := ids.FromString(assetIn) - if err != nil { - return err - } - outID, err := ids.FromString(assetOut) - if err != nil { - return err - } + inID := codec.LIDFromString(assetIn) + outID := codec.LIDFromString(assetOut) _, _, inDecimals, _, _, _, err := b.tcli.Asset(b.ctx, inID, true) if err != nil { return err @@ -1090,7 +1104,7 @@ func (b *Backend) CreateOrder(assetIn string, inTick string, assetOut string, ou } // Ensure have sufficient balance - bal, err := b.tcli.Balance(b.ctx, b.addrStr, ids.Empty) + bal, err := b.tcli.Balance(b.ctx, b.addrStr, codec.EmptyAddress) if err != nil { return err } @@ -1112,17 +1126,17 @@ func (b *Backend) CreateOrder(assetIn string, inTick string, assetOut string, ou } // Generate transaction - _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, &actions.CreateOrder{ + _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, []chain.Action{&actions.CreateOrder{ In: inID, InTick: iTick, Out: outID, OutTick: oTick, Supply: oSupply, - }, b.factory) + }}, b.factory) if err != nil { return fmt.Errorf("%w: unable to generate transaction", err) } - if inID == ids.Empty { + if inID == codec.EmptyAddress { if maxFee+oSupply > bal { return fmt.Errorf("insufficient balance (have: %s %s, want: %s %s)", hutils.FormatBalance(bal, tconsts.Decimals), tconsts.Symbol, hutils.FormatBalance(maxFee+oSupply, tconsts.Decimals), tconsts.Symbol) } @@ -1147,37 +1161,28 @@ func (b *Backend) CreateOrder(assetIn string, inTick string, assetOut string, ou return err } if !result.Success { - return fmt.Errorf("transaction failed on-chain: %s", result.Output) + return fmt.Errorf("transaction failed on-chain: %s", result.Outputs) } // We rely on order checking to clear backlog - return b.s.StoreOrder(tx.ID()) + return b.s.StoreOrder(codec.CreateLID(0, tx.ID())) } func (b *Backend) FillOrder(orderID string, orderOwner string, assetIn string, inTick string, assetOut string, amount string) error { - oID, err := ids.FromString(orderID) - if err != nil { - return err - } + oID := codec.LIDFromString(orderID) owner, err := codec.ParseAddressBech32(tconsts.HRP, orderOwner) if err != nil { return err } - inID, err := ids.FromString(assetIn) - if err != nil { - return err - } - outID, err := ids.FromString(assetOut) - if err != nil { - return err - } + inID := codec.LIDFromString(assetIn) + outID := codec.LIDFromString(assetOut) _, inSymbol, inDecimals, _, _, _, err := b.tcli.Asset(b.ctx, inID, true) if err != nil { return err } // Ensure have sufficient balance - bal, err := b.tcli.Balance(b.ctx, b.addrStr, ids.Empty) + bal, err := b.tcli.Balance(b.ctx, b.addrStr, codec.EmptyAddress) if err != nil { return err } @@ -1198,17 +1203,17 @@ func (b *Backend) FillOrder(orderID string, orderOwner string, assetIn string, i } // Generate transaction - _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, &actions.FillOrder{ + _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, []chain.Action{&actions.FillOrder{ Order: oID, Owner: owner, In: inID, Out: outID, Value: inAmount, - }, b.factory) + }}, b.factory) if err != nil { return fmt.Errorf("%w: unable to generate transaction", err) } - if inID == ids.Empty { + if inID == codec.EmptyAddress { if maxFee+inAmount > bal { return fmt.Errorf("insufficient balance (have: %s %s, want: %s %s)", hutils.FormatBalance(bal, tconsts.Decimals), tconsts.Symbol, hutils.FormatBalance(maxFee+inAmount, tconsts.Decimals), tconsts.Symbol) } @@ -1233,32 +1238,26 @@ func (b *Backend) FillOrder(orderID string, orderOwner string, assetIn string, i return err } if !result.Success { - return fmt.Errorf("transaction failed on-chain: %s", result.Output) + return fmt.Errorf("transaction failed on-chain: %s", result.Outputs) } return nil } func (b *Backend) CloseOrder(orderID string, assetOut string) error { - oID, err := ids.FromString(orderID) - if err != nil { - return err - } - outID, err := ids.FromString(assetOut) - if err != nil { - return err - } + oID := codec.LIDFromString(orderID) + outID := codec.LIDFromString(assetOut) // Ensure have sufficient balance - bal, err := b.tcli.Balance(b.ctx, b.addrStr, ids.Empty) + bal, err := b.tcli.Balance(b.ctx, b.addrStr, codec.EmptyAddress) if err != nil { return err } // Generate transaction - _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, &actions.CloseOrder{ + _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, []chain.Action{&actions.CloseOrder{ Order: oID, Out: outID, - }, b.factory) + }}, b.factory) if err != nil { return fmt.Errorf("%w: unable to generate transaction", err) } @@ -1278,7 +1277,7 @@ func (b *Backend) CloseOrder(orderID string, assetOut string) error { return err } if !result.Success { - return fmt.Errorf("transaction failed on-chain: %s", result.Output) + return fmt.Errorf("transaction failed on-chain: %s", result.Outputs) } return nil } @@ -1389,18 +1388,18 @@ func (b *Backend) Message(message string, url string) error { } // Ensure have sufficient balance - bal, err := b.tcli.Balance(b.ctx, b.addrStr, ids.Empty) + bal, err := b.tcli.Balance(b.ctx, b.addrStr, codec.EmptyAddress) if err != nil { return err } // Generate transaction - _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, &actions.Transfer{ + _, tx, maxFee, err := b.cli.GenerateTransaction(b.ctx, b.parser, []chain.Action{&actions.Transfer{ To: recipientAddr, - Asset: ids.Empty, + Asset: codec.EmptyAddress, Value: fee, Memo: data, - }, b.factory) + }}, b.factory) if err != nil { return fmt.Errorf("%w: unable to generate transaction", err) } @@ -1420,7 +1419,7 @@ func (b *Backend) Message(message string, url string) error { return err } if !result.Success { - return fmt.Errorf("transaction failed on-chain: %s", result.Output) + return fmt.Errorf("transaction failed on-chain: %s", result.Outputs) } return nil } diff --git a/examples/tokenvm/cmd/token-wallet/backend/storage.go b/examples/tokenvm/cmd/token-wallet/backend/storage.go index 9712584369..2dd85cdaaf 100644 --- a/examples/tokenvm/cmd/token-wallet/backend/storage.go +++ b/examples/tokenvm/cmd/token-wallet/backend/storage.go @@ -63,8 +63,8 @@ func (s *Storage) GetKey() (ed25519.PrivateKey, error) { return ed25519.PrivateKey(v), nil } -func (s *Storage) StoreAsset(assetID ids.ID, owned bool) error { - k := make([]byte, 1+ids.IDLen) +func (s *Storage) StoreAsset(assetID codec.LID, owned bool) error { + k := make([]byte, 1+codec.AddressLen) k[0] = assetsPrefix copy(k[1:], assetID[:]) v := []byte{0x0} @@ -74,21 +74,21 @@ func (s *Storage) StoreAsset(assetID ids.ID, owned bool) error { return s.db.Put(k, v) } -func (s *Storage) HasAsset(assetID ids.ID) (bool, error) { - k := make([]byte, 1+ids.IDLen) +func (s *Storage) HasAsset(assetID codec.LID) (bool, error) { + k := make([]byte, 1+codec.AddressLen) k[0] = assetsPrefix copy(k[1:], assetID[:]) return s.db.Has(k) } -func (s *Storage) GetAssets() ([]ids.ID, []bool, error) { +func (s *Storage) GetAssets() ([]codec.LID, []bool, error) { iter := s.db.NewIteratorWithPrefix([]byte{assetsPrefix}) defer iter.Release() - assets := []ids.ID{} + assets := []codec.LID{} owned := []bool{} for iter.Next() { - assets = append(assets, ids.ID(iter.Key()[1:])) + assets = append(assets, codec.LID(iter.Key()[1:])) owned = append(owned, iter.Value()[0] == 0x1) } return assets, owned, iter.Error() @@ -179,24 +179,24 @@ func (s *Storage) GetSolutions() ([]*FaucetSearchInfo, error) { return fs, iter.Error() } -func (s *Storage) StoreOrder(orderID ids.ID) error { +func (s *Storage) StoreOrder(orderID codec.LID) error { inverseTime := consts.MaxUint64 - uint64(time.Now().UnixMilli()) - k := make([]byte, 1+consts.Uint64Len+ids.IDLen) + k := make([]byte, 1+consts.Uint64Len+codec.AddressLen) k[0] = orderPrefix binary.BigEndian.PutUint64(k[1:], inverseTime) copy(k[1+consts.Uint64Len:], orderID[:]) return s.db.Put(k, nil) } -func (s *Storage) GetOrders() ([]ids.ID, [][]byte, error) { +func (s *Storage) GetOrders() ([]codec.LID, [][]byte, error) { iter := s.db.NewIteratorWithPrefix([]byte{orderPrefix}) defer iter.Release() - orders := []ids.ID{} + orders := []codec.LID{} keys := [][]byte{} for iter.Next() { k := iter.Key() - orders = append(orders, ids.ID(k[1+consts.Uint64Len:])) + orders = append(orders, codec.LID(k[1+consts.Uint64Len:])) keys = append(keys, k) } return orders, keys, iter.Error() diff --git a/examples/tokenvm/tests/e2e/e2e_test.go b/examples/tokenvm/tests/e2e/e2e_test.go index 60532dea6d..236e020ccc 100644 --- a/examples/tokenvm/tests/e2e/e2e_test.go +++ b/examples/tokenvm/tests/e2e/e2e_test.go @@ -16,6 +16,7 @@ import ( "github.com/ava-labs/avalanchego/config" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/utils/logging" + "github.com/ava-labs/hypersdk/chain" "github.com/ava-labs/hypersdk/codec" "github.com/ava-labs/hypersdk/crypto/ed25519" "github.com/ava-labs/hypersdk/examples/tokenvm/actions" @@ -511,7 +512,7 @@ var _ = ginkgo.Describe("[Test]", func() { } ginkgo.It("transfer in a single node (raw)", func() { - nativeBalance, err := instancesA[0].tcli.Balance(context.TODO(), sender, ids.Empty) + nativeBalance, err := instancesA[0].tcli.Balance(context.TODO(), sender, codec.EmptyAddress) gomega.Ω(err).Should(gomega.BeNil()) gomega.Ω(nativeBalance).Should(gomega.Equal(startAmount)) @@ -526,10 +527,10 @@ var _ = ginkgo.Describe("[Test]", func() { submit, tx, maxFee, err := instancesA[0].cli.GenerateTransaction( context.Background(), parser, - &actions.Transfer{ + []chain.Action{&actions.Transfer{ To: aother, Value: sendAmount, - }, + }}, factory, ) gomega.Ω(err).Should(gomega.BeNil()) @@ -546,7 +547,7 @@ var _ = ginkgo.Describe("[Test]", func() { hutils.Outf("{{yellow}}found transaction{{/}}\n") // Check sender balance - balance, err := instancesA[0].tcli.Balance(context.Background(), sender, ids.Empty) + balance, err := instancesA[0].tcli.Balance(context.Background(), sender, codec.EmptyAddress) gomega.Ω(err).Should(gomega.BeNil()) hutils.Outf( "{{yellow}}start=%d fee=%d send=%d balance=%d{{/}}\n", @@ -574,7 +575,7 @@ var _ = ginkgo.Describe("[Test]", func() { } // Check balance of recipient - balance, err := inst.tcli.Balance(context.Background(), codec.MustAddressBech32(consts.HRP, aother), ids.Empty) + balance, err := inst.tcli.Balance(context.Background(), codec.MustAddressBech32(consts.HRP, aother), codec.EmptyAddress) gomega.Ω(err).Should(gomega.BeNil()) gomega.Ω(balance).Should(gomega.Equal(sendAmount)) } @@ -791,10 +792,10 @@ func generateBlocks( submit, _, _, err := instances[cumulativeTxs%len(instances)].cli.GenerateTransaction( context.Background(), parser, - &actions.Transfer{ + []chain.Action{&actions.Transfer{ To: auth.NewED25519Address(other.PublicKey()), Value: 1, - }, + }}, factory, ) if failOnError { @@ -858,10 +859,10 @@ func acceptTransaction(cli *rpc.JSONRPCClient, tcli *trpc.JSONRPCClient) { submit, tx, maxFee, err := cli.GenerateTransaction( context.Background(), parser, - &actions.Transfer{ + []chain.Action{&actions.Transfer{ To: auth.NewED25519Address(other.PublicKey()), Value: sendAmount, - }, + }}, factory, ) gomega.Ω(err).Should(gomega.BeNil()) diff --git a/examples/tokenvm/tests/load/load_test.go b/examples/tokenvm/tests/load/load_test.go index c4278ef280..2d300f1696 100644 --- a/examples/tokenvm/tests/load/load_test.go +++ b/examples/tokenvm/tests/load/load_test.go @@ -303,7 +303,7 @@ var _ = ginkgo.BeforeSuite(func() { gomega.Ω(err).Should(gomega.BeNil()) for _, alloc := range g.CustomAllocation { - bal, err := cli.Balance(context.Background(), alloc.Address, ids.Empty) + bal, err := cli.Balance(context.Background(), alloc.Address, codec.EmptyAddress) gomega.Ω(err).Should(gomega.BeNil()) gomega.Ω(bal).Should(gomega.Equal(alloc.Balance)) } @@ -403,7 +403,13 @@ var _ = ginkgo.Describe("load tests vm", func() { for _, result := range blk.Results() { if !result.Success { unitPrices, _ := instances[0].cli.UnitPrices(context.Background(), false) - fmt.Println("tx failed", "unit prices:", unitPrices, "consumed:", result.Consumed, "fee:", result.Fee, "output:", string(result.Output)) + var resultOutputs string + for i := 0; i < len(result.Outputs); i++ { + for j := 0; j < len(result.Outputs[i]); j++ { + resultOutputs += fmt.Sprintf(" %s", result.Outputs[i][j]) + } + } + fmt.Println("tx failed", "unit prices:", unitPrices, "consumed:", result.Consumed, "fee:", result.Fee, "output:", resultOutputs) } gomega.Ω(result.Success).Should(gomega.BeTrue()) } @@ -533,10 +539,10 @@ func issueSimpleTx( ChainID: i.chainID, MaxFee: maxFee, }, - &actions.Transfer{ + []chain.Action{&actions.Transfer{ To: to, Value: amount, - }, + }}, ) tx, err := tx.Sign(factory, consts.ActionRegistry, consts.AuthRegistry) gomega.Ω(err).To(gomega.BeNil()) diff --git a/heap/heap_test.go b/heap/heap_test.go index 59380fd860..f18f08f20a 100644 --- a/heap/heap_test.go +++ b/heap/heap_test.go @@ -6,10 +6,10 @@ package heap import ( "testing" - "github.com/ava-labs/hypersdk/codec" - "github.com/ava-labs/avalanchego/ids" "github.com/stretchr/testify/require" + + "github.com/ava-labs/hypersdk/codec" ) type testItem[T comparable] struct {