Skip to content

Commit

Permalink
Add address[] support
Browse files Browse the repository at this point in the history
  • Loading branch information
dawsbot committed Apr 7, 2024
1 parent dd37f1f commit 571fbd6
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 82 deletions.
75 changes: 0 additions & 75 deletions src/classes/test/Contract/crv-abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,6 @@ export const abi: JSONABI =
// http://api.etherscan.io/api?module=contract&action=getabi&address=0x575CCD8e2D300e2377B43478339E364000318E2c&format=raw

[
{
name: 'Fund',
inputs: [
{
type: 'address',
name: 'recipient',
indexed: true,
},
{
type: 'uint256',
name: 'amount',
indexed: false,
},
],
anonymous: false,
type: 'event',
},
{
name: 'Claim',
inputs: [
{
type: 'address',
name: 'recipient',
indexed: true,
},
{
type: 'uint256',
name: 'claimed',
indexed: false,
},
],
anonymous: false,
type: 'event',
},
{
name: 'ToggleDisable',
inputs: [
{
type: 'address',
name: 'recipient',
indexed: false,
},
{
type: 'bool',
name: 'disabled',
indexed: false,
},
],
anonymous: false,
type: 'event',
},
{
name: 'CommitOwnership',
inputs: [
{
type: 'address',
name: 'admin',
indexed: false,
},
],
anonymous: false,
type: 'event',
},
{
name: 'ApplyOwnership',
inputs: [
{
type: 'address',
name: 'admin',
indexed: false,
},
],
anonymous: false,
type: 'event',
},
{
outputs: [],
inputs: [
Expand Down
1 change: 0 additions & 1 deletion src/classes/test/Contract/crv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { rpcUrls } from '../../../providers/test/rpc-urls';
import { Contract as EssentialEthContract } from '../../Contract';
import { abi } from './crv-abi';

// The JSONABI
const JSONABI = abi;

const rpcURL = rpcUrls.mainnet;
Expand Down
1 change: 0 additions & 1 deletion src/classes/test/Contract/ens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Contract as EssentialEthContract } from '../../Contract';
import { rpcUrls } from './../../../providers/test/rpc-urls';
import { ensABI } from './ens-abi';

// The JSONABI
const JSONABI = ensABI;

const rpcURL = rpcUrls.mainnet;
Expand Down
1 change: 0 additions & 1 deletion src/classes/test/Contract/fei.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Contract as EssentialEthContract } from '../../Contract';
import { rpcUrls } from './../../../providers/test/rpc-urls';
import { feiABI } from './fei-abi';

// The JSONABI
const JSONABI = feiABI;

const rpcURL = rpcUrls.mainnet;
Expand Down
10 changes: 10 additions & 0 deletions src/classes/test/Contract/jokerrace-abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { JSONABI } from '../../../types/Contract.types';

export const abi: JSONABI = [
{
name: 'getAllAddressesThatHaveVoted',
outputs: [{ type: 'address[]', name: '' }],
inputs: [],
type: 'function',
},
];
26 changes: 26 additions & 0 deletions src/classes/test/Contract/jokerrace.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { JsonRpcProvider } from '../../../index';
import { rpcUrls } from '../../../providers/test/rpc-urls';
import { Contract as EssentialEthContract } from '../../Contract';
import { abi } from './jokerrace-abi';

const JSONABI = abi;

const rpcURL = rpcUrls.oeth;
const essentialEthProvider = new JsonRpcProvider(rpcURL);

// https://optimistic.etherscan.io/address/0x5c11016ee4f8ad4ea2ab8b1b366f32d30d48a031#code
const contractAddress = '0x5c11016ee4f8ad4ea2ab8b1b366f32d30d48a031';

const essentialEthContract = new EssentialEthContract(
contractAddress,
JSONABI,
essentialEthProvider,
);
describe('jokerrace contract', () => {
it('should fetch "address[]"', async () => {
const addresses = await essentialEthContract.getAllAddressesThatHaveVoted();
expect(Array.isArray(addresses)).toBe(true);
expect(addresses.length).toBeGreaterThan(18);
expect(addresses).toContain('0x0b06ca5DcC8A10Be0951d4E140D4312702B8D0EC');
});
});
1 change: 0 additions & 1 deletion src/classes/test/Contract/uni.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Contract as EssentialEthContract } from '../../Contract';
import { rpcUrls } from './../../../providers/test/rpc-urls';
import { uniswapABI } from './uniswap-abi';

// The JSONABI
const JSONABI = uniswapABI;

const rpcURL = rpcUrls.mainnet;
Expand Down
17 changes: 14 additions & 3 deletions src/classes/utils/encode-decode-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ function expandType(type: ContractTypes) {
// https://docs.soliditylang.org/en/v0.8.7/types.html#integers
if (type === 'uint[]') {
return 'uint256[]';
} else if (type === 'int[]') {
}
if (type === 'int[]') {
return 'int256[]';
}
return type;
Expand Down Expand Up @@ -171,8 +172,17 @@ export function decodeRPCResponse(
return hexToUtf8(hexToDecode);
}
// chunk response every 64 characters
const encodedOutputs = slicedResponse.match(/.{1,64}/g);
const outputs = (encodedOutputs || []).map((output: string, i: number) => {
const encodedOutputs = slicedResponse.match(/.{1,64}/g) || [];
if (
jsonABIArgument?.outputs?.length === 1 &&
jsonABIArgument.outputs[0].type === 'address[]'
) {
const unformattedAddresses = encodedOutputs.slice(2);
return unformattedAddresses.map((unformattedAddress) => {
return toChecksumAddress(`0x${unformattedAddress.slice(24)}`);
});
}
const outputs = encodedOutputs.map((output: string, i: number) => {
const outputType = (rawOutputs || [])[i].type;
switch (outputType) {
case 'bool':
Expand All @@ -187,6 +197,7 @@ export function decodeRPCResponse(
return `0x${output}`;
case 'uint8':
return Number(hexToDecimal(`0x${output}`));

default:
throw new Error(
`essential-eth does not yet support "${outputType}" outputs. Make a PR today!"`,
Expand Down

0 comments on commit 571fbd6

Please sign in to comment.