Skip to content

Commit e8c1625

Browse files
authored
Merge pull request #49 from arda-org/new-default-values
Default options for new methods
2 parents c7fdc5e + cf8ff52 commit e8c1625

File tree

6 files changed

+135
-41
lines changed

6 files changed

+135
-41
lines changed

contracts/blank/interact/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import data from "./data.json";
44

55
const world = World.new({
66
chainId: envChain.id(),
7-
proxyUrl: envChain.publicProxyUrl(),
8-
gasPrice: 1_000_000_000,
9-
explorerUrl: envChain.explorerUrl(),
107
});
118

129
const loadWallet = () => world.newWalletFromFile("wallet.json");

contracts/vested-transfers/interact/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import data from "./data.json";
44

55
const world = World.new({
66
chainId: envChain.id(),
7-
proxyUrl: envChain.publicProxyUrl(),
8-
gasPrice: 1_000_000_000,
97
});
108

119
const loadWallet = () => world.newWalletFromFile("wallet.json");
@@ -19,7 +17,8 @@ program.command("deploy").action(async () => {
1917
codeMetadata: ["upgradeable"],
2018
gasLimit: 100_000_000,
2119
});
22-
console.log("Result:", result);
20+
console.log("Transaction:", result.tx.explorerUrl);
21+
console.log("Contract:", result.contract.explorerUrl);
2322
});
2423

2524
program.command("upgrade").action(async () => {
@@ -30,7 +29,7 @@ program.command("upgrade").action(async () => {
3029
codeMetadata: ["upgradeable"],
3130
gasLimit: 100_000_000,
3231
});
33-
console.log("Result:", result);
32+
console.log("Transaction:", result.tx.explorerUrl);
3433
});
3534

3635
program.command("ClaimDeveloperRewards").action(async () => {
@@ -40,7 +39,7 @@ program.command("ClaimDeveloperRewards").action(async () => {
4039
funcName: "ClaimDeveloperRewards",
4140
gasLimit: 10_000_000,
4241
});
43-
console.log("Result:", result);
42+
console.log("Transaction:", result.tx.explorerUrl);
4443
});
4544

4645
program.parse(process.argv);

xsuite/src/interact/envChain.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
export const devnetId = "D";
2-
export const testnetId = "T";
3-
export const mainnetId = "1";
1+
export const devnetChainId = "D";
2+
export const testnetChainId = "T";
3+
export const mainnetChainId = "1";
44

55
export const devnetPublicProxyUrl = "https://devnet-gateway.multiversx.com";
66
export const testnetPublicProxyUrl = "https://testnet-gateway.multiversx.com";
77
export const mainnetPublicProxyUrl = "https://gateway.multiversx.com";
88

9+
export const devnetMinGasPrice = 1_000_000_000;
10+
export const testnetMinGasPrice = 1_000_000_000;
11+
export const mainnetMinGasPrice = 1_000_000_000;
12+
913
export const devnetExplorerUrl = "https://devnet-explorer.multiversx.com";
1014
export const testnetExplorerUrl = "https://testnet-explorer.multiversx.com";
1115
export const mainnetExplorerUrl = "https://mainnet-explorer.multiversx.com";
@@ -30,22 +34,28 @@ export const envChain = {
3034
},
3135
id: () =>
3236
envChain.select({
33-
devnet: devnetId,
34-
testnet: testnetId,
35-
mainnet: mainnetId,
36-
}),
37+
devnet: devnetChainId,
38+
testnet: testnetChainId,
39+
mainnet: mainnetChainId,
40+
} as const),
3741
publicProxyUrl: () =>
3842
envChain.select({
3943
devnet: devnetPublicProxyUrl,
4044
testnet: testnetPublicProxyUrl,
4145
mainnet: mainnetPublicProxyUrl,
42-
}),
46+
} as const),
47+
minGasPrice: () =>
48+
envChain.select({
49+
devnet: devnetMinGasPrice,
50+
testnet: testnetMinGasPrice,
51+
mainnet: mainnetMinGasPrice,
52+
} as const),
4353
explorerUrl: () =>
4454
envChain.select({
4555
devnet: devnetExplorerUrl,
4656
testnet: testnetExplorerUrl,
4757
mainnet: mainnetExplorerUrl,
48-
}),
58+
} as const),
4959
};
5060

5161
const isChainName = (chain: any): chain is ChainName => {

xsuite/src/world/sworld.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ test("SWorld.proxy.getAccountWithKvs on empty bytes address", async () => {
9393
);
9494
});
9595

96+
test("SWorld.new with defined chainId", () => {
97+
expect(() => SWorld.new({ chainId: "D" })).toThrow(
98+
"chainId is not undefined.",
99+
);
100+
});
101+
102+
test("SWorld.newDevnet", () => {
103+
expect(() => SWorld.newDevnet()).toThrow("newDevnet is not implemented.");
104+
});
105+
106+
test("SWorld.newTestnet", () => {
107+
expect(() => SWorld.newTestnet()).toThrow("newTestnet is not implemented.");
108+
});
109+
110+
test("SWorld.newMainnet", () => {
111+
expect(() => SWorld.newMainnet()).toThrow("newMainnet is not implemented.");
112+
});
113+
96114
test("SWorld.createWallet", async () => {
97115
const wallet = await world.createWallet();
98116
expect(wallet.explorerUrl).toEqual(`${explorerUrl}/accounts/${wallet}`);

xsuite/src/world/sworld.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Wallet,
1111
expandCode,
1212
WorldDeployContractParams,
13+
WorldNewOptions,
1314
} from "./world";
1415

1516
let walletCounter = 0;
@@ -28,27 +29,34 @@ export class SWorld extends World {
2829
gasPrice: number;
2930
explorerUrl?: string;
3031
}) {
31-
super({ proxy, chainId: "S", gasPrice, explorerUrl });
32+
super({ chainId: "S", proxy, gasPrice, explorerUrl });
3233
this.proxy = proxy;
3334
this.sysAcc = this.newContract(new Uint8Array(32).fill(255));
3435
}
3536

36-
static new({
37-
proxyUrl,
38-
gasPrice,
39-
explorerUrl,
40-
}: {
41-
proxyUrl: string;
42-
gasPrice?: number;
43-
explorerUrl?: string;
44-
}) {
37+
static new(options: SWorldNewOptions) {
38+
if (options.chainId !== undefined) {
39+
throw new Error("chainId is not undefined.");
40+
}
4541
return new SWorld({
46-
proxy: new SProxy(proxyUrl),
47-
gasPrice: gasPrice ?? 0,
48-
explorerUrl,
42+
proxy: new SProxy(options.proxyUrl),
43+
gasPrice: options.gasPrice ?? 0,
44+
explorerUrl: options.explorerUrl,
4945
});
5046
}
5147

48+
static newDevnet(): World {
49+
throw new Error("newDevnet is not implemented.");
50+
}
51+
52+
static newTestnet(): World {
53+
throw new Error("newTestnet is not implemented.");
54+
}
55+
56+
static newMainnet(): World {
57+
throw new Error("newMainnet is not implemented.");
58+
}
59+
5260
static async start({
5361
gasPrice,
5462
explorerUrl,
@@ -184,6 +192,15 @@ const createContract = async (
184192
return contract;
185193
};
186194

195+
type SWorldNewOptions =
196+
| {
197+
chainId?: undefined;
198+
proxyUrl: string;
199+
gasPrice?: number;
200+
explorerUrl?: string;
201+
}
202+
| WorldNewOptions;
203+
187204
type SWorldCreateWalletAccount = Prettify<Omit<Account, "address">>;
188205

189206
type SWorldCreateContractAccount = Prettify<Omit<Account, "address">>;

xsuite/src/world/world.ts

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import { AddressEncodable } from "../data/AddressEncodable";
22
import { b64ToHexString } from "../data/utils";
33
import { Optional, Prettify } from "../helpers";
4+
import {
5+
devnetMinGasPrice,
6+
devnetExplorerUrl,
7+
devnetPublicProxyUrl,
8+
mainnetMinGasPrice,
9+
mainnetChainId,
10+
mainnetExplorerUrl,
11+
mainnetPublicProxyUrl,
12+
testnetMinGasPrice,
13+
testnetExplorerUrl,
14+
testnetPublicProxyUrl,
15+
devnetChainId,
16+
testnetChainId,
17+
} from "../interact/envChain";
418
import {
519
CallContractTxParams,
620
DeployContractTxParams,
@@ -37,17 +51,26 @@ export class World {
3751
this.explorerUrl = explorerUrl;
3852
}
3953

40-
static new({
41-
chainId,
42-
proxyUrl,
43-
gasPrice,
44-
explorerUrl,
45-
}: {
46-
chainId: string;
47-
proxyUrl: string;
48-
gasPrice: number;
49-
explorerUrl?: string;
50-
}) {
54+
static new({ chainId, proxyUrl, gasPrice, explorerUrl }: WorldNewOptions) {
55+
if (chainId === "D") {
56+
proxyUrl ??= devnetPublicProxyUrl;
57+
gasPrice ??= devnetMinGasPrice;
58+
explorerUrl ??= devnetExplorerUrl;
59+
} else if (chainId === "T") {
60+
proxyUrl ??= testnetPublicProxyUrl;
61+
gasPrice ??= testnetMinGasPrice;
62+
explorerUrl ??= testnetExplorerUrl;
63+
} else if (chainId === "1") {
64+
proxyUrl ??= mainnetPublicProxyUrl;
65+
gasPrice ??= mainnetMinGasPrice;
66+
explorerUrl ??= mainnetExplorerUrl;
67+
}
68+
if (proxyUrl === undefined) {
69+
throw new Error("proxyUrl is not defined.");
70+
}
71+
if (gasPrice === undefined) {
72+
throw new Error("gasPrice is not defined.");
73+
}
5174
return new World({
5275
chainId,
5376
proxy: new Proxy(proxyUrl),
@@ -56,6 +79,18 @@ export class World {
5679
});
5780
}
5881

82+
static newDevnet(options: WorldNewRealnetOptions = {}) {
83+
return World.new({ chainId: devnetChainId, ...options });
84+
}
85+
86+
static newTestnet(options: WorldNewRealnetOptions = {}) {
87+
return World.new({ chainId: testnetChainId, ...options });
88+
}
89+
90+
static newMainnet(options: WorldNewRealnetOptions = {}) {
91+
return World.new({ chainId: mainnetChainId, ...options });
92+
}
93+
5994
newWallet(signer: Signer) {
6095
return new Wallet({
6196
signer,
@@ -431,6 +466,24 @@ export const expandCode = (code: string) => {
431466
return code;
432467
};
433468

469+
export type WorldNewOptions = Prettify<
470+
| ({
471+
chainId: "D" | "T" | "1";
472+
} & WorldNewRealnetOptions)
473+
| {
474+
chainId: string;
475+
proxyUrl: string;
476+
gasPrice: number;
477+
explorerUrl?: string;
478+
}
479+
>;
480+
481+
type WorldNewRealnetOptions = {
482+
proxyUrl?: string;
483+
gasPrice?: number;
484+
explorerUrl?: string;
485+
};
486+
434487
type WorldExecuteTxParams = Prettify<
435488
Optional<Omit<TxParams, "sender" | "nonce" | "chainId">, "gasPrice">
436489
>;

0 commit comments

Comments
 (0)