Skip to content

Commit

Permalink
Merge pull request #47 from arda-org/fix-mandatory-gas-limit
Browse files Browse the repository at this point in the history
Make gasLimit mandatory when creating a world
  • Loading branch information
lcswillems authored Nov 13, 2023
2 parents 72e4f14 + b42c711 commit 04a266f
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 67 deletions.
7 changes: 3 additions & 4 deletions xsuite/src/data/TupleDecoder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Prettify } from "../helpers";
import { ByteReader } from "./ByteReader";
import { AbstractDecoder, Decoder } from "./Decoder";

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

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

type DecoderMapToValueMap<T> = {
type DecoderMapToValueMap<T> = Prettify<{
[K in keyof T]: T[K] extends Decoder<infer U> ? U : never;
} & {
// Pretiffy type: https://twitter.com/mattpocockuk/status/1622730173446557697
};
}>;
5 changes: 5 additions & 0 deletions xsuite/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Pretiffy type: https://twitter.com/mattpocockuk/status/1622730173446557697
// eslint-disable-next-line @typescript-eslint/ban-types
export type Prettify<T> = { [K in keyof T]: T[K] } & {};

export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
12 changes: 6 additions & 6 deletions xsuite/src/proxy/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class Tx {
value: (params.value ?? 0n).toString(),
receiver: params.receiver.toString(),
sender: params.sender.toString(),
gasPrice: params.gasPrice ?? 0,
gasPrice: params.gasPrice,
gasLimit: params.gasLimit,
data: params.data === undefined ? undefined : btoa(params.data),
chainID: params.chainId,
Expand Down Expand Up @@ -469,7 +469,7 @@ export type TxParams = {
value?: number | bigint;
receiver: Address;
sender: Address;
gasPrice?: number;
gasPrice: number;
gasLimit: number;
data?: string;
chainId: string;
Expand All @@ -480,7 +480,7 @@ export type DeployContractTxParams = {
nonce: number;
value?: number | bigint;
sender: Address;
gasPrice?: number;
gasPrice: number;
gasLimit: number;
code: string;
codeMetadata: CodeMetadata;
Expand All @@ -498,7 +498,7 @@ export type UpgradeContractTxParams = {
value?: number | bigint;
callee: Address;
sender: Address;
gasPrice?: number;
gasPrice: number;
gasLimit: number;
code: string;
codeMetadata: CodeMetadata;
Expand All @@ -512,7 +512,7 @@ export type TransferTxParams = {
value?: number | bigint;
receiver: Address;
sender: Address;
gasPrice?: number;
gasPrice: number;
gasLimit: number;
esdts?: { id: string; nonce?: number; amount: number | bigint }[];
chainId: string;
Expand All @@ -524,7 +524,7 @@ export type CallContractTxParams = {
value?: number | bigint;
callee: Address;
sender: Address;
gasPrice?: number;
gasPrice: number;
gasLimit: number;
funcName: string;
funcArgs?: Hex[];
Expand Down
44 changes: 30 additions & 14 deletions xsuite/src/world/sworld.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Prettify } from "../helpers";
import { SProxy } from "../proxy";
import { DeployContractTxParams } from "../proxy/proxy";
import { Account, Block } from "../proxy/sproxy";
import { DummySigner, Signer } from "./signer";
import { startSimulnet } from "./simulnet";
import { isContractAddress, numberToBytesAddress } from "./utils";
import { World, Contract, Wallet, expandCode } from "./world";
import {
World,
Contract,
Wallet,
expandCode,
WorldDeployContractParams,
} from "./world";

let walletCounter = 0;
let contractCounter = 0;
Expand All @@ -13,7 +19,7 @@ export class SWorld extends World {
proxy: SProxy;
sysAcc: SContract;

constructor({ proxy, gasPrice }: { proxy: SProxy; gasPrice?: number }) {
constructor({ proxy, gasPrice }: { proxy: SProxy; gasPrice: number }) {
super({ proxy, chainId: "S", gasPrice });
this.proxy = proxy;
this.sysAcc = new SContract({
Expand All @@ -23,7 +29,7 @@ export class SWorld extends World {
}

static new({ proxyUrl, gasPrice }: { proxyUrl: string; gasPrice?: number }) {
return new SWorld({ proxy: new SProxy(proxyUrl), gasPrice });
return new SWorld({ proxy: new SProxy(proxyUrl), gasPrice: gasPrice ?? 0 });
}

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

async createWallet(account: Omit<Account, "address"> = {}) {
async createWallet(account: SWorldCreateWalletAccount = {}) {
walletCounter += 1;
const address = numberToBytesAddress(walletCounter, false);
const wallet = new SWallet({
Expand All @@ -59,7 +65,7 @@ export class SWorld extends World {
return wallet;
}

createContract(account: Omit<Account, "address"> = {}) {
createContract(account: SWorldCreateContractAccount = {}) {
return createContract(this.proxy, account);
}

Expand All @@ -84,24 +90,22 @@ export class SWallet extends Wallet {
signer: Signer;
proxy: SProxy;
chainId: string;
gasPrice?: number;
gasPrice: number;
}) {
super({ signer, proxy, chainId, gasPrice });
this.proxy = proxy;
}

setAccount(account: Omit<Account, "address">) {
setAccount(account: SWalletSetAccountAccount) {
return setAccount(this.proxy, { address: this, ...account });
}

createContract(account: Omit<Account, "address" | "owner">) {
createContract(account: SWalletCreateContractAccount = {}) {
return createContract(this.proxy, { ...account, owner: this });
}

deployContract(
txParams: Omit<DeployContractTxParams, "sender" | "nonce" | "chainId">,
) {
return super.deployContract(txParams).then((data) => ({
deployContract(params: WorldDeployContractParams) {
return super.deployContract(params).then((data) => ({
...data,
contract: new SContract({
address: data.address,
Expand All @@ -125,7 +129,7 @@ export class SContract extends Contract {
this.proxy = proxy;
}

setAccount(account: Omit<Account, "address">) {
setAccount(account: SContractSetAccountAccount) {
return setAccount(this.proxy, { address: this, ...account });
}
}
Expand All @@ -151,3 +155,15 @@ const createContract = async (
await contract.setAccount(account);
return contract;
};

type SWorldCreateWalletAccount = Prettify<Omit<Account, "address">>;

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

type SWalletSetAccountAccount = Prettify<Omit<Account, "address">>;

type SWalletCreateContractAccount = Prettify<
Omit<Account, "address" | "owner">
>;

type SContractSetAccountAccount = Omit<Account, "address">;
2 changes: 1 addition & 1 deletion xsuite/src/world/world.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { World } from "./world";

test("World.new, World.newWallet, World.newContract", async () => {
const proxyUrl = await startSimulnet();
const world = World.new({ proxyUrl, chainId: "S" });
const world = World.new({ proxyUrl, chainId: "S", gasPrice: 0 });
const wallet = world.newWallet(new DummySigner(new Uint8Array(32)));
const contract = world.newContract(new Uint8Array(32));
expect(wallet.toTopBytes()).toEqual(new Uint8Array(32));
Expand Down
Loading

0 comments on commit 04a266f

Please sign in to comment.