-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix typing for prepareTransactionRequest
- Loading branch information
1 parent
831690a
commit 74bc6be
Showing
7 changed files
with
134 additions
and
102 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
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
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,81 +1,112 @@ | ||
import { it, expect, expectTypeOf } from 'vitest'; | ||
import { describe, it, expect, expectTypeOf } from 'vitest'; | ||
|
||
import { AbiFunctionNotFoundError, createPublicClient, http } from 'viem'; | ||
import { nitroTestnodeL2 } from '../chains'; | ||
import { arbOwnerPublicActions } from './arbOwnerPublicActions'; | ||
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; | ||
|
||
const client10 = createPublicClient({ | ||
chain: nitroTestnodeL2, | ||
transport: http(), | ||
}).extend(arbOwnerPublicActions({ arbOSVersion: 10 })); | ||
}).extend(arbOwnerPublicActions({ arbOsVersion: 10 })); | ||
const client11 = createPublicClient({ | ||
chain: nitroTestnodeL2, | ||
transport: http(), | ||
}).extend(arbOwnerPublicActions({ arbOSVersion: 11 })); | ||
}).extend(arbOwnerPublicActions({ arbOsVersion: 11 })); | ||
const client20 = createPublicClient({ | ||
chain: nitroTestnodeL2, | ||
transport: http(), | ||
}).extend(arbOwnerPublicActions({ arbOSVersion: 20 })); | ||
}).extend(arbOwnerPublicActions({ arbOsVersion: 20 })); | ||
const randomAccount = privateKeyToAccount(generatePrivateKey()); | ||
const upgradeExecutorAddress = '0x24198F8A339cd3C47AEa3A764A20d2dDaB4D1b5b'; | ||
|
||
it('Accept function name based on arbOSVersion', async () => { | ||
// Version 10 | ||
client10.arbOwnerReadContract({ | ||
functionName: 'onlyOnArbOS10', | ||
}); | ||
describe('Accept function name based on arbOSVersion', async () => { | ||
it('Version 10', () => { | ||
expectTypeOf<typeof client10.arbOwnerReadContract<'onlyOnArbOS10'>>().toBeCallableWith({ | ||
functionName: 'onlyOnArbOS10' | ||
}) | ||
|
||
expect( | ||
client10.arbOwnerReadContract({ | ||
// @ts-expect-error Not available for version 10 | ||
functionName: 'onlyOnArbOS20', | ||
}), | ||
).rejects.toThrowError(AbiFunctionNotFoundError); | ||
expectTypeOf<typeof client10.arbOwnerPrepareTransactionRequest<'setL1PricingRewardRecipient'>>().toBeCallableWith({ | ||
functionName: 'setL1PricingRewardRecipient', | ||
account: randomAccount.address, | ||
upgradeExecutor: upgradeExecutorAddress, | ||
args: [[randomAccount.address, randomAccount.address]] | ||
}) | ||
|
||
// Version 11 | ||
client11.arbOwnerReadContract({ | ||
functionName: 'onlyOnArbOS11', | ||
}); | ||
expect( | ||
client10.arbOwnerReadContract({ | ||
// @ts-expect-error Not available for version 10 | ||
functionName: 'onlyOnArbOS20', | ||
}), | ||
).rejects.toThrowError(AbiFunctionNotFoundError); | ||
|
||
expect( | ||
client11.arbOwnerReadContract({ | ||
// @ts-expect-error Not available for version 11 | ||
functionName: 'onlyOnArbOS20', | ||
}), | ||
).rejects.toThrowError(AbiFunctionNotFoundError); | ||
|
||
// Version 20 | ||
client20.arbOwnerReadContract({ | ||
functionName: 'getInfraFeeAccount', | ||
}); | ||
}) | ||
|
||
expect( | ||
client20.arbOwnerReadContract({ | ||
// @ts-expect-error Not available for version 20 | ||
functionName: 'onlyOnArbOS10', | ||
}), | ||
).rejects.toThrowError(AbiFunctionNotFoundError); | ||
it('Version 11', () => { | ||
expectTypeOf<typeof client11.arbOwnerReadContract<'onlyOnArbOS11'>>().toBeCallableWith({ | ||
functionName: 'onlyOnArbOS11' | ||
}) | ||
|
||
expectTypeOf<typeof client11.arbOwnerPrepareTransactionRequest<'setL1PricingRewardRecipient'>>().toBeCallableWith({ | ||
functionName: 'setL1PricingRewardRecipient', | ||
account: randomAccount.address, | ||
upgradeExecutor: upgradeExecutorAddress, | ||
args: [100n] | ||
}) | ||
|
||
expect( | ||
client11.arbOwnerReadContract({ | ||
// @ts-expect-error Not available for version 11 | ||
functionName: 'onlyOnArbOS20', | ||
}), | ||
).rejects.toThrowError(AbiFunctionNotFoundError); | ||
}) | ||
|
||
it('Version 20', () => { | ||
expectTypeOf<typeof client20.arbOwnerReadContract<'getInfraFeeAccount'>>().toBeCallableWith({ | ||
functionName: 'getInfraFeeAccount' | ||
}) | ||
|
||
expectTypeOf<typeof client20.arbOwnerPrepareTransactionRequest<'setL1PricingRewardRecipient'>>().toBeCallableWith({ | ||
functionName: 'setL1PricingRewardRecipient', | ||
account: randomAccount.address, | ||
upgradeExecutor: upgradeExecutorAddress, | ||
args: [randomAccount.address] | ||
}) | ||
|
||
expect( | ||
client20.arbOwnerReadContract({ | ||
// @ts-expect-error Not available for version 20 | ||
functionName: 'onlyOnArbOS10', | ||
}), | ||
).rejects.toThrowError(AbiFunctionNotFoundError); | ||
}) | ||
}); | ||
|
||
// Those tests won't fail if the return type is wrong | ||
// But they will display an error in the IDE | ||
it('Type return values for function in multiple versions', () => { | ||
// Version 10 | ||
expectTypeOf( | ||
client10.arbOwnerReadContract({ | ||
functionName: 'getAllChainOwners', | ||
}), | ||
).resolves.toEqualTypeOf<`0x${string}`>(); | ||
|
||
// Version 11 | ||
expectTypeOf( | ||
client11.arbOwnerReadContract({ | ||
functionName: 'getAllChainOwners', | ||
}), | ||
).resolves.toEqualTypeOf<bigint>(); | ||
describe('Type return values for function in multiple versions', () => { | ||
it('Version 10', () => { | ||
expectTypeOf( | ||
client10.arbOwnerReadContract({ | ||
functionName: 'getAllChainOwners', | ||
}), | ||
).resolves.toEqualTypeOf<`0x${string}`>(); | ||
}) | ||
|
||
// Version 20 | ||
expectTypeOf( | ||
client20.arbOwnerReadContract({ | ||
functionName: 'getAllChainOwners', | ||
}), | ||
).resolves.toEqualTypeOf<readonly `0x${string}`[]>(); | ||
}); | ||
it('Version 11', () => { | ||
expectTypeOf( | ||
client11.arbOwnerReadContract({ | ||
functionName: 'getAllChainOwners', | ||
}), | ||
).resolves.toEqualTypeOf<bigint>(); | ||
}) | ||
it('Version 11', () => { | ||
expectTypeOf( | ||
client20.arbOwnerReadContract({ | ||
functionName: 'getAllChainOwners', | ||
}), | ||
).resolves.toEqualTypeOf<readonly `0x${string}`[]>(); | ||
}) | ||
}); |