@upcoming/multichain-library 0.5.2 → 0.6.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 CHANGED
@@ -61,7 +61,10 @@ In both cases, `options` is:
61
61
  ```ts
62
62
  amount: string | bigint
63
63
  originPrivateKey: `0x${string}`
64
- originAddress: `0x${string}`
65
64
  to: `0x${string}`
66
65
  nonce?: number
67
66
  ```
67
+
68
+ ## BZZ helpers
69
+
70
+ - `approveGnosisBzz(options: ApproveGnosisBzzOptions): Promise<string>`
@@ -2,6 +2,7 @@ import { FixedPointNumber } from 'cafe-utility';
2
2
  export declare const Constants: {
3
3
  nullAddress: `0x${string}`;
4
4
  bzzGnosisAddress: `0x${string}`;
5
+ postageStampGnosisAddress: `0x${string}`;
5
6
  ethereumChainId: number;
6
7
  gnosisChainId: number;
7
8
  daiDustAmount: FixedPointNumber;
package/dist/Constants.js CHANGED
@@ -5,6 +5,7 @@ const cafe_utility_1 = require("cafe-utility");
5
5
  exports.Constants = {
6
6
  nullAddress: '0x0000000000000000000000000000000000000000',
7
7
  bzzGnosisAddress: '0xdbf3ea6f5bee45c02255b2c26a16f300502f68da',
8
+ postageStampGnosisAddress: '0x45a1502382541Cd610CC9068e88727426b696293',
8
9
  ethereumChainId: 1,
9
10
  gnosisChainId: 100,
10
11
  daiDustAmount: cafe_utility_1.FixedPointNumber.fromDecimalString('0.01', 18)
@@ -0,0 +1,9 @@
1
+ import { RollingValueProvider } from 'cafe-utility';
2
+ import { MultichainLibrarySettings } from './Settings';
3
+ export interface ApproveGnosisBzzOptions {
4
+ amount: string | bigint;
5
+ originPrivateKey: `0x${string}`;
6
+ spender: `0x${string}`;
7
+ nonce?: number;
8
+ }
9
+ export declare function approveGnosisBzz(options: ApproveGnosisBzzOptions, settings: MultichainLibrarySettings, jsonRpcProvider: RollingValueProvider<string>): Promise<`0x${string}`>;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.approveGnosisBzz = approveGnosisBzz;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ const viem_1 = require("viem");
6
+ const accounts_1 = require("viem/accounts");
7
+ const chains_1 = require("viem/chains");
8
+ const Constants_1 = require("./Constants");
9
+ const GasPriceSelector_1 = require("./GasPriceSelector");
10
+ const GnosisBzzAbi_1 = require("./GnosisBzzAbi");
11
+ const GnosisTransactionCount_1 = require("./GnosisTransactionCount");
12
+ async function approveGnosisBzz(options, settings, jsonRpcProvider) {
13
+ const account = (0, accounts_1.privateKeyToAccount)(options.originPrivateKey);
14
+ const client = (0, viem_1.createWalletClient)({
15
+ chain: chains_1.gnosis,
16
+ transport: (0, viem_1.http)(jsonRpcProvider.current())
17
+ });
18
+ for (let i = 0; i < 4; i++) {
19
+ try {
20
+ const hash = await client.writeContract({
21
+ account,
22
+ abi: GnosisBzzAbi_1.GnosisBzzABI,
23
+ address: Constants_1.Constants.bzzGnosisAddress,
24
+ functionName: 'approve',
25
+ args: [options.spender, BigInt(options.amount)],
26
+ gas: 100000n,
27
+ gasPrice: (0, GasPriceSelector_1.selectGasPrice)(i),
28
+ type: 'legacy',
29
+ chain: chains_1.gnosis,
30
+ nonce: options.nonce ?? (await (0, GnosisTransactionCount_1.getGnosisTransactionCount)(account.address, settings, jsonRpcProvider))
31
+ });
32
+ return hash;
33
+ }
34
+ catch (error) {
35
+ if (cafe_utility_1.Objects.errorMatches(error, 'FeeTooLow')) {
36
+ await cafe_utility_1.System.sleepMillis(cafe_utility_1.Dates.seconds(2));
37
+ }
38
+ else {
39
+ throw error;
40
+ }
41
+ }
42
+ }
43
+ throw Error('Failed to write contract after multiple attempts due to low fees.');
44
+ }
@@ -3,7 +3,6 @@ import { MultichainLibrarySettings } from './Settings';
3
3
  export interface TransferGnosisBzzOptions {
4
4
  amount: string | bigint;
5
5
  originPrivateKey: `0x${string}`;
6
- originAddress: `0x${string}`;
7
6
  to: `0x${string}`;
8
7
  nonce?: number;
9
8
  }
@@ -27,7 +27,7 @@ async function transferGnosisBzz(options, settings, jsonRpcProvider) {
27
27
  gasPrice: (0, GasPriceSelector_1.selectGasPrice)(i),
28
28
  type: 'legacy',
29
29
  chain: chains_1.gnosis,
30
- nonce: options.nonce ?? (await (0, GnosisTransactionCount_1.getGnosisTransactionCount)(options.originAddress, settings, jsonRpcProvider))
30
+ nonce: options.nonce ?? (await (0, GnosisTransactionCount_1.getGnosisTransactionCount)(account.address, settings, jsonRpcProvider))
31
31
  });
32
32
  return hash;
33
33
  }
