@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
|
@@ -823,3 +823,5 @@ export declare class InstrCodec extends Codec<Instr> {
|
|
|
823
823
|
}
|
|
824
824
|
export declare const instrCodec: InstrCodec;
|
|
825
825
|
export declare const instrsCodec: ArrayCodec<Instr>;
|
|
826
|
+
export declare function toU256(number: bigint): Instr;
|
|
827
|
+
export declare function toI256(number: bigint): Instr;
|
|
@@ -4,7 +4,7 @@ exports.I256Add = exports.BoolToByteVec = exports.BoolNeq = exports.BoolEq = exp
|
|
|
4
4
|
exports.Sha3 = exports.Sha256 = exports.Keccak256 = exports.Blake2b = exports.Assert = exports.IfFalse = exports.IfTrue = exports.Jump = exports.IsContractAddress = exports.IsAssetAddress = exports.AddressToByteVec = exports.AddressNeq = exports.AddressEq = exports.ByteVecConcat = exports.ByteVecSize = exports.ByteVecNeq = exports.ByteVecEq = exports.U256ToByteVec = exports.U256ToI256 = exports.I256ToByteVec = exports.I256ToU256 = exports.U256SHR = exports.U256SHL = exports.U256Xor = exports.U256BitOr = exports.U256BitAnd = exports.U256ModMul = exports.U256ModSub = exports.U256ModAdd = exports.U256Ge = exports.U256Gt = exports.U256Le = exports.U256Lt = exports.U256Neq = exports.U256Eq = exports.U256Mod = exports.U256Div = exports.U256Mul = exports.U256Sub = exports.U256Add = exports.I256Ge = exports.I256Gt = exports.I256Le = exports.I256Lt = exports.I256Neq = exports.I256Eq = exports.I256Mod = exports.I256Div = exports.I256Mul = exports.I256Sub = void 0;
|
|
5
5
|
exports.U256Exp = exports.I256Exp = exports.TxGasFee = exports.TxGasAmount = exports.TxGasPrice = exports.DEBUG = exports.BlockHash = exports.Swap = exports.AssertWithErrorCode = exports.Dup = exports.StoreLocalByIndex = exports.LoadLocalByIndex = exports.ContractIdToAddress = exports.Log9 = exports.Log8 = exports.Log7 = exports.Log6 = exports.EthEcRecover = exports.U256From32Byte = exports.U256From16Byte = exports.U256From8Byte = exports.U256From4Byte = exports.U256From2Byte = exports.U256From1Byte = exports.U256To32Byte = exports.U256To16Byte = exports.U256To8Byte = exports.U256To4Byte = exports.U256To2Byte = exports.U256To1Byte = exports.Zeros = exports.Encode = exports.ByteVecToAddress = exports.ByteVecSlice = exports.Log5 = exports.Log4 = exports.Log3 = exports.Log2 = exports.Log1 = exports.VerifyRelativeLocktime = exports.VerifyAbsoluteLocktime = exports.TxInputsSize = exports.TxInputAddressAt = exports.TxId = exports.BlockTarget = exports.BlockTimeStamp = exports.NetworkId = exports.VerifyED25519 = exports.VerifySecP256K1 = exports.VerifyTxSignature = void 0;
|
|
6
6
|
exports.CreateSubContractAndTransferToken = exports.CopyCreateContractAndTransferToken = exports.CreateContractAndTransferToken = exports.ContractExists = exports.StoreMutFieldByIndex = exports.LoadMutFieldByIndex = exports.CopyCreateSubContractWithToken = exports.CopyCreateSubContract = exports.CreateSubContractWithToken = exports.CreateSubContract = exports.LockApprovedAssets = exports.BurnToken = exports.CopyCreateContractWithToken = exports.MigrateWithFields = exports.MigrateSimple = exports.ContractCodeHash = exports.ContractInitialStateHash = exports.CallerCodeHash = exports.CallerInitialStateHash = exports.IsCalledFromTxScript = exports.CallerAddress = exports.CallerContractId = exports.SelfAddress = exports.SelfContractId = exports.DestroySelf = exports.CopyCreateContract = exports.CreateContractWithToken = exports.CreateContract = exports.TransferTokenToSelf = exports.TransferTokenFromSelf = exports.TransferToken = exports.TransferAlphToSelf = exports.TransferAlphFromSelf = exports.TransferAlph = exports.IsPaying = exports.TokenRemaining = exports.AlphRemaining = exports.ApproveToken = exports.ApproveAlph = exports.StoreMutField = exports.LoadMutField = exports.GroupOfAddress = exports.BoolToString = exports.I256ToString = exports.U256ToString = exports.AddModN = exports.MulModN = exports.GetSegregatedSignature = exports.VerifyBIP340Schnorr = exports.U256ModExp = void 0;
|
|
7
|
-
exports.instrsCodec = exports.instrCodec = exports.InstrCodec = exports.CallExternalBySelector = exports.MethodSelector = exports.CreateMapEntry = exports.MinimalContractDeposit = exports.PayGasFee = exports.LoadImmFieldByIndex = exports.LoadImmField = exports.ALPHTokenId = exports.SubContractIdOf = exports.SubContractId = exports.NullContractAddress = exports.CopyCreateSubContractAndTransferToken = void 0;
|
|
7
|
+
exports.toI256 = exports.toU256 = exports.instrsCodec = exports.instrCodec = exports.InstrCodec = exports.CallExternalBySelector = exports.MethodSelector = exports.CreateMapEntry = exports.MinimalContractDeposit = exports.PayGasFee = exports.LoadImmFieldByIndex = exports.LoadImmField = exports.ALPHTokenId = exports.SubContractIdOf = exports.SubContractId = exports.NullContractAddress = exports.CopyCreateSubContractAndTransferToken = void 0;
|
|
8
8
|
/*
|
|
9
9
|
Copyright 2018 - 2022 The Alephium Authors
|
|
10
10
|
This file is part of the alephium project.
|
|
@@ -1090,3 +1090,56 @@ class InstrCodec extends codec_1.Codec {
|
|
|
1090
1090
|
exports.InstrCodec = InstrCodec;
|
|
1091
1091
|
exports.instrCodec = new InstrCodec();
|
|
1092
1092
|
exports.instrsCodec = new array_codec_1.ArrayCodec(exports.instrCodec);
|
|
1093
|
+
function checkU256(number) {
|
|
1094
|
+
if (number < 0n || number >= 2n ** 256n) {
|
|
1095
|
+
throw new Error(`Invalid u256 number: ${number}`);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
function toU256(number) {
|
|
1099
|
+
checkU256(number);
|
|
1100
|
+
switch (number) {
|
|
1101
|
+
case 0n:
|
|
1102
|
+
return exports.U256Const0;
|
|
1103
|
+
case 1n:
|
|
1104
|
+
return exports.U256Const1;
|
|
1105
|
+
case 2n:
|
|
1106
|
+
return exports.U256Const2;
|
|
1107
|
+
case 3n:
|
|
1108
|
+
return exports.U256Const3;
|
|
1109
|
+
case 4n:
|
|
1110
|
+
return exports.U256Const4;
|
|
1111
|
+
case 5n:
|
|
1112
|
+
return exports.U256Const5;
|
|
1113
|
+
default:
|
|
1114
|
+
return (0, exports.U256Const)(number);
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
exports.toU256 = toU256;
|
|
1118
|
+
function checkI256(number) {
|
|
1119
|
+
const upperBound = 2n ** 255n;
|
|
1120
|
+
if (number < -upperBound || number >= upperBound) {
|
|
1121
|
+
throw new Error(`Invalid i256 number: ${number}`);
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
function toI256(number) {
|
|
1125
|
+
checkI256(number);
|
|
1126
|
+
switch (number) {
|
|
1127
|
+
case 0n:
|
|
1128
|
+
return exports.I256Const0;
|
|
1129
|
+
case 1n:
|
|
1130
|
+
return exports.I256Const1;
|
|
1131
|
+
case 2n:
|
|
1132
|
+
return exports.I256Const2;
|
|
1133
|
+
case 3n:
|
|
1134
|
+
return exports.I256Const3;
|
|
1135
|
+
case 4n:
|
|
1136
|
+
return exports.I256Const4;
|
|
1137
|
+
case 5n:
|
|
1138
|
+
return exports.I256Const5;
|
|
1139
|
+
case -1n:
|
|
1140
|
+
return exports.I256ConstN1;
|
|
1141
|
+
default:
|
|
1142
|
+
return (0, exports.I256Const)(number);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
exports.toI256 = toI256;
|
package/dist/src/constants.d.ts
CHANGED
|
@@ -4,7 +4,11 @@ export declare const MIN_UTXO_SET_AMOUNT: bigint;
|
|
|
4
4
|
export declare const ALPH_TOKEN_ID: string;
|
|
5
5
|
export declare const ONE_ALPH: bigint;
|
|
6
6
|
export declare const DUST_AMOUNT: bigint;
|
|
7
|
+
/**
|
|
8
|
+
* @deprecated `ZERO_ADDRESS` is deprecated. Use `NULL_CONTRACT_ADDRESS` instead.
|
|
9
|
+
*/
|
|
7
10
|
export declare const ZERO_ADDRESS = "tgx7VNFoP9DJiFMFgXXtafQZkUvyEdDHT9ryamHJYrjq";
|
|
11
|
+
export declare const NULL_CONTRACT_ADDRESS = "tgx7VNFoP9DJiFMFgXXtafQZkUvyEdDHT9ryamHJYrjq";
|
|
8
12
|
export declare const DEFAULT_GAS_AMOUNT = 20000;
|
|
9
13
|
export declare const DEFAULT_GAS_PRICE: bigint;
|
|
10
14
|
export declare const DEFAULT_GAS_ATTOALPH_AMOUNT: bigint;
|
package/dist/src/constants.js
CHANGED
|
@@ -17,14 +17,18 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
17
17
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
18
18
|
*/
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.MAP_ENTRY_DEPOSIT = exports.MINIMAL_CONTRACT_DEPOSIT = exports.DEFAULT_GAS_ALPH_AMOUNT = exports.DEFAULT_GAS_ATTOALPH_AMOUNT = exports.DEFAULT_GAS_PRICE = exports.DEFAULT_GAS_AMOUNT = exports.ZERO_ADDRESS = exports.DUST_AMOUNT = exports.ONE_ALPH = exports.ALPH_TOKEN_ID = exports.MIN_UTXO_SET_AMOUNT = exports.TOTAL_NUMBER_OF_CHAINS = exports.TOTAL_NUMBER_OF_GROUPS = void 0;
|
|
20
|
+
exports.MAP_ENTRY_DEPOSIT = exports.MINIMAL_CONTRACT_DEPOSIT = exports.DEFAULT_GAS_ALPH_AMOUNT = exports.DEFAULT_GAS_ATTOALPH_AMOUNT = exports.DEFAULT_GAS_PRICE = exports.DEFAULT_GAS_AMOUNT = exports.NULL_CONTRACT_ADDRESS = exports.ZERO_ADDRESS = exports.DUST_AMOUNT = exports.ONE_ALPH = exports.ALPH_TOKEN_ID = exports.MIN_UTXO_SET_AMOUNT = exports.TOTAL_NUMBER_OF_CHAINS = exports.TOTAL_NUMBER_OF_GROUPS = void 0;
|
|
21
21
|
exports.TOTAL_NUMBER_OF_GROUPS = 4;
|
|
22
22
|
exports.TOTAL_NUMBER_OF_CHAINS = exports.TOTAL_NUMBER_OF_GROUPS * exports.TOTAL_NUMBER_OF_GROUPS;
|
|
23
23
|
exports.MIN_UTXO_SET_AMOUNT = BigInt(1000000000000);
|
|
24
24
|
exports.ALPH_TOKEN_ID = ''.padStart(64, '0');
|
|
25
25
|
exports.ONE_ALPH = 10n ** 18n;
|
|
26
26
|
exports.DUST_AMOUNT = 10n ** 15n;
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated `ZERO_ADDRESS` is deprecated. Use `NULL_CONTRACT_ADDRESS` instead.
|
|
29
|
+
*/
|
|
27
30
|
exports.ZERO_ADDRESS = 'tgx7VNFoP9DJiFMFgXXtafQZkUvyEdDHT9ryamHJYrjq';
|
|
31
|
+
exports.NULL_CONTRACT_ADDRESS = 'tgx7VNFoP9DJiFMFgXXtafQZkUvyEdDHT9ryamHJYrjq';
|
|
28
32
|
exports.DEFAULT_GAS_AMOUNT = 20000;
|
|
29
33
|
exports.DEFAULT_GAS_PRICE = 10n ** 11n;
|
|
30
34
|
exports.DEFAULT_GAS_ATTOALPH_AMOUNT = BigInt(exports.DEFAULT_GAS_AMOUNT) * exports.DEFAULT_GAS_PRICE;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Val } from '../api';
|
|
2
|
+
import { Instr } from '../codec';
|
|
3
|
+
import { SignExecuteScriptTxParams } from '../signer';
|
|
4
|
+
import { HexString } from '../utils';
|
|
5
|
+
export declare class DappTransactionBuilder {
|
|
6
|
+
readonly callerAddress: string;
|
|
7
|
+
private callerLockupScript;
|
|
8
|
+
private approvedAssets;
|
|
9
|
+
private instrs;
|
|
10
|
+
constructor(callerAddress: string);
|
|
11
|
+
callContract(params: {
|
|
12
|
+
contractAddress: string;
|
|
13
|
+
methodIndex: number;
|
|
14
|
+
args: Val[];
|
|
15
|
+
attoAlphAmount?: bigint;
|
|
16
|
+
tokens?: {
|
|
17
|
+
id: HexString;
|
|
18
|
+
amount: bigint;
|
|
19
|
+
}[];
|
|
20
|
+
retLength?: number;
|
|
21
|
+
}): this;
|
|
22
|
+
getResult(): SignExecuteScriptTxParams;
|
|
23
|
+
private addTokenToMap;
|
|
24
|
+
private approveTokens;
|
|
25
|
+
}
|
|
26
|
+
export declare function genArgs(args: Val[]): Instr[];
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
4
|
+
This file is part of the alephium project.
|
|
5
|
+
|
|
6
|
+
The library is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
The library is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU Lesser General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.genArgs = exports.DappTransactionBuilder = void 0;
|
|
21
|
+
const address_1 = require("../address");
|
|
22
|
+
const codec_1 = require("../codec");
|
|
23
|
+
const lockup_script_codec_1 = require("../codec/lockup-script-codec");
|
|
24
|
+
const script_codec_1 = require("../codec/script-codec");
|
|
25
|
+
const constants_1 = require("../constants");
|
|
26
|
+
const error_1 = require("../error");
|
|
27
|
+
const utils_1 = require("../utils");
|
|
28
|
+
class DappTransactionBuilder {
|
|
29
|
+
constructor(callerAddress) {
|
|
30
|
+
this.callerAddress = callerAddress;
|
|
31
|
+
try {
|
|
32
|
+
this.callerLockupScript = lockup_script_codec_1.lockupScriptCodec.decode((0, utils_1.base58ToBytes)(this.callerAddress));
|
|
33
|
+
if (this.callerLockupScript.kind !== 'P2PKH' && this.callerLockupScript.kind !== 'P2SH') {
|
|
34
|
+
throw new Error(`Expected a P2PKH address or P2SH address`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
throw new error_1.TraceableError(`Invalid caller address: ${callerAddress}`, error);
|
|
39
|
+
}
|
|
40
|
+
this.approvedAssets = new Map();
|
|
41
|
+
this.instrs = [];
|
|
42
|
+
}
|
|
43
|
+
callContract(params) {
|
|
44
|
+
if (!(0, utils_1.isBase58)(params.contractAddress)) {
|
|
45
|
+
throw new Error(`Invalid contract address: ${params.contractAddress}, expected a base58 string`);
|
|
46
|
+
}
|
|
47
|
+
if (!(0, address_1.isContractAddress)(params.contractAddress)) {
|
|
48
|
+
throw new Error(`Invalid contract address: ${params.contractAddress}, expected a P2C address`);
|
|
49
|
+
}
|
|
50
|
+
if (params.methodIndex < 0) {
|
|
51
|
+
throw new Error(`Invalid method index: ${params.methodIndex}`);
|
|
52
|
+
}
|
|
53
|
+
const allTokens = (params.tokens ?? []).concat([{ id: constants_1.ALPH_TOKEN_ID, amount: params.attoAlphAmount ?? 0n }]);
|
|
54
|
+
const instrs = [
|
|
55
|
+
...genApproveAssets(this.callerLockupScript, this.approveTokens(allTokens)),
|
|
56
|
+
...genContractCall(params.contractAddress, params.methodIndex, params.args, params.retLength ?? 0)
|
|
57
|
+
];
|
|
58
|
+
this.instrs.push(...instrs);
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
getResult() {
|
|
62
|
+
const method = {
|
|
63
|
+
isPublic: true,
|
|
64
|
+
usePreapprovedAssets: this.approvedAssets.size > 0,
|
|
65
|
+
useContractAssets: false,
|
|
66
|
+
usePayToContractOnly: false,
|
|
67
|
+
argsLength: 0,
|
|
68
|
+
localsLength: 0,
|
|
69
|
+
returnLength: 0,
|
|
70
|
+
instrs: this.instrs
|
|
71
|
+
};
|
|
72
|
+
const script = { methods: [method] };
|
|
73
|
+
const bytecode = script_codec_1.scriptCodec.encode(script);
|
|
74
|
+
const tokens = Array.from(this.approvedAssets.entries()).map(([id, amount]) => ({ id, amount }));
|
|
75
|
+
this.approvedAssets.clear();
|
|
76
|
+
this.instrs = [];
|
|
77
|
+
return {
|
|
78
|
+
signerAddress: this.callerAddress,
|
|
79
|
+
signerKeyType: this.callerLockupScript.kind === 'P2PKH' ? 'default' : 'bip340-schnorr',
|
|
80
|
+
bytecode: (0, utils_1.binToHex)(bytecode),
|
|
81
|
+
attoAlphAmount: tokens.find((t) => t.id === constants_1.ALPH_TOKEN_ID)?.amount,
|
|
82
|
+
tokens: tokens.filter((t) => t.id !== constants_1.ALPH_TOKEN_ID)
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
addTokenToMap(tokenId, amount, map) {
|
|
86
|
+
const current = map.get(tokenId);
|
|
87
|
+
if (current !== undefined) {
|
|
88
|
+
map.set(tokenId, current + amount);
|
|
89
|
+
}
|
|
90
|
+
else if (amount > 0n) {
|
|
91
|
+
map.set(tokenId, amount);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
approveTokens(tokens) {
|
|
95
|
+
const tokenAmounts = new Map();
|
|
96
|
+
tokens.forEach((token) => {
|
|
97
|
+
if (!((0, utils_1.isHexString)(token.id) && token.id.length === 64)) {
|
|
98
|
+
throw new Error(`Invalid token id: ${token.id}`);
|
|
99
|
+
}
|
|
100
|
+
if (token.amount < 0n) {
|
|
101
|
+
throw new Error(`Invalid token amount: ${token.amount}`);
|
|
102
|
+
}
|
|
103
|
+
this.addTokenToMap(token.id, token.amount, tokenAmounts);
|
|
104
|
+
this.addTokenToMap(token.id, token.amount, this.approvedAssets);
|
|
105
|
+
});
|
|
106
|
+
return Array.from(tokenAmounts.entries()).map(([id, amount]) => ({ id, amount }));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.DappTransactionBuilder = DappTransactionBuilder;
|
|
110
|
+
function genApproveAssets(callerLockupScript, tokens) {
|
|
111
|
+
if (tokens.length === 0) {
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
const approveInstrs = tokens.flatMap((token) => {
|
|
115
|
+
if (token.id === constants_1.ALPH_TOKEN_ID) {
|
|
116
|
+
return [(0, codec_1.U256Const)(token.amount), codec_1.ApproveAlph];
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
const tokenId = (0, codec_1.BytesConst)((0, utils_1.hexToBinUnsafe)(token.id));
|
|
120
|
+
return [tokenId, (0, codec_1.U256Const)(token.amount), codec_1.ApproveToken];
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return [
|
|
124
|
+
(0, codec_1.AddressConst)(callerLockupScript),
|
|
125
|
+
...Array.from(Array(tokens.length - 1).keys()).map(() => codec_1.Dup),
|
|
126
|
+
...approveInstrs
|
|
127
|
+
];
|
|
128
|
+
}
|
|
129
|
+
function bigintToNumeric(value) {
|
|
130
|
+
return value >= 0 ? (0, codec_1.toU256)(value) : (0, codec_1.toI256)(value);
|
|
131
|
+
}
|
|
132
|
+
function strToNumeric(str) {
|
|
133
|
+
const regex = /^-?\d+[ui]?$/;
|
|
134
|
+
if (regex.test(str)) {
|
|
135
|
+
if (str.endsWith('i'))
|
|
136
|
+
return (0, codec_1.toI256)(BigInt(str.slice(0, str.length - 1)));
|
|
137
|
+
if (str.endsWith('u'))
|
|
138
|
+
return (0, codec_1.toU256)(BigInt(str.slice(0, str.length - 1)));
|
|
139
|
+
return bigintToNumeric(BigInt(str));
|
|
140
|
+
}
|
|
141
|
+
throw new Error(`Invalid number: ${str}`);
|
|
142
|
+
}
|
|
143
|
+
function toAddressOpt(str) {
|
|
144
|
+
if (!(0, utils_1.isBase58)(str))
|
|
145
|
+
return undefined;
|
|
146
|
+
try {
|
|
147
|
+
return lockup_script_codec_1.lockupScriptCodec.decode((0, utils_1.base58ToBytes)(str));
|
|
148
|
+
}
|
|
149
|
+
catch (_) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function genArgs(args) {
|
|
154
|
+
return args.flatMap((arg) => {
|
|
155
|
+
if (typeof arg === 'boolean')
|
|
156
|
+
return arg ? [codec_1.ConstTrue] : [codec_1.ConstFalse];
|
|
157
|
+
if (typeof arg === 'bigint')
|
|
158
|
+
return bigintToNumeric(arg);
|
|
159
|
+
if (typeof arg === 'string') {
|
|
160
|
+
if ((0, utils_1.isHexString)(arg))
|
|
161
|
+
return [(0, codec_1.BytesConst)((0, utils_1.hexToBinUnsafe)(arg))];
|
|
162
|
+
const addressOpt = toAddressOpt(arg);
|
|
163
|
+
if (addressOpt !== undefined)
|
|
164
|
+
return (0, codec_1.AddressConst)(addressOpt);
|
|
165
|
+
return strToNumeric(arg);
|
|
166
|
+
}
|
|
167
|
+
if (Array.isArray(arg))
|
|
168
|
+
return genArgs(arg);
|
|
169
|
+
if (arg instanceof Map)
|
|
170
|
+
throw new Error(`Map cannot be used as a function argument`);
|
|
171
|
+
if (typeof arg === 'object')
|
|
172
|
+
return genArgs(Object.values(arg));
|
|
173
|
+
throw new Error(`Unknown argument type: ${typeof arg}, arg: ${arg}`);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
exports.genArgs = genArgs;
|
|
177
|
+
function genContractCall(contractAddress, methodIndex, args, retLength) {
|
|
178
|
+
const argInstrs = genArgs(args);
|
|
179
|
+
return [
|
|
180
|
+
...argInstrs,
|
|
181
|
+
(0, codec_1.toU256)(BigInt(argInstrs.length)),
|
|
182
|
+
(0, codec_1.toU256)(BigInt(retLength)),
|
|
183
|
+
(0, codec_1.BytesConst)((0, address_1.contractIdFromAddress)(contractAddress)),
|
|
184
|
+
(0, codec_1.CallExternal)(methodIndex),
|
|
185
|
+
...Array.from(Array(retLength).keys()).map(() => codec_1.Pop)
|
|
186
|
+
];
|
|
187
|
+
}
|
|
@@ -31,8 +31,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
31
31
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
32
32
|
};
|
|
33
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
+
exports.DappTransactionBuilder = void 0;
|
|
34
35
|
__exportStar(require("./ralph"), exports);
|
|
35
36
|
__exportStar(require("./contract"), exports);
|
|
36
37
|
__exportStar(require("./events"), exports);
|
|
37
38
|
__exportStar(require("./script-simulator"), exports);
|
|
38
39
|
__exportStar(require("./deployment"), exports);
|
|
40
|
+
var dapp_tx_builder_1 = require("./dapp-tx-builder");
|
|
41
|
+
Object.defineProperty(exports, "DappTransactionBuilder", { enumerable: true, get: function () { return dapp_tx_builder_1.DappTransactionBuilder; } });
|
|
@@ -91,42 +91,10 @@ function invalidScriptField(tpe, value) {
|
|
|
91
91
|
return Error(`Invalid script field ${value} for type ${tpe}`);
|
|
92
92
|
}
|
|
93
93
|
function encodeScriptFieldI256(value) {
|
|
94
|
-
|
|
95
|
-
case 0n:
|
|
96
|
-
return codec_1.instrCodec.encode(codec_1.I256Const0);
|
|
97
|
-
case 1n:
|
|
98
|
-
return codec_1.instrCodec.encode(codec_1.I256Const1);
|
|
99
|
-
case 2n:
|
|
100
|
-
return codec_1.instrCodec.encode(codec_1.I256Const2);
|
|
101
|
-
case 3n:
|
|
102
|
-
return codec_1.instrCodec.encode(codec_1.I256Const3);
|
|
103
|
-
case 4n:
|
|
104
|
-
return codec_1.instrCodec.encode(codec_1.I256Const4);
|
|
105
|
-
case 5n:
|
|
106
|
-
return codec_1.instrCodec.encode(codec_1.I256Const5);
|
|
107
|
-
case -1n:
|
|
108
|
-
return codec_1.instrCodec.encode(codec_1.I256ConstN1);
|
|
109
|
-
default:
|
|
110
|
-
return codec_1.instrCodec.encode((0, codec_1.I256Const)(value));
|
|
111
|
-
}
|
|
94
|
+
return codec_1.instrCodec.encode((0, codec_1.toI256)(value));
|
|
112
95
|
}
|
|
113
96
|
function encodeScriptFieldU256(value) {
|
|
114
|
-
|
|
115
|
-
case 0n:
|
|
116
|
-
return codec_1.instrCodec.encode(codec_1.U256Const0);
|
|
117
|
-
case 1n:
|
|
118
|
-
return codec_1.instrCodec.encode(codec_1.U256Const1);
|
|
119
|
-
case 2n:
|
|
120
|
-
return codec_1.instrCodec.encode(codec_1.U256Const2);
|
|
121
|
-
case 3n:
|
|
122
|
-
return codec_1.instrCodec.encode(codec_1.U256Const3);
|
|
123
|
-
case 4n:
|
|
124
|
-
return codec_1.instrCodec.encode(codec_1.U256Const4);
|
|
125
|
-
case 5n:
|
|
126
|
-
return codec_1.instrCodec.encode(codec_1.U256Const5);
|
|
127
|
-
default:
|
|
128
|
-
return codec_1.instrCodec.encode((0, codec_1.U256Const)(value));
|
|
129
|
-
}
|
|
97
|
+
return codec_1.instrCodec.encode((0, codec_1.toU256)(value));
|
|
130
98
|
}
|
|
131
99
|
function encodeScriptFieldAsString(tpe, value) {
|
|
132
100
|
return (0, utils_1.binToHex)(encodeScriptField(tpe, value));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alephium/web3",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "A JS/TS library to interact with the Alephium platform",
|
|
5
5
|
"license": "GPL",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -45,14 +45,14 @@
|
|
|
45
45
|
"bn.js": "5.2.1",
|
|
46
46
|
"cross-fetch": "^3.1.5",
|
|
47
47
|
"crypto-browserify": "^3.12.0",
|
|
48
|
-
"elliptic": "6.
|
|
48
|
+
"elliptic": "6.6.0",
|
|
49
49
|
"eventemitter3": "^4.0.7",
|
|
50
50
|
"path-browserify": "^1.0.1",
|
|
51
51
|
"stream-browserify": "^3.0.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@babel/eslint-parser": "^7.21.3",
|
|
55
|
-
"@types/elliptic": "^6.4.
|
|
55
|
+
"@types/elliptic": "^6.4.18",
|
|
56
56
|
"@types/find-up": "^2.1.1",
|
|
57
57
|
"@types/fs-extra": "^9.0.13",
|
|
58
58
|
"@types/jest": "^27.5.2",
|
package/src/codec/instr-codec.ts
CHANGED
|
@@ -20,7 +20,7 @@ import { ArrayCodec } from './array-codec'
|
|
|
20
20
|
import { i256Codec, u256Codec, i32Codec } from './compact-int-codec'
|
|
21
21
|
import { ByteString, byteStringCodec, byteStringsCodec } from './bytestring-codec'
|
|
22
22
|
import { LockupScript, lockupScriptCodec } from './lockup-script-codec'
|
|
23
|
-
import { byteCodec, Codec } from './codec'
|
|
23
|
+
import { assert, byteCodec, Codec } from './codec'
|
|
24
24
|
import { intAs4BytesCodec } from './int-as-4bytes-codec'
|
|
25
25
|
import { Reader } from './reader'
|
|
26
26
|
export type Instr =
|
|
@@ -1263,3 +1263,58 @@ export class InstrCodec extends Codec<Instr> {
|
|
|
1263
1263
|
}
|
|
1264
1264
|
export const instrCodec = new InstrCodec()
|
|
1265
1265
|
export const instrsCodec = new ArrayCodec<Instr>(instrCodec)
|
|
1266
|
+
|
|
1267
|
+
function checkU256(number: bigint) {
|
|
1268
|
+
if (number < 0n || number >= 2n ** 256n) {
|
|
1269
|
+
throw new Error(`Invalid u256 number: ${number}`)
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
export function toU256(number: bigint) {
|
|
1274
|
+
checkU256(number)
|
|
1275
|
+
switch (number) {
|
|
1276
|
+
case 0n:
|
|
1277
|
+
return U256Const0
|
|
1278
|
+
case 1n:
|
|
1279
|
+
return U256Const1
|
|
1280
|
+
case 2n:
|
|
1281
|
+
return U256Const2
|
|
1282
|
+
case 3n:
|
|
1283
|
+
return U256Const3
|
|
1284
|
+
case 4n:
|
|
1285
|
+
return U256Const4
|
|
1286
|
+
case 5n:
|
|
1287
|
+
return U256Const5
|
|
1288
|
+
default:
|
|
1289
|
+
return U256Const(number)
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
function checkI256(number: bigint) {
|
|
1294
|
+
const upperBound = 2n ** 255n
|
|
1295
|
+
if (number < -upperBound || number >= upperBound) {
|
|
1296
|
+
throw new Error(`Invalid i256 number: ${number}`)
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
export function toI256(number: bigint) {
|
|
1301
|
+
checkI256(number)
|
|
1302
|
+
switch (number) {
|
|
1303
|
+
case 0n:
|
|
1304
|
+
return I256Const0
|
|
1305
|
+
case 1n:
|
|
1306
|
+
return I256Const1
|
|
1307
|
+
case 2n:
|
|
1308
|
+
return I256Const2
|
|
1309
|
+
case 3n:
|
|
1310
|
+
return I256Const3
|
|
1311
|
+
case 4n:
|
|
1312
|
+
return I256Const4
|
|
1313
|
+
case 5n:
|
|
1314
|
+
return I256Const5
|
|
1315
|
+
case -1n:
|
|
1316
|
+
return I256ConstN1
|
|
1317
|
+
default:
|
|
1318
|
+
return I256Const(number)
|
|
1319
|
+
}
|
|
1320
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -22,7 +22,11 @@ export const MIN_UTXO_SET_AMOUNT = BigInt(1000000000000)
|
|
|
22
22
|
export const ALPH_TOKEN_ID = ''.padStart(64, '0')
|
|
23
23
|
export const ONE_ALPH = 10n ** 18n
|
|
24
24
|
export const DUST_AMOUNT = 10n ** 15n
|
|
25
|
+
/**
|
|
26
|
+
* @deprecated `ZERO_ADDRESS` is deprecated. Use `NULL_CONTRACT_ADDRESS` instead.
|
|
27
|
+
*/
|
|
25
28
|
export const ZERO_ADDRESS = 'tgx7VNFoP9DJiFMFgXXtafQZkUvyEdDHT9ryamHJYrjq'
|
|
29
|
+
export const NULL_CONTRACT_ADDRESS = 'tgx7VNFoP9DJiFMFgXXtafQZkUvyEdDHT9ryamHJYrjq'
|
|
26
30
|
export const DEFAULT_GAS_AMOUNT = 20000
|
|
27
31
|
export const DEFAULT_GAS_PRICE = 10n ** 11n
|
|
28
32
|
export const DEFAULT_GAS_ATTOALPH_AMOUNT = BigInt(DEFAULT_GAS_AMOUNT) * DEFAULT_GAS_PRICE
|