Skip to content

Commit

Permalink
support transfer wasm vm oep4
Browse files Browse the repository at this point in the history
  • Loading branch information
MickWang committed Nov 13, 2019
1 parent 7db1999 commit e915a42
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cyano-wallet",
"version": "0.7.17",
"version": "0.7.19",
"private": true,
"scripts": {
"lint": "tslint -p .",
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Cyano wallet",
"author": "Matus Zamborsky <[email protected]>",
"description": "Cyano wallet - an Ontology wallet",
"version": "0.7.18",
"version": "0.7.19",

"browser_action": {
"default_title": "Open the wallet"
Expand Down
58 changes: 36 additions & 22 deletions src/background/api/tokenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ export async function getOEP4Token(contract: string): Promise<OEP4Token> {
let nameResponse;
let symbolResponse;
let decimalsResponse;
if (vmType === 'NEOVM') {
nameResponse = await client.sendRawTransaction(builder.queryName().serialize(), true);
symbolResponse = await client.sendRawTransaction(builder.querySymbol().serialize(), true);
decimalsResponse = await client.sendRawTransaction(builder.queryDecimals().serialize(), true);
} else if (vmType === 'WASMVM') {
debugger
if (vmType === 'WASMVM') {
const tx1 = TransactionBuilder.makeWasmVmInvokeTransaction(
'name',
[],
Expand All @@ -58,13 +53,17 @@ export async function getOEP4Token(contract: string): Promise<OEP4Token> {
);
symbolResponse = await client.sendRawTransaction(tx2.serialize(), true);
const tx3 = TransactionBuilder.makeWasmVmInvokeTransaction(
'decimal',
'decimals',
[],
new Address(contractAddr),
gasPrice,
gasLimit,
);
decimalsResponse = await client.sendRawTransaction(tx3.serialize(), true);
} else {
nameResponse = await client.sendRawTransaction(builder.queryName().serialize(), true);
symbolResponse = await client.sendRawTransaction(builder.querySymbol().serialize(), true);
decimalsResponse = await client.sendRawTransaction(builder.queryDecimals().serialize(), true);
}

return {
Expand Down Expand Up @@ -93,11 +92,7 @@ export async function getTokenBalance(contract: string, address: Address, vmType
contract = utils.reverseHex(contract);
const client = getClient();
let response;
if (vmType === 'NEOVM') {
const builder = new Oep4TxBuilder(new Address(contract));
const tx = builder.queryBalanceOf(address);
response = await client.sendRawTransaction(tx.serialize(), true);
} else {
if (vmType === 'WASMVM') {
const params = [new Parameter('param1', ParameterType.Address, address)];
const tx = TransactionBuilder.makeWasmVmInvokeTransaction(
'balanceOf',
Expand All @@ -107,6 +102,10 @@ export async function getTokenBalance(contract: string, address: Address, vmType
gasLimit,
);
response = await client.sendRawTransaction(tx.serialize(), true);
} else {
const builder = new Oep4TxBuilder(new Address(contract));
const tx = builder.queryBalanceOf(address);
response = await client.sendRawTransaction(tx.serialize(), true);
}

return Long.fromString(utils.reverseHex(response.Result.Result), true, 16).toString();
Expand All @@ -120,7 +119,6 @@ export async function transferToken(request: TransferRequest, password: string)
if (token === undefined) {
throw new Error('OEP-4 token not found.');
}

const contract = utils.reverseHex(token.contract);
const builder = new Oep4TxBuilder(new Address(contract));

Expand All @@ -129,15 +127,31 @@ export async function transferToken(request: TransferRequest, password: string)

const to = new Address(request.recipient);
const amount = String(request.amount);

const tx = builder.makeTransferTx(
from,
to,
encodeAmount(amount, token.decimals),
'500',
`${CONST.DEFAULT_GAS_LIMIT}`,
from,
);
let tx;
if (token.vmType === 'WASMVM') {
const params = [
new Parameter('from', ParameterType.Address, from),
new Parameter('to', ParameterType.Address, to),
new Parameter('amount', ParameterType.Long, encodeAmount(amount, token.decimals))
];
tx = TransactionBuilder.makeWasmVmInvokeTransaction(
'transfer',
params,
new Address(contract),
gasPrice,
`${CONST.DEFAULT_GAS_LIMIT}`,
from
);
} else {
tx = builder.makeTransferTx(
from,
to,
encodeAmount(amount, token.decimals),
'500',
`${CONST.DEFAULT_GAS_LIMIT}`,
from,
);
}

await TransactionBuilder.signTransactionAsync(tx, privateKey);

Expand Down
1 change: 1 addition & 0 deletions src/background/redux/settingsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const settingsReducer: Reducer<SettingsState> = (state = defaultState, ac
name: action.name,
specification: action.specification,
symbol: action.symbol,
vmType: action.vmType
},
],
};
Expand Down
1 change: 1 addition & 0 deletions src/popup/pages/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function prepareTokenAmounts(tokens: TokenState[] = [], items: TokenAmountState[
decimals: token.decimals,
name: token.name,
symbol: token.symbol,
vmType: token.vmType,
};
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/popup/pages/send/sendView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const SendView: React.SFC<Props> = (props) => (
input={{ ...t.input, value: t.input.value }}
error={t.meta.touched && t.meta.invalid}
disabled={props.locked || get(formProps.values, 'asset') === undefined}
action={<Button type="button" onClick={() => props.handleMax(formProps)} content="MAX" />}
action={get(formProps.values, 'asset') === 'ONG' || get(formProps.values, 'asset') === 'ONT'? <Button type="button" onClick={() => props.handleMax(formProps)} content="MAX" /> : undefined}
/>
)}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/popup/pages/tokenSettingsAdd/tokenSettingsAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const enhancer = (Component: React.ComponentType<Props>) => (props: RouteCompone
const token = await manager.getOEP4Token(contract);

// todo: proper spec
await actions.addToken(contract, token.name, token.symbol, token.decimals, 'OEP-4');
await actions.addToken(contract, token.name, token.symbol, token.decimals, 'OEP-4', token.vmType);

await actions.finishLoading();

Expand Down
16 changes: 12 additions & 4 deletions src/redux/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export interface TokenState {
name: string;
symbol: string;
decimals: number;
specification: 'OEP-4';
vmType: VmType
specification: 'OEP-4';
vmType: VmType;
}

export interface TrustedSc {
Expand Down Expand Up @@ -73,13 +73,21 @@ export const setSettings = (
trustedScs,
});

export const addToken = (contract: string, name: string, symbol: string, decimals: number, specification: 'OEP-4') => ({
export const addToken = (
contract: string,
name: string,
symbol: string,
decimals: number,
specification: 'OEP-4',
vmType: VmType,
) => ({
type: ADD_TOKEN,
contract,
name,
symbol,
decimals,
specification
specification,
vmType,
});

export const delToken = (contract: string) => ({ type: DEL_TOKEN, contract });
Expand Down

0 comments on commit e915a42

Please sign in to comment.