Skip to content

Commit 426b6fd

Browse files
committed
feat: create1 address calc works and autorecognize contract
1 parent b49629e commit 426b6fd

File tree

13 files changed

+100
-178
lines changed

13 files changed

+100
-178
lines changed

src/services/chain/contract.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import {
1111
InstantiateData,
1212
SubmittableExtrinsic,
1313
} from 'types';
14-
import { stringToU8a, compactAddLength, u8aToU8a } from '@polkadot/util';
14+
import { stringToU8a, compactAddLength, u8aToU8a, hexToU8a } from '@polkadot/util';
1515
import { ISubmittableResult } from '@polkadot/types/types';
1616
import { BlueprintSubmittableResult, Contract } from '@polkadot/api-contract/base';
1717
import { contractsAbi } from '@polkadot/types/interfaces/definitions';
18+
import { fromEthAddress, isEthDerived, toEthAddress } from 'ui/hooks';
1819

1920
export function createInstantiateTx(
2021
api: ApiPromise,
@@ -42,29 +43,12 @@ export function createInstantiateTx(
4243
value,
4344
};
4445

45-
const parsed_wasm = compactAddLength(wasm.slice(0));
4646
const codeOrBlueprint = codeHash
4747
? new BlueprintPromise(api, metadata, codeHash)
4848
: new CodePromise(api, metadata, wasm && wasm);
49-
// const transformed = transformUserInput(api.registry, constructor.args, argValues);
5049

5150
const transformed = transformUserInput(api.registry, constructor.args, argValues);
5251

53-
const tmp = constructor.toU8a(transformed);
54-
console.log('constructor.toU8a', tmp);
55-
console.log('constructor.', transformed);
56-
//
57-
// const tx = api.tx.revive.instantiateWithCode(
58-
// value!,
59-
// gasLimit!,
60-
// storageDepositLimit!,
61-
// parsed_wasm,
62-
// data,
63-
// salt,
64-
// );
65-
66-
// return tx;
67-
6852
return constructor.args.length > 0
6953
? codeOrBlueprint.tx[constructor.method](options, ...transformed)
7054
: codeOrBlueprint.tx[constructor.method](options);
@@ -74,8 +58,9 @@ export function createInstantiateTx(
7458
}
7559

7660
export async function getContractInfo(api: ApiPromise, address: string) {
61+
// TODO: fix isValidAddress
7762
if (isValidAddress(address) || true) {
78-
return (await api.query.revive.contractInfoOf(address.substring(0, 42))).unwrapOr(null);
63+
return (await api.query.revive.contractInfoOf(address)).unwrapOr(null);
7964
}
8065
}
8166

src/services/db/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface CodeBundleDocument {
1414
export interface ContractDocument extends CodeBundleDocument {
1515
abi: Record<string, unknown>;
1616
address: string;
17+
dotAddress: string;
1718
external?: boolean;
1819
}
1920

src/types/ui/hooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ export interface UIContract extends Pick<ContractPromise, 'abi' | 'tx'> {
4949
type: 'added' | 'instantiated';
5050
codeHash: string;
5151
address: string;
52+
dotAddress: string;
5253
}

src/ui/components/common/HeaderButtons.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface Props {
1414
contract: UIContract;
1515
}
1616

17-
export function HeaderButtons({ contract: { address, codeHash } }: Props) {
17+
export function HeaderButtons({ contract: { address, dotAddress, codeHash } }: Props) {
1818
const { api } = useApi();
1919
const { db } = useDatabase();
2020
const [isOnChain, setIsOnChain] = useState(true);
@@ -61,7 +61,7 @@ export function HeaderButtons({ contract: { address, codeHash } }: Props) {
6161
}}
6262
title="Forget contract"
6363
>
64-
<TrashIcon className="w-4 dark:text-gray-500 " />
64+
<TrashIcon className="w-4 dark:text-gray-500" />
6565
</button>
6666
</div>
6767
<ForgetContractModal confirm={forgetContract} isOpen={isOpen} setIsOpen={setIsOpen} />

src/ui/components/contract/ContractRow.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import { ContractDocument } from 'types';
99
import { useApi } from 'ui/contexts';
1010
import { displayDate, truncate } from 'lib/util';
1111
import { getContractInfo } from 'services/chain';
12+
import { fromEthAddress, toEthAddress } from 'ui/hooks';
1213

1314
interface Props {
1415
contract: ContractDocument;
1516
}
1617

17-
export function ContractRow({ contract: { address, name, date } }: Props) {
18+
export function ContractRow({ contract: { address, dotAddress, name, date } }: Props) {
1819
const { api } = useApi();
1920
const [isOnChain, setIsOnChain] = useState(true);
2021

@@ -46,7 +47,7 @@ export function ContractRow({ contract: { address, name, date } }: Props) {
4647
<div className="text-gray-500 dark:text-gray-400">{displayDate(date)}</div>
4748

4849
<div className="justify-self-end font-mono text-gray-500 dark:text-gray-400">
49-
<ObservedBalance address={address} />
50+
<ObservedBalance address={dotAddress} />
5051
</div>
5152
</Link>
5253
);

src/ui/components/contract/Interact.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const InteractTab = ({
4141
abi: { registry },
4242
tx,
4343
address,
44+
dotAddress,
4445
},
4546
}: Props) => {
4647
const { accounts, api } = useApi();
@@ -76,7 +77,7 @@ export const InteractTab = ({
7677
const params: Parameters<typeof api.call.reviveApi.call> = useMemo(() => {
7778
return [
7879
accountId,
79-
address.substring(0, 42),
80+
address,
8081
message?.isPayable
8182
? api.registry.createType('Balance', value)
8283
: api.registry.createType('Balance', BN_ZERO),
@@ -87,6 +88,7 @@ export const InteractTab = ({
8788
}, [
8889
accountId,
8990
address,
91+
dotAddress,
9092
api.registry,
9193
inputData,
9294
isCustom,
@@ -185,17 +187,10 @@ export const InteractTab = ({
185187

186188
const isValid = (result: SubmittableResult) => !result.isError && !result.dispatchError;
187189

188-
console.log('message');
189-
console.log(message);
190-
console.log('tx');
191-
console.log(tx);
192-
console.log(message.method);
193-
console.log(transformUserInput(registry, message.args, argValues));
194190
const extrinsic = tx[message.method](
195191
options,
196192
...transformUserInput(registry, message.args, argValues),
197193
);
198-
console.log(extrinsic);
199194

200195
newId.current = queue({
201196
extrinsic,

src/ui/components/instantiate/Step2.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@ import {
2222
useToggle,
2323
useStorageDepositLimit,
2424
useBalance,
25+
create2,
26+
toEthAddress,
27+
create1,
2528
} from 'ui/hooks';
2629
import { AbiMessage, Balance, OrFalsy } from 'types';
27-
import { decodeStorageDeposit, getGasLimit, getStorageDepositLimit } from 'lib/callOptions';
30+
import {
31+
decodeStorageDeposit,
32+
getGasLimit,
33+
getStorageDepositLimit,
34+
transformUserInput,
35+
} from 'lib/callOptions';
2836
import { BN_ZERO } from 'lib/bn';
2937
import { hasRevertFlag } from 'lib/hasRevertFlag';
38+
import { hexToU8a, stringToU8a } from '@polkadot/util';
39+
import { decodeAddress } from '@polkadot/keyring';
3040

3141
function validateSalt(value: OrFalsy<string>) {
3242
if (!!value && value.length === 66) {
@@ -63,20 +73,17 @@ export function Step2() {
6373
}, [metadata, setConstructorIndex]);
6474

6575
const [isUsingSalt, toggleIsUsingSalt] = useToggle(true);
76+
const code = metadata?.json.source.contract_binary;
6677

67-
const wasm = metadata?.info.source.wasm;
68-
69-
const params: Parameters<typeof api.call.contractsApi.instantiate> = useMemo(() => {
78+
const params: Parameters<typeof api.call.reviveApi.instantiate> = useMemo(() => {
7079
return [
7180
accountId,
7281
deployConstructor?.isPayable
7382
? api.registry.createType('Balance', value)
7483
: api.registry.createType('Balance', BN_ZERO),
7584
getGasLimit(isCustom, refTime.limit, proofSize.limit, api.registry),
7685
getStorageDepositLimit(storageDepositLimit.isActive, storageDepositLimit.value, api.registry),
77-
codeHashUrlParam
78-
? { Existing: codeHashUrlParam }
79-
: { Upload: metadata?.json.source.contract_binary }, // TODO: update type
86+
codeHashUrlParam ? { Existing: codeHashUrlParam } : { Upload: code }, // TODO: update type
8087
inputData ?? '',
8188
isUsingSalt ? salt.value : '',
8289
];
@@ -91,7 +98,7 @@ export function Step2() {
9198
storageDepositLimit.isActive,
9299
storageDepositLimit.value,
93100
codeHashUrlParam,
94-
metadata?.info.source.wasm,
101+
metadata?.json.source.contract_binary,
95102
inputData,
96103
isUsingSalt,
97104
salt.value,

src/ui/components/instantiate/Step3.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import { printBN } from 'lib/bn';
99
import { createInstantiateTx } from 'services/chain';
1010
import { SubmittableResult } from 'types';
1111
import { useApi, useInstantiate, useTransactions } from 'ui/contexts';
12-
import { useNewContract } from 'ui/hooks';
13-
import { stringToU8a } from '@polkadot/util';
12+
import { create2, toEthAddress, useNewContract } from 'ui/hooks';
13+
import { hexToU8a, stringToU8a } from '@polkadot/util';
1414
import { transformUserInput } from 'lib/callOptions';
15+
import { decodeAddress } from '@polkadot/keyring';
1516

1617
export function Step3() {
1718
const { codeHash: codeHashUrlParam } = useParams<{ codeHash: string }>();
@@ -30,13 +31,13 @@ export function Step3() {
3031
const isValid = (result: SubmittableResult) => !result.isError && !result.dispatchError;
3132

3233
if (data.accountId) {
34+
const constructor = metadata.findConstructor(constructorIndex);
35+
const transformed = transformUserInput(api.registry, constructor.args, data.argValues);
36+
const inputData = constructor.toU8a(transformed).slice(1); // exclude the first byte (the length byte)
37+
3338
const tx = createInstantiateTx(api, data);
3439

3540
if (!txId) {
36-
const constructor = metadata.findConstructor(constructorIndex);
37-
const transformed = transformUserInput(api.registry, constructor.args, data.argValues);
38-
const inputData = constructor.toU8a(transformed).slice(1); // exclude the first byte (the length byte)
39-
4041
const newId = queue({
4142
extrinsic: tx,
4243
accountId: data.accountId,

0 commit comments

Comments
 (0)