Skip to content

Commit 146f723

Browse files
committed
feat: support BN params, return BN instead of strings and signers rework
1 parent f767ca0 commit 146f723

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+591
-282
lines changed

README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ import { Gasp } from 'gasp-sdk';
5252

5353
const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/', {
5454
debug: true,
55-
// Optionally, pass a signer so it's not required in every call
56-
signer,
5755
// Optionally, pass a custom logger instance if needed
5856
logger,
5957
}).catch((e) => {
@@ -65,20 +63,19 @@ const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/', {
6563

6664
#### Signer
6765

68-
The SDK allows you to pass a signer instance during initialization. This is useful if you want to avoid passing the signer for every call. The signer should implement the necessary methods for signing transactions.
66+
The SDK allows you to pass a signer instance after the initialization. This is useful if you want to avoid passing the signer for every call. The signer should implement the necessary methods for signing transactions.
6967

7068
To use signer provided by the SDK, you can initialize it as follows:
7169

7270
```ts
7371
import { Gasp } from 'gasp-sdk';
7472

7573
const pk = '...';
76-
const signer = Gasp.defaultSigner.create(pk);
77-
78-
const sdk = await Gasp.create('...', { signer });
74+
const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/');
75+
sdk.setSigner(sdk.signers.ethers.create(pk))
7976
```
8077

81-
In case you want to use your own signer, you can implement the `Signer` interface and pass it to the SDK. Example implementation can be found [here](./packages/sdk/src/modules/core/EthersSigner.ts).
78+
In case you want to use your own signer, you can implement the `Signer` interface and pass it to the SDK. Example implementation can be found [here](./packages/sdk/src/modules/signer/EthersSigner.ts).
8279

8380
#### Logger
8481

@@ -130,9 +127,9 @@ The Pool module enables operations related to liquidity pools
130127
```ts
131128
import { Gasp, PoolType } from 'gasp-sdk';
132129

133-
const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/', {
134-
signer: Gasp.defaultSigner.create('...'),
135-
});
130+
const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/');
131+
132+
sdk.setSigner(sdk.signers.ethers.create('...'));
136133

137134
// Retrieve pools information
138135
const pools = await sdk.pool.getPools();
@@ -166,7 +163,7 @@ import { Gasp, PoolType } from 'gasp-sdk';
166163

167164
const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/');
168165

169-
const signer = Gasp.defaultSigner.create('...');
166+
const signer = sdk.signers.ethers.create('...');
170167

171168
// Create a new pool
172169
const result = await sdk.rewards

packages/sdk/README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ import { Gasp } from 'gasp-sdk';
5252

5353
const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/', {
5454
debug: true,
55-
// Optionally, pass a signer so it's not required in every call
56-
signer,
5755
// Optionally, pass a custom logger instance if needed
5856
logger,
5957
}).catch((e) => {
@@ -65,20 +63,19 @@ const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/', {
6563

6664
#### Signer
6765

68-
The SDK allows you to pass a signer instance during initialization. This is useful if you want to avoid passing the signer for every call. The signer should implement the necessary methods for signing transactions.
66+
The SDK allows you to pass a signer instance after the initialization. This is useful if you want to avoid passing the signer for every call. The signer should implement the necessary methods for signing transactions.
6967

7068
To use signer provided by the SDK, you can initialize it as follows:
7169

7270
```ts
7371
import { Gasp } from 'gasp-sdk';
7472

7573
const pk = '...';
76-
const signer = Gasp.defaultSigner.create(pk);
77-
78-
const sdk = await Gasp.create('...', { signer });
74+
const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/');
75+
sdk.setSigner(sdk.signers.ethers.create(pk));
7976
```
8077

81-
In case you want to use your own signer, you can implement the `Signer` interface and pass it to the SDK. Example implementation can be found [here](./src/modules/core/EthersSigner.ts).
78+
In case you want to use your own signer, you can implement the `Signer` interface and pass it to the SDK. Example implementation can be found [here](./src/modules/signer/EthersSigner.ts).
8279

8380
#### Logger
8481

@@ -130,9 +127,9 @@ The Pool module enables operations related to liquidity pools
130127
```ts
131128
import { Gasp, PoolType } from 'gasp-sdk';
132129

133-
const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/', {
134-
signer: Gasp.defaultSigner.create('...'),
135-
});
130+
const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/');
131+
132+
sdk.setSigner(sdk.signers.ethers.create('...'));
136133

