@alephium/web3 0.2.0-test.0 → 0.2.0-test.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/contracts/add/add.ral +5 -8
- package/contracts/greeter/greeter.ral +3 -3
- package/contracts/greeter/greeter_interface.ral +1 -0
- package/contracts/greeter_main.ral +3 -5
- package/contracts/main.ral +0 -2
- package/contracts/sub/sub.ral +2 -1
- package/contracts/test/metadata.ral +18 -0
- package/contracts/test/warnings.ral +8 -0
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/scripts/create-project.js +2 -1
- package/dist/src/api/api-alephium.d.ts +37 -7
- package/dist/src/api/api-alephium.js +19 -3
- package/dist/src/api/api-explorer.d.ts +16 -0
- package/dist/src/api/api-explorer.js +26 -0
- package/dist/src/contract/contract.d.ts +86 -52
- package/dist/src/contract/contract.js +325 -218
- package/dist/src/global.d.ts +3 -0
- package/dist/src/global.js +38 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/signer/node-wallet.d.ts +1 -3
- package/dist/src/signer/node-wallet.js +2 -5
- package/dist/src/signer/signer.d.ts +1 -1
- package/dist/src/signer/signer.js +3 -2
- package/dist/src/test/index.d.ts +1 -2
- package/dist/src/test/index.js +4 -4
- package/dist/src/test/privatekey-wallet.d.ts +2 -3
- package/dist/src/test/privatekey-wallet.js +4 -4
- package/dist/src/utils/subscription.d.ts +0 -1
- package/dist/src/utils/subscription.js +2 -1
- package/dist/src/utils/utils.d.ts +2 -2
- package/dist/src/utils/utils.js +2 -2
- package/gitignore +0 -1
- package/package.json +3 -5
- package/scripts/create-project.ts +2 -1
- package/src/api/api-alephium.ts +57 -8
- package/src/api/api-explorer.ts +30 -0
- package/src/contract/contract.ts +430 -317
- package/src/contract/events.ts +2 -2
- package/src/contract/ralph.test.ts +4 -4
- package/src/global.ts +36 -0
- package/src/index.ts +1 -0
- package/src/signer/node-wallet.ts +2 -11
- package/src/signer/signer.ts +4 -3
- package/src/test/index.ts +2 -3
- package/src/test/privatekey-wallet.ts +4 -5
- package/src/transaction/status.ts +1 -1
- package/src/utils/subscription.ts +3 -3
- package/src/utils/utils.test.ts +1 -1
- package/src/utils/utils.ts +4 -4
- package/templates/base/package.json +2 -2
- package/templates/base/src/greeter.ts +8 -7
- package/templates/react/package.json +2 -2
- package/templates/react/src/App.tsx +2 -2
- package/test/contract.test.ts +60 -25
- package/test/events.test.ts +20 -17
- package/test/transaction.test.ts +10 -9
package/src/contract/events.ts
CHANGED
|
@@ -31,7 +31,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
|
|
|
31
31
|
this.startPolling()
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
override startPolling() {
|
|
34
|
+
override startPolling(): void {
|
|
35
35
|
this.eventEmitter.on('tick', async () => {
|
|
36
36
|
await this.polling()
|
|
37
37
|
})
|
|
@@ -42,7 +42,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
|
|
|
42
42
|
return this.fromCount
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
override async polling() {
|
|
45
|
+
override async polling(): Promise<void> {
|
|
46
46
|
try {
|
|
47
47
|
const events = await this.provider.events.getEventsContractContractaddress(this.contractAddress, {
|
|
48
48
|
start: this.fromCount
|
|
@@ -133,9 +133,9 @@ describe('contract', function () {
|
|
|
133
133
|
it('should test buildScriptByteCode', async () => {
|
|
134
134
|
const variables = { x: true, y: 0x05, z: 'ff', a: '1C2RAVWSuaXw8xtUxqVERR7ChKBE1XgscNFw73NSHE1v3' }
|
|
135
135
|
const fieldsSig: node.FieldsSig = {
|
|
136
|
-
signature: 'Not really',
|
|
137
136
|
names: ['x', 'y', 'z', 'a'],
|
|
138
|
-
types: ['Bool', 'U256', 'ByteVec', 'Address']
|
|
137
|
+
types: ['Bool', 'U256', 'ByteVec', 'Address'],
|
|
138
|
+
isMutable: [false, false, false, false]
|
|
139
139
|
}
|
|
140
140
|
const bytecode = ralph.buildScriptByteCode('-{0}-{1}-{2}-{3}-', variables, fieldsSig)
|
|
141
141
|
expect(bytecode).toEqual('-03-1305-1401ff-1500a3cd757be03c7dac8d48bf79e2a7d6e735e018a9c054b99138c7b29738c437ec-')
|
|
@@ -150,9 +150,9 @@ describe('contract', function () {
|
|
|
150
150
|
e: [false, true]
|
|
151
151
|
}
|
|
152
152
|
const fieldsSig: node.FieldsSig = {
|
|
153
|
-
signature: 'Not really',
|
|
154
153
|
names: ['a', 'b', 'c', 'd', 'e'],
|
|
155
|
-
types: ['I256', 'U256', 'ByteVec', 'Address', '[Bool;2]']
|
|
154
|
+
types: ['I256', 'U256', 'ByteVec', 'Address', '[Bool;2]'],
|
|
155
|
+
isMutable: [false, false, false, false]
|
|
156
156
|
}
|
|
157
157
|
const encoded = ralph.buildContractByteCode('ff', fields, fieldsSig)
|
|
158
158
|
expect(encoded).toEqual(
|
package/src/global.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { NodeProvider } from './api'
|
|
20
|
+
|
|
21
|
+
let _currentNodeProvider: NodeProvider | undefined = undefined
|
|
22
|
+
|
|
23
|
+
export function setCurrentNodeProvider(provider: NodeProvider | string): void {
|
|
24
|
+
if (typeof provider == 'string') {
|
|
25
|
+
_currentNodeProvider = new NodeProvider(provider)
|
|
26
|
+
} else {
|
|
27
|
+
_currentNodeProvider = provider
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function getCurrentNodeProvider(): NodeProvider {
|
|
32
|
+
if (typeof _currentNodeProvider === 'undefined') {
|
|
33
|
+
throw Error('No node provider is set.')
|
|
34
|
+
}
|
|
35
|
+
return _currentNodeProvider
|
|
36
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -16,15 +16,14 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { NodeProvider } from '../api'
|
|
20
19
|
import { Account, SignerWithNodeProvider } from '../signer'
|
|
21
20
|
|
|
22
21
|
export class NodeWallet extends SignerWithNodeProvider {
|
|
23
22
|
public walletName: string
|
|
24
23
|
public accounts: Account[] | undefined
|
|
25
24
|
|
|
26
|
-
constructor(
|
|
27
|
-
super(
|
|
25
|
+
constructor(walletName: string, alwaysSubmitTx = true) {
|
|
26
|
+
super(alwaysSubmitTx)
|
|
28
27
|
this.walletName = walletName
|
|
29
28
|
}
|
|
30
29
|
|
|
@@ -45,14 +44,6 @@ export class NodeWallet extends SignerWithNodeProvider {
|
|
|
45
44
|
return accounts
|
|
46
45
|
}
|
|
47
46
|
|
|
48
|
-
static async FromCliqueClient(
|
|
49
|
-
provider: NodeProvider,
|
|
50
|
-
walletName: string,
|
|
51
|
-
alwaysSubmitTx = true
|
|
52
|
-
): Promise<NodeWallet> {
|
|
53
|
-
return new NodeWallet(provider, walletName, alwaysSubmitTx)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
47
|
async signRaw(signerAddress: string, hexString: string): Promise<string> {
|
|
57
48
|
const currentActiveAddressResponse = await this.provider.wallets.getWalletsWalletNameAddresses(this.walletName)
|
|
58
49
|
const { activeAddress } = currentActiveAddressResponse
|
package/src/signer/signer.ts
CHANGED
|
@@ -23,6 +23,7 @@ import * as utils from '../utils'
|
|
|
23
23
|
import { Eq, assertType } from '../utils'
|
|
24
24
|
import blake from 'blakejs'
|
|
25
25
|
import { Token } from '../api/api-alephium'
|
|
26
|
+
import { getCurrentNodeProvider } from '../global'
|
|
26
27
|
|
|
27
28
|
const ec = new EC('secp256k1')
|
|
28
29
|
|
|
@@ -42,7 +43,7 @@ export interface Account {
|
|
|
42
43
|
|
|
43
44
|
export type SubmitTx = { submitTx?: boolean }
|
|
44
45
|
export type SignerAddress = { signerAddress: string }
|
|
45
|
-
type TxBuildParams<T> = Omit<T, 'fromPublicKey'> & SignerAddress & SubmitTx
|
|
46
|
+
type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'targetBlockHash'> & SignerAddress & SubmitTx
|
|
46
47
|
|
|
47
48
|
export type GetAccountsParams = undefined
|
|
48
49
|
export type GetAccountsResult = Account[]
|
|
@@ -167,8 +168,8 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
167
168
|
}
|
|
168
169
|
}
|
|
169
170
|
|
|
170
|
-
constructor(
|
|
171
|
-
this.provider =
|
|
171
|
+
constructor(alwaysSubmitTx: boolean) {
|
|
172
|
+
this.provider = getCurrentNodeProvider()
|
|
172
173
|
this.alwaysSubmitTx = alwaysSubmitTx
|
|
173
174
|
}
|
|
174
175
|
|
package/src/test/index.ts
CHANGED
|
@@ -16,15 +16,14 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { NodeProvider } from '../api'
|
|
20
19
|
import { NodeWallet } from '../signer'
|
|
21
20
|
|
|
22
21
|
export const testWalletName = 'alephium-web3-test-only-wallet'
|
|
23
22
|
export const testAddress = '1DrDyTr9RpRsQnDnXo2YRiPzPW4ooHX5LLoqXrqfMrpQH'
|
|
24
23
|
export const testPassword = 'alph'
|
|
25
24
|
|
|
26
|
-
export async function
|
|
27
|
-
const wallet = new NodeWallet(
|
|
25
|
+
export async function testNodeWallet(): Promise<NodeWallet> {
|
|
26
|
+
const wallet = new NodeWallet(testWalletName)
|
|
28
27
|
await wallet.unlock(testPassword)
|
|
29
28
|
return wallet
|
|
30
29
|
}
|
|
@@ -17,7 +17,6 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { ec as EC } from 'elliptic'
|
|
20
|
-
import { NodeProvider } from '../api'
|
|
21
20
|
import { Account, SignerWithNodeProvider } from '../signer'
|
|
22
21
|
import * as utils from '../utils'
|
|
23
22
|
|
|
@@ -29,17 +28,17 @@ export class PrivateKeyWallet extends SignerWithNodeProvider {
|
|
|
29
28
|
readonly address: string
|
|
30
29
|
readonly group: number
|
|
31
30
|
|
|
32
|
-
constructor(
|
|
33
|
-
super(
|
|
31
|
+
constructor(privateKey: string, alwaysSubmitTx = true) {
|
|
32
|
+
super(alwaysSubmitTx)
|
|
34
33
|
this.privateKey = privateKey
|
|
35
34
|
this.publicKey = utils.publicKeyFromPrivateKey(privateKey)
|
|
36
35
|
this.address = utils.addressFromPublicKey(this.publicKey)
|
|
37
36
|
this.group = utils.groupOfAddress(this.address)
|
|
38
37
|
}
|
|
39
38
|
|
|
40
|
-
static Random(
|
|
39
|
+
static Random(alwaysSubmitTx = true): PrivateKeyWallet {
|
|
41
40
|
const keyPair = ec.genKeyPair()
|
|
42
|
-
return new PrivateKeyWallet(
|
|
41
|
+
return new PrivateKeyWallet(keyPair.getPrivate().toString('hex'), alwaysSubmitTx)
|
|
43
42
|
}
|
|
44
43
|
|
|
45
44
|
async getAccounts(): Promise<Account[]> {
|
|
@@ -33,7 +33,7 @@ export class TxStatusSubscription extends Subscription<TxStatus> {
|
|
|
33
33
|
this.startPolling()
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
override async polling() {
|
|
36
|
+
override async polling(): Promise<void> {
|
|
37
37
|
try {
|
|
38
38
|
const txStatus = await this.provider.transactions.getTransactionsStatus({
|
|
39
39
|
txId: this.txId,
|
|
@@ -17,13 +17,13 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import EventEmitter from 'eventemitter3'
|
|
20
|
+
import { getCurrentNodeProvider } from '../global'
|
|
20
21
|
import { NodeProvider } from '../api'
|
|
21
22
|
|
|
22
23
|
type MessageCallback<Message> = (message: Message) => Promise<void>
|
|
23
24
|
type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void>
|
|
24
25
|
|
|
25
26
|
export interface SubscribeOptions<Message> {
|
|
26
|
-
provider: NodeProvider
|
|
27
27
|
pollingInterval: number
|
|
28
28
|
messageCallback: MessageCallback<Message>
|
|
29
29
|
errorCallback: ErrorCallback<Message>
|
|
@@ -40,7 +40,7 @@ export abstract class Subscription<Message> {
|
|
|
40
40
|
protected cancelled: boolean
|
|
41
41
|
|
|
42
42
|
constructor(options: SubscribeOptions<Message>) {
|
|
43
|
-
this.provider =
|
|
43
|
+
this.provider = getCurrentNodeProvider()
|
|
44
44
|
this.pollingInterval = options.pollingInterval
|
|
45
45
|
this.messageCallback = options.messageCallback
|
|
46
46
|
this.errorCallback = options.errorCallback
|
|
@@ -49,7 +49,7 @@ export abstract class Subscription<Message> {
|
|
|
49
49
|
this.eventEmitter = new EventEmitter()
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
startPolling() {
|
|
52
|
+
startPolling(): void {
|
|
53
53
|
this.eventEmitter.on('tick', async () => {
|
|
54
54
|
await this.polling()
|
|
55
55
|
|
package/src/utils/utils.test.ts
CHANGED
|
@@ -151,7 +151,7 @@ describe('utils', function () {
|
|
|
151
151
|
'0a38bc48fbb4300f1e305b201cd6129372d867122efb814d871d18c0bfe43b56',
|
|
152
152
|
'4f51cd1f0af97cf5ec9c7a3397eaeea549d55a93c216e54f2ab4a8cf29f6f865'
|
|
153
153
|
)
|
|
154
|
-
).toBe('
|
|
154
|
+
).toBe('0e28f15ca290002c31d691aa008aa56ac12356b0380efb6c88fff929b6a268a9')
|
|
155
155
|
})
|
|
156
156
|
|
|
157
157
|
it('should convert from string to hex and back', () => {
|
package/src/utils/utils.ts
CHANGED
|
@@ -190,7 +190,7 @@ export function contractIdFromTx(txId: string, outputIndex: number): string {
|
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
export function subContractId(parentContractId: string, pathInHex: string): string {
|
|
193
|
-
const data = Buffer.concat([hexToBinUnsafe(
|
|
193
|
+
const data = Buffer.concat([hexToBinUnsafe(parentContractId), hexToBinUnsafe(pathInHex)])
|
|
194
194
|
|
|
195
195
|
return binToHex(blake.blake2b(blake.blake2b(data, undefined, 32), undefined, 32))
|
|
196
196
|
}
|
|
@@ -203,11 +203,11 @@ export function stringToHex(str: string): string {
|
|
|
203
203
|
return hex
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
export function hexToString(str:
|
|
207
|
-
return Buffer.from(str
|
|
206
|
+
export function hexToString(str: string): string {
|
|
207
|
+
return Buffer.from(str, 'hex').toString()
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
export function timeout(ms: number) {
|
|
210
|
+
export function timeout(ms: number): Promise<void> {
|
|
211
211
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
212
212
|
}
|
|
213
213
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"version": "0.1.0",
|
|
4
4
|
"license": "GPL",
|
|
5
5
|
"config": {
|
|
6
|
-
"alephium_version": "1.
|
|
6
|
+
"alephium_version": "1.5.0-rc6"
|
|
7
7
|
},
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "rm -rf dist && npx tsc --build .",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"stop-devnet": "node scripts/stop-devnet.js"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@alephium/web3": "0.
|
|
15
|
+
"@alephium/web3": "0.2.0-rc.7"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@types/elliptic": "^6.4.13",
|
|
@@ -2,21 +2,22 @@
|
|
|
2
2
|
* greeter.ts
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { TestContractParams, setCurrentNodeProvider, Project } from '@alephium/web3'
|
|
6
|
+
import { testNodeWallet } from '@alephium/web3/test'
|
|
7
7
|
|
|
8
8
|
async function greet() {
|
|
9
|
-
|
|
9
|
+
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
10
|
+
await Project.build()
|
|
10
11
|
|
|
11
|
-
const greeter =
|
|
12
|
+
const greeter = Project.contract('greeter/greeter.ral')
|
|
12
13
|
|
|
13
14
|
const testParams: TestContractParams = {
|
|
14
15
|
initialFields: { btcPrice: 1 }
|
|
15
16
|
}
|
|
16
|
-
const testResult = await greeter.testPublicMethod(
|
|
17
|
+
const testResult = await greeter.testPublicMethod('greet', testParams)
|
|
17
18
|
console.log(testResult)
|
|
18
19
|
|
|
19
|
-
const signer = await
|
|
20
|
+
const signer = await testNodeWallet()
|
|
20
21
|
|
|
21
22
|
const deployTx = await greeter.transactionForDeployment(signer, { initialFields: testParams.initialFields })
|
|
22
23
|
const greeterContractId = deployTx.contractId
|
|
@@ -26,7 +27,7 @@ async function greet() {
|
|
|
26
27
|
const submitResult = await signer.submitTransaction(deployTx.unsignedTx, deployTx.txId)
|
|
27
28
|
console.log(submitResult)
|
|
28
29
|
|
|
29
|
-
const main =
|
|
30
|
+
const main = Project.script('greeter_main.ral')
|
|
30
31
|
|
|
31
32
|
const mainScriptTx = await main.transactionForDeployment(signer, {
|
|
32
33
|
initialFields: { greeterContractId: greeterContractId }
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "my-dapp",
|
|
3
3
|
"version": "0.1.0",
|
|
4
4
|
"config": {
|
|
5
|
-
"alephium_version": "1.
|
|
5
|
+
"alephium_version": "1.5.0-rc6"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@testing-library/jest-dom": "^5.16.4",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"@types/node": "^16.11.26",
|
|
13
13
|
"@types/react": "^18.0.3",
|
|
14
14
|
"@types/react-dom": "^18.0.0",
|
|
15
|
-
"@alephium/web3": "0.
|
|
15
|
+
"@alephium/web3": "0.2.0-rc.7",
|
|
16
16
|
"react": "^18.0.0",
|
|
17
17
|
"react-dom": "^18.0.0",
|
|
18
18
|
"react-scripts": "5.0.1",
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import React, { useEffect, useState } from 'react'
|
|
2
2
|
import './App.css'
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { ExplorerProvider, Contract, Script } from '@alephium/web3'
|
|
5
5
|
import contractJson from './artifacts/greeter.ral.json'
|
|
6
6
|
import scriptJson from './artifacts/greeter_main.ral.json'
|
|
7
7
|
|
|
8
8
|
function Dashboard() {
|
|
9
|
-
const api = new
|
|
9
|
+
const api = new ExplorerProvider('https://mainnet-backend.alephium.org')
|
|
10
10
|
const contract = Contract.fromJson(contractJson).toString()
|
|
11
11
|
const script = Script.fromJson(scriptJson).toString()
|
|
12
12
|
const [blocks, setBlocks] = useState('')
|
package/test/contract.test.ts
CHANGED
|
@@ -18,31 +18,32 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18
18
|
|
|
19
19
|
import * as fs from 'fs'
|
|
20
20
|
import * as path from 'path'
|
|
21
|
-
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
import { testWallet } from '../src/test'
|
|
21
|
+
import { setCurrentNodeProvider } from '../src'
|
|
22
|
+
import { Contract, Project, Script, TestContractParams } from '../src/contract'
|
|
23
|
+
import { testNodeWallet } from '../src/test'
|
|
25
24
|
import { addressFromContractId } from '../src/utils'
|
|
26
25
|
|
|
27
26
|
describe('contract', function () {
|
|
28
27
|
async function testSuite1() {
|
|
29
|
-
|
|
28
|
+
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
29
|
+
await Project.build()
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
const
|
|
31
|
+
// ignore unused private function warnings
|
|
32
|
+
const add = Project.contract('add/add.ral', { errorOnWarnings: false })
|
|
33
|
+
const sub = Project.contract('sub/sub.ral')
|
|
33
34
|
|
|
34
35
|
const subState = sub.toState({ result: 0 }, { alphAmount: BigInt('1000000000000000000') })
|
|
35
36
|
const testParams: TestContractParams = {
|
|
36
|
-
initialFields: {
|
|
37
|
+
initialFields: { sub: subState.contractId, result: 0 },
|
|
37
38
|
testArgs: { array: [2, 1] },
|
|
38
39
|
existingContracts: [subState]
|
|
39
40
|
}
|
|
40
|
-
const testResult = await add.testPublicMethod(
|
|
41
|
+
const testResult = await add.testPublicMethod('add', testParams)
|
|
41
42
|
expect(testResult.returns).toEqual([[3, 1]])
|
|
42
43
|
expect(testResult.contracts[0].codeHash).toEqual(sub.codeHash)
|
|
43
44
|
expect(testResult.contracts[0].fields.result).toEqual(1)
|
|
44
45
|
expect(testResult.contracts[1].codeHash).toEqual(add.codeHash)
|
|
45
|
-
expect(testResult.contracts[1].fields.
|
|
46
|
+
expect(testResult.contracts[1].fields.sub).toEqual(subState.contractId)
|
|
46
47
|
expect(testResult.contracts[1].fields.result).toEqual(3)
|
|
47
48
|
const events = testResult.events.sort((a, b) => a.name.localeCompare(b.name))
|
|
48
49
|
expect(events[0].name).toEqual('Add')
|
|
@@ -52,10 +53,10 @@ describe('contract', function () {
|
|
|
52
53
|
expect(events[1].fields.x).toEqual(2)
|
|
53
54
|
expect(events[1].fields.y).toEqual(1)
|
|
54
55
|
|
|
55
|
-
const testResultPrivate = await add.testPrivateMethod(
|
|
56
|
+
const testResultPrivate = await add.testPrivateMethod('addPrivate', testParams)
|
|
56
57
|
expect(testResultPrivate.returns).toEqual([[3, 1]])
|
|
57
58
|
|
|
58
|
-
const signer = await
|
|
59
|
+
const signer = await testNodeWallet()
|
|
59
60
|
|
|
60
61
|
const subDeployTx = await sub.transactionForDeployment(signer, {
|
|
61
62
|
initialFields: { result: 0 },
|
|
@@ -71,7 +72,7 @@ describe('contract', function () {
|
|
|
71
72
|
expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
|
|
72
73
|
|
|
73
74
|
const addDeployTx = await add.transactionForDeployment(signer, {
|
|
74
|
-
initialFields: {
|
|
75
|
+
initialFields: { sub: subContractId, result: 0 },
|
|
75
76
|
initialTokenAmounts: []
|
|
76
77
|
})
|
|
77
78
|
expect(addDeployTx.fromGroup).toEqual(0)
|
|
@@ -85,13 +86,13 @@ describe('contract', function () {
|
|
|
85
86
|
const addContractAddress = addressFromContractId(addContractId)
|
|
86
87
|
|
|
87
88
|
// Check state for add/sub before main script is executed
|
|
88
|
-
let fetchedSubState = await sub.fetchState(
|
|
89
|
+
let fetchedSubState = await sub.fetchState(subContractAddress, 0)
|
|
89
90
|
expect(fetchedSubState.fields.result).toEqual(0)
|
|
90
|
-
let fetchedAddState = await add.fetchState(
|
|
91
|
-
expect(fetchedAddState.fields.
|
|
91
|
+
let fetchedAddState = await add.fetchState(addContractAddress, 0)
|
|
92
|
+
expect(fetchedAddState.fields.sub).toEqual(subContractId)
|
|
92
93
|
expect(fetchedAddState.fields.result).toEqual(0)
|
|
93
94
|
|
|
94
|
-
const main =
|
|
95
|
+
const main = Project.script('main.ral')
|
|
95
96
|
const mainScriptTx = await main.transactionForDeployment(signer, {
|
|
96
97
|
initialFields: { addContractId: addContractId }
|
|
97
98
|
})
|
|
@@ -102,27 +103,28 @@ describe('contract', function () {
|
|
|
102
103
|
expect(mainSubmitResult.toGroup).toEqual(0)
|
|
103
104
|
|
|
104
105
|
// Check state for add/sub after main script is executed
|
|
105
|
-
fetchedSubState = await sub.fetchState(
|
|
106
|
+
fetchedSubState = await sub.fetchState(subContractAddress, 0)
|
|
106
107
|
expect(fetchedSubState.fields.result).toEqual(1)
|
|
107
|
-
fetchedAddState = await add.fetchState(
|
|
108
|
-
expect(fetchedAddState.fields.
|
|
108
|
+
fetchedAddState = await add.fetchState(addContractAddress, 0)
|
|
109
|
+
expect(fetchedAddState.fields.sub).toEqual(subContractId)
|
|
109
110
|
expect(fetchedAddState.fields.result).toEqual(3)
|
|
110
111
|
}
|
|
111
112
|
|
|
112
113
|
async function testSuite2() {
|
|
113
|
-
|
|
114
|
+
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
115
|
+
await Project.build()
|
|
114
116
|
|
|
115
|
-
const greeter =
|
|
117
|
+
const greeter = Project.contract('greeter/greeter.ral')
|
|
116
118
|
|
|
117
119
|
const testParams: TestContractParams = {
|
|
118
120
|
initialFields: { btcPrice: 1 }
|
|
119
121
|
}
|
|
120
|
-
const testResult = await greeter.testPublicMethod(
|
|
122
|
+
const testResult = await greeter.testPublicMethod('greet', testParams)
|
|
121
123
|
expect(testResult.returns).toEqual([1])
|
|
122
124
|
expect(testResult.contracts[0].codeHash).toEqual(greeter.codeHash)
|
|
123
125
|
expect(testResult.contracts[0].fields.btcPrice).toEqual(1)
|
|
124
126
|
|
|
125
|
-
const signer = await
|
|
127
|
+
const signer = await testNodeWallet()
|
|
126
128
|
|
|
127
129
|
const deployTx = await greeter.transactionForDeployment(signer, {
|
|
128
130
|
initialFields: { btcPrice: 1 },
|
|
@@ -136,7 +138,7 @@ describe('contract', function () {
|
|
|
136
138
|
expect(submitResult.txId).toEqual(deployTx.txId)
|
|
137
139
|
|
|
138
140
|
const greeterContractId = deployTx.contractId
|
|
139
|
-
const main =
|
|
141
|
+
const main = Project.script('greeter_main.ral')
|
|
140
142
|
|
|
141
143
|
const mainScriptTx = await main.transactionForDeployment(signer, {
|
|
142
144
|
initialFields: { greeterContractId: greeterContractId }
|
|
@@ -167,6 +169,14 @@ describe('contract', function () {
|
|
|
167
169
|
Script.fromJson(loadJson(fileName))
|
|
168
170
|
}
|
|
169
171
|
|
|
172
|
+
it('should load source files by order', async () => {
|
|
173
|
+
const sourceFiles = await Project['loadSourceFiles']('./contracts') // `loadSourceFiles` is a private method
|
|
174
|
+
expect(sourceFiles.length).toEqual(8)
|
|
175
|
+
sourceFiles.slice(0, 5).forEach((c) => expect(c.type).toEqual(0)) // contracts
|
|
176
|
+
sourceFiles.slice(5, 7).forEach((s) => expect(s.type).toEqual(1)) // scripts
|
|
177
|
+
sourceFiles.slice(7).forEach((i) => expect(i.type).toEqual(3)) // interfaces
|
|
178
|
+
})
|
|
179
|
+
|
|
170
180
|
it('should load contract from json', async () => {
|
|
171
181
|
loadContract('./artifacts/add/add.ral.json')
|
|
172
182
|
loadContract('./artifacts/sub/sub.ral.json')
|
|
@@ -175,4 +185,29 @@ describe('contract', function () {
|
|
|
175
185
|
loadContract('./artifacts/greeter/greeter.ral.json')
|
|
176
186
|
loadScript('./artifacts/greeter_main.ral.json')
|
|
177
187
|
})
|
|
188
|
+
|
|
189
|
+
it('should extract metadata of contracts', async () => {
|
|
190
|
+
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
191
|
+
await Project.build()
|
|
192
|
+
|
|
193
|
+
const contract = Project.contract('test/metadata.ral', { errorOnWarnings: false })
|
|
194
|
+
expect(contract.functions.map((func) => func.name)).toEqual(['foo', 'bar', 'baz'])
|
|
195
|
+
expect(contract.publicFunctions()).toEqual(['foo'])
|
|
196
|
+
expect(contract.usingPreapprovedAssetsFunctions()).toEqual(['foo'])
|
|
197
|
+
expect(contract.usingAssetsInContractFunctions()).toEqual(['bar'])
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
it('should handle compiler warnings', async () => {
|
|
201
|
+
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
202
|
+
await Project.build()
|
|
203
|
+
const contract = Project.contract('test/warnings.ral', { errorOnWarnings: false })
|
|
204
|
+
expect(contract.publicFunctions()).toEqual(['foo'])
|
|
205
|
+
|
|
206
|
+
expect(() => Project.contract('test/warnings.ral')).toThrowError(
|
|
207
|
+
'Compilation warnings:\n - Found unused variables in Warnings: foo.y\n - Found unused fields in Warnings: b'
|
|
208
|
+
)
|
|
209
|
+
expect(() => Project.contract('test/warnings.ral', { ignoreUnusedConstantsWarnings: false })).toThrowError(
|
|
210
|
+
'Compilation warnings:\n - Found unused variables in Warnings: foo.y\n - Found unused constants in Warnings: C\n - Found unused fields in Warnings: b'
|
|
211
|
+
)
|
|
212
|
+
})
|
|
178
213
|
})
|
package/test/events.test.ts
CHANGED
|
@@ -16,17 +16,19 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { NodeProvider } from '../src/api'
|
|
20
19
|
import { subscribeToEvents } from '../src/contract/events'
|
|
21
|
-
import {
|
|
20
|
+
import { Project } from '../src/contract'
|
|
22
21
|
import { NodeWallet, SignExecuteScriptTxParams } from '../src/signer'
|
|
23
22
|
import { ContractEvent } from '../src/api/api-alephium'
|
|
24
|
-
import {
|
|
23
|
+
import { testNodeWallet } from '../src/test'
|
|
25
24
|
import { SubscribeOptions, timeout } from '../src/utils'
|
|
25
|
+
import { setCurrentNodeProvider } from '../src'
|
|
26
26
|
|
|
27
27
|
describe('events', function () {
|
|
28
|
-
async function deployContract(
|
|
29
|
-
|
|
28
|
+
async function deployContract(signer: NodeWallet): Promise<[string, string]> {
|
|
29
|
+
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
30
|
+
await Project.build()
|
|
31
|
+
const sub = Project.contract('sub/sub.ral')
|
|
30
32
|
const subDeployTx = await sub.transactionForDeployment(signer, {
|
|
31
33
|
initialFields: { result: 0 },
|
|
32
34
|
initialTokenAmounts: []
|
|
@@ -35,9 +37,10 @@ describe('events', function () {
|
|
|
35
37
|
const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
|
|
36
38
|
expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
|
|
37
39
|
|
|
38
|
-
|
|
40
|
+
// ignore unused private function warnings
|
|
41
|
+
const add = Project.contract('add/add.ral', { errorOnWarnings: false })
|
|
39
42
|
const addDeployTx = await add.transactionForDeployment(signer, {
|
|
40
|
-
initialFields: {
|
|
43
|
+
initialFields: { sub: subContractId, result: 0 },
|
|
41
44
|
initialTokenAmounts: []
|
|
42
45
|
})
|
|
43
46
|
const addSubmitResult = await signer.submitTransaction(addDeployTx.unsignedTx, addDeployTx.txId)
|
|
@@ -53,13 +56,13 @@ describe('events', function () {
|
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
it('should subscribe contract events', async () => {
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
60
|
+
await Project.build()
|
|
61
|
+
const signer = await testNodeWallet()
|
|
58
62
|
|
|
59
|
-
const [contractAddress, contractId] = await deployContract(
|
|
63
|
+
const [contractAddress, contractId] = await deployContract(signer)
|
|
60
64
|
const events: Array<ContractEvent> = []
|
|
61
65
|
const subscriptOptions: SubscribeOptions<ContractEvent> = {
|
|
62
|
-
provider: provider,
|
|
63
66
|
pollingInterval: 500,
|
|
64
67
|
messageCallback: (event: ContractEvent): Promise<void> => {
|
|
65
68
|
events.push(event)
|
|
@@ -72,7 +75,7 @@ describe('events', function () {
|
|
|
72
75
|
}
|
|
73
76
|
}
|
|
74
77
|
const subscription = subscribeToEvents(subscriptOptions, contractAddress)
|
|
75
|
-
const script =
|
|
78
|
+
const script = Project.script('main.ral')
|
|
76
79
|
const scriptTxParams = await script.paramsForDeployment({
|
|
77
80
|
initialFields: { addContractId: contractId },
|
|
78
81
|
signerAddress: (await signer.getAccounts())[0].address
|
|
@@ -93,13 +96,13 @@ describe('events', function () {
|
|
|
93
96
|
}, 15000)
|
|
94
97
|
|
|
95
98
|
it('should cancel event subscription', async () => {
|
|
96
|
-
|
|
97
|
-
|
|
99
|
+
setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
100
|
+
await Project.build()
|
|
101
|
+
const signer = await testNodeWallet()
|
|
98
102
|
|
|
99
|
-
const [contractAddress, contractId] = await deployContract(
|
|
103
|
+
const [contractAddress, contractId] = await deployContract(signer)
|
|
100
104
|
const events: Array<ContractEvent> = []
|
|
101
105
|
const subscriptOptions = {
|
|
102
|
-
provider: provider,
|
|
103
106
|
pollingInterval: 500,
|
|
104
107
|
messageCallback: (event: ContractEvent): Promise<void> => {
|
|
105
108
|
events.push(event)
|
|
@@ -112,7 +115,7 @@ describe('events', function () {
|
|
|
112
115
|
}
|
|
113
116
|
}
|
|
114
117
|
const subscription = subscribeToEvents(subscriptOptions, contractAddress)
|
|
115
|
-
const script =
|
|
118
|
+
const script = Project.script('main.ral')
|
|
116
119
|
const scriptTx0 = await script.transactionForDeployment(signer, {
|
|
117
120
|
initialFields: { addContractId: contractId }
|
|
118
121
|
})
|