carbon-js-sdk 0.2.7-beta.3 → 0.2.7-beta.4
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/lib/clients/NEOClient.js +58 -39
- package/lib/provider/o3/O3Wallet.js +5 -5
- package/package.json +1 -1
package/lib/clients/NEOClient.js
CHANGED
|
@@ -1,4 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
2
21
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
22
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
23
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -19,7 +38,7 @@ const address_1 = require("../util/address");
|
|
|
19
38
|
const blockchain_1 = require("../util/blockchain");
|
|
20
39
|
const generic_1 = require("../util/generic");
|
|
21
40
|
const plugin_1 = __importDefault(require("@cityofzion/neon-api/lib/plugin"));
|
|
22
|
-
const
|
|
41
|
+
const Neon = __importStar(require("@cityofzion/neon-core"));
|
|
23
42
|
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
24
43
|
const ethers_1 = require("ethers");
|
|
25
44
|
const lodash_1 = require("lodash");
|
|
@@ -38,16 +57,16 @@ class NEOClient {
|
|
|
38
57
|
if (!hex || typeof (hex) !== "string")
|
|
39
58
|
return "0";
|
|
40
59
|
const res = hex.length % 2 !== 0 ? `0${hex}` : hex;
|
|
41
|
-
return new bignumber_js_1.default(res ?
|
|
60
|
+
return new bignumber_js_1.default(res ? Neon.u.reverseHex(res) : "00", 16).shiftedBy(-exp).toString();
|
|
42
61
|
}
|
|
43
62
|
getExternalBalances(sdk, address, url, whitelistDenoms) {
|
|
44
63
|
return __awaiter(this, void 0, void 0, function* () {
|
|
45
64
|
const tokenQueryResults = yield sdk.token.getAllTokens();
|
|
46
|
-
const account = new
|
|
65
|
+
const account = new Neon.wallet.Account(address);
|
|
47
66
|
const tokens = tokenQueryResults.filter(token => blockchain_1.blockchainForChainId(token.chainId.toNumber()) == this.blockchain &&
|
|
48
67
|
token.tokenAddress.length == 40 &&
|
|
49
68
|
token.bridgeAddress.length == 40);
|
|
50
|
-
const client = new
|
|
69
|
+
const client = new Neon.rpc.RPCClient(url, "2.5.2"); // TODO: should we change the RPC version??
|
|
51
70
|
// NOTE: fetching of tokens is chunked in sets of 15 as we may hit
|
|
52
71
|
// the gas limit on the RPC node and error out otherwise
|
|
53
72
|
const promises = lodash_1.chunk(tokens, 75).map((partition) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -56,8 +75,8 @@ class NEOClient {
|
|
|
56
75
|
for (const token of partition) {
|
|
57
76
|
if (whitelistDenoms && !whitelistDenoms.includes(token.denom))
|
|
58
77
|
continue;
|
|
59
|
-
const sb = new
|
|
60
|
-
sb.emitAppCall(
|
|
78
|
+
const sb = new Neon.sc.ScriptBuilder();
|
|
79
|
+
sb.emitAppCall(Neon.u.reverseHex(token.tokenAddress), "balanceOf", [Neon.u.reverseHex(account.scriptHash)]);
|
|
61
80
|
try {
|
|
62
81
|
const response = yield client.invokeScript(sb.str);
|
|
63
82
|
acc[token.denom.toUpperCase()] = ((_a = response.stack[0]) === null || _a === void 0 ? void 0 : _a.type) === "Integer" // Happens on polychain devnet
|
|
@@ -83,13 +102,13 @@ class NEOClient {
|
|
|
83
102
|
}
|
|
84
103
|
lockDeposit(token, feeAmountInput, swthAddress, neoPrivateKey) {
|
|
85
104
|
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
-
const account = new
|
|
105
|
+
const account = new Neon.wallet.Account(neoPrivateKey);
|
|
87
106
|
const networkConfig = this.getNetworkConfig();
|
|
88
|
-
const scriptHash =
|
|
107
|
+
const scriptHash = Neon.u.reverseHex(token.bridgeAddress);
|
|
89
108
|
const fromAssetHash = token.tokenAddress;
|
|
90
|
-
const fromAddress =
|
|
109
|
+
const fromAddress = Neon.u.reverseHex(account.scriptHash);
|
|
91
110
|
const targetProxyHash = this.getTargetProxyHash(token);
|
|
92
|
-
const toAssetHash =
|
|
111
|
+
const toAssetHash = Neon.u.str2hexstring(token.id);
|
|
93
112
|
const addressBytes = address_1.SWTHAddress.getAddressBytes(swthAddress, networkConfig.network);
|
|
94
113
|
const toAddress = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(addressBytes));
|
|
95
114
|
const amount = ethers_1.ethers.BigNumber.from(token.externalBalance);
|
|
@@ -99,7 +118,7 @@ class NEOClient {
|
|
|
99
118
|
if (amount.lt(feeAmount)) {
|
|
100
119
|
return false;
|
|
101
120
|
}
|
|
102
|
-
const sb = new
|
|
121
|
+
const sb = new Neon.sc.ScriptBuilder();
|
|
103
122
|
sb.emitAppCall(scriptHash, "lock", [
|
|
104
123
|
fromAssetHash,
|
|
105
124
|
fromAddress,
|
|
@@ -133,26 +152,26 @@ class NEOClient {
|
|
|
133
152
|
}
|
|
134
153
|
const publicKeyOutput = yield o3Wallet.getPublicKeyOutput();
|
|
135
154
|
const networkConfig = this.getNetworkConfig();
|
|
136
|
-
const scriptHash =
|
|
137
|
-
const fromAssetHash =
|
|
155
|
+
const scriptHash = Neon.u.reverseHex(token.bridgeAddress);
|
|
156
|
+
const fromAssetHash = Neon.u.reverseHex(token.tokenAddress);
|
|
138
157
|
const fromAddress = util_1.AddressUtils.NEOAddress.publicKeyToAddress(publicKeyOutput.publicKey);
|
|
139
158
|
const targetProxyHash = this.getTargetProxyHash(token);
|
|
140
|
-
const toAssetHash =
|
|
159
|
+
const toAssetHash = Neon.u.str2hexstring(token.id);
|
|
141
160
|
const toAddress = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(address));
|
|
142
161
|
const nonce = Math.floor(Math.random() * 1000000);
|
|
143
162
|
if (amount.lt(feeAmount)) {
|
|
144
163
|
throw new Error("Invalid amount");
|
|
145
164
|
}
|
|
146
165
|
const data = [
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
166
|
+
Neon.sc.ContractParam.hash160(fromAssetHash),
|
|
167
|
+
Neon.sc.ContractParam.hash160(fromAddress),
|
|
168
|
+
Neon.sc.ContractParam.byteArray(targetProxyHash, "hex"),
|
|
169
|
+
Neon.sc.ContractParam.byteArray(toAssetHash, "hex"),
|
|
170
|
+
Neon.sc.ContractParam.byteArray(toAddress, "hex"),
|
|
171
|
+
Neon.sc.ContractParam.integer(amount.toNumber()),
|
|
172
|
+
Neon.sc.ContractParam.integer(feeAmount.toNumber()),
|
|
173
|
+
Neon.sc.ContractParam.byteArray(networkConfig.feeAddress, "hex"),
|
|
174
|
+
Neon.sc.ContractParam.integer(nonce),
|
|
156
175
|
];
|
|
157
176
|
const tx = yield o3Wallet.getDAPI().invoke({
|
|
158
177
|
scriptHash,
|
|
@@ -167,20 +186,20 @@ class NEOClient {
|
|
|
167
186
|
var _a;
|
|
168
187
|
return __awaiter(this, void 0, void 0, function* () {
|
|
169
188
|
const { feeAmount, address, amount, token, ledger, signCompleteCallback, } = params;
|
|
170
|
-
const compressedPublicKey =
|
|
189
|
+
const compressedPublicKey = Neon.wallet.getPublicKeyEncoded(ledger.publicKey);
|
|
171
190
|
const networkConfig = this.getNetworkConfig();
|
|
172
|
-
const scriptHash =
|
|
191
|
+
const scriptHash = Neon.u.reverseHex(token.bridgeAddress);
|
|
173
192
|
const fromAssetHash = token.tokenAddress;
|
|
174
193
|
const fromAddress = ledger.scriptHash;
|
|
175
194
|
const targetProxyHash = this.getTargetProxyHash(token);
|
|
176
|
-
const toAssetHash =
|
|
195
|
+
const toAssetHash = Neon.u.str2hexstring(token.id);
|
|
177
196
|
const toAddress = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(address));
|
|
178
197
|
const feeAddress = networkConfig.feeAddress;
|
|
179
198
|
const nonce = Math.floor(Math.random() * 1000000);
|
|
180
199
|
if (amount.lt(feeAmount)) {
|
|
181
200
|
throw new Error("Invalid amount");
|
|
182
201
|
}
|
|
183
|
-
const sb = new
|
|
202
|
+
const sb = new Neon.sc.ScriptBuilder();
|
|
184
203
|
const data = [
|
|
185
204
|
fromAssetHash,
|
|
186
205
|
fromAddress,
|
|
@@ -198,12 +217,12 @@ class NEOClient {
|
|
|
198
217
|
? new plugin_1.default.neonDB.instance("https://api.switcheo.network")
|
|
199
218
|
: new plugin_1.default.neoCli.instance(rpcUrl);
|
|
200
219
|
let invokeTxConfig = {
|
|
201
|
-
account: Object.assign(Object.assign({}, new
|
|
220
|
+
account: Object.assign(Object.assign({}, new Neon.wallet.Account("")), {
|
|
202
221
|
// overwrite the address and public key to values provided by the ledger.
|
|
203
222
|
address: ledger.displayAddress, publicKey: compressedPublicKey }),
|
|
204
223
|
signingFunction: (tx, publicKey) => __awaiter(this, void 0, void 0, function* () {
|
|
205
224
|
const signature = yield ledger.sign(tx);
|
|
206
|
-
const witness =
|
|
225
|
+
const witness = Neon.tx.Witness.fromSignature(signature, publicKey);
|
|
207
226
|
return witness.serialize();
|
|
208
227
|
}),
|
|
209
228
|
api: apiProvider,
|
|
@@ -231,25 +250,25 @@ class NEOClient {
|
|
|
231
250
|
var _a, _b, _c, _d, _e;
|
|
232
251
|
return __awaiter(this, void 0, void 0, function* () {
|
|
233
252
|
const url = this.getProviderUrl();
|
|
234
|
-
const sb = new
|
|
253
|
+
const sb = new Neon.sc.ScriptBuilder();
|
|
235
254
|
sb.emitAppCall(scriptHash, "symbol", []);
|
|
236
255
|
sb.emitAppCall(scriptHash, "name", []);
|
|
237
256
|
sb.emitAppCall(scriptHash, "decimals", []);
|
|
238
|
-
const response = yield
|
|
257
|
+
const response = yield Neon.rpc.Query.invokeScript(sb.str).execute(url);
|
|
239
258
|
if (((_a = response === null || response === void 0 ? void 0 : response.result) === null || _a === void 0 ? void 0 : _a.state) !== "HALT")
|
|
240
259
|
throw new Error("retrieve failed");
|
|
241
|
-
const symbol =
|
|
242
|
-
const name =
|
|
260
|
+
const symbol = Neon.u.hexstring2str((_b = response.result.stack) === null || _b === void 0 ? void 0 : _b[0].value);
|
|
261
|
+
const name = Neon.u.hexstring2str((_c = response.result.stack) === null || _c === void 0 ? void 0 : _c[1].value);
|
|
243
262
|
const decimals = parseInt((_e = (_d = response.result.stack) === null || _d === void 0 ? void 0 : _d[2].value) !== null && _e !== void 0 ? _e : "0", 10);
|
|
244
263
|
return { address: scriptHash, decimals, name, symbol };
|
|
245
264
|
});
|
|
246
265
|
}
|
|
247
266
|
wrapNeoToNneo(neoAmount, neoPrivateKey) {
|
|
248
267
|
return __awaiter(this, void 0, void 0, function* () {
|
|
249
|
-
const account = new
|
|
268
|
+
const account = new Neon.wallet.Account(neoPrivateKey);
|
|
250
269
|
const rpcUrl = yield this.getProviderUrl();
|
|
251
270
|
const wrapperContractScriptHash = this.getConfig().wrapperScriptHash;
|
|
252
|
-
const wrapperContractAddress =
|
|
271
|
+
const wrapperContractAddress = Neon.wallet.getAddressFromScriptHash(wrapperContractScriptHash);
|
|
253
272
|
// Build config
|
|
254
273
|
const intent = plugin_1.default.makeIntent({ NEO: neoAmount.toNumber() }, wrapperContractAddress);
|
|
255
274
|
const props = {
|
|
@@ -257,7 +276,7 @@ class NEOClient {
|
|
|
257
276
|
operation: "mintTokens",
|
|
258
277
|
args: []
|
|
259
278
|
};
|
|
260
|
-
const script =
|
|
279
|
+
const script = Neon.sc.createScript(props);
|
|
261
280
|
const networkConfig = this.getNetworkConfig();
|
|
262
281
|
const apiProvider = networkConfig.network === CarbonSDK_1.default.Network.MainNet
|
|
263
282
|
? new plugin_1.default.neonDB.instance("https://api.switcheo.network")
|
|
@@ -276,13 +295,13 @@ class NEOClient {
|
|
|
276
295
|
}
|
|
277
296
|
formatWithdrawalAddress(address) {
|
|
278
297
|
return __awaiter(this, void 0, void 0, function* () {
|
|
279
|
-
const isValidAddress =
|
|
298
|
+
const isValidAddress = Neon.wallet.isAddress(address);
|
|
280
299
|
if (!isValidAddress) {
|
|
281
300
|
throw new Error("invalid address");
|
|
282
301
|
}
|
|
283
|
-
const scriptHash =
|
|
302
|
+
const scriptHash = Neon.wallet.getScriptHashFromAddress(address);
|
|
284
303
|
// return the little endian version of the address
|
|
285
|
-
return
|
|
304
|
+
return Neon.u.reverseHex(scriptHash);
|
|
286
305
|
});
|
|
287
306
|
}
|
|
288
307
|
/**
|
|
@@ -36,7 +36,7 @@ const amino_1 = require("@cosmjs/amino");
|
|
|
36
36
|
const constant_1 = require("../../constant");
|
|
37
37
|
const index_1 = require("../../index");
|
|
38
38
|
const generic_1 = require("../../util/generic");
|
|
39
|
-
const
|
|
39
|
+
const Neon = __importStar(require("@cityofzion/neon-core-next"));
|
|
40
40
|
const neo_dapi_1 = __importDefault(require("neo-dapi"));
|
|
41
41
|
const neo3_dapi_1 = __importDefault(require("neo3-dapi"));
|
|
42
42
|
const util_1 = require("../../util");
|
|
@@ -110,8 +110,8 @@ class O3Wallet {
|
|
|
110
110
|
if (this.publicKey === "") {
|
|
111
111
|
throw new Error("O3 wallet is not connected. Please reconnect and perform this transaction again.");
|
|
112
112
|
}
|
|
113
|
-
const scriptHash =
|
|
114
|
-
const tokenScriptHash =
|
|
113
|
+
const scriptHash = Neon.u.reverseHex(token.bridgeAddress);
|
|
114
|
+
const tokenScriptHash = Neon.u.reverseHex(token.tokenAddress);
|
|
115
115
|
const nonce = Math.floor(Math.random() * 1000000);
|
|
116
116
|
const scriptHashAccount = this.neoNetwork === "N3MainNet"
|
|
117
117
|
? util_1.AddressUtils.N3Address.publicKeyToScriptHash(this.publicKey)
|
|
@@ -122,7 +122,7 @@ class O3Wallet {
|
|
|
122
122
|
{ type: O3Types.ArgTypes.ByteArray, value: toAddress },
|
|
123
123
|
{ type: O3Types.ArgTypes.Integer, value: amount.toString(10) },
|
|
124
124
|
{ type: O3Types.ArgTypes.Integer, value: feeAmount.toString(10) },
|
|
125
|
-
{ type: O3Types.ArgTypes.ByteArray, value:
|
|
125
|
+
{ type: O3Types.ArgTypes.ByteArray, value: Neon.u.HexString.fromHex(this.networkConfig.feeAddress, true) },
|
|
126
126
|
{ type: O3Types.ArgTypes.Integer, value: nonce.toString() },
|
|
127
127
|
];
|
|
128
128
|
const signers = [{
|
|
@@ -180,7 +180,7 @@ class O3Wallet {
|
|
|
180
180
|
balances.forEach((balance) => {
|
|
181
181
|
var _a, _b;
|
|
182
182
|
const assetId = (_b = (_a = balance.assetID) === null || _a === void 0 ? void 0 : _a.replace('0x', '')) !== null && _b !== void 0 ? _b : balance.contract.replace('0x', '');
|
|
183
|
-
const tokenContract =
|
|
183
|
+
const tokenContract = Neon.u.reverseHex(assetId);
|
|
184
184
|
const tokenInfo = tokenMap[tokenContract];
|
|
185
185
|
if (!tokenInfo) {
|
|
186
186
|
return;
|