@@ -3,7 +3,6 @@ import { MultichainLibrarySettings } from './Settings';
3
3
  export interface TransferGnosisNativeOptions {
4
4
  amount: string | bigint;
5
5
  originPrivateKey: `0x${string}`;
6
- originAddress: `0x${string}`;
7
6
  to: `0x${string}`;
8
7
  nonce?: number;
9
8
  }
@@ -19,13 +19,13 @@ async function transferGnosisNative(options, settings, jsonRpcProvider) {
19
19
  const serializedTransaction = await account.signTransaction({
20
20
  chain: Constants_1.Constants.gnosisChainId,
21
21
  chainId: Constants_1.Constants.gnosisChainId,
22
- account: options.originAddress,
22
+ account: account.address,
23
23
  gas: 21000n,
24
24
  gasPrice: (0, GasPriceSelector_1.selectGasPrice)(i),
25
25
  type: 'legacy',
26
26
  to: options.to,
27
27
  value: BigInt(options.amount),
28
- nonce: options.nonce ?? (await (0, GnosisTransactionCount_1.getGnosisTransactionCount)(options.originAddress, settings, jsonRpcProvider))
28
+ nonce: options.nonce ?? (await (0, GnosisTransactionCount_1.getGnosisTransactionCount)(account.address, settings, jsonRpcProvider))
29
29
  });
30
30
  const hash = await client.sendRawTransaction({ serializedTransaction });
31
31
  return hash;
@@ -0,0 +1,15 @@
1
+ export declare const GnosisPostageStampABI: {
2
+ inputs: {
3
+ internalType: string;
4
+ name: string;
5
+ type: string;
6
+ }[];
7
+ name: string;
8
+ outputs: {
9
+ internalType: string;
10
+ name: string;
11
+ type: string;
12
+ }[];
13
+ stateMutability: string;
14
+ type: string;
15
+ }[];
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GnosisPostageStampABI = void 0;
4
+ exports.GnosisPostageStampABI = [
5
+ {
6
+ inputs: [
7
+ {
8
+ internalType: 'address',
9
+ name: '_owner',
10
+ type: 'address'
11
+ },
12
+ {
13
+ internalType: 'uint256',
14
+ name: '_initialBalancePerChunk',
15
+ type: 'uint256'
16
+ },
17
+ {
18
+ internalType: 'uint8',
19
+ name: '_depth',
20
+ type: 'uint8'
21
+ },
22
+ {
23
+ internalType: 'uint8',
24
+ name: '_bucketDepth',
25
+ type: 'uint8'
26
+ },
27
+ {
28
+ internalType: 'bytes32',
29
+ name: '_nonce',
30
+ type: 'bytes32'
31
+ },
32
+ {
33
+ internalType: 'bool',
34
+ name: '_immutable',
35
+ type: 'bool'
36
+ }
37
+ ],
38
+ name: 'createBatch',
39
+ outputs: [
40
+ {
41
+ internalType: 'bytes32',
42
+ name: '',
43
+ type: 'bytes32'
44
+ }
45
+ ],
46
+ stateMutability: 'nonpayable',
47
+ type: 'function'
48
+ }
49
+ ];
@@ -0,0 +1,13 @@
1
+ import { RollingValueProvider } from 'cafe-utility';
2
+ import { MultichainLibrarySettings } from './Settings';
3
+ export interface CreateBatchGnosisOptions {
4
+ originPrivateKey: `0x${string}`;
5
+ owner: `0x${string}`;
6
+ depth: number;
7
+ amount: string | bigint;
8
+ bucketDepth: 16;
9
+ batchNonce: string;
10
+ immutable: boolean;
11
+ nonce?: number;
12
+ }
13
+ export declare function createBatchGnosis(options: CreateBatchGnosisOptions, settings: MultichainLibrarySettings, jsonRpcProvider: RollingValueProvider<string>): Promise<`0x${string}`>;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createBatchGnosis = createBatchGnosis;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ const viem_1 = require("viem");
6
+ const accounts_1 = require("viem/accounts");
7
+ const chains_1 = require("viem/chains");
8
+ const Constants_1 = require("./Constants");
9
+ const Fetch_1 = require("./Fetch");
10
+ const GasPriceSelector_1 = require("./GasPriceSelector");
11
+ const GnosisPostageStampAbi_1 = require("./GnosisPostageStampAbi");
12
+ const GnosisTransactionCount_1 = require("./GnosisTransactionCount");
13
+ async function createBatchGnosis(options, settings, jsonRpcProvider) {
14
+ const account = (0, accounts_1.privateKeyToAccount)(options.originPrivateKey);
15
+ const client = (0, viem_1.createWalletClient)({
16
+ chain: chains_1.gnosis,
17
+ transport: (0, viem_1.http)(jsonRpcProvider.current())
18
+ });
19
+ for (let i = 0; i < 4; i++) {
20
+ try {
21
+ const hash = await client.writeContract({
22
+ account,
23
+ abi: GnosisPostageStampAbi_1.GnosisPostageStampABI,
24
+ address: Constants_1.Constants.postageStampGnosisAddress,
25
+ functionName: 'createBatch',
26
+ args: [
27
+ options.owner,
28
+ BigInt(options.amount),
29
+ options.depth,
30
+ options.bucketDepth,
31
+ options.batchNonce,
32
+ options.immutable
33
+ ],
34
+ gas: 1000000n,
35
+ gasPrice: (0, GasPriceSelector_1.selectGasPrice)(i),
36
+ type: 'legacy',
37
+ chain: chains_1.gnosis,
38
+ nonce: options.nonce ?? (await (0, GnosisTransactionCount_1.getGnosisTransactionCount)(account.address, settings, jsonRpcProvider))
39
+ });
40
+ const payload = { jsonrpc: '2.0', id: 1, method: 'eth_getTransactionReceipt', params: [hash] };
41
+ for (let j = 0; j < 4; j++) {
42
+ try {
43
+ await cafe_utility_1.System.sleepMillis(cafe_utility_1.Dates.seconds(5));
44
+ const response = await (0, Fetch_1.durableFetch)(jsonRpcProvider, settings, 'POST', payload);
45
+ const data = await response.json();
46
+ const object = cafe_utility_1.Types.asObject(data);
47
+ if (object.result !== null) {
48
+ const result = cafe_utility_1.Types.asObject(object.result);
49
+ const logs = cafe_utility_1.Types.asArray(result.logs).map(x => cafe_utility_1.Types.asObject(x));
50
+ const event = logs.find(x => cafe_utility_1.Types.asString(x.address).toLowerCase() ===
51
+ Constants_1.Constants.postageStampGnosisAddress.toLowerCase());
52
+ if (event) {
53
+ const topics = cafe_utility_1.Types.asArray(event.topics);
54
+ if (topics[1]) {
55
+ return cafe_utility_1.Types.asHexString(topics[1]);
56
+ }
57
+ }
58
+ }
59
+ }
60
+ catch (error) {
61
+ console.error(error);
62
+ }
63
+ }
64
+ throw Error('Failed to fetch transaction receipt after multiple attempts.');
65
+ }
66
+ catch (error) {
67
+ if (cafe_utility_1.Objects.errorMatches(error, 'FeeTooLow')) {
68
+ await cafe_utility_1.System.sleepMillis(cafe_utility_1.Dates.seconds(2));
69
+ }
70
+ else {
71
+ throw error;
72
+ }
73
+ }
74
+ }
75
+ throw Error('Failed to write contract after multiple attempts due to low fees.');
76
+ }
@@ -0,0 +1,3 @@
1
+ import { RollingValueProvider } from 'cafe-utility';
2
+ import { MultichainLibrarySettings } from './Settings';
3
+ export declare function getStoragePriceGnosis(settings: MultichainLibrarySettings, jsonRpcProvider: RollingValueProvider<string>): Promise<bigint>;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getStoragePriceGnosis = getStoragePriceGnosis;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ const Fetch_1 = require("./Fetch");
6
+ async function getStoragePriceGnosis(settings, jsonRpcProvider) {
7
+ const payload = {
8
+ jsonrpc: '2.0',
9
+ id: 1,
10
+ method: 'eth_call',
11
+ params: [
12
+ {
13
+ to: '0x47eef336e7fe5bed98499a4696bce8f28c1b0a8b', // price oracle
14
+ data: '0x9d1b464a00000000000000000000000047eef336e7fe5bed98499a4696bce8f28c1b0a8b'
15
+ },
16
+ 'latest'
17
+ ]
18
+ };
19
+ const response = await (0, Fetch_1.durableFetch)(jsonRpcProvider, settings, 'POST', payload);
20
+ const data = await response.json();
21
+ const object = cafe_utility_1.Types.asObject(data);
22
+ const price = cafe_utility_1.Types.asHexString(object.result, { strictPrefix: true, uneven: true });
23
+ return BigInt(price);
24
+ }
@@ -3,13 +3,11 @@ import { MultichainLibrarySettings } from './Settings';
3
3
  export interface GnosisSwapAutoOptions {
4
4
  amount: string | bigint;
5
5
  originPrivateKey: `0x${string}`;
6
- originAddress: `0x${string}`;
7
6
  to: `0x${string}`;
8
7
  }
9
8
  export declare function swapOnGnosisAuto(options: GnosisSwapAutoOptions, settings: MultichainLibrarySettings, jsonRpcProvider: RollingValueProvider<string>): Promise<`0x${string}`>;
10
9
  export interface GnosisSwapCustomOptions {
11
10
  originPrivateKey: `0x${string}`;
12
- originAddress: `0x${string}`;
13
11
  gas: bigint | string | number;
14
12
  gasPrice: bigint | string | number;
15
13
  to: `0x${string}`;
@@ -9,10 +9,10 @@ const Constants_1 = require("./Constants");
9
9
  const GnosisTransactionCount_1 = require("./GnosisTransactionCount");
10
10
  const SushiSwap_1 = require("./SushiSwap");
11
11
  async function swapOnGnosisAuto(options, settings, jsonRpcProvider) {
12
- const quote = await (0, SushiSwap_1.getSushiSwapQuote)(options.amount.toString(), options.originAddress, options.to, settings);
12
+ const account = (0, accounts_1.privateKeyToAccount)(options.originPrivateKey);
13
+ const quote = await (0, SushiSwap_1.getSushiSwapQuote)(options.amount.toString(), account.address, options.to, settings);
13
14
  return swapOnGnosisCustom({
14
15
  originPrivateKey: options.originPrivateKey,
15
- originAddress: options.originAddress,
16
16
  gas: (BigInt(quote.tx.gas) * 5n) / 4n, // add 25% buffer
17
17
  gasPrice: BigInt(quote.tx.gasPrice),
18
18
  to: quote.tx.to,
@@ -27,14 +27,14 @@ async function swapOnGnosisCustom(options, settings, jsonRpcProvider) {
27
27
  .signTransaction({
28
28
  chain: Constants_1.Constants.gnosisChainId,
29
29
  chainId: Constants_1.Constants.gnosisChainId,
30
- account: options.originAddress,
30
+ account: account.address,
31
31
  gas: (BigInt(options.gas) * 5n) / 4n, // add 25% buffer
32
32
  gasPrice: BigInt(options.gasPrice),
33
33
  type: 'legacy',
34
34
  to: options.to,
35
35
  value: BigInt(options.value),
36
36
  data: options.data,
37
- nonce: await (0, GnosisTransactionCount_1.getGnosisTransactionCount)(options.originAddress, settings, jsonRpcProvider)
37
+ nonce: await (0, GnosisTransactionCount_1.getGnosisTransactionCount)(account.address, settings, jsonRpcProvider)
38
38
  })
39
39
  .then(signedTx => client.sendRawTransaction({ serializedTransaction: signedTx }));
40
40
  }
@@ -3,7 +3,6 @@ import { MultichainLibrarySettings } from './Settings';
3
3
  export interface MultiTransferGnosisNativeOptions {
4
4
  amount: string | bigint;
5
5
  originPrivateKey: `0x${string}`;
6
- originAddress: `0x${string}`;
7
6
  to: `0x${string}`[];
8
7
  nonce?: number;
9
8
  }
@@ -1,18 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.multiTransferGnosisNative = multiTransferGnosisNative;
4
+ const accounts_1 = require("viem/accounts");
4
5
  const GnosisNativeTransfer_1 = require("./GnosisNativeTransfer");
5
6
  const GnosisTransactionCount_1 = require("./GnosisTransactionCount");
6
7
  async function multiTransferGnosisNative(options, settings, jsonRpcProvider) {
8
+ const account = (0, accounts_1.privateKeyToAccount)(options.originPrivateKey);
7
9
  const transactions = [];
8
10
  const errors = [];
9
- let nonce = options.nonce ?? (await (0, GnosisTransactionCount_1.getGnosisTransactionCount)(options.originAddress, settings, jsonRpcProvider));
11
+ let nonce = options.nonce ?? (await (0, GnosisTransactionCount_1.getGnosisTransactionCount)(account.address, settings, jsonRpcProvider));
10
12
  for (const address of options.to) {
11
13
  try {
12
14
  const transaction = await (0, GnosisNativeTransfer_1.transferGnosisNative)({
13
15
  amount: options.amount,
14
16
  originPrivateKey: options.originPrivateKey,
15
- originAddress: options.originAddress,
16
17
  to: address,
17
18
  nonce: nonce++
18
19
  }, settings, jsonRpcProvider);
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { FixedPointNumber, RollingValueProvider } from 'cafe-utility';
2
2
  import { Constants } from './Constants';
3
+ import { ApproveGnosisBzzOptions } from './GnosisBzzApprove';
3
4
  import { TransferGnosisBzzOptions } from './GnosisBzzTransfer';
4
5
  import { TransferGnosisNativeOptions } from './GnosisNativeTransfer';
6
+ import { CreateBatchGnosisOptions } from './GnosisPostageStampCreateBatch';
5
7
  import { GnosisSwapAutoOptions, GnosisSwapCustomOptions } from './GnosisSwap';
6
8
  import { MultiTransferGnosisNativeOptions, MultiTransferGnosisNativeResult } from './MultiGnosisNativeTransfer';
7
9
  import { MultichainLibrarySettings } from './Settings';
@@ -21,6 +23,9 @@ export declare class MultichainLibrary {
21
23
  transferGnosisNative(options: TransferGnosisNativeOptions): Promise<`0x${string}`>;
22
24
  multiTransferGnosisNative(options: MultiTransferGnosisNativeOptions): Promise<MultiTransferGnosisNativeResult>;
23
25
  transferGnosisBzz(options: TransferGnosisBzzOptions): Promise<`0x${string}`>;
26
+ approveGnosisBzz(options: ApproveGnosisBzzOptions): Promise<`0x${string}`>;
27
+ createBatchGnosis(options: CreateBatchGnosisOptions): Promise<`0x${string}`>;
28
+ getStoragePriceGnosis(): Promise<bigint>;
24
29
  waitForGnosisBzzBalanceToIncrease(address: string, initialBalance: bigint): Promise<void>;
25
30
  waitForGnosisNativeBalanceToDecrease(address: `0x${string}`, initialBalance: bigint): Promise<void>;
26
31
  waitForGnosisNativeBalanceToIncrease(address: `0x${string}`, initialBalance: bigint): Promise<void>;
package/dist/index.js CHANGED
@@ -3,10 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MultichainLibrary = void 0;
4
4
  const cafe_utility_1 = require("cafe-utility");
5
5
  const Constants_1 = require("./Constants");
6
+ const GnosisBzzApprove_1 = require("./GnosisBzzApprove");
6
7
  const GnosisBzzBalance_1 = require("./GnosisBzzBalance");
7
8
  const GnosisBzzTransfer_1 = require("./GnosisBzzTransfer");
8
9
  const GnosisNativeBalance_1 = require("./GnosisNativeBalance");
9
10
  const GnosisNativeTransfer_1 = require("./GnosisNativeTransfer");
11
+ const GnosisPostageStampCreateBatch_1 = require("./GnosisPostageStampCreateBatch");
12
+ const GnosisPostageStampStoragePrice_1 = require("./GnosisPostageStampStoragePrice");
10
13
  const GnosisSwap_1 = require("./GnosisSwap");
11
14
  const GnosisTransactionCount_1 = require("./GnosisTransactionCount");
12
15
  const MultiGnosisNativeTransfer_1 = require("./MultiGnosisNativeTransfer");
@@ -47,6 +50,15 @@ class MultichainLibrary {
47
50
  transferGnosisBzz(options) {
48
51
  return (0, GnosisBzzTransfer_1.transferGnosisBzz)(options, this.settings, this.jsonRpcProvider);
49
52
  }
53
+ approveGnosisBzz(options) {
54
+ return (0, GnosisBzzApprove_1.approveGnosisBzz)(options, this.settings, this.jsonRpcProvider);
55
+ }
56
+ createBatchGnosis(options) {
57
+ return (0, GnosisPostageStampCreateBatch_1.createBatchGnosis)(options, this.settings, this.jsonRpcProvider);
58
+ }
59
+ getStoragePriceGnosis() {
60
+ return (0, GnosisPostageStampStoragePrice_1.getStoragePriceGnosis)(this.settings, this.jsonRpcProvider);
61
+ }
50
62
  waitForGnosisBzzBalanceToIncrease(address, initialBalance) {
51
63
  return (0, Waiter_1.waitForGnosisBzzBalanceToIncrease)(address, initialBalance, this.settings, this.jsonRpcProvider);
52
64
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@upcoming/multichain-library",
3
3
  "private": false,
4
- "version": "0.5.2",
4
+ "version": "0.6.0",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "exports": {