Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tranfert transaction issue #18

Open
clementjuventin opened this issue Oct 10, 2022 · 1 comment
Open

Tranfert transaction issue #18

clementjuventin opened this issue Oct 10, 2022 · 1 comment

Comments

@clementjuventin
Copy link

Hi guys,
First of all, thanks for developing an aptos sdk in golang. It really helps people like me :)

I ran into a problem while trying to do a simple transfer.
Here is my code...

func transfert(from utils.Account, to utils.Account, aptosValue uint64) error {
	accountInfo, err := aptosClient.GetAccount(ctx, from.Address)
	if err != nil {
		return err
	}
	addr, _ := models.HexToAccountAddress(to.Address)

	tx := models.Transaction{}
	err = tx.SetChainID(settings.ChainID).
		SetSender(from.Address).
		SetPayload(getTransferPayload(addr, aptosValue*settings.AptosUnit)).
		SetExpirationTimestampSecs(uint64(time.Now().Add(10 * time.Minute).Unix())).
		SetGasUnitPrice(settings.GasPrice).
		SetMaxGasAmount(uint64(settings.MaxGasPrice)).
		SetSequenceNumber(accountInfo.SequenceNumber).Error()
	if err != nil {
		return err
	}

	msgBytes, err := tx.GetSigningMessage()
	if err != nil {
		panic(err)
	}

	var pk1, _ = hex.DecodeString(from.PrivateKeyHex[2:])
	priv1 := ed25519.NewKeyFromSeed(pk1)
	signature := ed25519.Sign(priv1, msgBytes)
	var authErr = tx.SetAuthenticator(models.TransactionAuthenticatorEd25519{
		PublicKey: priv1.Public().(ed25519.PublicKey),
		Signature: signature,
	}).Error()
	if authErr != nil {
		panic(authErr)
	}
	rawTx, err := aptosClient.SubmitTransaction(ctx, tx.UserTransaction)
	if err != nil {
		panic(err)
	}

	utils.Log(utils.Info, 0, "Transfert 1 APTOS from "+from.Address+" to "+to.Address+" TX: "+rawTx.Hash)
	return nil
}

func getTransferPayload(to models.AccountAddress, amount uint64) models.TransactionPayload {
	return models.EntryFunctionPayload{
		Module: models.Module{
			Address: addr0x1,
			Name:    "coin",
		},
		Function:      "transfer",
		TypeArguments: []models.TypeTag{aptosCoinTypeTag},
		Arguments:     []interface{}{to, amount},
	}
}

As you can see, it is strongly inspired by your example script. The problem is that this part of the code triggers an error :

	msgBytes, err := tx.GetSigningMessage()
	if err != nil {
		panic(err)
	}

Output : panic: non-optional enum value is nil

I don't see what my script is missing to make it work. I hope you can help me understand the problem if there is one. Thanks in advance.

@Kumazan
Copy link
Contributor

Kumazan commented Oct 11, 2022

Is aptosCoinTypeTag initialized?

@Kumazan Kumazan closed this as completed Oct 11, 2022
@Kumazan Kumazan reopened this Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants