Skip to content

Commit b42c711

Browse files
committed
Make gasLimit mandatory when creating a world
1 parent 72e4f14 commit b42c711

File tree

6 files changed

+107
-67
lines changed

6 files changed

+107
-67
lines changed

xsuite/src/data/TupleDecoder.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Prettify } from "../helpers";
12
import { ByteReader } from "./ByteReader";
23
import { AbstractDecoder, Decoder } from "./Decoder";
34

@@ -25,8 +26,6 @@ export class TupleDecoder<T extends DecoderMap<any>> extends AbstractDecoder<
2526

2627
export type DecoderMap<T> = Record<string, Decoder<T>>;
2728

28-
type DecoderMapToValueMap<T> = {
29+
type DecoderMapToValueMap<T> = Prettify<{
2930
[K in keyof T]: T[K] extends Decoder<infer U> ? U : never;
30-
} & {
31-
// Pretiffy type: https://twitter.com/mattpocockuk/status/1622730173446557697
32-
};
31+
}>;

xsuite/src/helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Pretiffy type: https://twitter.com/mattpocockuk/status/1622730173446557697
2+
// eslint-disable-next-line @typescript-eslint/ban-types
3+
export type Prettify<T> = { [K in keyof T]: T[K] } & {};
4+
5+
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

xsuite/src/proxy/proxy.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class Tx {
241241
value: (params.value ?? 0n).toString(),
242242
receiver: params.receiver.toString(),
243243
sender: params.sender.toString(),
244-
gasPrice: params.gasPrice ?? 0,
244+
gasPrice: params.gasPrice,
245245
gasLimit: params.gasLimit,
246246
data: params.data === undefined ? undefined : btoa(params.data),
247247
chainID: params.chainId,
@@ -469,7 +469,7 @@ export type TxParams = {
469469
value?: number | bigint;
470470
receiver: Address;
471471
sender: Address;
472-
gasPrice?: number;
472+
gasPrice: number;
473473
gasLimit: number;
474474
data?: string;
475475
chainId: string;
@@ -480,7 +480,7 @@ export type DeployContractTxParams = {
480480
nonce: number;
481481
value?: number | bigint;
482482
sender: Address;
483-
gasPrice?: number;
483+
gasPrice: number;
484484
gasLimit: number;
485485
code: string;
486486
codeMetadata: CodeMetadata;
@@ -498,7 +498,7 @@ export type UpgradeContractTxParams = {
498498
value?: number | bigint;
499499
callee: Address;
500500
sender: Address;
501-
gasPrice?: number;
501+
gasPrice: number;
502502
gasLimit: number;
503503
code: string;
504504
codeMetadata: CodeMetadata;
@@ -512,7 +512,7 @@ export type TransferTxParams = {
512512
value?: number | bigint;
513513
receiver: Address;
514514
sender: Address;
515-
gasPrice?: number;
515+
gasPrice: number;
516516
gasLimit: number;
517517
esdts?: { id: string; nonce?: number; amount: number | bigint }[];
518518
chainId: string;
@@ -524,7 +524,7 @@ export type CallContractTxParams = {
524524
value?: number | bigint;
525525
callee: Address;
526526
sender: Address;
527-
gasPrice?: number;
527+
gasPrice: number;
528528
gasLimit: number;
529529
funcName: string;
530530
funcArgs?: Hex[];

xsuite/src/world/sworld.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import { Prettify } from "../helpers";
12
import { SProxy } from "../proxy";
2-
import { DeployContractTxParams } from "../proxy/proxy";
33
import { Account, Block } from "../proxy/sproxy";
44
import { DummySigner, Signer } from "./signer";
55
import { startSimulnet } from "./simulnet";
66
import { isContractAddress, numberToBytesAddress } from "./utils";
7-
import { World, Contract, Wallet, expandCode } from "./world";
7+
import {
8+
World,
9+
Contract,
10+
Wallet,
11+
expandCode,
12+
WorldDeployContractParams,
13+
} from "./world";
814

915
let walletCounter = 0;
1016
let contractCounter = 0;
@@ -13,7 +19,7 @@ export class SWorld extends World {
1319
proxy: SProxy;
1420
sysAcc: SContract;
1521

16-
constructor({ proxy, gasPrice }: { proxy: SProxy; gasPrice?: number }) {
22+
constructor({ proxy, gasPrice }: { proxy: SProxy; gasPrice: number }) {
1723
super({ proxy, chainId: "S", gasPrice });
1824
this.proxy = proxy;
1925
this.sysAcc = new SContract({
@@ -23,7 +29,7 @@ export class SWorld extends World {
2329
}
2430

2531
static new({ proxyUrl, gasPrice }: { proxyUrl: string; gasPrice?: number }) {
26-
return new SWorld({ proxy: new SProxy(proxyUrl), gasPrice });
32+
return new SWorld({ proxy: new SProxy(proxyUrl), gasPrice: gasPrice ?? 0 });
2733
}
2834

2935
static async start({
@@ -46,7 +52,7 @@ export class SWorld extends World {
4652
return new SContract({ address, proxy: this.proxy });
4753
}
4854

49-
async createWallet(account: Omit<Account, "address"> = {}) {
55+
async createWallet(account: SWorldCreateWalletAccount = {}) {
5056
walletCounter += 1;
5157
const address = numberToBytesAddress(walletCounter, false);
5258
const wallet = new SWallet({
@@ -59,7 +65,7 @@ export class SWorld extends World {
5965
return wallet;
6066
}
6167

62-
createContract(account: Omit<Account, "address"> = {}) {
68+
createContract(account: SWorldCreateContractAccount = {}) {
6369
return createContract(this.proxy, account);
6470
}
6571

@@ -84,24 +90,22 @@ export class SWallet extends Wallet {
8490
signer: Signer;
8591
proxy: SProxy;
8692
chainId: string;
87-
gasPrice?: number;
93+
gasPrice: number;
8894
}) {
8995
super({ signer, proxy, chainId, gasPrice });
9096
this.proxy = proxy;
9197
}
9298

93-
setAccount(account: Omit<Account, "address">) {
99+
setAccount(account: SWalletSetAccountAccount) {
94100
return setAccount(this.proxy, { address: this, ...account });
95101
}
96102

97-
createContract(account: Omit<Account, "address" | "owner">) {
103+
createContract(account: SWalletCreateContractAccount = {}) {
98104
return createContract(this.proxy, { ...account, owner: this });
99105
}
100106

101-
deployContract(
102-
txParams: Omit<DeployContractTxParams, "sender" | "nonce" | "chainId">,
103-
) {
104-
return super.deployContract(txParams).then((data) => ({
107+
deployContract(params: WorldDeployContractParams) {
108+
return super.deployContract(params).then((data) => ({
105109
...data,
106110
contract: new SContract({
107111
address: data.address,
@@ -125,7 +129,7 @@ export class SContract extends Contract {
125129
this.proxy = proxy;
126130
}
127131

128-
setAccount(account: Omit<Account, "address">) {
132+
setAccount(account: SContractSetAccountAccount) {
129133
return setAccount(this.proxy, { address: this, ...account });
130134
}
131135
}
@@ -151,3 +155,15 @@ const createContract = async (
151155
await contract.setAccount(account);
152156
return contract;
153157
};
158+
159+
type SWorldCreateWalletAccount = Prettify<Omit<Account, "address">>;
160+
161+
type SWorldCreateContractAccount = Prettify<Omit<Account, "address">>;
162+
163+
type SWalletSetAccountAccount = Prettify<Omit<Account, "address">>;
164+
165+
type SWalletCreateContractAccount = Prettify<
166+
Omit<Account, "address" | "owner">
167+
>;
168+
169+
type SContractSetAccountAccount = Omit<Account, "address">;

xsuite/src/world/world.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { World } from "./world";
66

77
test("World.new, World.newWallet, World.newContract", async () => {
88
const proxyUrl = await startSimulnet();
9-
const world = World.new({ proxyUrl, chainId: "S" });
9+
const world = World.new({ proxyUrl, chainId: "S", gasPrice: 0 });
1010
const wallet = world.newWallet(new DummySigner(new Uint8Array(32)));
1111
const contract = world.newContract(new Uint8Array(32));
1212
expect(wallet.toTopBytes()).toEqual(new Uint8Array(32));

0 commit comments

Comments
 (0)