-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix conflicts
- Loading branch information
Showing
18 changed files
with
303 additions
and
19 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import type { Account, Address, Balance } from '@/types' | ||
import type { Account, Address, Balance, Hash } from '@/types' | ||
|
||
export interface ProviderEntity { | ||
connect(): Promise<Account[]> | ||
getBalance(address: string): Promise<Balance> | ||
signMessage(address: string, message: string): Promise<string> | ||
getBalance(address: Address): Promise<Balance> | ||
signMessage(address: Address, message: string): Promise<string> | ||
signatureVerify(message: string, signature: string, address: Address): boolean | ||
joinPool(address: string, poolId: number, amount: number): Promise<string> | ||
joinPool(address: Address, poolId: number, amount: number): Promise<Hash> | ||
bondExtra(address: Address, amount: number): Promise<Hash> | ||
claim(address: Address): Promise<Hash> | ||
} |
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,46 @@ | ||
import { NoAvailableAccountsError, NoProviderAvailableError } from '@/errors'; | ||
import { UserRejectedError } from '@/errors/user-rejected-error'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { web3Window } from '..'; | ||
import { connect } from './connect'; | ||
|
||
vi.mock('@/types', () => ({ | ||
web3Window: { | ||
ic: { | ||
plug: { | ||
requestConnect: vi.fn(), | ||
principalId: 'wil-123', | ||
}, | ||
}, | ||
}, | ||
})) | ||
|
||
const principalId = 'wil-123'; | ||
|
||
describe('Connect case', () => { | ||
it('should successfully connects and retrieves an account', async () => { | ||
web3Window.ic.plug.requestConnect.mockResolvedValue(true) | ||
const account = await connect() | ||
|
||
expect(account).toEqual(principalId) | ||
}) | ||
|
||
it('should throws error when no provider is available', async () => { | ||
web3Window.ic.plug.requestConnect.mockResolvedValue(false) | ||
|
||
await expect(connect()).rejects.toThrow(NoProviderAvailableError) | ||
}) | ||
|
||
it('should throws error when no accounts are available', async () => { | ||
web3Window.ic.plug.requestConnect.mockResolvedValue(true) | ||
web3Window.ic.plug.principalId = undefined; | ||
|
||
await expect(connect()).rejects.toThrow(NoAvailableAccountsError) | ||
}) | ||
|
||
it('should throws error when the user rejects the connection', async () => { | ||
web3Window.ic.plug.requestConnect.mockRejectedValue(new Error('The agent creation was rejected')) | ||
|
||
await expect(connect()).rejects.toThrow(UserRejectedError) | ||
}) | ||
}) |
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,24 @@ | ||
import { NoAvailableAccountsError, NoProviderAvailableError } from '@/errors'; | ||
import { UserRejectedError } from '@/errors/user-rejected-error'; | ||
import { web3Window } from '@/types'; | ||
import type { PrincipalAddress } from './types'; | ||
|
||
export async function connect(): Promise<PrincipalAddress> { | ||
try { | ||
const hasConnected = await web3Window.ic.plug.requestConnect(); | ||
if (!hasConnected) | ||
throw new NoProviderAvailableError() | ||
|
||
const account: PrincipalAddress | undefined = web3Window.ic.plug.principalId; | ||
if (!account || account.length === 0) | ||
throw new NoAvailableAccountsError() | ||
|
||
return account | ||
} | ||
catch (error) { | ||
if ((error as Error).message.toLowerCase().includes('the agent creation was rejected')) | ||
throw new UserRejectedError() | ||
|
||
throw error | ||
} | ||
} |
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,35 @@ | ||
import type { ProviderEntity } from '@/entities/provider-entity'; | ||
import type { Account, Balance } from '..'; | ||
import { connect } from './connect'; | ||
|
||
export class InternetComputerProvider implements ProviderEntity { | ||
async connect(): Promise<Account[]> { | ||
const principalAddress = await connect() | ||
|
||
return [{ name: 'Principal Account', address: principalAddress }] | ||
} | ||
|
||
getBalance(address: string): Promise<Balance> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
signMessage(address: string, message: string): Promise<string> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
signatureVerify(message: string, signature: string, address: string): boolean { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
joinPool(address: string, poolId: number, amount: number): Promise<string> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
bondExtra(address: string, amount: number): Promise<string> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
claim(address: string): Promise<string> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} |
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 @@ | ||
export type PrincipalAddress = string |
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,50 @@ | ||
import { UserRejectedError } from '@/errors/user-rejected-error' | ||
import { Networks } from '@/networks' | ||
import { web3FromAddress } from '@polkadot/extension-dapp' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { bondExtra } from './bond-extra' | ||
|
||
vi.mock('@/networks', () => ({ | ||
Networks: { | ||
validNetwork: { decimals: 10 }, | ||
}, | ||
})) | ||
|
||
vi.mock('@polkadot/extension-dapp', () => ({ | ||
web3FromAddress: vi.fn(), | ||
})) | ||
|
||
const address = 'wil123' | ||
const amount = 100 | ||
const validHash = 'hash123' | ||
|
||
describe('Bond Extra value case', () => { | ||
it('should call bondExtra function with correct params', async () => { | ||
const injector = { signer: 'signer' }; | ||
(web3FromAddress as any).mockResolvedValue(injector) | ||
|
||
const bondExtraMock = vi.fn().mockImplementation(() => ({ | ||
signAndSend: vi.fn().mockResolvedValue({ toString: () => validHash }), | ||
})) | ||
|
||
const api: any = { tx: { nominationPools: { bondExtra: bondExtraMock } } } | ||
const hash = await bondExtra('validNetwork' as any, api, address, amount) | ||
|
||
expect(web3FromAddress).toHaveBeenCalledWith(address) | ||
expect(bondExtraMock).toHaveBeenCalledWith({ | ||
FreeBalance: 100 * 10 ** (Networks as any).validNetwork.decimals, | ||
}) | ||
expect(hash).toEqual(validHash) | ||
}) | ||
|
||
it('should throw error when the tx is rejected by user', async () => { | ||
(web3FromAddress as any).mockResolvedValue({ signer: 'signer' }) | ||
|
||
const bondExtraMock = vi.fn().mockImplementation(() => ({ | ||
signAndSend: vi.fn().mockRejectedValue(new Error('Rejected by user')), | ||
})) | ||
const api: any = { tx: { nominationPools: { bondExtra: bondExtraMock } } } | ||
|
||
await expect(bondExtra('validNetwork' as any, api, address, amount)).rejects.toThrow(UserRejectedError) | ||
}) | ||
}) |
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,26 @@ | ||
import { UserRejectedError } from '@/errors/user-rejected-error'; | ||
import type { Network } from '@/networks'; | ||
import { Networks } from '@/networks'; | ||
import type { Address } from '@/types'; | ||
import type { ApiPromise } from '@polkadot/api'; | ||
import { web3FromAddress } from '@polkadot/extension-dapp'; | ||
|
||
export async function bondExtra(network: Network, api: ApiPromise, address: Address, amount: number) { | ||
try { | ||
const precision = Networks[network].decimals | ||
const injector = await web3FromAddress(address) | ||
const tx = api.tx.nominationPools.bondExtra({ | ||
FreeBalance: amount * 10 ** precision, | ||
}); | ||
const signer = { signer: injector.signer } | ||
|
||
const hash = await tx.signAndSend(address, signer) | ||
return hash.toString() | ||
} | ||
catch (error) { | ||
if ((error as Error).message.toLowerCase().includes('rejected by user')) | ||
throw new UserRejectedError() | ||
|
||
throw error | ||
} | ||
} |
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,53 @@ | ||
import { UserRejectedError } from '@/errors/user-rejected-error'; | ||
import { web3FromAddress } from '@polkadot/extension-dapp'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { claim } from './claim'; | ||
|
||
vi.mock('@polkadot/extension-dapp', () => ({ | ||
web3FromAddress: vi.fn(), | ||
})) | ||
|
||
const address = 'wil123' | ||
|
||
describe('Claim case', () => { | ||
it('should successfully submit claim tx and return hash', async () => { | ||
const injector = { signer: 'signer' }; | ||
(web3FromAddress as any).mockResolvedValue(injector) | ||
|
||
const signAndSendMock = vi.fn().mockResolvedValue({ toString: () => 'hash123' }) | ||
const claimPayoutMock = vi.fn().mockImplementation(() => ({ | ||
signAndSend: signAndSendMock, | ||
})) | ||
|
||
const api: any = { | ||
tx: { | ||
nominationPools: { | ||
claimPayout: claimPayoutMock, | ||
}, | ||
}, | ||
} | ||
|
||
const hash = await claim(api, address) | ||
|
||
expect(web3FromAddress).toHaveBeenCalledWith(address) | ||
expect(claimPayoutMock).toHaveBeenCalled() | ||
expect(signAndSendMock).toHaveBeenCalledWith(address, { signer: injector.signer }) | ||
expect(hash).toBe('hash123') | ||
}) | ||
|
||
it('should throw error when the user reject tx', async () => { | ||
(web3FromAddress as any).mockResolvedValue({ signer: 'signer' }) | ||
|
||
const api: any = { | ||
tx: { | ||
nominationPools: { | ||
claimPayout: vi.fn().mockImplementation(() => ({ | ||
signAndSend: vi.fn().mockRejectedValue(new Error('Rejected by user')), | ||
})), | ||
}, | ||
}, | ||
} | ||
|
||
await expect(claim(api, address)).rejects.toThrow(UserRejectedError) | ||
}) | ||
}) |
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,21 @@ | ||
import { UserRejectedError } from '@/errors/user-rejected-error'; | ||
import type { Address } from '@/types'; | ||
import type { ApiPromise } from '@polkadot/api'; | ||
import { web3FromAddress } from '@polkadot/extension-dapp'; | ||
|
||
export async function claim(api: ApiPromise, address: Address) { | ||
try { | ||
const injector = await web3FromAddress(address) | ||
const tx = api.tx.nominationPools.claimPayout() | ||
const signer = { signer: injector.signer } | ||
|
||
const hash = await tx.signAndSend(address, signer) | ||
return hash.toString() | ||
} | ||
catch (error) { | ||
if ((error as Error).message.toLowerCase().includes('rejected by user')) | ||
throw new UserRejectedError() | ||
|
||
throw error | ||
} | ||
} |
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
Oops, something went wrong.