Skip to content

Commit

Permalink
Full coverage sequence provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Jul 26, 2023
1 parent 3b125a7 commit 02ee50f
Show file tree
Hide file tree
Showing 6 changed files with 418 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const tests = async () => {
assert.equal(provider.getChainId(), 31337, 'provider chainId is 31337')
})

await test('getAccounts', async () => {
await test('getAddress', async () => {
const address = wallet.getAddress()
assert.true(ethers.utils.isAddress(address), 'wallet address is valid')
})
Expand Down
4 changes: 0 additions & 4 deletions packages/provider/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,6 @@ export class SequenceClient {
return connectedSession.networks
}

getAccounts(): string[] {
return [this.getAddress()]
}

async signMessage(
message: ethers.BytesLike,
options?: OptionalEIP6492 & OptionalChainId
Expand Down
17 changes: 7 additions & 10 deletions packages/provider/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ISequenceProvider {

getNetworks(): Promise<NetworkConfig[]>
getChainId(): number

setDefaultChainId(chainId: ChainIdLike): void

isOpened(): boolean
Expand Down Expand Up @@ -98,7 +99,7 @@ export class SequenceProvider extends ethers.providers.BaseProvider implements I
}

listAccounts(): string[] {
return this.client.getAccounts()
return [this.client.getAddress()]
}

// @deprecated use getSigner() instead
Expand Down Expand Up @@ -228,13 +229,13 @@ export class SequenceProvider extends ethers.providers.BaseProvider implements I
}

if (method === 'eth_accounts') {
return this.client.getAccounts()
return [this.client.getAddress()]
}

if (method === 'wallet_switchEthereumChain') {
const args = params[0] as { chainId: string } | number | string
const chainId = normalizeChainId(args)
return this.client.setDefaultChainId(chainId)
return this.setDefaultChainId(chainId)
}

// Usually these methods aren't used by calling the provider
Expand All @@ -253,7 +254,7 @@ export class SequenceProvider extends ethers.providers.BaseProvider implements I
) {
// We pass the chainId to the client, if we don't pass one
// the client will use its own default chainId
return this.client.send({ method, params })
return this.client.send({ method, params }, this.getChainId())
}

// Forward call to the corresponding provider
Expand Down Expand Up @@ -513,12 +514,8 @@ export class SingleNetworkSequenceProvider extends SequenceProvider {
return super.getSigner(this._useChainId(chainId))
}

async perform(method: string, params: any): Promise<any> {
if (method === 'wallet_switchEthereumChain') {
throw new Error('This provider only supports a single network; use the parent provider to switch networks.')
}

return super.perform(method, params)
setDefaultChainId(_chainId: ChainIdLike): void {
throw new Error(`This provider only supports the network ${this.chainId}; use the parent provider to switch networks.`)
}

static is(cand: any): cand is SingleNetworkSequenceProvider {
Expand Down
8 changes: 2 additions & 6 deletions packages/provider/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export interface ISequenceSigner extends ethers.Signer {
ethers.utils.Deferrable<commons.transaction.Transaction>[] |
ethers.utils.Deferrable<commons.transaction.Transaction>
),
options?: {
chainId?: ChainIdLike,
}
options?: OptionalChainIdLike
): Promise<commons.transaction.TransactionResponse>

utils: WalletUtils
Expand Down Expand Up @@ -139,9 +137,7 @@ export class SequenceSigner implements ISequenceSigner {
ethers.utils.Deferrable<ethers.providers.TransactionRequest>[] |
ethers.utils.Deferrable<ethers.providers.TransactionRequest>
),
options?: {
chainId?: ChainIdLike,
}
options?: OptionalChainIdLike
) {
const chainId = await this.useChainId(options?.chainId)
const resolved = await resolveArrayProperties(transaction)
Expand Down
9 changes: 0 additions & 9 deletions packages/provider/tests/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,24 +518,15 @@ describe('SequenceClient', () => {
const result1 = new Promise(() => client.getAddress())
await expect(result1).to.be.rejectedWith('Sequence session not connected')

const result2 = new Promise(() => client.getAccounts())
await expect(result2).to.be.rejectedWith('Sequence session not connected')

await client.connect({ app: 'This is a test' })

const result3 = client.getAddress()
expect(result3).to.equal(session.accountAddress)

const result4 = client.getAccounts()
expect(result4).to.deep.equal([session.accountAddress])

await client.disconnect()

const result5 = new Promise(() => client.getAddress())
await expect(result5).to.be.rejectedWith('Sequence session not connected')

const result6 = new Promise(() => client.getAccounts())
await expect(result6).to.be.rejectedWith('Sequence session not connected')
})

it('should call sign message', async () => {
Expand Down
Loading

0 comments on commit 02ee50f

Please sign in to comment.