mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-06-06 14:16:39 +00:00
112 direct call to issue2liquid (#121)
* removed IssueResponseHandler * added asset-registration endpoint config * added methods to locally issue the asset and register it * added issue_service directory configuration * removed obsolete configuration options Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com> --------- Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
20af6d2c46
commit
f11f66e14f
@ -550,7 +550,6 @@ func New(
|
|||||||
app.GetSubspace(machinemoduletypes.ModuleName),
|
app.GetSubspace(machinemoduletypes.ModuleName),
|
||||||
)
|
)
|
||||||
machineModule := machinemodule.NewAppModule(appCodec, app.MachineKeeper, app.AccountKeeper, app.BankKeeper)
|
machineModule := machinemodule.NewAppModule(appCodec, app.MachineKeeper, app.AccountKeeper, app.BankKeeper)
|
||||||
go app.MachineKeeper.IssueResponseHandler(logger)
|
|
||||||
|
|
||||||
app.AssetKeeper = *assetmodulekeeper.NewKeeper(
|
app.AssetKeeper = *assetmodulekeeper.NewKeeper(
|
||||||
appCodec,
|
appCodec,
|
||||||
|
@ -13,9 +13,8 @@ const DefaultConfigTemplate = `
|
|||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
[planetmint]
|
[planetmint]
|
||||||
osc-service-port = {{ .PlmntConfig.OSCServicePort }}
|
|
||||||
watchmen-endpoint = "{{ .PlmntConfig.WatchmenEndpoint }}"
|
asset-registry-endpoint = "{{ .PlmntConfig.AssetRegistryEndpoint }}"
|
||||||
watchmen-port = {{ .PlmntConfig.WatchmenPort }}
|
|
||||||
token-denom = "{{ .PlmntConfig.TokenDenom }}"
|
token-denom = "{{ .PlmntConfig.TokenDenom }}"
|
||||||
stake-denom = "{{ .PlmntConfig.StakeDenom }}"
|
stake-denom = "{{ .PlmntConfig.StakeDenom }}"
|
||||||
fee-denom = "{{ .PlmntConfig.FeeDenom }}"
|
fee-denom = "{{ .PlmntConfig.FeeDenom }}"
|
||||||
@ -23,20 +22,21 @@ config-root-dir = "{{ .PlmntConfig.ConfigRootDir }}"
|
|||||||
pop-epochs = {{ .PlmntConfig.PoPEpochs }}
|
pop-epochs = {{ .PlmntConfig.PoPEpochs }}
|
||||||
issuance-endpoint = "{{ .PlmntConfig.IssuanceEndpoint }}"
|
issuance-endpoint = "{{ .PlmntConfig.IssuanceEndpoint }}"
|
||||||
issuance-port = {{ .PlmntConfig.IssuancePort }}
|
issuance-port = {{ .PlmntConfig.IssuancePort }}
|
||||||
|
issuance-service-dir = {{ .PlmntConfig.IssuanceServiceDir }}
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
// Config defines Planetmint's top level configuration
|
// Config defines Planetmint's top level configuration
|
||||||
type Config struct {
|
type Config struct {
|
||||||
OSCServicePort int `mapstructure:"osc-service-port" json:"osc-service-port"`
|
AssetRegistryEndpoint string `mapstructure:"asset-registry-endpoint " json:"asset-registry-endpoint "`
|
||||||
WatchmenEndpoint string `mapstructure:"watchmen-endpoint" json:"watchmen-endpoint"`
|
TokenDenom string `mapstructure:"token-denom" json:"token-denom"`
|
||||||
WatchmenPort int `mapstructure:"watchmen-port" json:"watchmen-port"`
|
StakeDenom string `mapstructure:"stake-denom" json:"stake-denom"`
|
||||||
TokenDenom string `mapstructure:"token-denom" json:"token-denom"`
|
FeeDenom string `mapstructure:"fee-denom" json:"fee-denom"`
|
||||||
StakeDenom string `mapstructure:"stake-denom" json:"stake-denom"`
|
ConfigRootDir string `mapstructure:"config-root-dir" json:"config-root-dir"`
|
||||||
FeeDenom string `mapstructure:"fee-denom" json:"fee-denom"`
|
PoPEpochs int `mapstructure:"pop-epochs" json:"pop-epochs"`
|
||||||
ConfigRootDir string `mapstructure:"config-root-dir" json:"config-root-dir"`
|
IssuanceEndpoint string `mapstructure:"issuance-endpoint" json:"issuance-endpoint"`
|
||||||
PoPEpochs int `mapstructure:"pop-epochs" json:"pop-epochs"`
|
IssuancePort int `mapstructure:"issuance-port" json:"issuance-port"`
|
||||||
IssuanceEndpoint string `mapstructure:"issuance-endpoint" json:"issuance-endpoint"`
|
IssuanceServiceDir string `mapstructure:"issuance-service-dir" json:"issuance-service-dir"`
|
||||||
IssuancePort int `mapstructure:"issuance-port" json:"issuance-port"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// cosmos-sdk wide global singleton
|
// cosmos-sdk wide global singleton
|
||||||
@ -53,16 +53,15 @@ func DefaultConfig() *Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Config{
|
return &Config{
|
||||||
OSCServicePort: 8766,
|
AssetRegistryEndpoint: "https://assets.rddl.io/register_asset",
|
||||||
WatchmenEndpoint: "lab.r3c.network",
|
TokenDenom: "plmnt",
|
||||||
WatchmenPort: 7401,
|
StakeDenom: "plmntstake",
|
||||||
TokenDenom: "plmnt",
|
FeeDenom: "plmnt",
|
||||||
StakeDenom: "plmntstake",
|
ConfigRootDir: filepath.Join(currentUser.HomeDir, ".planetmint-go"),
|
||||||
FeeDenom: "plmnt",
|
PoPEpochs: 24, // 24 CometBFT epochs of 5s equate 120s
|
||||||
ConfigRootDir: filepath.Join(currentUser.HomeDir, ".planetmint-go"),
|
IssuanceEndpoint: "lab.r3c.network",
|
||||||
PoPEpochs: 24, // 24 CometBFT epochs of 5s equate 120s
|
IssuancePort: 7401,
|
||||||
IssuanceEndpoint: "lab.r3c.network",
|
IssuanceServiceDir: "/opt/issuer_service",
|
||||||
IssuancePort: 7401,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
package keeper
|
|
||||||
|
|
||||||
import (
|
|
||||||
config "github.com/planetmint/planetmint-go/config"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/cometbft/cometbft/libs/log"
|
|
||||||
"github.com/hypebeast/go-osc/osc"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (k Keeper) IssueResponseHandler(logger log.Logger) {
|
|
||||||
conf := config.GetConfig()
|
|
||||||
addr := "0.0.0.0:" + strconv.FormatInt(int64(conf.OSCServicePort), 10)
|
|
||||||
d := osc.NewStandardDispatcher()
|
|
||||||
err := d.AddMsgHandler("/rddl/resp", func(msg *osc.Message) {
|
|
||||||
logger.Info("Issue Response: " + msg.String())
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Unable to add handler to OSC service.")
|
|
||||||
}
|
|
||||||
server := &osc.Server{
|
|
||||||
Addr: addr,
|
|
||||||
Dispatcher: d,
|
|
||||||
}
|
|
||||||
err = server.ListenAndServe()
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Unable to start the OSC service.")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,15 @@
|
|||||||
package keeper
|
package keeper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
config "github.com/planetmint/planetmint-go/config"
|
config "github.com/planetmint/planetmint-go/config"
|
||||||
"github.com/planetmint/planetmint-go/util"
|
"github.com/planetmint/planetmint-go/util"
|
||||||
@ -10,7 +17,6 @@ import (
|
|||||||
|
|
||||||
"github.com/btcsuite/btcd/btcutil/hdkeychain"
|
"github.com/btcsuite/btcd/btcutil/hdkeychain"
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
"github.com/crgimenes/go-osc"
|
|
||||||
|
|
||||||
errorsmod "cosmossdk.io/errors"
|
errorsmod "cosmossdk.io/errors"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
@ -72,24 +78,90 @@ func validateExtendedPublicKey(issuer string, cfg chaincfg.Params) bool {
|
|||||||
return isValidExtendedPublicKey
|
return isValidExtendedPublicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k msgServer) issueMachineNFT(machine *types.Machine) error {
|
func (k msgServer) issueNFTAsset(name string, machine_address string) (asset_id string, contract string, err error) {
|
||||||
conf := config.GetConfig()
|
conf := config.GetConfig()
|
||||||
client := osc.NewClient(conf.WatchmenEndpoint, conf.WatchmenPort)
|
|
||||||
machine_precision := strconv.FormatInt(int64(machine.Precision), 10)
|
|
||||||
machine_amount := strconv.FormatInt(int64(machine.Amount), 10)
|
|
||||||
machine_type := strconv.FormatUint(uint64(machine.GetType()), 10)
|
|
||||||
|
|
||||||
msg := osc.NewMessage("/rddl/issue")
|
cmdName := "poetry"
|
||||||
msg.Append(machine.Name)
|
cmdArgs := []string{"run", "python", "issuer_service/issue2liquid.py", name, machine_address}
|
||||||
msg.Append(machine.Ticker)
|
|
||||||
msg.Append(machine.Domain)
|
|
||||||
msg.Append(machine_amount)
|
|
||||||
msg.Append("1")
|
|
||||||
msg.Append(machine_precision)
|
|
||||||
msg.Append(machine.Metadata.GetAdditionalDataCID())
|
|
||||||
msg.Append(machine.GetIssuerPlanetmint())
|
|
||||||
msg.Append(machine_type)
|
|
||||||
err := client.Send(msg)
|
|
||||||
|
|
||||||
return err
|
// Create a new command
|
||||||
|
cmd := exec.Command(cmdName, cmdArgs...)
|
||||||
|
|
||||||
|
// If you want to set the working directory
|
||||||
|
cmd.Dir = conf.IssuanceServiceDir
|
||||||
|
|
||||||
|
// Capture the output
|
||||||
|
var stdout, stderr bytes.Buffer
|
||||||
|
cmd.Stdout = &stdout
|
||||||
|
cmd.Stderr = &stderr
|
||||||
|
|
||||||
|
// Execute the command
|
||||||
|
err = cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("cmd.Run() failed with %s\n", err)
|
||||||
|
err = errorsmod.Wrap(types.ErrMachineNFTIssuance, stderr.String())
|
||||||
|
}
|
||||||
|
lines := strings.Split(stdout.String(), "\n")
|
||||||
|
if len(lines) == 3 {
|
||||||
|
asset_id = lines[0]
|
||||||
|
contract = lines[1]
|
||||||
|
} else {
|
||||||
|
err = errorsmod.Wrap(types.ErrMachineNFTIssuanceNoOutput, stderr.String())
|
||||||
|
}
|
||||||
|
return asset_id, contract, err
|
||||||
|
}
|
||||||
|
func (k msgServer) registerAsset(asset_id string, contract string) error {
|
||||||
|
|
||||||
|
conf := config.GetConfig()
|
||||||
|
|
||||||
|
// Create your request payload
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"asset_id": asset_id,
|
||||||
|
"contract": contract,
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return errorsmod.Wrap(types.ErrAssetRegistryReqFailure, "Marshall "+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", conf.AssetRegistryEndpoint, bytes.NewBuffer(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
return errorsmod.Wrap(types.ErrAssetRegistryReqFailure, "Request creation: "+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set headers
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("accept", "application/json")
|
||||||
|
|
||||||
|
// Send request
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return errorsmod.Wrap(types.ErrAssetRegistryReqSending, err.Error())
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Read response
|
||||||
|
if resp.StatusCode > 299 {
|
||||||
|
return errorsmod.Wrap(types.ErrAssetRegistryRepsonse, "Error reading response body:"+strconv.Itoa(resp.StatusCode))
|
||||||
|
}
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return errorsmod.Wrap(types.ErrAssetRegistryRepsonse, "Error reading response body:"+err.Error())
|
||||||
|
}
|
||||||
|
result_obj := string(body)
|
||||||
|
if strings.Contains(result_obj, asset_id) {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return errorsmod.Wrap(types.ErrAssetRegistryRepsonse, "does not confirm asset registration")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k msgServer) issueMachineNFT(machine *types.Machine) error {
|
||||||
|
asset_id, contract, err := k.issueNFTAsset(machine.Name, machine.Address)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return k.registerAsset(asset_id, contract)
|
||||||
}
|
}
|
||||||
|
@ -17,4 +17,9 @@ var (
|
|||||||
ErrMachineTypeUndefined = errorsmod.Register(ModuleName, 8, "the machine type has to be defined")
|
ErrMachineTypeUndefined = errorsmod.Register(ModuleName, 8, "the machine type has to be defined")
|
||||||
ErrInvalidTrustAnchorKey = errorsmod.Register(ModuleName, 9, "invalid trust anchor pubkey")
|
ErrInvalidTrustAnchorKey = errorsmod.Register(ModuleName, 9, "invalid trust anchor pubkey")
|
||||||
ErrTrustAnchorAlreadyRegistered = errorsmod.Register(ModuleName, 10, "trust anchor is already registered")
|
ErrTrustAnchorAlreadyRegistered = errorsmod.Register(ModuleName, 10, "trust anchor is already registered")
|
||||||
|
ErrMachineNFTIssuance = errorsmod.Register(ModuleName, 11, "the machine NFT could not be issued")
|
||||||
|
ErrMachineNFTIssuanceNoOutput = errorsmod.Register(ModuleName, 12, "the machine NFT issuing process derivated")
|
||||||
|
ErrAssetRegistryReqFailure = errorsmod.Register(ModuleName, 13, "request to asset registry could not be created")
|
||||||
|
ErrAssetRegistryReqSending = errorsmod.Register(ModuleName, 14, "request to asset registry could not be sent")
|
||||||
|
ErrAssetRegistryRepsonse = errorsmod.Register(ModuleName, 15, "request response issue")
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user