Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
kairoski03 committed Sep 12, 2024
2 parents dfeba27 + 8c3d41d commit 5145ba5
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 29 deletions.
27 changes: 23 additions & 4 deletions src/components/injective/Compiler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ interface InterfaceProps {
setFileName: Dispatch<React.SetStateAction<string>>;
compileTarget: string;
client: any;
reset: () => void;
}

const RCV_EVENT_LOG_PREFIX = `[==> EVENT_RCV]`;
Expand All @@ -48,7 +47,6 @@ export const Compiler: React.FunctionComponent<InterfaceProps> = ({
setFileName,
client,
compileTarget,
reset,
}) => {
const [iconSpin, setIconSpin] = useState<string>('');
const [wasm, setWasm] = useState<string>('');
Expand All @@ -67,10 +65,11 @@ export const Compiler: React.FunctionComponent<InterfaceProps> = ({
const { injectiveAddress, chainId } = useWalletStore();

useEffect(() => {
init();
exists();
setSchemaObj();
}, [compileTarget]);

const init = () => {
const init = async () => {
setWasm('');
setChecksum('');
setFileName('');
Expand All @@ -81,6 +80,26 @@ export const Compiler: React.FunctionComponent<InterfaceProps> = ({
setTimestamp('');
};

const exists = async () => {
try {
const artifacts = await client?.fileManager.readdir(
'browser/' + compileTarget + '/artifacts',
);
const filesName = Object.keys(artifacts || {});
await Promise.all(
filesName.map(async (f) => {
if (getExtensionOfFilename(f) === '.wasm') {
const wasmFile = await client?.fileManager.readFile('browser/' + f);
setWasm(wasmFile || '');
setFileName(f);
}
}),
);
} catch (e: any) {
client.terminal.log({ type: 'error', value: `${e.message}` });
}
};

const removeArtifacts = async () => {
log.info(`removeArtifacts ${'browser/' + compileTarget + '/artifacts'}`);
try {
Expand Down
128 changes: 109 additions & 19 deletions src/components/injective/Contract.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React, { useState } from 'react';
import { Network, getNetworkEndpoints } from '@injectivelabs/networks';
import { Button, Form as ReactForm } from 'react-bootstrap';
import { ChainGrpcWasmApi, MsgExecuteContract } from '@injectivelabs/sdk-ts';
import { Button, InputGroup, Form as ReactForm } from 'react-bootstrap';
import {
ChainGrpcWasmApi,
MsgExecuteContract,
spotPriceToChainPriceToFixed,
spotQuantityToChainQuantityToFixed,
} from '@injectivelabs/sdk-ts';
import Form from '@rjsf/core';
import validator from '@rjsf/validator-ajv8';
import { ChainId } from '@injectivelabs/ts-types';
Expand All @@ -10,6 +15,7 @@ import { log } from '../../utils/logger';
import { useWalletStore } from './WalletContextProvider';

interface InterfaceProps {
compileTarget: string;
contractAddress: string;
client: any;
fund: number;
Expand All @@ -18,12 +24,16 @@ interface InterfaceProps {
}

export const Contract: React.FunctionComponent<InterfaceProps> = ({
compileTarget,
contractAddress,
client,
schemaExec,
fund,
schemaQuery,
}) => {
const [price, setPrice] = useState('');
const [quantity, setQuantity] = useState('');

const [queryMsgErr, setQueryMsgErr] = useState('');
const [queryResult, setQueryResult] = useState('');

Expand All @@ -37,14 +47,54 @@ export const Contract: React.FunctionComponent<InterfaceProps> = ({
const executeKeplr = async () => {
try {
const funds = fund ? [{ denom: 'inj', amount: fund.toString() }] : [];
const usdtFunds = [
{
denom: 'peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5',
amount: spotQuantityToChainQuantityToFixed({
value: fund.toString(),
baseDecimals: 6,
}),
},
];

const executeMsg_ = { ...executeMsg };
recursiveValueChange(executeMsg_, stringToNumber);
const msg = MsgExecuteContract.fromJSON({
contractAddress: contractAddress,
sender: injectiveAddress,
msg: executeMsg_,
funds: funds,
const fixedPrice = spotPriceToChainPriceToFixed({
value: price,
baseDecimals: 18,
quoteDecimals: 6,
});
const fixedQuantity = spotQuantityToChainQuantityToFixed({
value: quantity,
baseDecimals: 18,
});

console.log(fixedPrice, fixedQuantity, usdtFunds);
recursiveValueChange(executeMsg_, stringToNumber);
const msg = compileTarget.split('/').find((dir) => dir === 'atomic-order-example')
? MsgExecuteContract.fromJSON({
contractAddress: contractAddress,
sender: injectiveAddress,
msg: {
swap_spot: {
price: spotPriceToChainPriceToFixed({
value: price,
baseDecimals: 18,
quoteDecimals: 6,
}),
quantity: spotQuantityToChainQuantityToFixed({
value: quantity,
baseDecimals: 18,
}),
},
},
funds: usdtFunds,
})
: MsgExecuteContract.fromJSON({
contractAddress: contractAddress,
sender: injectiveAddress,
msg: executeMsg_,
funds: funds,
});
const txResult = await injectiveBroadcastMsg(msg, injectiveAddress);
await client.terminal.log({
type: 'info',
Expand Down Expand Up @@ -104,6 +154,13 @@ export const Contract: React.FunctionComponent<InterfaceProps> = ({
const uiSchemaQuery = generateUiSchemaFromSchema(schemaQuery);
const uiSchemaExecute = generateUiSchemaFromSchema(schemaExec);

const handlePriceChange = (e: any) => {
setPrice(e.target.value);
};
const handleQuantityChange = (e: any) => {
setQuantity(e.target.value);
};

return (
<div>
<ReactForm>
Expand Down Expand Up @@ -137,17 +194,50 @@ export const Contract: React.FunctionComponent<InterfaceProps> = ({
style={{ display: 'flex', alignItems: 'center', margin: '0.3em 0.3em' }}
className="mb-2"
>
<Form
schema={schemaExec}
validator={validator}
uiSchema={uiSchemaExecute}
onChange={handleExecuteChange}
formData={executeMsg || {}}
>
<Button onClick={executeKeplr} size={'sm'}>
Execute
</Button>
</Form>
{compileTarget.split('/').find((dir) => dir === 'atomic-order-example') ? (
<ReactForm>
<ReactForm.Text className="text-muted" style={{ marginBottom: '4px' }}>
<small>Price</small>
</ReactForm.Text>
<InputGroup>
<ReactForm.Control
type="number"
placeholder="0"
value={price}
onChange={handlePriceChange}
size="sm"
/>
<ReactForm.Control type="text" placeholder="" value={'USDT'} size="sm" readOnly />
</InputGroup>
<ReactForm.Text className="text-muted" style={{ marginBottom: '4px' }}>
<small>Quantity</small>
</ReactForm.Text>
<InputGroup>
<ReactForm.Control
type="number"
placeholder="0"
value={quantity}
onChange={handleQuantityChange}
size="sm"
/>
</InputGroup>
<Button onClick={executeKeplr} size={'sm'}>
Execute
</Button>
</ReactForm>
) : (
<Form
schema={schemaExec}
validator={validator}
uiSchema={uiSchemaExecute}
onChange={handleExecuteChange}
formData={executeMsg || {}}
>
<Button onClick={executeKeplr} size={'sm'}>
Execute
</Button>
</Form>
)}
</div>
<div>
<span style={{ color: 'red' }}>{executeMsgErr}</span>
Expand Down
36 changes: 34 additions & 2 deletions src/components/injective/Instantiate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import Form from '@rjsf/core';
import validator from '@rjsf/validator-ajv8';
import { Contract } from './Contract';
import { useWalletStore } from './WalletContextProvider';
import axios from 'axios';
import { INJECTIVE_COMPILER_CONSUMER_API_ENDPOINT } from '../../const/endpoint';
import { BigNumberInBase } from '@injectivelabs/utils';

interface InterfaceProps {
compileTarget: string;
codeID: string;
client: any;
setCodeID: Dispatch<React.SetStateAction<string>>;
Expand All @@ -37,6 +41,7 @@ export interface InjectiveDeployHistoryCreateDto {
}

export const Instantiate: React.FunctionComponent<InterfaceProps> = ({
compileTarget,
client,
codeID,
setCodeID,
Expand Down Expand Up @@ -91,10 +96,13 @@ export const Instantiate: React.FunctionComponent<InterfaceProps> = ({
resolve(contractAddress);
});
};

const instantiateKeplr = async () => {
try {
const funds = fund ? { denom: 'inj', amount: fund.toString() } : undefined;
const funds =
fund === 0
? undefined
: { denom: 'inj', amount: new BigNumberInBase(fund).toWei().toFixed() };
const msg = MsgInstantiateContract.fromJSON({
sender: injectiveAddress,
admin: immutableChecked ? '' : injectiveAddress,
Expand All @@ -105,6 +113,29 @@ export const Instantiate: React.FunctionComponent<InterfaceProps> = ({
});
const txResult = await injectiveBroadcastMsg(msg, injectiveAddress);
const contract = await getContract(txResult!.txHash);
if (contract) {
const injectiveDeployHistoryCreateDto: InjectiveDeployHistoryCreateDto = {
chainId: chainId,
account: injectiveAddress,
codeId: codeID,
contractAddress: contract as string,
compileTimestamp: Number(timestamp),
deployTimestamp: null,
txHash: txResult!.txHash,
checksum: checksum,
isSrcUploaded: true,
createdBy: 'REMIX',
};
try {
const res = await axios.post(
INJECTIVE_COMPILER_CONSUMER_API_ENDPOINT + '/injective/deploy-histories',
injectiveDeployHistoryCreateDto,
);
log.info(`deploy-histories api res`, res);
} catch (e) {
log.error(`deploy-histories api error`);
}
}
log.debug('Contract address:', contract);
setContractAddress(contract as any);
setDisabled(true);
Expand Down Expand Up @@ -318,6 +349,7 @@ export const Instantiate: React.FunctionComponent<InterfaceProps> = ({
</ReactForm.Group>
{contractAddress ? (
<Contract
compileTarget={compileTarget}
contractAddress={contractAddress || ''}
client={client}
fund={fund}
Expand Down
1 change: 0 additions & 1 deletion src/components/injective/Project.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ export const Project: React.FunctionComponent<InterfaceProps> = ({ client }) =>
setFileName={setFileName}
compileTarget={compileTarget}
client={client}
reset={reset}
/>
</div>
);
Expand Down
13 changes: 11 additions & 2 deletions src/components/injective/StoreCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface InterfaceProps {
}

export const StoreCode: React.FunctionComponent<InterfaceProps> = ({
compileTarget,
wasm,
checksum,
client,
Expand Down Expand Up @@ -85,8 +86,9 @@ export const StoreCode: React.FunctionComponent<InterfaceProps> = ({
if (txResult?.txHash) {
await waitGetCodeID(txResult!.txHash);
} else {
throw new Error('Error while broadcasting');
throw new Error('Error while broadcasting. Please Check your wallet is locked');
}

} catch (error: any) {
await client.terminal.log({ type: 'error', value: error?.message?.toString() });
}
Expand Down Expand Up @@ -204,7 +206,13 @@ export const StoreCode: React.FunctionComponent<InterfaceProps> = ({
onChange={(e) => setFund(Number(e.target.value))}
onBlur={handleBlur}
/>
<Form.Control type="text" placeholder="" value={'inj'} size="sm" readOnly />
<Form.Control
type="text"
placeholder=""
value={compileTarget === 'injective/atomic-order-example' ? 'USDT' : 'inj'}
size="sm"
readOnly
/>
</InputGroup>
</Form>
{/*<Form>*/}
Expand Down Expand Up @@ -236,6 +244,7 @@ export const StoreCode: React.FunctionComponent<InterfaceProps> = ({
{codeID && (
<>
<Instantiate
compileTarget={compileTarget}
client={client}
codeID={codeID || ''}
setCodeID={setCodeID}
Expand Down
2 changes: 1 addition & 1 deletion src/const/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const INJECTIVE_COMPILER_CONSUMER_ENDPOINT = INJECTIVE_COMPILER_CONSUMER_
const INJECTIVE_COMPILER_CONSUMER_API_ENDPOINT_POOL = {
local: 'http://localhost:8000',
dev: 'https://dev.compiler.welldonestudio.io',
prod: 'https://prod.compiler.welldonestudio.io/',
prod: 'https://prod.compiler.welldonestudio.io',
};

export const INJECTIVE_COMPILER_CONSUMER_API_ENDPOINT =
Expand Down

0 comments on commit 5145ba5

Please sign in to comment.