-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
108 additions
and
119 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { TransactionDescription } from '@ethersproject/abi' | ||
import { Contract } from '@ethersproject/contracts' | ||
import { Web3Provider } from '@ethersproject/providers' | ||
import erc20Abi from '../externalData/balances/erc-20-abi' | ||
import type { Provider } from '../provider' | ||
|
||
function createWeb3ProviderWrapper (provider: Provider) { | ||
const wrappedSend = (request: { method: string, params?: any[] }, cb: (error: any, response: any) => void) => { | ||
provider.sendAsync({ | ||
method: request.method, | ||
params: request.params || [], | ||
id: 1, | ||
jsonrpc: '2.0', | ||
_origin: 'frame.eth' }, cb) | ||
} | ||
|
||
return { | ||
sendAsync: wrappedSend, | ||
send: wrappedSend | ||
} | ||
} | ||
|
||
export default class Erc20Contract { | ||
private contract: Contract | ||
|
||
constructor (address: Address, provider: Provider) { | ||
const web3Provider = new Web3Provider(createWeb3ProviderWrapper(provider)) | ||
this.contract = new Contract(address, erc20Abi, web3Provider) | ||
} | ||
|
||
static isApproval (data: TransactionDescription) { | ||
return ( | ||
data.name === 'approve' && | ||
data.functionFragment.inputs.length === 2 && | ||
(data.functionFragment.inputs[0].name || '').toLowerCase().endsWith('spender') && data.functionFragment.inputs[0].type === 'address' && | ||
(data.functionFragment.inputs[1].name || '').toLowerCase().endsWith('value') && data.functionFragment.inputs[1].type === 'uint256' | ||
) | ||
} | ||
|
||
decodeCallData (calldata: string) { | ||
try { | ||
return this.contract.interface.parseTransaction({ data: calldata }) | ||
} catch (e) { | ||
// call does not match ERC-20 interface | ||
} | ||
} | ||
|
||
encodeCallData (fn: string, params: any[]) { | ||
return this.contract.interface.encodeFunctionData(fn, params) | ||
} | ||
|
||
async getTokenData () { | ||
try { | ||
const calls = await Promise.all([ | ||
this.contract.decimals(), | ||
this.contract.name(), | ||
this.contract.symbol() | ||
]) | ||
|
||
return { | ||
decimals: calls[0], | ||
name: calls[1], | ||
symbol: calls[2] | ||
} | ||
} catch (e) { | ||
return { | ||
decimals: 0, | ||
name: '', | ||
symbol: '' | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters