-
Notifications
You must be signed in to change notification settings - Fork 71
/
index.ts
178 lines (151 loc) · 6.06 KB
/
index.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-console */
import { IAccount } from '../../src/interfaces/IAccount'
import { IEventFilter } from '../../src/interfaces/IEventFilter'
import { ClientFactory } from '../../src/web3/ClientFactory'
import { WalletClient } from '../../src/web3/WalletClient'
import { EventPoller } from '../../src/web3/EventPoller'
import { ITransactionData } from '../../src/interfaces/ITransactionData'
import { EOperationStatus } from '../../src/interfaces/EOperationStatus'
import { IRollsData } from '../../src/interfaces/IRollsData'
import * as dotenv from 'dotenv'
import { Client } from '../../src/web3/Client'
import { IProvider, ProviderType } from '../../src/interfaces/IProvider'
import { CHAIN_ID, fromMAS } from '../../src'
import { getEnvVariable } from '../utils'
const path = require('path')
const chalk = require('chalk')
dotenv.config({
path: path.resolve(__dirname, '..', '.env'),
})
const publicApi = getEnvVariable('JSON_RPC_URL_PUBLIC')
const privateApi = getEnvVariable('JSON_RPC_URL_PRIVATE')
const chainId = CHAIN_ID.BuildNet
const deployerPrivateKey = getEnvVariable('DEPLOYER_PRIVATE_KEY')
const receiverPrivateKey = getEnvVariable('RECEIVER_PRIVATE_KEY')
;(async () => {
const header = '='.repeat(process.stdout.columns - 1)
console.log(header)
console.log(`${chalk.green.bold('Massa Smart Contract Interaction Example')}`)
console.log(header)
try {
// init account
const deployerAccount: IAccount =
await WalletClient.getAccountFromSecretKey(deployerPrivateKey)
console.log('Deployer Wallet ', deployerAccount)
// init web3 client with base account
const web3Client: Client = await ClientFactory.createCustomClient(
[
{ url: publicApi, type: ProviderType.PUBLIC } as IProvider,
{ url: privateApi, type: ProviderType.PRIVATE } as IProvider,
],
chainId,
true,
deployerAccount
)
await web3Client.wallet().addAccountsToWallet([deployerAccount])
// get wallet balance
const deployerAccountBalance = await web3Client
.wallet()
.getAccountBalance(deployerAccount.address as string)
console.log(
`Deployer Wallet Address: ${
deployerAccount.address
} with balance (candidate, final) = (${deployerAccountBalance?.candidate.toString()}, ${deployerAccountBalance?.final.toString()})`
)
// get wallet accounts
let walletAccounts = await web3Client.wallet().getWalletAccounts()
console.log('Wallet Accounts ', walletAccounts)
// get wallet info
const walletInfo = await web3Client.wallet().walletInfo()
console.log('Wallet Info ', walletInfo)
// add a new wallet
console.log('Adding a new Account ...')
await web3Client.wallet().addSecretKeysToWallet([receiverPrivateKey])
// get wallet accounts
walletAccounts = await web3Client.wallet().getWalletAccounts()
console.log('Wallet Accounts (UPDATED) ', walletAccounts)
// get the second account (the receiver)
const senderAccount = walletAccounts[0]
const receiverAccount = walletAccounts[1]
if (!receiverAccount?.address || !senderAccount?.address) {
throw new Error('Missing receiver account address')
}
// get receiver's wallet balance
const receiverAccountBalanceBefore = await web3Client
.wallet()
.getAccountBalance(receiverAccount.address)
console.log(
`Receiver Wallet Balance (Before): ${
receiverAccount.address
} with balance (candidate, final) = (${receiverAccountBalanceBefore?.candidate.toString()}, ${receiverAccountBalanceBefore?.final.toString()})`
)
const message = 'hello world'
// sign a random wallet message using account2
const signedMessage = await web3Client
.wallet()
.signMessage(message, chainId, receiverAccount.address)
console.log('Wallet sender signing a message... ', signedMessage)
if (!deployerAccount?.publicKey || !signedMessage) {
throw new Error('Missing publicKey or signed message')
}
const isVerified = await web3Client
.wallet()
.verifySignature(message, signedMessage)
console.log('Signature verification: ', isVerified)
// send from base account to receiver
const txId = await web3Client.wallet().sendTransaction({
amount: fromMAS(1),
fee: fromMAS(0.01),
recipientAddress: receiverAccount.address,
} as ITransactionData)
console.log('Money Transfer:: TxId ', txId[0])
// await finalization
console.log('Awaiting Finalization ...')
await web3Client
.smartContracts()
.awaitRequiredOperationStatus(txId[0], EOperationStatus.FINAL_SUCCESS)
console.log('Polling events ... ')
const events = await EventPoller.getEventsOnce(
{
start: null,
end: null,
original_operation_id: txId[0],
original_caller_address: null,
emitter_address: null,
} as IEventFilter,
web3Client
)
console.log('Polled Events ', events)
// get receiver's wallet after
const receiverAccountBalanceAfter = await web3Client
.wallet()
.getAccountBalance(receiverAccount.address)
console.log(
`Receiver Wallet Balance (After): ${
receiverAccount.address
} with balance (candidate, final) = (${receiverAccountBalanceAfter?.candidate.toString()}, ${receiverAccountBalanceAfter?.final.toString()})`
)
// get sender's wallet after
const senderAccountBalanceAfter = await web3Client
.wallet()
.getAccountBalance(senderAccount.address)
console.log(
`Sender Wallet Balance (After): ${
receiverAccount.address
} with balance (candidate, final) = (${senderAccountBalanceAfter?.candidate.toString()}, ${senderAccountBalanceAfter?.final.toString()})`
)
// sender buys some rolls
const buyRollsTxId = await web3Client.wallet().buyRolls({
amount: 2n,
fee: fromMAS(0.01),
} as IRollsData)
console.log('Buy Rolls Tx Id ', buyRollsTxId)
process.exit(0)
} catch (ex) {
console.error(ex)
const msg = chalk.red(`Error = ${ex}`)
console.error(msg)
process.exit(-1)
}
})()