· 4 years ago · Jun 18, 2021, 02:08 PM
1// SDK & Node setup
2const { Universal: Ae, MemoryAccount, Node } = require('@aeternity/aepp-sdk')
3
4const privkey1 = '9096c6312d24e9768fa13e4e1267f35c370447001ce97cdf809808ddd7d4fd7f70137ec5930787af365fba4ab89c280bb97e2906befa75a7b2b0f8d3c62b1aac'
5const pubkey1 = 'ak_rMqRduEE8LiWTKsixtXUYui764HW87DJ2xZNmaGpqWz1o59DH'
6
7const privkey2 = 'ccc44a65df014555da2bda6bdb6fb72de8e8cb11cac89ff7457b1981a793a16183d55a5093ea6c66685653bf006d8964a14a6c4e1c7ad39e6a95a63afdf9ffe4'
8const pubkey2 = 'ak_214WSEqTB1MbkEKmS9Sh5V4fqHY9b6Xterex15JQTdyiDLdXYW'
9
10
11
12// accounts that will be used for the transactions
13const acc1 = MemoryAccount({ keypair: { secretKey: privkey1, publicKey: pubkey1 } });
14const acc2 = MemoryAccount({ keypair: { secretKey: privkey2, publicKey: pubkey2 } });
15
16
17
18// a reference to the aeternity blockchain
19var Chain;
20
21// instantiate a connection to the aeternity blockchain
22const main = async () => {
23 const node1 = await Node({ url: 'https://testnet.aeternity.io'})
24 // const node2 = ...
25
26 Chain = await Ae({
27 // Define Node
28 nodes: [
29 { name: 'someNode', instance: node1 },
30 // mode2
31 ],
32 compilerUrl: 'https://latest.compiler.aepps.com',
33 accounts: [
34 acc1,
35 acc2
36 ],
37 address: pubkey1
38
39 })
40 const height = await Chain.height()
41 console.log('Connected to Testnet Node! Current Block:', height)
42
43
44
45
46
47// CONTRACT DEPLOYMENT
48
49// the code of your contract - watch out for correct indentations !
50 const code =
51 `
52contract SimpleToken =
53
54 record state = {
55 total_supply : int,
56 name : string,
57 balances : map(address, int)
58 }
59
60 entrypoint init(initial_balance : int, name : string) =
61 { name = name,
62 total_supply = initial_balance,
63 balances = {[Call.caller] = initial_balance}
64 }
65
66 entrypoint name() : string =
67 state.name
68
69 entrypoint balance(account : address) : int =
70 state.balances[account = 0]
71
72
73 stateful entrypoint transfer(recipient : address, value : int) =
74 require(value >= 0, "NON_NEGATIVE_VALUE_REQUIRED")
75 require(balance(Call.caller) >= value, "Not enough funds")
76
77 put(state{ balances[Call.caller] = state.balances[Call.caller] - value } )
78 put(state{ balances[recipient] = state.balances[recipient = 0] + value })
79 true
80`
81
82 // create a contract instance
83 const SimpleToken = await Chain.getContractInstance(code);
84
85 // Deploy the contract
86 try {
87 console.log("Deploying contract....")
88 console.log("Using account for deployment: ", Chain.addresses()[0]);
89 await SimpleToken.methods.init(1337,'SimpleToken');
90 console.log("Contract deployed successfully!")
91 console.log("Contract address: ", SimpleToken.deployInfo.address)
92 console.log("Transaction ID: ", SimpleToken.deployInfo.transaction)
93 console.log("\n \n")
94 } catch(e){
95 console.log("Something went wrong, did you set up the SDK properly?");
96 console.log("Deployment failed: ", e)
97 }
98
99
100
101// await new Promise(resolve => setTimeout(resolve, 4000));
102
103
104
105 // CONTRACT FUNCTION CALL
106
107 try{
108 let callresult = await SimpleToken.methods.transfer('ak_214WSEqTB1MbkEKmS9Sh5V4fqHY9b6Xterex15JQTdyiDLdXYW', 42);
109 //var myContract = SimpleToken.methods;
110 console.log("Transaction ID: ", callresult.hash);
111 console.log("Advice: log the full callResult object for more useful information!")
112 console.log("Function call returned: ", callresult.decodedResult);
113 } catch (e){
114 console.log("Calling your function errored: ", e)
115 }
116 }
117 main();
118
119