Skip to content

Commit

Permalink
Merge branch 'master' into lf/feat/include-pnpm-and-vite
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel committed Oct 15, 2024
2 parents c6aa87f + 5edcbe9 commit e3da112
Show file tree
Hide file tree
Showing 21 changed files with 13,995 additions and 13,018 deletions.
3 changes: 2 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# production
/build
/dist

# misc
.DS_Store
Expand All @@ -22,5 +23,5 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

#env file
# .env file
.env
3 changes: 0 additions & 3 deletions app/src/components/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ type ProvidersProps = {
export function Providers({ children }: ProvidersProps) {
const { muiTheme, theme } = useTheme();

console.log(`asd muiTheme`, muiTheme);


return (
<ThemeProvider theme={muiTheme}>
<QueryClientProvider client={queryClient}>
Expand Down
2 changes: 1 addition & 1 deletion app/src/context/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default function useTheme() {
},
});
return {
theme: currentTheme,
theme: currentTheme as Theme,
editorTheme,
setTheme,
themeColor,
Expand Down
2 changes: 1 addition & 1 deletion app/src/features/editor/components/SolidityEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AceEditor from "react-ace";
import "ace-builds/webpack-resolver";
import "ace-builds/src-noconflict/mode-rust";
import "ace-builds/src-noconflict/theme-chrome";
import "ace-builds/src-noconflict/theme-tomorrow_night_bright";
import "ace-builds/src-noconflict/theme-tomorrow_night";
import { StyledBorder } from "../../../components/shared";
import "ace-mode-solidity/build/remix-ide/mode-solidity";
import ActionOverlay from "./ActionOverlay";
Expand Down
2 changes: 1 addition & 1 deletion app/src/features/editor/components/SwayEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AceEditor from "react-ace";
import "ace-builds/webpack-resolver";
import "ace-builds/src-noconflict/mode-rust";
import "ace-builds/src-noconflict/theme-chrome";
import "ace-builds/src-noconflict/theme-tomorrow_night_bright";
import "ace-builds/src-noconflict/theme-tomorrow_night";
import { StyledBorder } from "../../../components/shared";
import ActionOverlay from "./ActionOverlay";
import { Toolchain } from "./ToolchainDropdown";
Expand Down
13 changes: 11 additions & 2 deletions app/src/features/editor/examples/sway/multiasset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const EXAMPLE_SWAY_CONTRACT_MULTIASSET = `// ERC1155 equivalent in Sway.
contract;
use standards::src5::{AccessError, SRC5, State};
use standards::src20::SRC20;
use standards::src20::{SetDecimalsEvent, SetNameEvent, SetSymbolEvent, SRC20, TotalSupplyEvent};
use standards::src3::SRC3;
use std::{
asset::{
Expand Down Expand Up @@ -58,19 +58,22 @@ impl MultiAsset for Contract {
require_access_owner();
storage.name.insert(asset, StorageString {});
storage.name.get(asset).write_slice(name);
SetNameEvent::new(asset, Some(name), msg_sender().unwrap()).log();
}
#[storage(read, write)]
fn set_symbol(asset: AssetId, symbol: String) {
require_access_owner();
storage.symbol.insert(asset, StorageString {});
storage.symbol.get(asset).write_slice(symbol);
SetSymbolEvent::new(asset, Some(symbol), msg_sender().unwrap()).log();
}
#[storage(read, write)]
fn set_decimals(asset: AssetId, decimals: u8) {
require_access_owner();
storage.decimals.insert(asset, decimals);
SetDecimalsEvent::new(asset, decimals, msg_sender().unwrap()).log();
}
}
Expand Down Expand Up @@ -113,8 +116,12 @@ impl SRC20 for Contract {
impl SRC3 for Contract {
#[storage(read, write)]
fn mint(recipient: Identity, sub_id: SubId, amount: u64) {
fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64) {
require_access_owner();
let sub_id = match sub_id {
Some(id) => id,
None => SubId::zero(),
};
let asset_id = AssetId::new(ContractId::this(), sub_id);
let supply = storage.total_supply.get(asset_id).try_read();
if supply.is_none() {
Expand All @@ -127,6 +134,7 @@ impl SRC3 for Contract {
.total_supply
.insert(asset_id, current_supply + amount);
mint_to(recipient, sub_id, amount);
TotalSupplyEvent::new(asset_id, current_supply + amount, msg_sender().unwrap()).log();
}
#[payable]
Expand All @@ -142,6 +150,7 @@ impl SRC3 for Contract {
.total_supply
.insert(asset_id, current_supply - amount);
burn(sub_id, amount);
TotalSupplyEvent::new(asset_id, current_supply - amount, msg_sender().unwrap()).log();
}
}
`;
32 changes: 27 additions & 5 deletions app/src/features/editor/examples/sway/singleasset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ contract;
use standards::src3::SRC3;
use standards::src5::{AccessError, SRC5, State};
use standards::src20::SRC20;
use standards::src20::{SetDecimalsEvent, SetNameEvent, SetSymbolEvent, SRC20, TotalSupplyEvent};
use std::{
asset::{
burn,
Expand Down Expand Up @@ -108,14 +108,16 @@ impl SRC5 for Contract {
impl SRC3 for Contract {
#[storage(read, write)]
fn mint(recipient: Identity, sub_id: SubId, amount: u64) {
require(sub_id == DEFAULT_SUB_ID, "incorrect-sub-id");
fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64) {
require(sub_id.is_some() && sub_id.unwrap() == DEFAULT_SUB_ID, "incorrect-sub-id");
require_access_owner();
let current_supply = storage.total_supply.read();
storage
.total_supply
.write(amount + storage.total_supply.read());
.write(current_supply + amount);
mint_to(recipient, DEFAULT_SUB_ID, amount);
TotalSupplyEvent::new(AssetId::default(), current_supply + amount, msg_sender().unwrap()).log();
}
#[payable]
Expand All @@ -129,10 +131,30 @@ impl SRC3 for Contract {
);
require_access_owner();
let current_supply = storage.total_supply.read();
storage
.total_supply
.write(storage.total_supply.read() - amount);
.write(current_supply - amount);
burn(DEFAULT_SUB_ID, amount);
TotalSupplyEvent::new(AssetId::default(), current_supply - amount, msg_sender().unwrap()).log();
}
}
abi EmitSRC20Events {
fn emit_src20_events();
}
impl EmitSRC20Events for Contract {
fn emit_src20_events() {
// Metadata that is stored as a configurable should only be emitted once.
let asset = AssetId::default();
let sender = msg_sender().unwrap();
let name = Some(String::from_ascii_str(from_str_array(NAME)));
let symbol = Some(String::from_ascii_str(from_str_array(SYMBOL)));
SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
SetDecimalsEvent::new(asset, DECIMALS, sender).log();
}
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AceEditor from "react-ace";
import "ace-builds/webpack-resolver";
import "ace-builds/src-noconflict/mode-json";
import "ace-builds/src-noconflict/theme-chrome";
import "ace-builds/src-noconflict/theme-tomorrow_night_bright";
import "ace-builds/src-noconflict/theme-tomorrow_night";
import useTheme from "../../../context/theme";
import { StyledBorder } from "../../../components/shared";
import {
Expand Down
21 changes: 8 additions & 13 deletions app/src/features/interact/components/ContractInterface.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useContractFunctions } from "../hooks/useContractFunctions";
import { FunctionInterface, SdkJsonAbiType } from "./FunctionInterface";
import { FunctionInterface } from "./FunctionInterface";
import { AbiHelper } from "../utils/abi";
import { useMemo, useState } from "react";
import { CopyableHex } from "../../../components/shared";
import { FunctionFragment } from "fuels";
Expand All @@ -10,8 +11,6 @@ interface ContractInterfaceProps {
updateLog: (entry: string) => void;
}

export type AbiTypeMap = Map<number, SdkJsonAbiType>;

export function ContractInterface({
contractId,
updateLog,
Expand All @@ -24,14 +23,6 @@ export function ContractInterface({

const { contract, functionNames } = useContractFunctions(contractId);

const typeMap: AbiTypeMap = useMemo(() => {
console.log("abi types", contract?.interface.jsonAbi);
return new Map(
[], // TODO
// contract?.interface.jsonAbi.types.map((type) => [type.typeId, type]),
);
}, [contract]);

function isType<T>(item: T | undefined): item is T {
return !!item;
}
Expand All @@ -41,13 +32,17 @@ export function ContractInterface({
.map((functionName) => contract?.interface.functions[functionName])
.filter(isType<FunctionFragment>);

const abiHelper = useMemo(() => {
return new AbiHelper(contract?.interface?.jsonAbi);
}, [contract]);

const functionInterfaces = useMemo(
() =>
functionFragments.map((functionFragment, index) => (
<div key={`${index}`} style={{ marginBottom: "15px" }}>
<FunctionInterface
contractId={contractId}
typeMap={typeMap}
abiHelper={abiHelper}
functionFragment={functionFragment}
functionName={functionFragment.name}
response={responses[contractId + functionFragment.name]}
Expand All @@ -61,7 +56,7 @@ export function ContractInterface({
/>
</div>
)),
[contractId, functionFragments, responses, typeMap, updateLog],
[contractId, functionFragments, responses, abiHelper, updateLog],
);

return (
Expand Down
65 changes: 22 additions & 43 deletions app/src/features/interact/components/FunctionInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,12 @@ import { useCallback, useMemo } from "react";
import { InputInstance } from "./FunctionParameters";
import { FunctionCallAccordion } from "./FunctionCallAccordion";
import { getTypeInfo } from "../utils/getTypeInfo";
import { AbiTypeMap } from "./ContractInterface";
import React from "react";

export interface SdkJsonAbiArgument {
readonly type: number;
readonly name: string;
readonly typeArguments: readonly SdkJsonAbiArgument[] | null;
}

export interface SdkJsonAbiType {
readonly typeId: number;
readonly type: string;
readonly components: readonly SdkJsonAbiArgument[] | null;
readonly typeParameters: readonly number[] | null;
}
import { AbiHelper } from "../utils/abi";

export interface FunctionInterfaceProps {
contractId: string;
typeMap: AbiTypeMap;
abiHelper: AbiHelper;
functionFragment: FunctionFragment | undefined;
functionName: string;
response?: string | Error;
Expand All @@ -31,39 +18,33 @@ export interface FunctionInterfaceProps {

export function FunctionInterface({
contractId,
typeMap,
abiHelper,
functionFragment,
functionName,
response,
setResponse,
updateLog,
}: FunctionInterfaceProps) {
const toInputInstance = useCallback(
(typeId: number, name: string): InputInstance => {
const input = typeMap?.get(typeId);
if (!input) {
return {
name,
type: {
literal: "string",
swayType: "Unknown",
},
};
}
const typeInfo = getTypeInfo(input, typeMap);
(typeId: string | number, name: string): InputInstance => {
const { concreteType, metadataType } = abiHelper.getTypesById(typeId);
const typeInfo = getTypeInfo(concreteType, abiHelper);

switch (typeInfo.literal) {
case "vector":
return {
name,
type: typeInfo,
components: [toInputInstance(input.typeParameters?.at(0) ?? 0, "")],
components: [
toInputInstance(concreteType?.typeArguments?.at(0) ?? "", ""),
],
};
case "object":
return {
name,
type: typeInfo,
components: input.components?.map((c) =>
toInputInstance(c.type, c.name),
components: metadataType?.components?.map((c) =>
toInputInstance(c.typeId, c.name),
),
};
case "option":
Expand All @@ -73,8 +54,8 @@ export function FunctionInterface({
type: typeInfo,
components: [
toInputInstance(
input.components?.at(0)?.type ?? 0,
input.components?.at(0)?.name ?? "",
metadataType?.components?.at(0)?.typeId ?? "",
metadataType?.components?.at(0)?.name ?? "",
),
],
};
Expand All @@ -85,25 +66,23 @@ export function FunctionInterface({
};
}
},
[typeMap],
[abiHelper],
);

const outputType = useMemo(() => {
// const outputTypeId = functionFragment?.jsonFn.output?.type;
const outputTypeId = 0; // TODO
const outputTypeId = functionFragment?.jsonFn?.output;
if (outputTypeId !== undefined) {
const sdkType = typeMap.get(outputTypeId);
return sdkType ? getTypeInfo(sdkType, typeMap).literal : undefined;
const sdkType = abiHelper.getConcreteTypeById(outputTypeId);
return sdkType ? getTypeInfo(sdkType, abiHelper).literal : undefined;
}
}, [functionFragment?.jsonFn.output, typeMap]);
}, [functionFragment?.jsonFn?.output, abiHelper]);

const inputInstances: InputInstance[] = useMemo(
() =>
functionFragment?.jsonFn.inputs.map(
(input) => toInputInstance(0, input.name), // TODO
// toInputInstance(input.type, input.name),
functionFragment?.jsonFn?.inputs.map((input) =>
toInputInstance(input.concreteTypeId, input.name),
) ?? [],
[functionFragment?.jsonFn.inputs, toInputInstance],
[functionFragment?.jsonFn?.inputs, toInputInstance],
);

return (
Expand Down
2 changes: 1 addition & 1 deletion app/src/features/interact/components/FunctionToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function FunctionToolbar({

const title = parameters.length ? "Parameters" : "No Parameters";

const isReadOnly = !!contract?.functions[functionName].isReadOnly();
const isReadOnly = !!contract?.functions[functionName]?.isReadOnly();

return (
<Toolbar style={{ padding: "0 2px 0", justifyContent: "space-between" }}>
Expand Down
6 changes: 3 additions & 3 deletions app/src/features/interact/hooks/useCallFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useContract } from "./useContract";
import { modifyJsonStringify } from "../utils/modifyJsonStringify";
import { CallType } from "../../../utils/types";
import { CallableParamValue } from "../components/FunctionParameters";
import { DryRunResult, FunctionResult } from "fuels";
import { Contract, DryRunResult, FunctionResult } from "fuels";

interface CallFunctionProps {
parameters: CallableParamValue[];
Expand Down Expand Up @@ -55,10 +55,10 @@ export function useCallFunction({

async function handleSuccess(
data:
| DryRunResult<any>
| DryRunResult<Contract>
| {
transactionId: string;
waitForResult: () => Promise<FunctionResult<any>>;
waitForResult: () => Promise<FunctionResult<Contract>>;
},
) {
if ("transactionId" in data) {
Expand Down
Loading

0 comments on commit e3da112

Please sign in to comment.