Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding generic signCustomTransaction function needed on EVMAuthenticators #628

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/antelope/types/EvmContractData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,5 @@ export interface EvmContractFactoryData {
timestamp?: string;
manager?: EvmContractManagerI;
}

export type EvmFunctionParam = string | number | boolean;
1 change: 1 addition & 0 deletions src/antelope/types/EvmTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,4 @@ export interface EvmTransfer {
timestamp: number; // integer representing ms from epoch
id?: string; // id of the NFT transferred (ERC721 or ERC1155 only)
}

3 changes: 2 additions & 1 deletion src/antelope/wallets/authenticators/EVMAuthenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useChainStore } from 'src/antelope/stores/chain';
import { useEVMStore } from 'src/antelope/stores/evm';
import { createTraceFunction, isTracingAll, useFeedbackStore } from 'src/antelope/stores/feedback';
import { usePlatformStore } from 'src/antelope/stores/platform';
import { AntelopeError, EvmTransactionResponse, ExceptionError, TokenClass, addressString } from 'src/antelope/types';
import { AntelopeError, EvmABI, EvmFunctionParam, EvmTransactionResponse, ExceptionError, TokenClass, addressString } from 'src/antelope/types';

export abstract class EVMAuthenticator {

Expand All @@ -25,6 +25,7 @@ export abstract class EVMAuthenticator {
abstract logout(): Promise<void>;
abstract getSystemTokenBalance(address: addressString | string): Promise<BigNumber>;
abstract getERC20TokenBalance(address: addressString | string, tokenAddress: addressString | string): Promise<BigNumber>;
abstract signCustomTransaction(contract: string, abi: EvmABI, parameters: EvmFunctionParam[], value?: BigNumber): Promise<EvmTransactionResponse | WriteContractResult>;
abstract transferTokens(token: TokenClass, amount: BigNumber, to: addressString | string): Promise<EvmTransactionResponse | SendTransactionResult | WriteContractResult>;
abstract prepareTokenForTransfer(token: TokenClass | null, amount: BigNumber, to: string): Promise<void>;
abstract wrapSystemToken(amount: BigNumber): Promise<EvmTransactionResponse | WriteContractResult>;
Expand Down
18 changes: 18 additions & 0 deletions src/antelope/wallets/authenticators/InjectedProviderAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
AntelopeError,
ERC20_TYPE,
EthereumProvider,
EvmABI,
EvmFunctionParam,
EvmTransactionResponse,
TokenClass,
addressString,
Expand Down Expand Up @@ -72,6 +74,22 @@ export abstract class InjectedProviderAuth extends EVMAuthenticator {

// EVMAuthenticator API ----------------------------------------------------------

async signCustomTransaction(contract: string, abi: EvmABI, parameters: EvmFunctionParam[], value?: BigNumber): Promise<EvmTransactionResponse> {
this.trace('signCustomTransaction', contract, [abi], parameters, value?.toString());
// TODO: implement this method and remove this comment
// https://github.com/telosnetwork/telos-wallet/issues/625

const method = abi[0].name;
if (abi.length > 1) {
console.warn(
`signCustomTransaction: abi contains more than one function,
we asume the first one (${method}) is the one to be called`,
);
}

return Promise.resolve({} as EvmTransactionResponse);
}

async wrapSystemToken(amount: BigNumber): Promise<EvmTransactionResponse> {
this.trace('wrapSystemToken', amount.toString());
const chainSettings = this.getChainSettings();
Expand Down
191 changes: 84 additions & 107 deletions src/antelope/wallets/authenticators/OreIdAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { AuthProvider, ChainNetwork, OreId, OreIdOptions, JSONObject, UserChainA
import { BigNumber, ethers } from 'ethers';
import { WebPopup } from 'oreid-webpopup';
import {
EvmABI,
EvmFunctionParam,
erc20Abi,
escrowAbiWithdraw,
stlosAbiDeposit,
Expand Down Expand Up @@ -209,43 +211,6 @@ export class OreIdAuth extends EVMAuthenticator {
}
}

async transferTokens(token: TokenClass, amount: ethers.BigNumber, to: addressString): Promise<EvmTransactionResponse> {
this.trace('transferTokens', token, amount, to);
this.checkIntegrity();

// prepare variables
const from = this.getAccountAddress();
const value = amount.toHexString();
const abi = erc20Abi;

// transaction body: transfer system tokens
const systemTransfer = {
from,
to,
value,
};

// transaction body: transfer erc20 tokens
const erc20Transfer = {
from,
to: token.address,
'contract': {
abi,
'parameters': [to, value],
'method': 'transfer',
},
} as unknown as JSONObject;

let transactionBody = null as unknown as JSONObject;
if (token.isSystem) {
transactionBody = systemTransfer;
} else {
transactionBody = erc20Transfer;
}

return this.performOreIdTransaction(from, transactionBody);
}

async prepareTokenForTransfer(token: TokenClass | null, amount: ethers.BigNumber, to: string): Promise<void> {
this.trace('prepareTokenForTransfer', [token], amount, to);
}
Expand Down Expand Up @@ -291,30 +256,54 @@ export class OreIdAuth extends EVMAuthenticator {
} as EvmTransactionResponse;
}

async wrapSystemToken(amount: BigNumber): Promise<EvmTransactionResponse> {
this.trace('wrapSystemToken', amount);
async signCustomTransaction(contract: string, abi: EvmABI, parameters: EvmFunctionParam[], value?: BigNumber): Promise<EvmTransactionResponse> {
this.trace('signCustomTransaction', contract, [abi], parameters, value?.toString());
this.checkIntegrity();

// prepare variables
const chainSettings = this.getChainSettings();
const wrappedSystemTokenContractAddress = chainSettings.getWrappedSystemToken().address as addressString;
const from = this.getAccountAddress();
const value = amount.toHexString();
const abi = wtlosAbiDeposit;
const method = abi[0].name;

// if the developer is passing more than one function in the abi
// we must warn we asume the first one is the one to be called
if (abi.length > 1) {
console.warn(
`signCustomTransaction: abi contains more than one function,
we asume the first one (${method}) is the one to be called`,
);
}

// transaction body: wrap system token
const wrapTransaction = {
const transactionBody = {
from,
to: wrappedSystemTokenContractAddress,
value,
to: contract,
'contract': {
abi,
'parameters': [],
'method': 'deposit',
parameters,
'method': abi[0].name,
},
} as unknown as JSONObject;

return this.performOreIdTransaction(from, wrapTransaction);
if (value) {
transactionBody.value = value.toHexString();
}

return this.performOreIdTransaction(from, transactionBody);
}

async wrapSystemToken(amount: BigNumber): Promise<EvmTransactionResponse> {
this.trace('wrapSystemToken', amount);
this.checkIntegrity();

// prepare variables
const chainSettings = this.getChainSettings();
const wrappedSystemTokenContractAddress = chainSettings.getWrappedSystemToken().address as addressString;

return this.signCustomTransaction(
wrappedSystemTokenContractAddress,
wtlosAbiDeposit,
[],
amount,
);
}

async unwrapSystemToken(amount: BigNumber): Promise<EvmTransactionResponse> {
Expand All @@ -324,22 +313,13 @@ export class OreIdAuth extends EVMAuthenticator {
// prepare variables
const chainSettings = this.getChainSettings();
const wrappedSystemTokenContractAddress = chainSettings.getWrappedSystemToken().address as addressString;
const from = this.getAccountAddress();
const value = amount.toHexString();
const abi = wtlosAbiWithdraw;

// transaction body: unwrap system token
const unwrapTransaction = {
from,
to: wrappedSystemTokenContractAddress,
'contract': {
abi,
'parameters': [value],
'method': 'withdraw',
},
} as unknown as JSONObject;

return this.performOreIdTransaction(from, unwrapTransaction);
return this.signCustomTransaction(
wrappedSystemTokenContractAddress,
wtlosAbiWithdraw,
[value],
);
}

async stakeSystemTokens(amount: BigNumber): Promise<EvmTransactionResponse> {
Expand All @@ -349,23 +329,13 @@ export class OreIdAuth extends EVMAuthenticator {
// prepare variables
const chainSettings = this.getChainSettings();
const stakedSystemTokenContractAddress = chainSettings.getStakedSystemToken().address as addressString;
const from = this.getAccountAddress();
const value = amount.toHexString();
const abi = stlosAbiDeposit;

// transaction body: stake system token
const stakeTransaction = {
from,
to: stakedSystemTokenContractAddress,
value,
'contract': {
abi,
'parameters': [],
'method': stlosAbiDeposit[0].name,
},
} as unknown as JSONObject;

return this.performOreIdTransaction(from, stakeTransaction);
return this.signCustomTransaction(
stakedSystemTokenContractAddress,
stlosAbiDeposit,
[],
amount,
);
}

async unstakeSystemTokens(amount: BigNumber): Promise<EvmTransactionResponse> {
Expand All @@ -375,22 +345,14 @@ export class OreIdAuth extends EVMAuthenticator {
// prepare variables
const chainSettings = this.getChainSettings();
const stakedSystemTokenContractAddress = chainSettings.getStakedSystemToken().address as addressString;
const from = this.getAccountAddress();
const value = amount.toHexString();
const abi = stlosAbiWithdraw;

// transaction body: unstake system token
const unstakeTransaction = {
from,
to: stakedSystemTokenContractAddress,
'contract': {
abi,
'parameters': [value, from, from],
'method': 'withdraw',
},
} as unknown as JSONObject;
const from = this.getAccountAddress();

return this.performOreIdTransaction(from, unstakeTransaction);
return this.signCustomTransaction(
stakedSystemTokenContractAddress,
stlosAbiWithdraw,
[value, from, from],
);
}

async withdrawUnstakedTokens() : Promise<EvmTransactionResponse> {
Expand All @@ -400,21 +362,36 @@ export class OreIdAuth extends EVMAuthenticator {
// prepare variables
const chainSettings = this.getChainSettings();
const escrowContractAddress = chainSettings.getEscrowContractAddress();
const from = this.getAccountAddress();
const abi = escrowAbiWithdraw;

// transaction body: withdraw staked tokens
const withdrawTransaction = {
from,
to: escrowContractAddress,
'contract': {
abi,
'parameters': [],
'method': 'withdraw',
},
} as unknown as JSONObject;
return this.signCustomTransaction(
escrowContractAddress,
escrowAbiWithdraw,
[],
);
}

return this.performOreIdTransaction(from, withdrawTransaction);
async transferTokens(token: TokenClass, amount: ethers.BigNumber, to: addressString): Promise<EvmTransactionResponse> {
this.trace('transferTokens', token, amount, to);
this.checkIntegrity();

// prepare variables
const from = this.getAccountAddress();
const value = amount.toHexString();
const transferAbi = erc20Abi.filter(abi => abi.name === 'transfer');

if (token.isSystem) {
return this.performOreIdTransaction(from, {
from,
to,
value,
});
} else {
return this.signCustomTransaction(
token.address,
transferAbi,
[to, value],
);
}
}

async isConnectedTo(chainId: string): Promise<boolean> {
Expand Down
17 changes: 17 additions & 0 deletions src/antelope/wallets/authenticators/WalletConnectAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { usePlatformStore } from 'src/antelope/stores/platform';
import {
AntelopeError,
EvmABI,
EvmFunctionParam,
TokenClass,
addressString,
escrowAbiWithdraw,
Expand Down Expand Up @@ -242,6 +243,22 @@ export class WalletConnectAuth extends EVMAuthenticator {
return BigNumber.from(balance);
}

async signCustomTransaction(contract: string, abi: EvmABI, parameters: EvmFunctionParam[], value?: BigNumber): Promise<SendTransactionResult | WriteContractResult> {
this.trace('signCustomTransaction', contract, [abi], parameters, value?.toString());
// TODO: implement this method and remove this comment
// https://github.com/telosnetwork/telos-wallet/issues/626

const method = abi[0].name;
if (abi.length > 1) {
console.warn(
`signCustomTransaction: abi contains more than one function,
we asume the first one (${method}) is the one to be called`,
);
}

return Promise.resolve({} as SendTransactionResult);
}

async transferTokens(token: TokenClass, amount: BigNumber, to: addressString): Promise<SendTransactionResult | WriteContractResult> {
this.trace('transferTokens', token, amount, to);
if (!this.sendConfig) {
Expand Down