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

Convert values coming in from WalletConnect #70

Merged
merged 5 commits into from
Dec 5, 2021
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
84 changes: 68 additions & 16 deletions src/lib/walletAdapters/WalletConnectAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,99 @@
import { Signer } from 'ethers'
import { BigNumber, Signer } from 'ethers'
import { getSigner } from '../../../testLib/utils'
import { WalletConnectAdapter } from './WalletConnectAdapter'

describe('Wallet Connect Adapter', () => {
let adapter: WalletConnectAdapter | null = null
let signer: Signer | null = null

describe('Wallet Connect Adapter', function (this: {
adapter: WalletConnectAdapter
signer: Signer
}) {
beforeEach(async () => {
signer = getSigner(9)

adapter = new WalletConnectAdapter(signer)
this.signer = getSigner(9)
this.adapter = new WalletConnectAdapter(this.signer)
})

test('send tx', async () => {
const from = await signer?.getAddress()
const from = await this.signer.getAddress()
const to = await getSigner(0).getAddress()
const chainId = await this.signer.getChainId()

const method = 'eth_sendTransaction'
const params = [
{
data: '',
data: '0x123456789a',
from,
to,
value: '0x3e8',
gas: '25000',
Copy link
Contributor

@ilanolkies ilanolkies Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add two tests? One for the payload with values and another for the defaults that trigger estimations

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new test and refactored the existing test to use spyon to get the parameters being sent to sendTransaction.

gasPrice: '60000000',
chainId,
},
]

const result = await adapter?.handleCall(method, params)
const mockIt = jest.fn()

expect(result).toBeDefined()
expect(result).toContain('0x')
jest.spyOn(this.signer, 'sendTransaction').mockImplementation((tx: any) => {
mockIt(tx)
return Promise.resolve(tx)
})

await this.adapter.handleCall(method, params)

expect(mockIt).toBeCalledWith({
data: params[0].data,
from,
to,
chainId,
nonce: undefined,
gasPrice: BigNumber.from(params[0].gasPrice),
gasLimit: BigNumber.from(params[0].gas),
value: BigNumber.from('0x3e8'),
})
})

test('default parameters are set', async () => {
const method = 'eth_sendTransaction'

const from = await this.signer.getAddress()
const to = await getSigner(0).getAddress()

const params = [
{
to,
from,
},
]

const mockIt = jest.fn()

jest.spyOn(this.signer, 'sendTransaction').mockImplementation((tx: any) => {
mockIt(tx)
return Promise.resolve(tx)
})

await this.adapter.handleCall(method, params)

expect(mockIt).toBeCalledWith({
to,
from,
data: '0x',
chainId: undefined,
gasLimit: undefined,
gasPrice: undefined,
nonce: undefined,
value: BigNumber.from(0),
})
})

// method not supported by ganache-cli
// eslint-disable-next-line jest/no-disabled-tests
test.skip('personal sign', async () => {
const from = await signer?.getAddress()
const from = await this.signer.getAddress()
const message = '0x68656c6c6f20776f726c6421'

const method = 'personal_sign'
const params = [message, from]

const result = await adapter?.handleCall(method, params)
const result = await this.adapter.handleCall(method, params)

expect(result).toBeDefined()
expect(result).toContain('0x')
Expand All @@ -56,7 +108,7 @@ describe('Wallet Connect Adapter', () => {
'{"domain":{"chainId":31,"name":"Ether Mail","version":"1"},"message":{"contents":"{ from: { name: \'Cow\', wallet: \'0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826\' }, to: { name: \'Bob\', wallet: \'0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB\' }, contents: \'Hello, Bob!\' }","from":"Diego","to":"Cesar"},"primaryType":"Mail","types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"}],"Mail":[{"name":"from","type":"string"},{"name":"to","type":"string"},{"name":"contents","type":"string"}]}}',
]

const result = await adapter?.handleCall(method, params)
const result = await this.adapter.handleCall(method, params)

expect(result).toBeDefined()
expect(result).toContain('0x')
Expand Down
17 changes: 12 additions & 5 deletions src/lib/walletAdapters/WalletConnectAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TransactionResponse } from '@ethersproject/providers'
import { Signer, constants, utils } from 'ethers'
import { TransactionRequest } from '@ethersproject/abstract-provider'
import { Signer, utils, BigNumber } from 'ethers'
import { RIFWallet } from '../core'
export class WalletConnectAdapter {
private resolvers: IResolver[]
Expand Down Expand Up @@ -41,13 +42,19 @@ class SendTransactionResolver implements IResolver {
async resolve(params: any[]) {
const payload = params.reduce((prev, curr) => ({ ...prev, ...curr }), {})

if (payload.data === '') {
// TODO: assign undefined once the RIFWallet changes are applied
payload.data = constants.HashZero
const formattedPayload: TransactionRequest = {
to: payload.to,
from: payload.from,
nonce: payload.nonce,
data: payload.data || '0x',
value: BigNumber.from(payload.value || 0),
chainId: payload.chainId,
gasLimit: payload.gas ? BigNumber.from(payload.gas) : undefined, // WC's gas to gasLimit
gasPrice: payload.gasPrice ? BigNumber.from(payload.gasPrice) : undefined,
}

return this.signer
.sendTransaction(payload)
.sendTransaction(formattedPayload)
.then((tx: TransactionResponse) => tx.hash)
}
}
Expand Down