137134
// Retrieve pools information
138135
const pools = await sdk.pool.getPools();
@@ -166,7 +163,7 @@ import { Gasp, PoolType } from 'gasp-sdk';
166163

167164
const sdk = await Gasp.create('wss://rollup-prod-rpc.gasp.xyz/');
168165

169-
const signer = Gasp.defaultSigner.create('...');
166+
const signer = sdk.signers.ethers.create('...');
170167

171168
// Create a new pool
172169
const result = await sdk.rewards

packages/sdk/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"@polkadot/api": "11.1.1",
2424
"big.js": "6.2.1",
25+
"bn.js": "^5.2.1",
2526
"ethers": "^6.13.5",
2627
"gasp-type-definitions": "0.0.2-feat-new-chains-integration-GASP-2054.8",
2728
"gasp-types": "0.0.2-feat-new-chains-integration-GASP-2054.8",
@@ -40,5 +41,8 @@
4041
"access": "public",
4142
"registry": "https://registry.npmjs.org/"
4243
},
43-
"private": false
44+
"private": false,
45+
"optionalDependencies": {
46+
"@wagmi/core": "^2.16.7"
47+
}
4448
}

packages/sdk/src/instance.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import { RewardsModule } from './modules/rewards/Rewards';
88
import { RolldownModule } from './modules/rolldown/Rolldown';
99
import { Signer } from './types/common';
1010
import { GaspLogger, Logger, emptyLogger } from './modules/core/Logger';
11-
import { TxModule } from './modules/core/TxModule';
12-
import { EthersSigner } from './modules/core/EthersSigner';
11+
import { TxModule } from './modules/tx/TxModule';
1312
import { GaspError } from './error/GaspError';
13+
import { SignerModule } from './modules/signer/Signer';
1414

1515
interface SDKConfig {
1616
debug?: boolean;
17-
signer?: Signer;
1817
logger?: Logger;
1918
}
2019

@@ -27,12 +26,13 @@ export class Gasp {
2726
public readonly rolldown: RolldownModule;
2827

2928
public readonly tx: TxModule;
29+
public readonly signers: SignerModule;
3030

3131
public readonly api: ApiPromise;
3232

33-
static readonly defaultSigner = EthersSigner;
33+
public signer?: Signer;
3434

35-
constructor(api: ApiPromise, logger: Logger, readonly signer?: Signer) {
35+
private constructor(api: ApiPromise, logger: Logger) {
3636
this.pool = new PoolModule(this, api, logger);
3737
this.account = new AccountModule(this, api, logger);
3838
this.asset = new AssetModule(this, api, logger);
@@ -41,10 +41,15 @@ export class Gasp {
4141
this.rolldown = new RolldownModule(this, api, logger);
4242

4343
this.tx = new TxModule(this, api, logger);
44+
this.signers = new SignerModule(this, api, logger);
4445

4546
this.api = api;
4647
}
4748

49+
setSigner(signer: Signer) {
50+
this.signer = signer;
51+
}
52+
4853
static async create(_urls: string[] | string, config?: SDKConfig) {
4954
const urls = Array.isArray(_urls) ? _urls : [_urls];
5055

@@ -60,6 +65,6 @@ export class Gasp {
6065
? config?.logger ?? new GaspLogger()
6166
: emptyLogger;
6267

63-
return new Gasp(api, logger, config?.signer);
68+
return new Gasp(api, logger);
6469
}
6570
}

packages/sdk/src/modules/account/getAssetBalance.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { GaspError } from '../../error/GaspError';
22
import { Balance } from './types';
33
import { ModuleContext } from '../core/BaseModule';
4+
import { BN } from 'bn.js';
45

56
export interface GetAssetBalanceParams {
67
account: string;
@@ -22,9 +23,9 @@ export const getAssetBalance = async (
2223
return {
2324
asset,
2425
balance: {
25-
free: res.free.toString(),
26-
reserved: res.reserved.toString(),
27-
frozen: res.frozen.toString(),
26+
free: new BN(res.free),
27+
reserved: new BN(res.reserved),
28+
frozen: new BN(res.frozen),
2829
},
2930
};
3031
};

packages/sdk/src/modules/account/getBalances.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Balance } from './types';
22
import { ModuleContext } from '../core/BaseModule';
33
import { GaspError } from '../../error/GaspError';
4+
import BN from 'bn.js';
45

56
export interface GetBalancesParams {
67
account: string;
@@ -22,9 +23,9 @@ export const getBalances = async (
2223
return {
2324
asset: key.args[1].toString(),
2425
balance: {
25-
free: value.free.toString(),
26-
reserved: value.reserved.toString(),
27-
frozen: value.frozen.toString(),
26+
free: new BN(value.free),
27+
reserved: new BN(value.reserved),
28+
frozen: new BN(value.frozen),
2829
},
2930
};
3031
});
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import BN from 'bn.js';
2+
13
export interface Balance {
24
asset: string;
35
balance: {
4-
free: string;
5-
reserved: string;
6-
frozen: string;
6+
free: BN;
7+
reserved: BN;
8+
frozen: BN;
79
};
810
}

packages/sdk/src/modules/asset/Asset.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ export class AssetModule extends BaseModule {
8686
* console.log(result);
8787
* ```
8888
*/
89-
transfer(params: TransferParams, signer: Signer) {
90-
return transfer(this.context, params, signer, this.submitTx);
89+
transfer(params: TransferParams, signer?: Signer) {
90+
return transfer(
91+
this.context,
92+
params,
93+
this.getSigner(signer),
94+
this.submitTx
95+
);
9196
}
9297
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { GaspError } from '../../error/GaspError';
22
import { ModuleContext } from '../core/BaseModule';
3+
import BN from 'bn.js';
34

45
export interface GetIssuanceParams {
56
asset: string;
@@ -8,7 +9,7 @@ export interface GetIssuanceParams {
89
export const getIssuance = async (
910
{ api }: ModuleContext,
1011
{ asset }: GetIssuanceParams
11-
): Promise<string> => {
12+
): Promise<BN> => {
1213
const issuance = await api.query.tokens.totalIssuance(asset).catch((e) => {
1314
throw new GaspError(
1415
'Unable to fetch asset metadata',
@@ -17,5 +18,5 @@ export const getIssuance = async (
1718
);
1819
});
1920

20-
return issuance.toString();
21+
return new BN(issuance);
2122
};

packages/sdk/src/modules/asset/transfer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ import {
55
SubmitHandler,
66
SubmittableTx,
77
} from '../core/BaseModule';
8+
import BN from 'bn.js';
89

910
export interface TransferParams {
1011
sender: string;
1112
recipient: string;
1213
asset: string;
13-
amount: string;
14+
amount: BN | string;
1415
}
1516

1617
export interface TransferResult extends FeeMetadata {
1718
asset: string;
1819
from: string;
1920
to: string;
20-
amount: string;
21+
amount: BN;
2122
}
2223

2324
export const transfer = (
@@ -36,7 +37,7 @@ export const transfer = (
3637
asset: data.currencyId.toString(),
3738
from: data.from.toString(),
3839
to: data.to.toString(),
39-
amount: data.amount.toString(),
40+
amount: new BN(data.amount),
4041
}),
4142
eventType: api.events.tokens.Transfer,
4243
});

0 commit comments

Comments
 (0)