Skip to content

Commit

Permalink
delegateAsset: create and transfer asset endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
inciner8r committed Apr 2, 2024
1 parent 84c155e commit c088acb
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 4 deletions.
56 changes: 54 additions & 2 deletions api/v1/delegateassetcreation/delegateassetcreation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/delegateAssetCreation")
{
g.GET("", getAssets)
g.POST("", deletegateAssetCreation)
//g.GET("", getAssets)
//g.POST("", deletegateAssetCreation)
g.POST("/transfer", assetTransfer)
g.Use(paseto.PASETO)
g.POST("/store", storeAsset)
g.POST("/create", createAsset)
}
}

Expand Down Expand Up @@ -94,3 +96,53 @@ func getAssets(c *gin.Context) {
}
httphelper.SuccessResponse(c, "assets fetched", assets)
}

func createAsset(c *gin.Context) {
var request AssetCreateRequest
err := c.BindJSON(&request)
if err != nil {
httphelper.ErrResponse(c, http.StatusForbidden, "payload is invalid")
return
}

abiS := signatureSeries.SignatureSeriesABI

tx, err := rawtransaction.SendRawTransactionCreateAssetSignature(abiS, request.ContractAddress, request.MetaDataHash, request.RoyaltyPercentBasisPoint)

if err != nil {
httphelper.NewInternalServerError(c, "", "failed to call %v of %v, error: %v", "delegateAssetCreation", "StoreFront", err.Error())
return
}
transactionHash := tx.Hash().String()
payload := DelegateAssetCreationPayload{
TransactionHash: transactionHash,
}
logwrapper.Infof("trasaction hash is %v", transactionHash)
httphelper.SuccessResponse(c, "request successfully send, asset will be delegated soon", payload)

}

func assetTransfer(c *gin.Context) {
var request AssetTransferRequest
err := c.BindJSON(&request)
if err != nil {
httphelper.ErrResponse(c, http.StatusForbidden, "payload is invalid")
return
}

abiS := signatureSeries.SignatureSeriesABI

tx, err := rawtransaction.SendRawTransactionTransferAssetSignature(abiS, request.ContractAddress, common.HexToAddress(request.FromAddress), common.HexToAddress(request.ToAddress), request.TokenId)

if err != nil {
httphelper.NewInternalServerError(c, "", "failed to call %v of %v, error: %v", "delegateAssetCreation", "StoreFront", err.Error())
return
}
transactionHash := tx.Hash().String()
payload := DelegateAssetCreationPayload{
TransactionHash: transactionHash,
}
logwrapper.Infof("trasaction hash is %v", transactionHash)
httphelper.SuccessResponse(c, "request successfully send, asset will be delegated soon", payload)

}
13 changes: 13 additions & 0 deletions api/v1/delegateassetcreation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ type AssetStoreRequest struct {
StorefrontId string `json:"storefrontId" binding:"required"`
ContractAddress string `json:"contractAddress" binding:"required"`
}

type AssetCreateRequest struct {
MetaDataHash string `json:"metaDataHash" binding:"required"`
RoyaltyPercentBasisPoint *big.Int `json:"royaltyPercentBasisPoint" binding:"required"`
ContractAddress string `json:"contractAddress" binding:"required"`
}

type AssetTransferRequest struct {
FromAddress string `json:"fromAddress" binding:"required,hexadecimal"`
ToAddress string `json:"toAddress" binding:"required,hexadecimal"`
TokenId *big.Int `json:"tokenId" binding:"required"`
ContractAddress string `json:"contractAddress" binding:"required"`
}
120 changes: 118 additions & 2 deletions config/smartcontract/rawtransaction/rawtransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func SendRawTransactionDelegateSignature(abiS string, method string, address str
}

auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(300000) // in units
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(3000000) // in units
auth.GasPrice = gasPrice

addressVar := common.HexToAddress(address)
Expand All @@ -158,3 +158,119 @@ func SendRawTransactionDelegateSignature(abiS string, method string, address str
logrus.Info("transaction send", tx.Hash().String())
return tx, nil
}

func SendRawTransactionCreateAssetSignature(abiS string, address string, metdata string, royalty *big.Int) (*types.Transaction, error) {

client, err := smartcontract.GetClient()
if err != nil {
return nil, err
}
privateKey, err := crypto.HexToECDSA(envconfig.EnvVars.WALLET_PRIVATE_KEY)
if err != nil {
logwrapper.Errorf("failed to parse Private Key, error %v", err)
return nil, err
}

publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
logwrapper.Errorf("cannot assert type: publicKey is not of type *ecdsa.PublicKey, error %v", err)
return nil, err
}
nonce, err := client.PendingNonceAt(context.Background(), crypto.PubkeyToAddress(*publicKeyECDSA))
if err != nil {
logwrapper.Warnf("failed to get nonce")
return nil, err
}

gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
if err != nil {
logwrapper.Warnf("failed to get gas price")
return nil, err
}
}

auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(80001))
if err != nil {
logwrapper.Warnf("failed to get transactops")
return nil, err
}

auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(3000000) // in units
auth.GasPrice = gasPrice

addressVar := common.HexToAddress(address)
instance, err := signatureSeries.NewSignatureSeries(addressVar, client)
if err != nil {
logwrapper.Warnf("failed to create instance")
return nil, err
}
tx, err := instance.CreateAsset(auth, metdata, royalty)
if err != nil {
logwrapper.Warnf("failed to execute transaction")
return nil, err
}
logrus.Info("transaction send", tx.Hash().String())
return tx, nil
}

func SendRawTransactionTransferAssetSignature(abiS string, address string, from common.Address, to common.Address, tokenId *big.Int) (*types.Transaction, error) {

client, err := smartcontract.GetClient()
if err != nil {
return nil, err
}
privateKey, err := crypto.HexToECDSA(envconfig.EnvVars.WALLET_PRIVATE_KEY)
if err != nil {
logwrapper.Errorf("failed to parse Private Key, error %v", err)
return nil, err
}

publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
logwrapper.Errorf("cannot assert type: publicKey is not of type *ecdsa.PublicKey, error %v", err)
return nil, err
}
nonce, err := client.PendingNonceAt(context.Background(), crypto.PubkeyToAddress(*publicKeyECDSA))
if err != nil {
logwrapper.Warnf("failed to get nonce")
return nil, err
}

gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
if err != nil {
logwrapper.Warnf("failed to get gas price")
return nil, err
}
}

auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(80001))
if err != nil {
logwrapper.Warnf("failed to get transactops")
return nil, err
}

auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(3000000) // in units
auth.GasPrice = gasPrice

addressVar := common.HexToAddress(address)
instance, err := signatureSeries.NewSignatureSeries(addressVar, client)
if err != nil {
logwrapper.Warnf("failed to create instance")
return nil, err
}
tx, err := instance.SafeTransferFrom(auth, from, to, tokenId)
if err != nil {
logwrapper.Warnf("failed to execute transaction")
return nil, err
}
logrus.Info("transaction send", tx.Hash().String())
return tx, nil
}

0 comments on commit c088acb

Please sign in to comment.