joeqian10 / neo-gogogo

MIT License
7 stars 7 forks source link

Nep5Helper.Mint #15

Closed marius-matioc closed 4 years ago

marius-matioc commented 4 years ago

Please implement the mint method for Nep5 coins

joeqian10 commented 4 years ago

Please implement the mint method for Nep5 coins

Since each NEP5 token may have a different method (name) for minting, there is no way or no need to implement a general mint method for all NEP5 tokens.

joeqian10 commented 4 years ago

@marius-matioc for minting CGAS from GAS using this SDK, here is an example:

package Sample

import (
    "github.com/joeqian10/neo-gogogo/helper"
    "github.com/joeqian10/neo-gogogo/sc"
    "github.com/joeqian10/neo-gogogo/tx"
)

// For more info about CGAS, refer to https://github.com/neo-ngd/CGAS-Contract
const (
    CGAS_SCRIPT_HASH_STRING = "74f2dc36a68fdc4682034178eb2220729231db76"
    CGAS_CONTRACT_ADDRESS = "AScKxyXmNtEnTLTvbVhNQyTJmgytxhwSnM"
)

func main() {
    // A mintTokens method for CGAS users, who can transfer GAS to CGAS contract address by constructing InvocationTransaction and convert GAS to CGAS by invoking mintTokens method.
    // Upon successful invocation, CGAS in the equal value of the GAS will be added to the user's asset account.

    // First, to create an InvocationTransaction, need to build the invocation script
    sb := sc.NewScriptBuilder()
    scriptHash, _ := helper.UInt160FromString(CGAS_SCRIPT_HASH_STRING)
    sb.MakeInvocationScript(scriptHash.Bytes(), "mintTokens", nil)
    script := sb.ToArray()

    // Second, instantiate an object of InvocationTransaction
    myTx := tx.NewInvocationTransaction(script)

    // Third, add CoinReference (Transaction Input) to the InvocationTransaction object
    // Here is only an example, the users need to fill in this, you can make it as a parameter to this function 
    cr := tx.CoinReference{
        PrevHash:  helper.UInt256{},
        PrevIndex: 0,
    }
    myTx.Inputs = append(myTx.Inputs, &cr)

    // Fourth, add TransactionOutput to the InvocationTransaction object
    assetId := tx.GasToken // asset id MUST be GAS
    txOut := tx.TransactionOutput{
        AssetId:    assetId,
        Value:      helper.Fixed8{}, // value is up to the user
        ScriptHash: scriptHash,
    }
    myTx.Outputs = append(myTx.Outputs, &txOut)

    // Fifth, any other step you need to take to process the InvocationTransaction object, like signing...
    // Please let me know
}
marius-matioc commented 4 years ago

Thanks, this helps a lot. However, the method could be made CGAS specific, which is the most useful case, or, when generalized to NEP5 provide as arguments the name of the mint method, the contract strings, a wallet and the amount.

joeqian10 commented 4 years ago

Thanks, this helps a lot. However, the method could be made CGAS specific, which is the most useful case, or, when generalized to NEP5 provide as arguments the name of the mint method, the contract strings, a wallet and the amount.

Since "mint" is not a NEP5 standard method and we need to keep this SDK simple and lightweight, and one can easily use other parts of this SDK to implement it, we decided not to add "mint" method. Furthermore, there are other similar nonstandard methods like "burn" which are all avoided in this SDK.