@alephium/web3 0.2.0-rc.10 → 0.2.0-rc.11

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.
@@ -16,14 +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 { ContractEvent } from '../api/api-alephium'
19
+ import { node } from '../api'
20
20
  import { Subscription, SubscribeOptions } from '../utils'
21
21
 
22
- export class EventSubscription extends Subscription<ContractEvent> {
22
+ export class EventSubscription extends Subscription<node.ContractEvent> {
23
23
  readonly contractAddress: string
24
24
  private fromCount: number
25
25
 
26
- constructor(options: SubscribeOptions<ContractEvent>, contractAddress: string, fromCount?: number) {
26
+ constructor(options: SubscribeOptions<node.ContractEvent>, contractAddress: string, fromCount?: number) {
27
27
  super(options)
28
28
  this.contractAddress = contractAddress
29
29
  this.fromCount = typeof fromCount === 'undefined' ? 0 : fromCount
@@ -67,7 +67,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
67
67
  }
68
68
 
69
69
  export function subscribeToEvents(
70
- options: SubscribeOptions<ContractEvent>,
70
+ options: SubscribeOptions<node.ContractEvent>,
71
71
  contractAddress: string,
72
72
  fromCount?: number
73
73
  ): EventSubscription {
@@ -17,9 +17,9 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  import { Buffer } from 'buffer/'
20
+ import { Val } from '../api'
20
21
  import { bs58, isHexString } from '../utils'
21
- import { node } from '../api'
22
- import { Fields, Val } from './contract'
22
+ import { Fields, FieldsSig } from './contract'
23
23
 
24
24
  const bigIntZero = BigInt(0)
25
25
 
@@ -236,7 +236,7 @@ export function encodeScriptField(tpe: string, value: Val): Uint8Array {
236
236
 
237
237
  const scriptFieldRegex = /\{([0-9]*)\}/g
238
238
 
239
- export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: node.FieldsSig): string {
239
+ export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: FieldsSig): string {
240
240
  return bytecodeTemplate.replace(scriptFieldRegex, (_, fieldIndex: string) => {
241
241
  const fieldName = fieldsSig.names[`${fieldIndex}`]
242
242
  const fieldType = fieldsSig.types[`${fieldIndex}`]
@@ -249,7 +249,7 @@ export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fi
249
249
  })
250
250
  }
251
251
 
252
- export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: node.FieldsSig): string {
252
+ export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: FieldsSig): string {
253
253
  const fieldsEncoded = fieldsSig.names.flatMap((fieldName, fieldIndex) => {
254
254
  const fieldType = fieldsSig.types[`${fieldIndex}`]
255
255
  if (fieldName in fields) {
@@ -17,14 +17,24 @@ 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'
20
+ import {
21
+ fromApiNumber256,
22
+ fromApiTokens,
23
+ NodeProvider,
24
+ Number256,
25
+ toApiNumber256,
26
+ toApiNumber256Optional,
27
+ toApiTokens,
28
+ Token
29
+ } from '../api'
21
30
  import { node } from '../api'
22
31
  import * as utils from '../utils'
23
32
  import { Eq, assertType } from '../utils'
24
33
  import blake from 'blakejs'
25
- import { Token } from '../api/api-alephium'
26
34
  import { getCurrentNodeProvider } from '../global'
27
35
 
36
+ export type OutputRef = node.OutputRef
37
+
28
38
  const ec = new EC('secp256k1')
29
39
 
30
40
  export interface SignResult {
@@ -50,13 +60,13 @@ export type GetAccountsResult = Account[]
50
60
 
51
61
  export interface SignTransferTxParams {
52
62
  signerAddress: string
53
- destinations: node.Destination[]
54
- utxos?: node.OutputRef[]
63
+ destinations: Destination[]
64
+ utxos?: OutputRef[]
55
65
  gasAmount?: number
56
- gasPrice?: string
66
+ gasPrice?: Number256
57
67
  submitTx?: boolean
58
68
  }
59
- assertType<Eq<SignTransferTxParams, TxBuildParams<node.BuildTransaction>>>()
69
+ assertType<Eq<keyof SignTransferTxParams, keyof TxBuildParams<node.BuildTransaction>>>()
60
70
  export interface SignTransferTxResult {
61
71
  fromGroup: number
62
72
  toGroup: number
@@ -69,14 +79,14 @@ assertType<Eq<SignTransferTxResult, SignResult>>()
69
79
  export interface SignDeployContractTxParams {
70
80
  signerAddress: string
71
81
  bytecode: string
72
- initialAttoAlphAmount?: string
82
+ initialAttoAlphAmount?: Number256
73
83
  initialTokenAmounts?: Token[]
74
- issueTokenAmount?: string
84
+ issueTokenAmount?: Number256
75
85
  gasAmount?: number
76
- gasPrice?: string
86
+ gasPrice?: Number256
77
87
  submitTx?: boolean
78
88
  }
79
- assertType<Eq<SignDeployContractTxParams, TxBuildParams<node.BuildDeployContractTx>>>()
89
+ assertType<Eq<keyof SignDeployContractTxParams, keyof TxBuildParams<node.BuildDeployContractTx>>>()
80
90
  export interface SignDeployContractTxResult {
81
91
  fromGroup: number
82
92
  toGroup: number
@@ -92,12 +102,12 @@ export interface SignExecuteScriptTxParams {
92
102
  signerAddress: string
93
103
  bytecode: string
94
104
  attoAlphAmount?: string
95
- tokens?: node.Token[]
105
+ tokens?: Token[]
96
106
  gasAmount?: number
97
107
  gasPrice?: string
98
108
  submitTx?: boolean
99
109
  }
100
- assertType<Eq<SignExecuteScriptTxParams, TxBuildParams<node.BuildExecuteScriptTx>>>()
110
+ assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
101
111
  export interface SignExecuteScriptTxResult {
102
112
  fromGroup: number
103
113
  toGroup: number
@@ -177,9 +187,12 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
177
187
  return (await this.getAccounts())[0].address
178
188
  }
179
189
 
180
- async submitTransaction(unsignedTx: string, txHash: string, signerAddress?: string): Promise<SubmissionResult> {
190
+ async submitTransaction(unsignedTx: string, signerAddress?: string): Promise<SubmissionResult> {
191
+ const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx })
192
+ const txId = decoded.unsignedTx.txId
193
+
181
194
  const address = typeof signerAddress !== 'undefined' ? signerAddress : await this.defaultSignerAddress()
182
- const signature = await this.signRaw(address, txHash)
195
+ const signature = await this.signRaw(address, txId)
183
196
  const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
184
197
  return this.provider.transactions.postTransactionsSubmit(params)
185
198
  }
@@ -207,7 +220,12 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
207
220
  }
208
221
 
209
222
  async buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult> {
210
- return this.provider.transactions.postTransactionsBuild(await this.usePublicKey(params))
223
+ const data: node.BuildTransaction = {
224
+ ...(await this.usePublicKey(params)),
225
+ destinations: toApiDestinations(params.destinations),
226
+ gasPrice: toApiNumber256Optional(params.gasPrice)
227
+ }
228
+ return this.provider.transactions.postTransactionsBuild(data)
211
229
  }
212
230
 
213
231
  async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
@@ -221,7 +239,14 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
221
239
  }
222
240
 
223
241
  async buildContractCreationTx(params: SignDeployContractTxParams): Promise<node.BuildDeployContractTxResult> {
224
- return this.provider.contracts.postContractsUnsignedTxDeployContract(await this.usePublicKey(params))
242
+ const data: node.BuildDeployContractTx = {
243
+ ...(await this.usePublicKey(params)),
244
+ initialAttoAlphAmount: toApiNumber256Optional(params.initialAttoAlphAmount),
245
+ initialTokenAmounts: toApiTokens(params.initialTokenAmounts),
246
+ issueTokenAmount: toApiNumber256Optional(params.issueTokenAmount),
247
+ gasPrice: toApiNumber256Optional(params.gasPrice)
248
+ }
249
+ return this.provider.contracts.postContractsUnsignedTxDeployContract(data)
225
250
  }
226
251
 
227
252
  async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
@@ -230,7 +255,11 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
230
255
  }
231
256
 
232
257
  async buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult> {
233
- return this.provider.contracts.postContractsUnsignedTxExecuteScript(await this.usePublicKey(params))
258
+ const data: node.BuildExecuteScriptTx = {
259
+ ...(await this.usePublicKey(params)),
260
+ tokens: toApiTokens(params.tokens)
261
+ }
262
+ return this.provider.contracts.postContractsUnsignedTxExecuteScript(data)
234
263
  }
235
264
 
236
265
  // in general, wallet should show the decoded information to user for confirmation
@@ -312,3 +341,24 @@ export function verifySignedMessage(message: string, publicKey: string, signatur
312
341
  const messageHash = blake.blake2b(extendedMessage, undefined, 32)
313
342
  return verifyHexString(utils.binToHex(messageHash), publicKey, signature)
314
343
  }
344
+
345
+ export interface Destination {
346
+ address: string
347
+ attoAlphAmount: Number256
348
+ tokens?: Token[]
349
+ lockTime?: number
350
+ message?: string
351
+ }
352
+ assertType<Eq<keyof Destination, keyof node.Destination>>
353
+
354
+ export function toApiDestination(data: Destination): node.Destination {
355
+ return { ...data, attoAlphAmount: toApiNumber256(data.attoAlphAmount), tokens: toApiTokens(data.tokens) }
356
+ }
357
+
358
+ export function toApiDestinations(data: Destination[]): node.Destination[] {
359
+ return data.map(toApiDestination)
360
+ }
361
+
362
+ export function fromApiDestination(data: node.Destination): Destination {
363
+ return { ...data, attoAlphAmount: fromApiNumber256(data.attoAlphAmount), tokens: fromApiTokens(data.tokens) }
364
+ }
@@ -16,9 +16,11 @@ 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 { TxStatus } from '../api/api-alephium'
19
+ import { node } from '../api'
20
20
  import { Subscription, SubscribeOptions } from '../utils'
21
21
 
22
+ export type TxStatus = node.TxStatus
23
+
22
24
  export class TxStatusSubscription extends Subscription<TxStatus> {
23
25
  readonly txId: string
24
26
  readonly fromGroup?: number
@@ -12,7 +12,7 @@
12
12
  "stop-devnet": "node scripts/stop-devnet.js"
13
13
  },
14
14
  "dependencies": {
15
- "@alephium/web3": "0.2.0-rc.9"
15
+ "@alephium/web3": "0.2.0-rc.11"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/elliptic": "^6.4.13",
@@ -9,7 +9,7 @@ async function greet() {
9
9
  web3.setCurrentNodeProvider('http://127.0.0.1:22973')
10
10
  await Project.build()
11
11
 
12
- const greeter = Project.contract('greeter/greeter.ral')
12
+ const greeter = Project.contract('Greeter')
13
13
 
14
14
  const testParams: TestContractParams = {
15
15
  initialFields: { btcPrice: 1 }
@@ -24,10 +24,10 @@ async function greet() {
24
24
  console.log(deployTx.fromGroup)
25
25
  console.log(deployTx.toGroup)
26
26
 
27
- const submitResult = await signer.submitTransaction(deployTx.unsignedTx, deployTx.txId)
27
+ const submitResult = await signer.submitTransaction(deployTx.unsignedTx)
28
28
  console.log(submitResult)
29
29
 
30
- const main = Project.script('greeter_main.ral')
30
+ const main = Project.script('GreeterMain')
31
31
 
32
32
  const mainScriptTx = await main.transactionForDeployment(signer, {
33
33
  initialFields: { greeterContractId: greeterContractId }
@@ -35,7 +35,7 @@ async function greet() {
35
35
  console.log(mainScriptTx.fromGroup)
36
36
  console.log(mainScriptTx.toGroup)
37
37
 
38
- const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
38
+ const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx)
39
39
  console.log(mainSubmitResult)
40
40
  }
41
41
 
@@ -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.2.0-rc.9",
15
+ "@alephium/web3": "0.2.0-rc.11",
16
16
  "react": "^18.0.0",
17
17
  "react-dom": "^18.0.0",
18
18
  "react-scripts": "5.0.1",