@alephium/web3 1.8.5 → 1.9.0
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/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/codec/instr-codec.d.ts +2 -0
- package/dist/src/codec/instr-codec.js +54 -1
- package/dist/src/constants.d.ts +4 -0
- package/dist/src/constants.js +5 -1
- package/dist/src/contract/dapp-tx-builder.d.ts +26 -0
- package/dist/src/contract/dapp-tx-builder.js +187 -0
- package/dist/src/contract/index.d.ts +1 -0
- package/dist/src/contract/index.js +3 -0
- package/dist/src/contract/ralph.js +2 -34
- package/package.json +3 -3
- package/src/codec/instr-codec.ts +56 -1
- package/src/constants.ts +4 -0
- package/src/contract/dapp-tx-builder.ts +209 -0
- package/src/contract/index.ts +1 -0
- package/src/contract/ralph.ts +4 -49
|
@@ -0,0 +1,209 @@
|
|
|
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 { contractIdFromAddress, isContractAddress } from '../address'
|
|
20
|
+
import { Val } from '../api'
|
|
21
|
+
import {
|
|
22
|
+
AddressConst,
|
|
23
|
+
ApproveAlph,
|
|
24
|
+
ApproveToken,
|
|
25
|
+
BytesConst,
|
|
26
|
+
CallExternal,
|
|
27
|
+
ConstFalse,
|
|
28
|
+
ConstTrue,
|
|
29
|
+
Dup,
|
|
30
|
+
Instr,
|
|
31
|
+
Pop,
|
|
32
|
+
toI256,
|
|
33
|
+
toU256,
|
|
34
|
+
U256Const,
|
|
35
|
+
Method
|
|
36
|
+
} from '../codec'
|
|
37
|
+
import { LockupScript, lockupScriptCodec } from '../codec/lockup-script-codec'
|
|
38
|
+
import { scriptCodec } from '../codec/script-codec'
|
|
39
|
+
import { ALPH_TOKEN_ID } from '../constants'
|
|
40
|
+
import { TraceableError } from '../error'
|
|
41
|
+
import { SignExecuteScriptTxParams } from '../signer'
|
|
42
|
+
import { base58ToBytes, binToHex, HexString, hexToBinUnsafe, isBase58, isHexString } from '../utils'
|
|
43
|
+
|
|
44
|
+
export class DappTransactionBuilder {
|
|
45
|
+
private callerLockupScript: LockupScript
|
|
46
|
+
private approvedAssets: Map<HexString, bigint>
|
|
47
|
+
private instrs: Instr[]
|
|
48
|
+
|
|
49
|
+
constructor(public readonly callerAddress: string) {
|
|
50
|
+
try {
|
|
51
|
+
this.callerLockupScript = lockupScriptCodec.decode(base58ToBytes(this.callerAddress))
|
|
52
|
+
if (this.callerLockupScript.kind !== 'P2PKH' && this.callerLockupScript.kind !== 'P2SH') {
|
|
53
|
+
throw new Error(`Expected a P2PKH address or P2SH address`)
|
|
54
|
+
}
|
|
55
|
+
} catch (error) {
|
|
56
|
+
throw new TraceableError(`Invalid caller address: ${callerAddress}`, error)
|
|
57
|
+
}
|
|
58
|
+
this.approvedAssets = new Map<HexString, bigint>()
|
|
59
|
+
this.instrs = []
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
callContract(params: {
|
|
63
|
+
contractAddress: string
|
|
64
|
+
methodIndex: number
|
|
65
|
+
args: Val[]
|
|
66
|
+
attoAlphAmount?: bigint
|
|
67
|
+
tokens?: { id: HexString; amount: bigint }[]
|
|
68
|
+
retLength?: number
|
|
69
|
+
}) {
|
|
70
|
+
if (!isBase58(params.contractAddress)) {
|
|
71
|
+
throw new Error(`Invalid contract address: ${params.contractAddress}, expected a base58 string`)
|
|
72
|
+
}
|
|
73
|
+
if (!isContractAddress(params.contractAddress)) {
|
|
74
|
+
throw new Error(`Invalid contract address: ${params.contractAddress}, expected a P2C address`)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (params.methodIndex < 0) {
|
|
78
|
+
throw new Error(`Invalid method index: ${params.methodIndex}`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const allTokens = (params.tokens ?? []).concat([{ id: ALPH_TOKEN_ID, amount: params.attoAlphAmount ?? 0n }])
|
|
82
|
+
const instrs = [
|
|
83
|
+
...genApproveAssets(this.callerLockupScript, this.approveTokens(allTokens)),
|
|
84
|
+
...genContractCall(params.contractAddress, params.methodIndex, params.args, params.retLength ?? 0)
|
|
85
|
+
]
|
|
86
|
+
this.instrs.push(...instrs)
|
|
87
|
+
return this
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
getResult(): SignExecuteScriptTxParams {
|
|
91
|
+
const method: Method = {
|
|
92
|
+
isPublic: true,
|
|
93
|
+
usePreapprovedAssets: this.approvedAssets.size > 0,
|
|
94
|
+
useContractAssets: false,
|
|
95
|
+
usePayToContractOnly: false,
|
|
96
|
+
argsLength: 0,
|
|
97
|
+
localsLength: 0,
|
|
98
|
+
returnLength: 0,
|
|
99
|
+
instrs: this.instrs
|
|
100
|
+
}
|
|
101
|
+
const script = { methods: [method] }
|
|
102
|
+
const bytecode = scriptCodec.encode(script)
|
|
103
|
+
const tokens = Array.from(this.approvedAssets.entries()).map(([id, amount]) => ({ id, amount }))
|
|
104
|
+
this.approvedAssets.clear()
|
|
105
|
+
this.instrs = []
|
|
106
|
+
return {
|
|
107
|
+
signerAddress: this.callerAddress,
|
|
108
|
+
signerKeyType: this.callerLockupScript.kind === 'P2PKH' ? 'default' : 'bip340-schnorr',
|
|
109
|
+
bytecode: binToHex(bytecode),
|
|
110
|
+
attoAlphAmount: tokens.find((t) => t.id === ALPH_TOKEN_ID)?.amount,
|
|
111
|
+
tokens: tokens.filter((t) => t.id !== ALPH_TOKEN_ID)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private addTokenToMap(tokenId: HexString, amount: bigint, map: Map<HexString, bigint>) {
|
|
116
|
+
const current = map.get(tokenId)
|
|
117
|
+
if (current !== undefined) {
|
|
118
|
+
map.set(tokenId, current + amount)
|
|
119
|
+
} else if (amount > 0n) {
|
|
120
|
+
map.set(tokenId, amount)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private approveTokens(tokens: { id: HexString; amount: bigint }[]): { id: HexString; amount: bigint }[] {
|
|
125
|
+
const tokenAmounts = new Map<HexString, bigint>()
|
|
126
|
+
tokens.forEach((token) => {
|
|
127
|
+
if (!(isHexString(token.id) && token.id.length === 64)) {
|
|
128
|
+
throw new Error(`Invalid token id: ${token.id}`)
|
|
129
|
+
}
|
|
130
|
+
if (token.amount < 0n) {
|
|
131
|
+
throw new Error(`Invalid token amount: ${token.amount}`)
|
|
132
|
+
}
|
|
133
|
+
this.addTokenToMap(token.id, token.amount, tokenAmounts)
|
|
134
|
+
this.addTokenToMap(token.id, token.amount, this.approvedAssets)
|
|
135
|
+
})
|
|
136
|
+
return Array.from(tokenAmounts.entries()).map(([id, amount]) => ({ id, amount }))
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function genApproveAssets(callerLockupScript: LockupScript, tokens: { id: HexString; amount: bigint }[]): Instr[] {
|
|
141
|
+
if (tokens.length === 0) {
|
|
142
|
+
return []
|
|
143
|
+
}
|
|
144
|
+
const approveInstrs = tokens.flatMap((token) => {
|
|
145
|
+
if (token.id === ALPH_TOKEN_ID) {
|
|
146
|
+
return [U256Const(token.amount), ApproveAlph]
|
|
147
|
+
} else {
|
|
148
|
+
const tokenId = BytesConst(hexToBinUnsafe(token.id))
|
|
149
|
+
return [tokenId, U256Const(token.amount), ApproveToken]
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
return [
|
|
153
|
+
AddressConst(callerLockupScript),
|
|
154
|
+
...Array.from(Array(tokens.length - 1).keys()).map(() => Dup),
|
|
155
|
+
...approveInstrs
|
|
156
|
+
]
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function bigintToNumeric(value: bigint): Instr {
|
|
160
|
+
return value >= 0 ? toU256(value) : toI256(value)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function strToNumeric(str: string): Instr {
|
|
164
|
+
const regex = /^-?\d+[ui]?$/
|
|
165
|
+
if (regex.test(str)) {
|
|
166
|
+
if (str.endsWith('i')) return toI256(BigInt(str.slice(0, str.length - 1)))
|
|
167
|
+
if (str.endsWith('u')) return toU256(BigInt(str.slice(0, str.length - 1)))
|
|
168
|
+
return bigintToNumeric(BigInt(str))
|
|
169
|
+
}
|
|
170
|
+
throw new Error(`Invalid number: ${str}`)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function toAddressOpt(str: string): LockupScript | undefined {
|
|
174
|
+
if (!isBase58(str)) return undefined
|
|
175
|
+
try {
|
|
176
|
+
return lockupScriptCodec.decode(base58ToBytes(str))
|
|
177
|
+
} catch (_) {
|
|
178
|
+
return undefined
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function genArgs(args: Val[]): Instr[] {
|
|
183
|
+
return args.flatMap((arg) => {
|
|
184
|
+
if (typeof arg === 'boolean') return arg ? [ConstTrue] : [ConstFalse]
|
|
185
|
+
if (typeof arg === 'bigint') return bigintToNumeric(arg)
|
|
186
|
+
if (typeof arg === 'string') {
|
|
187
|
+
if (isHexString(arg)) return [BytesConst(hexToBinUnsafe(arg))]
|
|
188
|
+
const addressOpt = toAddressOpt(arg)
|
|
189
|
+
if (addressOpt !== undefined) return AddressConst(addressOpt)
|
|
190
|
+
return strToNumeric(arg)
|
|
191
|
+
}
|
|
192
|
+
if (Array.isArray(arg)) return genArgs(arg)
|
|
193
|
+
if (arg instanceof Map) throw new Error(`Map cannot be used as a function argument`)
|
|
194
|
+
if (typeof arg === 'object') return genArgs(Object.values(arg))
|
|
195
|
+
throw new Error(`Unknown argument type: ${typeof arg}, arg: ${arg}`)
|
|
196
|
+
})
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function genContractCall(contractAddress: string, methodIndex: number, args: Val[], retLength: number): Instr[] {
|
|
200
|
+
const argInstrs = genArgs(args)
|
|
201
|
+
return [
|
|
202
|
+
...argInstrs,
|
|
203
|
+
toU256(BigInt(argInstrs.length)),
|
|
204
|
+
toU256(BigInt(retLength)),
|
|
205
|
+
BytesConst(contractIdFromAddress(contractAddress)),
|
|
206
|
+
CallExternal(methodIndex),
|
|
207
|
+
...Array.from(Array(retLength).keys()).map(() => Pop)
|
|
208
|
+
]
|
|
209
|
+
}
|
package/src/contract/index.ts
CHANGED
package/src/contract/ralph.ts
CHANGED
|
@@ -26,24 +26,11 @@ import {
|
|
|
26
26
|
ConstFalse,
|
|
27
27
|
ConstTrue,
|
|
28
28
|
i256Codec,
|
|
29
|
-
I256Const,
|
|
30
|
-
I256Const0,
|
|
31
|
-
I256Const1,
|
|
32
|
-
I256Const2,
|
|
33
|
-
I256Const3,
|
|
34
|
-
I256Const4,
|
|
35
|
-
I256Const5,
|
|
36
|
-
I256ConstN1,
|
|
37
29
|
i32Codec,
|
|
38
30
|
instrCodec,
|
|
39
31
|
u256Codec,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
U256Const1,
|
|
43
|
-
U256Const2,
|
|
44
|
-
U256Const3,
|
|
45
|
-
U256Const4,
|
|
46
|
-
U256Const5
|
|
32
|
+
toU256,
|
|
33
|
+
toI256
|
|
47
34
|
} from '../codec'
|
|
48
35
|
import { boolCodec } from '../codec/codec'
|
|
49
36
|
import { TraceableError } from '../error'
|
|
@@ -118,43 +105,11 @@ function invalidScriptField(tpe: string, value: Val): Error {
|
|
|
118
105
|
}
|
|
119
106
|
|
|
120
107
|
function encodeScriptFieldI256(value: bigint): Uint8Array {
|
|
121
|
-
|
|
122
|
-
case 0n:
|
|
123
|
-
return instrCodec.encode(I256Const0)
|
|
124
|
-
case 1n:
|
|
125
|
-
return instrCodec.encode(I256Const1)
|
|
126
|
-
case 2n:
|
|
127
|
-
return instrCodec.encode(I256Const2)
|
|
128
|
-
case 3n:
|
|
129
|
-
return instrCodec.encode(I256Const3)
|
|
130
|
-
case 4n:
|
|
131
|
-
return instrCodec.encode(I256Const4)
|
|
132
|
-
case 5n:
|
|
133
|
-
return instrCodec.encode(I256Const5)
|
|
134
|
-
case -1n:
|
|
135
|
-
return instrCodec.encode(I256ConstN1)
|
|
136
|
-
default:
|
|
137
|
-
return instrCodec.encode(I256Const(value))
|
|
138
|
-
}
|
|
108
|
+
return instrCodec.encode(toI256(value))
|
|
139
109
|
}
|
|
140
110
|
|
|
141
111
|
function encodeScriptFieldU256(value: bigint): Uint8Array {
|
|
142
|
-
|
|
143
|
-
case 0n:
|
|
144
|
-
return instrCodec.encode(U256Const0)
|
|
145
|
-
case 1n:
|
|
146
|
-
return instrCodec.encode(U256Const1)
|
|
147
|
-
case 2n:
|
|
148
|
-
return instrCodec.encode(U256Const2)
|
|
149
|
-
case 3n:
|
|
150
|
-
return instrCodec.encode(U256Const3)
|
|
151
|
-
case 4n:
|
|
152
|
-
return instrCodec.encode(U256Const4)
|
|
153
|
-
case 5n:
|
|
154
|
-
return instrCodec.encode(U256Const5)
|
|
155
|
-
default:
|
|
156
|
-
return instrCodec.encode(U256Const(value))
|
|
157
|
-
}
|
|
112
|
+
return instrCodec.encode(toU256(value))
|
|
158
113
|
}
|
|
159
114
|
|
|
160
115
|
export function encodeScriptFieldAsString(tpe: string, value: Val): string {
|