-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
137 lines (100 loc) · 4.31 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// SDK & Node setup
const { Universal: Ae, MemoryAccount, Node } = require('@aeternity/aepp-sdk')
const privkey1 = ''
const pubkey1 = ''
const privkey2 = ''
const pubkey2 = ''
// accounts that will be used for the transactions
const acc1 = MemoryAccount({ keypair: { secretKey: privkey1, publicKey: pubkey1 } });
const acc2 = MemoryAccount({ keypair: { secretKey: privkey2, publicKey: pubkey2 } });
if(privkey1.length < 1 || privkey2.length < 1 || pubkey1.length < 1 || pubkey2.length < 1 ) {
console.log("Ooops, did you provide the keys like seen in the video ?")
}
// a reference to the aeternity blockchain
var Chain;
// instantiate a connection to the aeternity blockchain
const main = async () => {
const node1 = await Node({ url: 'https://testnet.aeternity.io/', internalUrl: 'https://testnet.aeternity.io/'})
// const node2 = ...
Chain = await Ae({
// Define Node
nodes: [
{ name: 'someNode', instance: node1 },
// mode2
],
compilerUrl: 'https://latest.compiler.aepps.com',
accounts: [
acc1,
acc2
],
address: pubkey2
})
const height = await Chain.height()
console.log('Connected to Testnet Node! Current Block:', height)
// CONTRACT DEPLOYMENT
// the code of your contract - watch out for correct indentations !
const code =
`
contract SimpleToken =
record state = {
total_supply : int,
name : string,
balances : map(address, int)
}
entrypoint init(initial_balance : int, name : string) =
{ name = name,
total_supply = initial_balance,
balances = {[Call.caller] = initial_balance}
}
entrypoint name() : string =
state.name
entrypoint balance(account : address) : int =
state.balances[account = 0]
stateful entrypoint transfer(recipient : address, value : int) =
require(value >= 0, "NON_NEGATIVE_VALUE_REQUIRED")
require(balance(Call.caller) >= value, "Not enough funds")
put(state{ balances[Call.caller] = state.balances[Call.caller] - value } )
put(state{ balances[recipient] = state.balances[recipient = 0] + value })
true
`
// create a contract instance
const SimpleToken = await Chain.getContractInstance(code);
// Deploy the contract
try {
console.log("Deploying contract....")
console.log("Using account for deployment: ", Chain.addresses()[0]);
await SimpleToken.methods.init(1337,'SimpleToken');
console.log("Contract deployed successfully!")
console.log("Contract address: ", SimpleToken.deployInfo.address)
console.log("Transaction ID: ", SimpleToken.deployInfo.transaction)
console.log("\n \n")
} catch(e){
console.log("Something went wrong, did you set up the SDK properly?");
console.log("Deployment failed: ", e)
}
// await new Promise(resolve => setTimeout(resolve, 4000));
// CONTRACT FUNCTION CALL
const options = {
amount: 1337,
onAccount: 'ak_214WSEqTB1MbkEKmS9Sh5V4fqHY9b6Xterex15JQTdyiDLdXYW'
}
try{
let callresult = await SimpleToken.methods.transfer('ak_214WSEqTB1MbkEKmS9Sh5V4fqHY9b6Xterex15JQTdyiDLdXYW', 42);
// const myContract = SimpleToken.methods;
// const callresult = await myContract.transfer('ak_214WSEqTB1MbkEKmS9Sh5V4fqHY9b6Xterex15JQTdyiDLdXYW', 42);
// explicitly do a transaction for that function call
// let callresult = await SimpleToken.methods.transfer.send('ak_214WSEqTB1MbkEKmS9Sh5V4fqHY9b6Xterex15JQTdyiDLdXYW', 42);
// just dry-run the transaction to check if it would succeed at current block
// let callresult = await SimpleToken.methods.transfer.get('ak_214WSEqTB1MbkEKmS9Sh5V4fqHY9b6Xterex15JQTdyiDLdXYW', 42);
console.log("Transaction ID: ", callresult.hash);
console.log("Advice: log the full callResult object for more useful information!")
console.log("Function call returned: ", callresult.decodedResult);
} catch (e){
console.log("Calling your function errored: ", e)
}
// optionally, give the sync some time:
//await new Promise(resolve => setTimeout(resolve, 3000));
const spendResult = await Chain.spend(1337, "ak_214WSEqTB1MbkEKmS9Sh5V4fqHY9b6Xterex15JQTdyiDLdXYW")
console.log("Spend result: ", spendResult)
}
main();