|
| 1 | +// fungible_asset is an example of how to create and transfer fungible assets |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/aptos-labs/aptos-go-sdk" |
| 8 | + "github.com/aptos-labs/aptos-go-sdk/bcs" |
| 9 | + "github.com/aptos-labs/aptos-go-sdk/crypto" |
| 10 | +) |
| 11 | +const testEd25519PrivateKey = "ed25519-priv-0xc5338cd251c22daa8c9c9cc94f498cc8a5c7e1d2e75287a5dda91096fe64efa5" |
| 12 | + |
| 13 | +const TransferScriptPayload = "0x008601a11ceb0b0700000a06010002030206050806070e1708252010451900000001010000010003060c05030d6170746f735f6163636f756e74087472616e736665720000000000000000000000000000000000000000000000000000000000000001166170746f733a3a7363726970745f636f6d706f7365720100000100050a000b010b0211000200020920000000000000000000000000000000000000000000000000000000000000000209086400000000000000" |
| 14 | +const TransferAmount = uint64(1_000) |
| 15 | +const FundAmount = uint64(100_000_000) |
| 16 | + |
| 17 | +func example(networkConfig aptos.NetworkConfig) { |
| 18 | + // Create a client for Aptos |
| 19 | + client, err := aptos.NewClient(networkConfig) |
| 20 | + if err != nil { |
| 21 | + panic("Failed to create client:" + err.Error()) |
| 22 | + } |
| 23 | + |
| 24 | + // Create a sender locally |
| 25 | + key := crypto.Ed25519PrivateKey{} |
| 26 | + err = key.FromHex(testEd25519PrivateKey) |
| 27 | + if err != nil { |
| 28 | + panic("Failed to decode Ed25519 private key:" + err.Error()) |
| 29 | + } |
| 30 | + sender, err := aptos.NewAccountFromSigner(&key) |
| 31 | + if err != nil { |
| 32 | + panic("Failed to create sender:" + err.Error()) |
| 33 | + } |
| 34 | + |
| 35 | + // Fund the sender with the faucet to create it on-chain |
| 36 | + println("SENDER: ", sender.Address.String()) |
| 37 | + err = client.Fund(sender.Address, FundAmount) |
| 38 | + if err != nil { |
| 39 | + panic("Failed to fund sender:" + err.Error()) |
| 40 | + } |
| 41 | + receiver := &aptos.AccountAddress{} |
| 42 | + |
| 43 | + // Now run a script version |
| 44 | + fmt.Printf("\n== Now running script version ==\n") |
| 45 | + runScript(client, sender, receiver) |
| 46 | + |
| 47 | + if err != nil { |
| 48 | + panic("Failed to get store balance:" + err.Error()) |
| 49 | + } |
| 50 | + // fmt.Printf("After Script: Receiver Before transfer: %d, after transfer: %d\n", receiverAfterBalance, receiverAfterAfterBalance) |
| 51 | +} |
| 52 | + |
| 53 | +func runScript(client *aptos.Client, alice *aptos.Account, bob *aptos.AccountAddress) { |
| 54 | + scriptBytes, err := aptos.ParseHex(TransferScriptPayload) |
| 55 | + if err != nil { |
| 56 | + panic("Failed to parse script:" + err.Error()) |
| 57 | + } |
| 58 | + |
| 59 | + var payload aptos.TransactionPayload |
| 60 | + payload.UnmarshalBCS(bcs.NewDeserializer(scriptBytes)) |
| 61 | + |
| 62 | + |
| 63 | + // 1. Build transaction |
| 64 | + rawTxn, err := client.BuildTransaction(alice.AccountAddress(), payload); |
| 65 | + |
| 66 | + if err != nil { |
| 67 | + panic("Failed to build transaction:" + err.Error()) |
| 68 | + } |
| 69 | + |
| 70 | + // 2. Simulate transaction (optional) |
| 71 | + // This is useful for understanding how much the transaction will cost |
| 72 | + // and to ensure that the transaction is valid before sending it to the network |
| 73 | + // This is optional, but recommended |
| 74 | + simulationResult, err := client.SimulateTransaction(rawTxn, alice) |
| 75 | + if err != nil { |
| 76 | + panic("Failed to simulate transaction:" + err.Error()) |
| 77 | + } |
| 78 | + fmt.Printf("\n=== Simulation ===\n") |
| 79 | + fmt.Printf("Gas unit price: %d\n", simulationResult[0].GasUnitPrice) |
| 80 | + fmt.Printf("Gas used: %d\n", simulationResult[0].GasUsed) |
| 81 | + fmt.Printf("Total gas fee: %d\n", simulationResult[0].GasUsed*simulationResult[0].GasUnitPrice) |
| 82 | + fmt.Printf("Status: %s\n", simulationResult[0].VmStatus) |
| 83 | + |
| 84 | + // 3. Sign transaction |
| 85 | + signedTxn, err := rawTxn.SignedTransaction(alice) |
| 86 | + if err != nil { |
| 87 | + panic("Failed to sign transaction:" + err.Error()) |
| 88 | + } |
| 89 | + |
| 90 | + // 4. Submit transaction |
| 91 | + submitResult, err := client.SubmitTransaction(signedTxn) |
| 92 | + if err != nil { |
| 93 | + panic("Failed to submit transaction:" + err.Error()) |
| 94 | + } |
| 95 | + txnHash := submitResult.Hash |
| 96 | + |
| 97 | + // 5. Wait for the transaction to complete |
| 98 | + _, err = client.WaitForTransaction(txnHash) |
| 99 | + if err != nil { |
| 100 | + panic("Failed to wait for transaction:" + err.Error()) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// main This example shows how to send a transaction with a script |
| 105 | +func main() { |
| 106 | + example(aptos.DevnetConfig) |
| 107 | +} |
0 commit comments