@kynesyslabs/demosdk 1.0.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/README.md +59 -0
- package/build/index.d.ts +3 -0
- package/build/index.js +30 -0
- package/build/index.js.map +1 -0
- package/build/multichain/core/evm.d.ts +56 -0
- package/build/multichain/core/evm.js +196 -0
- package/build/multichain/core/evm.js.map +1 -0
- package/build/multichain/core/ibc.d.ts +50 -0
- package/build/multichain/core/ibc.js +224 -0
- package/build/multichain/core/ibc.js.map +1 -0
- package/build/multichain/core/index.d.ts +7 -0
- package/build/multichain/core/index.js +19 -0
- package/build/multichain/core/index.js.map +1 -0
- package/build/multichain/core/multiversx.d.ts +38 -0
- package/build/multichain/core/multiversx.js +230 -0
- package/build/multichain/core/multiversx.js.map +1 -0
- package/build/multichain/core/types/defaultChain.d.ts +150 -0
- package/build/multichain/core/types/defaultChain.js +49 -0
- package/build/multichain/core/types/defaultChain.js.map +1 -0
- package/build/multichain/core/types/interfaces.d.ts +82 -0
- package/build/multichain/core/types/interfaces.js +3 -0
- package/build/multichain/core/types/interfaces.js.map +1 -0
- package/build/multichain/core/utils.d.ts +9 -0
- package/build/multichain/core/utils.js +22 -0
- package/build/multichain/core/utils.js.map +1 -0
- package/build/multichain/core/xrp.d.ts +54 -0
- package/build/multichain/core/xrp.js +189 -0
- package/build/multichain/core/xrp.js.map +1 -0
- package/build/multichain/index.d.ts +3 -0
- package/build/multichain/index.js +30 -0
- package/build/multichain/index.js.map +1 -0
- package/build/multichain/localsdk/evm.d.ts +25 -0
- package/build/multichain/localsdk/evm.js +61 -0
- package/build/multichain/localsdk/evm.js.map +1 -0
- package/build/multichain/localsdk/ibc.d.ts +10 -0
- package/build/multichain/localsdk/ibc.js +25 -0
- package/build/multichain/localsdk/ibc.js.map +1 -0
- package/build/multichain/localsdk/index.d.ts +3 -0
- package/build/multichain/localsdk/index.js +10 -0
- package/build/multichain/localsdk/index.js.map +1 -0
- package/build/multichain/localsdk/multiversx.d.ts +16 -0
- package/build/multichain/localsdk/multiversx.js +51 -0
- package/build/multichain/localsdk/multiversx.js.map +1 -0
- package/build/multichain/localsdk/xrp.d.ts +22 -0
- package/build/multichain/localsdk/xrp.js +77 -0
- package/build/multichain/localsdk/xrp.js.map +1 -0
- package/build/multichain/websdk/evm.d.ts +4 -0
- package/build/multichain/websdk/evm.js +11 -0
- package/build/multichain/websdk/evm.js.map +1 -0
- package/build/multichain/websdk/ibc.d.ts +4 -0
- package/build/multichain/websdk/ibc.js +12 -0
- package/build/multichain/websdk/ibc.js.map +1 -0
- package/build/multichain/websdk/index.d.ts +3 -0
- package/build/multichain/websdk/index.js +10 -0
- package/build/multichain/websdk/index.js.map +1 -0
- package/build/multichain/websdk/multiversx.d.ts +14 -0
- package/build/multichain/websdk/multiversx.js +57 -0
- package/build/multichain/websdk/multiversx.js.map +1 -0
- package/build/multichain/websdk/xrp.d.ts +4 -0
- package/build/multichain/websdk/xrp.js +11 -0
- package/build/multichain/websdk/xrp.js.map +1 -0
- package/build/types/ISignature.d.ts +5 -0
- package/build/types/ISignature.js +3 -0
- package/build/types/ISignature.js.map +1 -0
- package/build/types/Operation.d.ts +11 -0
- package/build/types/Operation.js +3 -0
- package/build/types/Operation.js.map +1 -0
- package/build/types/Transaction.d.ts +10 -0
- package/build/types/Transaction.js +3 -0
- package/build/types/Transaction.js.map +1 -0
- package/build/types/TransactionContent.d.ts +13 -0
- package/build/types/TransactionContent.js +3 -0
- package/build/types/TransactionContent.js.map +1 -0
- package/build/types/TxFee.d.ts +5 -0
- package/build/types/TxFee.js +3 -0
- package/build/types/TxFee.js.map +1 -0
- package/build/types/ValidityData.d.ts +14 -0
- package/build/types/ValidityData.js +3 -0
- package/build/types/ValidityData.js.map +1 -0
- package/build/types/index.d.ts +6 -0
- package/build/types/index.js +3 -0
- package/build/types/index.js.map +1 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# DEMOS SDKs
|
|
2
|
+
|
|
3
|
+
This repo contains all the Demos SDKs
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
### Core
|
|
8
|
+
|
|
9
|
+
TODO: Update once ported.
|
|
10
|
+
|
|
11
|
+
### Multichain SDKs
|
|
12
|
+
|
|
13
|
+
Check out the [multichain documentation](./documentation/multichain/README.md).
|
|
14
|
+
|
|
15
|
+
## Organization
|
|
16
|
+
|
|
17
|
+
The packages are structured like this:
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
./src
|
|
21
|
+
├── core # blockchain interface stuff <todo: port from frontend>
|
|
22
|
+
│
|
|
23
|
+
├── types # shared types
|
|
24
|
+
│
|
|
25
|
+
├── multichain
|
|
26
|
+
│ ├── core # default chain sdk implementation for all chains
|
|
27
|
+
│ ├── localsdk # local sdks for all chains
|
|
28
|
+
│ └── websdk # web sdks for all chains
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Development setup
|
|
32
|
+
```sh
|
|
33
|
+
yarn install
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Running tests
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
# multichain
|
|
40
|
+
yarn test:multichain
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Publishing on NPM
|
|
44
|
+
|
|
45
|
+
TODO!
|
|
46
|
+
|
|
47
|
+
## What has Changed?
|
|
48
|
+
|
|
49
|
+
### EVM Web sdk
|
|
50
|
+
|
|
51
|
+
#### 1. Constructor parameter order:
|
|
52
|
+
|
|
53
|
+
previous: `rpc_url`, `isEIP1559`, `chain_id` \
|
|
54
|
+
new: `rpc_url`, `chain_id`, `isEIP1559`
|
|
55
|
+
|
|
56
|
+
## TODOs
|
|
57
|
+
|
|
58
|
+
- Review `getBalance` return type. Should it be a string or an object?
|
|
59
|
+
-
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.xmwebsdk = exports.xmlocalsdk = exports.types = void 0;
|
|
27
|
+
exports.types = __importStar(require("./types"));
|
|
28
|
+
exports.xmlocalsdk = __importStar(require("./multichain/localsdk"));
|
|
29
|
+
exports.xmwebsdk = __importStar(require("./multichain/websdk"));
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAgC;AAChC,oEAAmD;AACnD,gEAA+C"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Contract, JsonRpcProvider, TransactionRequest, Wallet } from 'ethers';
|
|
2
|
+
import { DefaultChain, IEVMDefaultChain } from './types/defaultChain';
|
|
3
|
+
import { IPayOptions } from './types/interfaces';
|
|
4
|
+
export declare class EVM extends DefaultChain implements IEVMDefaultChain {
|
|
5
|
+
provider: JsonRpcProvider;
|
|
6
|
+
wallet: Wallet;
|
|
7
|
+
contracts: Map<string, Contract>;
|
|
8
|
+
isEIP1559: boolean;
|
|
9
|
+
chainId: number;
|
|
10
|
+
constructor(rpc_url?: string, eip1559?: boolean, chainId?: number);
|
|
11
|
+
setRPC(rpc_url: string): Promise<void>;
|
|
12
|
+
connect(): Promise<boolean>;
|
|
13
|
+
connectWallet(privateKey: string): Promise<void>;
|
|
14
|
+
signTransaction(transaction: TransactionRequest, options?: {
|
|
15
|
+
privateKey?: string;
|
|
16
|
+
}): Promise<string>;
|
|
17
|
+
signTransactions(transactions: TransactionRequest[], options?: {
|
|
18
|
+
privateKey?: string;
|
|
19
|
+
}): Promise<string[]>;
|
|
20
|
+
preparePay(address: string, amount: string): Promise<string>;
|
|
21
|
+
preparePays(payments: IPayOptions[]): Promise<string[]>;
|
|
22
|
+
prepareTransfer(address: string, amount: string): Promise<string>;
|
|
23
|
+
prepareTransfers(payments: IPayOptions[]): Promise<string[]>;
|
|
24
|
+
prepareBaseTxWithType(): Promise<{
|
|
25
|
+
type: number;
|
|
26
|
+
maxFeePerGas: bigint | null;
|
|
27
|
+
maxPriorityFeePerGas: bigint | null;
|
|
28
|
+
gasLimit: number;
|
|
29
|
+
chainId: number;
|
|
30
|
+
} | {
|
|
31
|
+
gasPrice: bigint | null;
|
|
32
|
+
gasLimit: number;
|
|
33
|
+
chainId: number;
|
|
34
|
+
}>;
|
|
35
|
+
getEmptyTransaction(): Promise<{
|
|
36
|
+
type: number;
|
|
37
|
+
maxFeePerGas: bigint | null;
|
|
38
|
+
maxPriorityFeePerGas: bigint | null;
|
|
39
|
+
gasLimit: number;
|
|
40
|
+
chainId: number;
|
|
41
|
+
} | {
|
|
42
|
+
gasPrice: bigint | null;
|
|
43
|
+
gasLimit: number;
|
|
44
|
+
chainId: number;
|
|
45
|
+
}>;
|
|
46
|
+
getAddress(): string;
|
|
47
|
+
getBalance(address: string): Promise<string>;
|
|
48
|
+
createRawTransaction(tx_data: any): Promise<any>;
|
|
49
|
+
waitForReceipt(tx_hash: string): Promise<any>;
|
|
50
|
+
getContractInstance(address: string, abi: string): Promise<Contract>;
|
|
51
|
+
readFromContract(contract_instance: Contract, function_name: string, args: any): Promise<any>;
|
|
52
|
+
writeToContract(contract_instance: Contract, function_name: string, args: any): Promise<any>;
|
|
53
|
+
listenForEvent(event: string, contract: string, abi: any[]): Promise<any>;
|
|
54
|
+
listenForAllEvents(contract: string, abi: any[]): Promise<any>;
|
|
55
|
+
disconnect(): Promise<boolean>;
|
|
56
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EVM = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const defaultChain_1 = require("./types/defaultChain");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
class EVM extends defaultChain_1.DefaultChain {
|
|
8
|
+
constructor(rpc_url = '', eip1559 = true, chainId = 11155111) {
|
|
9
|
+
super(rpc_url);
|
|
10
|
+
this.contracts = new Map();
|
|
11
|
+
this.name = 'evm';
|
|
12
|
+
this.chainId = chainId;
|
|
13
|
+
this.isEIP1559 = eip1559;
|
|
14
|
+
if (rpc_url) {
|
|
15
|
+
this.setRPC(rpc_url);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// INFO Set of methods for connecting to an RPC while
|
|
19
|
+
// retaining a granular control over the instance status
|
|
20
|
+
async setRPC(rpc_url) {
|
|
21
|
+
this.provider = new ethers_1.JsonRpcProvider(rpc_url);
|
|
22
|
+
}
|
|
23
|
+
async connect() {
|
|
24
|
+
(0, utils_1.required)(this.provider, 'Provider not connected');
|
|
25
|
+
try {
|
|
26
|
+
const network = await this.provider.getNetwork();
|
|
27
|
+
this.connected = Boolean(network.name);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
this.connected = false;
|
|
31
|
+
}
|
|
32
|
+
return this.connected;
|
|
33
|
+
}
|
|
34
|
+
// static override async create(
|
|
35
|
+
// rpc_url: string = '',
|
|
36
|
+
// eip1559: boolean = true,
|
|
37
|
+
// chainId: number = 11155111
|
|
38
|
+
// ): Promise<EVM> {
|
|
39
|
+
// const instance = new EVM(rpc_url, eip1559, chainId)
|
|
40
|
+
// if (rpc_url) {
|
|
41
|
+
// await instance.connect()
|
|
42
|
+
// }
|
|
43
|
+
// return instance
|
|
44
|
+
// }
|
|
45
|
+
// INFO Connecting a wallet through a private key (string)
|
|
46
|
+
// REVIEW should private key be a string or a Buffer?
|
|
47
|
+
async connectWallet(privateKey) {
|
|
48
|
+
(0, utils_1.required)(this.provider, 'Provider not connected');
|
|
49
|
+
this.wallet = new ethers_1.Wallet(privateKey, this.provider);
|
|
50
|
+
}
|
|
51
|
+
// INFO Signing a transaction
|
|
52
|
+
// with a private key or by using our stored wallet
|
|
53
|
+
// REVIEW should private key be a string or a Buffer?
|
|
54
|
+
async signTransaction(transaction, options) {
|
|
55
|
+
const txs = await this.signTransactions([transaction], options);
|
|
56
|
+
return txs[0];
|
|
57
|
+
}
|
|
58
|
+
async signTransactions(transactions, options) {
|
|
59
|
+
(0, utils_1.required)(this.wallet || options?.privateKey, 'Wallet not connected');
|
|
60
|
+
if (options?.privateKey) {
|
|
61
|
+
this.wallet = new ethers_1.Wallet(options.privateKey, this.provider);
|
|
62
|
+
}
|
|
63
|
+
// INFO: Get the current nonce
|
|
64
|
+
const this_address = this.getAddress();
|
|
65
|
+
let currentNonce = await this.provider.getTransactionCount(this_address);
|
|
66
|
+
// INFO: Return a list of signed transactions
|
|
67
|
+
return Promise.all(transactions.map(async (tx) => {
|
|
68
|
+
tx.nonce = currentNonce;
|
|
69
|
+
// INFO: Increment the nonce for the next transaction
|
|
70
|
+
currentNonce++;
|
|
71
|
+
return this.wallet.signTransaction(tx);
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
// SECTION Specific methods
|
|
75
|
+
// REVIEW Should prepare methods be like:
|
|
76
|
+
// prepare = { pay(), send(), ...}
|
|
77
|
+
async preparePay(address, amount) {
|
|
78
|
+
const tx = await this.preparePays([{ address, amount }]);
|
|
79
|
+
return tx[0];
|
|
80
|
+
}
|
|
81
|
+
async preparePays(payments) {
|
|
82
|
+
(0, utils_1.required)(this.wallet, 'Wallet not connected');
|
|
83
|
+
const baseTx = await this.prepareBaseTxWithType();
|
|
84
|
+
const txs = payments.map((payment) => {
|
|
85
|
+
const tx = {
|
|
86
|
+
...baseTx,
|
|
87
|
+
to: payment.address,
|
|
88
|
+
value: (0, ethers_1.parseEther)(payment.amount),
|
|
89
|
+
};
|
|
90
|
+
// INFO: wallet.checkTransaction was removed in ethers v6
|
|
91
|
+
// const checkedTx = this.wallet.checkTransaction(tx);
|
|
92
|
+
// console.log('checked', checkedTx);
|
|
93
|
+
return tx;
|
|
94
|
+
});
|
|
95
|
+
return this.signTransactions(txs);
|
|
96
|
+
}
|
|
97
|
+
async prepareTransfer(address, amount) {
|
|
98
|
+
return this.preparePay(address, amount);
|
|
99
|
+
}
|
|
100
|
+
async prepareTransfers(payments) {
|
|
101
|
+
return this.preparePays(payments);
|
|
102
|
+
}
|
|
103
|
+
// SECTION EVM Specific methods
|
|
104
|
+
// INFO Generic transaction skeleton for both EIP-1559 and legacy chains
|
|
105
|
+
async prepareBaseTxWithType() {
|
|
106
|
+
const feeData = await this.provider.getFeeData();
|
|
107
|
+
// console.log({ baseFeePerGas: parseInt(feeData.lastBaseFeePerGas._hex, 16) });
|
|
108
|
+
const baseTx = {
|
|
109
|
+
// REVIEW: is there a way to get the gas limit from the provider?
|
|
110
|
+
gasLimit: 21000,
|
|
111
|
+
chainId: this.chainId,
|
|
112
|
+
};
|
|
113
|
+
if (this.isEIP1559) {
|
|
114
|
+
return {
|
|
115
|
+
...baseTx,
|
|
116
|
+
type: 2,
|
|
117
|
+
maxFeePerGas: feeData.maxFeePerGas,
|
|
118
|
+
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
...baseTx,
|
|
123
|
+
// INFO: ethers v5 used this line 👇
|
|
124
|
+
// gasPrice: feeData.lastBaseFeePerGas
|
|
125
|
+
gasPrice: feeData.gasPrice,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
// INFO Generic empty tx skeleton for this chain
|
|
129
|
+
async getEmptyTransaction() {
|
|
130
|
+
// NOTE This is a redirection to the prepareBaseTxWithType method as in a evm chain is like that
|
|
131
|
+
return await this.prepareBaseTxWithType();
|
|
132
|
+
}
|
|
133
|
+
// megabudino was here – return the address of the wallet
|
|
134
|
+
getAddress() {
|
|
135
|
+
return this.wallet.address;
|
|
136
|
+
}
|
|
137
|
+
async getBalance(address) {
|
|
138
|
+
let balance_raw = await this.provider.getBalance(address);
|
|
139
|
+
return (0, ethers_1.formatEther)(balance_raw);
|
|
140
|
+
}
|
|
141
|
+
// SECTION EVM Exclusive methods
|
|
142
|
+
async createRawTransaction(tx_data) {
|
|
143
|
+
throw new Error('Not implemented');
|
|
144
|
+
}
|
|
145
|
+
async waitForReceipt(tx_hash) {
|
|
146
|
+
return await this.provider.getTransactionReceipt(tx_hash);
|
|
147
|
+
}
|
|
148
|
+
// SECTION Not implemented methods
|
|
149
|
+
async getContractInstance(address, abi) {
|
|
150
|
+
console.log(this);
|
|
151
|
+
if (!this.provider) {
|
|
152
|
+
throw new Error('Provider not connected');
|
|
153
|
+
}
|
|
154
|
+
let contract = new ethers_1.Contract(address, abi, this.provider);
|
|
155
|
+
return contract;
|
|
156
|
+
}
|
|
157
|
+
// REVIEW Reader for contracts
|
|
158
|
+
// ANCHOR MVP
|
|
159
|
+
async readFromContract(contract_instance, function_name, args) {
|
|
160
|
+
return await contract_instance[function_name](...args);
|
|
161
|
+
}
|
|
162
|
+
// REVIEW Writer for contracts
|
|
163
|
+
async writeToContract(contract_instance, function_name, args) {
|
|
164
|
+
(0, utils_1.required)(this.wallet);
|
|
165
|
+
return await contract_instance[function_name](...args); // NOTE Ensure it is writeable i guess
|
|
166
|
+
}
|
|
167
|
+
// SECTION Event listener
|
|
168
|
+
async listenForEvent(event, contract, abi) {
|
|
169
|
+
if (!this.provider) {
|
|
170
|
+
throw new Error('Provider not connected');
|
|
171
|
+
}
|
|
172
|
+
let contractInstance = new ethers_1.Contract(contract, abi, this.provider);
|
|
173
|
+
// REVIEW THis could work
|
|
174
|
+
return contractInstance.on(event, (data) => {
|
|
175
|
+
////console.log(data)
|
|
176
|
+
// TODO Do something with the data
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
async listenForAllEvents(contract, abi) {
|
|
180
|
+
if (!this.provider) {
|
|
181
|
+
throw new Error('Provider not connected');
|
|
182
|
+
}
|
|
183
|
+
let contractInstance = new ethers_1.Contract(contract, abi, this.provider);
|
|
184
|
+
// REVIEW 99% Won't work
|
|
185
|
+
return contractInstance.on('*', (data) => {
|
|
186
|
+
////console.log(data)
|
|
187
|
+
// TODO Do something with the data
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
async disconnect() {
|
|
191
|
+
this.resetInstance();
|
|
192
|
+
return !this.connected;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
exports.EVM = EVM;
|
|
196
|
+
//# sourceMappingURL=evm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evm.js","sourceRoot":"","sources":["../../../../src/multichain/core/evm.ts"],"names":[],"mappings":";;;AAAA,mCAQe;AACf,uDAAqE;AAErE,mCAAkC;AAElC,MAAa,GAAI,SAAQ,2BAAY;IAQjC,YACI,UAA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C,CAAC,CAAA;IACN,CAAC;IAED,KAAK,CAAC,UAAU;QACZ,IAAI,CAAC,aAAa,EAAE,CAAA;QACpB,OAAO,CAAC,IAAI,CAAC,SAAS,CAAA;IAC1B,CAAC;CACJ;AArQD,kBAqQC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { DirectSecp256k1HdWallet, DirectSecp256k1Wallet } from '@cosmjs/proto-signing';
|
|
2
|
+
import { SigningStargateClient, StargateClient } from '@cosmjs/stargate';
|
|
3
|
+
import { DefaultChain, IBCDefaultChain } from './types/defaultChain';
|
|
4
|
+
import { IBCConnectWalletOptions, IBCGetBalanceOptions, IBCPreparePayOptions, IBCSignTxOptions, IBCTransaction, IPayOptions } from './types/interfaces';
|
|
5
|
+
export declare class IBC extends DefaultChain implements IBCDefaultChain {
|
|
6
|
+
address: string;
|
|
7
|
+
chainID: string;
|
|
8
|
+
provider: StargateClient;
|
|
9
|
+
wallet: SigningStargateClient;
|
|
10
|
+
signer: DirectSecp256k1Wallet | DirectSecp256k1HdWallet;
|
|
11
|
+
gasPrice: string;
|
|
12
|
+
multiplier: number;
|
|
13
|
+
constructor(rpc_url: string);
|
|
14
|
+
connect(rpc_url?: string): Promise<boolean>;
|
|
15
|
+
connectWallet(privateKey: string, options: IBCConnectWalletOptions): Promise<SigningStargateClient>;
|
|
16
|
+
getAddress(): string;
|
|
17
|
+
/**
|
|
18
|
+
* Get the balance of the address
|
|
19
|
+
* @param address The address
|
|
20
|
+
* @returns The balance of the address in the specified denomination
|
|
21
|
+
*/
|
|
22
|
+
getBalance(address: string, options: IBCGetBalanceOptions): Promise<string>;
|
|
23
|
+
getEmptyTransaction(): IBCTransaction;
|
|
24
|
+
preparePay(receiver: string, amount: string, options: IBCPreparePayOptions): Promise<Uint8Array>;
|
|
25
|
+
prepareTransfer(receiver: string, amount: string, options: IBCPreparePayOptions): Promise<Uint8Array>;
|
|
26
|
+
/**
|
|
27
|
+
* Prepare multiple payments
|
|
28
|
+
* @param payments An array of payments
|
|
29
|
+
* @param options Specifies the denomination of the token
|
|
30
|
+
* @returns An array of signed transactions
|
|
31
|
+
*/
|
|
32
|
+
prepareTransfers(payments: IPayOptions[], options: IBCPreparePayOptions): Promise<Uint8Array[]>;
|
|
33
|
+
/**
|
|
34
|
+
* Prepare multiple payments
|
|
35
|
+
* @param payments An array of payments
|
|
36
|
+
* @param options Specifies the denomination of the token
|
|
37
|
+
* @returns An array of signed transactions
|
|
38
|
+
*/
|
|
39
|
+
preparePays(payments: IPayOptions[], options: IBCPreparePayOptions): Promise<Uint8Array[]>;
|
|
40
|
+
signTransaction(tx: IBCTransaction, options?: IBCSignTxOptions): Promise<Uint8Array>;
|
|
41
|
+
/**
|
|
42
|
+
* Estimate the fee for a transaction
|
|
43
|
+
* @param tx The transaction
|
|
44
|
+
* @returns The estimate fee for the transaction
|
|
45
|
+
*/
|
|
46
|
+
private estimateTxFee;
|
|
47
|
+
signTransactions(transactions: IBCTransaction[], options?: IBCSignTxOptions): Promise<Uint8Array[]>;
|
|
48
|
+
disconnect(): Promise<boolean>;
|
|
49
|
+
ibcSend(): Promise<void>;
|
|
50
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IBC = void 0;
|
|
4
|
+
const proto_signing_1 = require("@cosmjs/proto-signing");
|
|
5
|
+
const stargate_1 = require("@cosmjs/stargate");
|
|
6
|
+
const tx_1 = require("cosmjs-types/cosmos/tx/v1beta1/tx");
|
|
7
|
+
const defaultChain_1 = require("./types/defaultChain");
|
|
8
|
+
const utils_1 = require("./utils");
|
|
9
|
+
class IBC extends defaultChain_1.DefaultChain {
|
|
10
|
+
constructor(rpc_url) {
|
|
11
|
+
super(rpc_url);
|
|
12
|
+
this.address = '';
|
|
13
|
+
this.chainID = '';
|
|
14
|
+
// IBC options
|
|
15
|
+
this.gasPrice = '';
|
|
16
|
+
this.multiplier = 2.0;
|
|
17
|
+
this.name = 'ibc';
|
|
18
|
+
// INFO: We can't await here, so we call .setRPC in .create()
|
|
19
|
+
// if (rpc_url) {
|
|
20
|
+
// this.setRPC(rpc_url);
|
|
21
|
+
// }
|
|
22
|
+
}
|
|
23
|
+
// override async setRpc(rpc_url: string) {
|
|
24
|
+
// this.provider = await StargateClient.connect(rpc_url)
|
|
25
|
+
// }
|
|
26
|
+
// static override async create(rpc_url: string): Promise<IBC> {
|
|
27
|
+
// const instance = new IBC(rpc_url)
|
|
28
|
+
// if (rpc_url) {
|
|
29
|
+
// // INFO: Set rpc url and ping it.
|
|
30
|
+
// await instance.setRPC(rpc_url)
|
|
31
|
+
// await instance.connect()
|
|
32
|
+
// }
|
|
33
|
+
// return instance
|
|
34
|
+
// }
|
|
35
|
+
// INFO: rpc_url used to avoid overwriting this.provider for test mocking
|
|
36
|
+
async connect(rpc_url) {
|
|
37
|
+
if (rpc_url !== this.rpc_url) {
|
|
38
|
+
this.provider = await stargate_1.StargateClient.connect(this.rpc_url);
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const chain_id = await this.provider.getChainId();
|
|
42
|
+
this.chainID = chain_id;
|
|
43
|
+
this.connected = Boolean(chain_id);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
this.connected = false;
|
|
47
|
+
}
|
|
48
|
+
return this.connected;
|
|
49
|
+
}
|
|
50
|
+
async connectWallet(privateKey, options) {
|
|
51
|
+
(0, utils_1.required)(options.prefix, 'Address prefix not provided');
|
|
52
|
+
(0, utils_1.required)(options.gasPrice, 'Gas price not provided');
|
|
53
|
+
// INFO: Store the gasPrice for preparePays and other methods
|
|
54
|
+
this.gasPrice = options.gasPrice;
|
|
55
|
+
// INFO: Check if privateKey is a mnemonic or a private key
|
|
56
|
+
const isPrivateKey = privateKey.split(' ').length === 1;
|
|
57
|
+
// INFO: Create a signer using the appropriate wallet
|
|
58
|
+
if (isPrivateKey) {
|
|
59
|
+
// TODO: Test this block!
|
|
60
|
+
const buffer = Buffer.from(privateKey, 'hex');
|
|
61
|
+
this.signer = await proto_signing_1.DirectSecp256k1Wallet.fromKey(buffer, options.prefix);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
this.signer = await proto_signing_1.DirectSecp256k1HdWallet.fromMnemonic(privateKey, {
|
|
65
|
+
prefix: options.prefix,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// INFO: Store the address as .getAddress can't be async
|
|
69
|
+
const wallet_accounts = await this.signer.getAccounts();
|
|
70
|
+
const this_account = wallet_accounts.find((account) => account.address.startsWith(options.prefix));
|
|
71
|
+
if (this_account) {
|
|
72
|
+
this.address = this_account.address;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
throw new Error(`No account found for prefix: ${options.prefix}`);
|
|
76
|
+
}
|
|
77
|
+
this.wallet = await stargate_1.SigningStargateClient.connectWithSigner(this.rpc_url, this.signer);
|
|
78
|
+
return this.wallet;
|
|
79
|
+
}
|
|
80
|
+
getAddress() {
|
|
81
|
+
return this.address;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get the balance of the address
|
|
85
|
+
* @param address The address
|
|
86
|
+
* @returns The balance of the address in the specified denomination
|
|
87
|
+
*/
|
|
88
|
+
async getBalance(address, options) {
|
|
89
|
+
(0, utils_1.required)(this.provider, 'Provider not connected');
|
|
90
|
+
const coins = await this.provider.getBalance(address, options.denom);
|
|
91
|
+
return coins.amount;
|
|
92
|
+
}
|
|
93
|
+
getEmptyTransaction() {
|
|
94
|
+
return {
|
|
95
|
+
signerAddress: this.getAddress(),
|
|
96
|
+
messages: [
|
|
97
|
+
{
|
|
98
|
+
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
|
|
99
|
+
value: {
|
|
100
|
+
fromAddress: this.getAddress(),
|
|
101
|
+
toAddress: '',
|
|
102
|
+
amount: [{ denom: '', amount: '' }],
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
// INFO: Fees are to be estimated when filling the tx
|
|
107
|
+
fee: null,
|
|
108
|
+
memo: '',
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
async preparePay(receiver, amount, options) {
|
|
112
|
+
// INFO: Call preparePays with a single payment
|
|
113
|
+
const tx = await this.preparePays([{ address: receiver, amount }], options);
|
|
114
|
+
return tx[0];
|
|
115
|
+
}
|
|
116
|
+
prepareTransfer(receiver, amount, options) {
|
|
117
|
+
return this.preparePay(receiver, amount, options);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Prepare multiple payments
|
|
121
|
+
* @param payments An array of payments
|
|
122
|
+
* @param options Specifies the denomination of the token
|
|
123
|
+
* @returns An array of signed transactions
|
|
124
|
+
*/
|
|
125
|
+
prepareTransfers(payments, options) {
|
|
126
|
+
return this.preparePays(payments, options);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Prepare multiple payments
|
|
130
|
+
* @param payments An array of payments
|
|
131
|
+
* @param options Specifies the denomination of the token
|
|
132
|
+
* @returns An array of signed transactions
|
|
133
|
+
*/
|
|
134
|
+
async preparePays(payments, options) {
|
|
135
|
+
// INFO: Create an array of transactions
|
|
136
|
+
const txs = payments.map((payment) => {
|
|
137
|
+
const tx = this.getEmptyTransaction();
|
|
138
|
+
// INFO: Fill the tx
|
|
139
|
+
tx.messages[0].value.toAddress = payment.address;
|
|
140
|
+
tx.messages[0].value.amount = [
|
|
141
|
+
{
|
|
142
|
+
denom: options.denom,
|
|
143
|
+
amount: payment.amount,
|
|
144
|
+
},
|
|
145
|
+
];
|
|
146
|
+
return tx;
|
|
147
|
+
});
|
|
148
|
+
// INFO: Estimate the fee for the first tx
|
|
149
|
+
const fees = await this.estimateTxFee(txs[0]);
|
|
150
|
+
// INFO: Since all txs here are similar, set same fee for all
|
|
151
|
+
txs.forEach((tx) => {
|
|
152
|
+
tx.fee = fees;
|
|
153
|
+
});
|
|
154
|
+
// INFO: Sign and return the txs
|
|
155
|
+
return await this.signTransactions(txs);
|
|
156
|
+
}
|
|
157
|
+
async signTransaction(tx, options) {
|
|
158
|
+
// INFO: Call signTransactions with a single tx
|
|
159
|
+
const signed_txs = await this.signTransactions([tx], options);
|
|
160
|
+
return signed_txs[0];
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Estimate the fee for a transaction
|
|
164
|
+
* @param tx The transaction
|
|
165
|
+
* @returns The estimate fee for the transaction
|
|
166
|
+
*/
|
|
167
|
+
async estimateTxFee(tx) {
|
|
168
|
+
const signerAddress = this.getAddress();
|
|
169
|
+
const gasEstimate = await this.wallet.simulate(signerAddress, tx.messages, tx.memo);
|
|
170
|
+
const gasLimit = Math.round(gasEstimate * this.multiplier);
|
|
171
|
+
return (0, stargate_1.calculateFee)(gasLimit, this.gasPrice);
|
|
172
|
+
}
|
|
173
|
+
async signTransactions(transactions, options) {
|
|
174
|
+
(0, utils_1.required)(this.wallet, 'Wallet not connected');
|
|
175
|
+
if (options?.privateKey) {
|
|
176
|
+
const { privateKey, ...connectOptions } = options;
|
|
177
|
+
await this.connectWallet(privateKey, connectOptions);
|
|
178
|
+
}
|
|
179
|
+
// NOTE: Sequence management happens here
|
|
180
|
+
// INFO: Get account on network
|
|
181
|
+
const address = this.getAddress();
|
|
182
|
+
const account = await this.wallet.getAccount(address);
|
|
183
|
+
if (!account) {
|
|
184
|
+
throw new Error(`Account ${address} not found`);
|
|
185
|
+
}
|
|
186
|
+
// INFO: Store the current sequence
|
|
187
|
+
let current_sequence = account.sequence;
|
|
188
|
+
const signed_txs = transactions.map(async (tx) => {
|
|
189
|
+
const signerInfo = {
|
|
190
|
+
sequence: current_sequence,
|
|
191
|
+
accountNumber: account.accountNumber,
|
|
192
|
+
chainId: this.chainID,
|
|
193
|
+
};
|
|
194
|
+
if (tx.fee === null) {
|
|
195
|
+
// INFO: Throw an error if fee is not set
|
|
196
|
+
console.error('Fee not set for tx: ', tx);
|
|
197
|
+
throw new Error('Fee not set for tx');
|
|
198
|
+
}
|
|
199
|
+
// INFO: Increment the sequence for next round
|
|
200
|
+
current_sequence++;
|
|
201
|
+
// INFO: Sign the tx
|
|
202
|
+
const signed_tx = await this.wallet.sign(tx.signerAddress, tx.messages, tx.fee, tx.memo, signerInfo);
|
|
203
|
+
// INFO: Convert raw tx to bytes array (Ready for broadcast)
|
|
204
|
+
const tx_bytes = tx_1.TxRaw.encode(signed_tx).finish();
|
|
205
|
+
return tx_bytes;
|
|
206
|
+
});
|
|
207
|
+
// INFO: Return the signed transactions
|
|
208
|
+
return await Promise.all(signed_txs);
|
|
209
|
+
}
|
|
210
|
+
async disconnect() {
|
|
211
|
+
this.resetInstance();
|
|
212
|
+
this.address = '';
|
|
213
|
+
this.gasPrice = '';
|
|
214
|
+
return !this.connected;
|
|
215
|
+
}
|
|
216
|
+
// SECTION: Unimplemented methods
|
|
217
|
+
async ibcSend() {
|
|
218
|
+
// TODO: Implement IBC send
|
|
219
|
+
// REFERENCE: https://github.com/cosmos/cosmjs/blob/33271bc51c/packages/stargate/src/signingstargateclient.ts#L246
|
|
220
|
+
throw new Error('Method not implemented');
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
exports.IBC = IBC;
|
|
224
|
+
//# sourceMappingURL=ibc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ibc.js","sourceRoot":"","sources":["../../../../src/multichain/core/ibc.ts"],"names":[],"mappings":";;;AAAA,yDAG8B;AAC9B,+CAIyB;AACzB,0DAAyD;AAEzD,uDAAoE;AASpE,mCAAkC;AAElC,MAAa,GAAI,SAAQ,2BAAY;IAWjC,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAA;QAXlB,YAAO,GAAW,EAAE,CAAA;QACpB,YAAO,GAAW,EAAE,CAAA;QAKpB,cAAc;QACd,aAAQ,GAAW,EAAE,CAAA;QACrB,eAAU,GAAW,GAAG,CAAA;QAIpB,IAAI,CAAC,IAAI,GAAG,KAAK,CAAA;QAEjB,6DAA6D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AAE,CAAC,QAAQ,EACX,EAAE,CAAC,GAAG,EACN,EAAE,CAAC,IAAI,EACP,UAAU,CACb,CAAA;YAED,4DAA4D;YAC5D,MAAM,QAAQ,GAAG,UAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,EAAE,CAAA;YACjD,OAAO,QAAQ,CAAA;QACnB,CAAC,CAAC,CAAA;QAEF,uCAAuC;QACvC,OAAO,MAAM,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;IACxC,CAAC;IAED,KAAK,CAAC,UAAU;QACZ,IAAI,CAAC,aAAa,EAAE,CAAA;QACpB,IAAI,CAAC,OAAO,GAAG,EAAE,CAAA;QACjB,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAA;QAClB,OAAO,CAAC,IAAI,CAAC,SAAS,CAAA;IAC1B,CAAC;IAED,iCAAiC;IACjC,KAAK,CAAC,OAAO;QACT,2BAA2B;QAC3B,kHAAkH;QAClH,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAA;IAC7C,CAAC;CACJ;AAvSD,kBAuSC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { DefaultChain, IBCDefaultChain, IDefaultChainLocal, IDefaultChainWeb, IEVMDefaultChain, } from './types/defaultChain';
|
|
2
|
+
export { EGLDSignTxOptions, IBCConnectWalletOptions, IBCGetBalanceOptions, IBCPreparePayOptions, IBCSignTxOptions, IBCTransaction, IPayOptions, TransactionResponse, } from './types/interfaces';
|
|
3
|
+
export { required } from './utils';
|
|
4
|
+
export { EVM } from './evm';
|
|
5
|
+
export { IBC } from './ibc';
|
|
6
|
+
export { MULTIVERSX } from './multiversx';
|
|
7
|
+
export { XRPL, xrplGetLastSequence } from './xrp';
|