From 659876ec797398e97dc2fd16d7abfb2bb08d211b Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Fri, 13 Nov 2020 15:01:08 -0500 Subject: Add assetID parsing to export API --- plugin/evm/service.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/plugin/evm/service.go b/plugin/evm/service.go index 65ef3a2..fc7ed69 100644 --- a/plugin/evm/service.go +++ b/plugin/evm/service.go @@ -91,6 +91,17 @@ func (api *SnowmanAPI) IssueBlock(ctx context.Context) error { return api.vm.tryBlockGen() } +// parseAssetID parses an assetID string into an ID +func (service *AvaxAPI) parseAssetID(assetID string) (ids.ID, error) { + if assetID == "" { + return ids.ID{}, fmt.Errorf("assetID is required") + } else if assetID == "AVAX" { + return service.vm.ctx.AVAXAssetID, nil + } else { + return ids.FromString(assetID) + } +} + // ClientVersion returns the version of the vm running func (service *AvaxAPI) ClientVersion() string { return version } @@ -232,8 +243,6 @@ func (service *AvaxAPI) Import(_ *http.Request, args *ImportArgs, response *api. type ExportAVAXArgs struct { api.UserPass - // AssetID of the tokens - AssetID ids.ID `json:"assetID"` // Amount of asset to send Amount json.Uint64 `json:"amount"` @@ -247,7 +256,7 @@ type ExportAVAXArgs struct { func (service *AvaxAPI) ExportAVAX(_ *http.Request, args *ExportAVAXArgs, response *api.JSONTxID) error { return service.Export(nil, &ExportArgs{ ExportAVAXArgs: *args, - AssetID: service.vm.ctx.AVAXAssetID, + AssetID: service.vm.ctx.AVAXAssetID.String(), }, response) } @@ -255,15 +264,17 @@ func (service *AvaxAPI) ExportAVAX(_ *http.Request, args *ExportAVAXArgs, respon type ExportArgs struct { ExportAVAXArgs // AssetID of the tokens - AssetID ids.ID `json:"assetID"` + AssetID string `json:"assetID"` } // Export exports an asset from the C-Chain to the X-Chain // It must be imported on the X-Chain to complete the transfer func (service *AvaxAPI) Export(_ *http.Request, args *ExportArgs, response *api.JSONTxID) error { log.Info("EVM: Export called") - if args.AssetID == ids.Empty { - return fmt.Errorf("assetID is required") + + assetID, err := service.parseAssetID(args.AssetID) + if err != nil { + return err } if args.Amount == 0 { @@ -290,7 +301,7 @@ func (service *AvaxAPI) Export(_ *http.Request, args *ExportArgs, response *api. // Create the transaction tx, err := service.vm.newExportTx( - args.AssetID, // AssetID + assetID, // AssetID uint64(args.Amount), // Amount chainID, // ID of the chain to send the funds to to, // Address -- cgit v1.2.3-70-g09d2