All files / ethers.js/src.ts/_tests test-providers-ccip.ts

97.59% Statements 203/208
77.77% Branches 21/27
100% Functions 4/4
97.59% Lines 203/208

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 2091x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import assert from "assert";
 
import {
    concat, dataLength,
    keccak256,
    toBeArray,
    isCallException, isError
} from "../index.js";
 
import { connect, setupProviders } from "./create-provider.js";
 
setupProviders();
 
describe("Test CCIP execution", function() {
 
    // This matches the verify method in the Solidity contract against the
    // processed data from the endpoint
    const verify = function(sender: string, data: string, result: string): void {
        const check = concat([
            toBeArray(dataLength(sender)), sender,
            toBeArray(dataLength(data)), data
        ]);
        assert.equal(result, keccak256(check), "response is equal");
    }
 
    const address = "0xb66e9b20258712bfb9fd40acb13d7712ef149d6e";
    const networkName = "sepolia";
 
    const calldata = "0x1234";
 
    it("testGet passes under normal operation", async function() {
        this.timeout(60000);
 
        const provider = connect(networkName);
 
        // testGet(bytes callData = "0x1234")
        const tx = {
            to: address, enableCcipRead: true,
            data: "0xa5f3271e000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000"
        }
 
        const result = await provider.call(tx);
        verify(address, calldata, result);
    });
 
    it("testGet should fail with CCIP not explicitly enabled by overrides", async function() {
        this.timeout(60000);
 
        const provider = connect(networkName);
 
        // testGet(bytes callData = "0x1234")
        const tx = {
            to: address,
            data: "0xa5f3271e000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000"
        }
 
        await assert.rejects(async function() {
            const result = await provider.call(tx);
            console.log(result);
        }, (error: unknown) => {
            const offchainErrorData = concat([
                "0x556f1830000000000000000000000000",
                address,
                "0x00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000140b1494be100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004068747470733a2f2f6574686572732e7269636d6f6f2e776f726b6572732e6465762f746573742d636369702d726561642f7b73656e6465727d2f7b646174617d00000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d4d79206578747261206461746100000000000000000000000000000000000000"
            ]);
            return (isCallException(error) && error.data === offchainErrorData);
        });
    });
 
    it("testGet should fail with CCIP explicitly disabled on provider", async function() {
        this.timeout(60000);
 
        const provider = connect(networkName);
        provider.disableCcipRead = true;
 
        // testGetFail(bytes callData = "0x1234")
        const tx = {
            to: address, enableCcipRead: true,
            data: "0xa5f3271e000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000"
        }
 
        await assert.rejects(async function() {
            const result = await provider.call(tx);
            console.log(result);
        }, (error: unknown) => {
            const offchainErrorData = concat([
                "0x556f1830000000000000000000000000",
                address,
                "0x00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000140b1494be100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004068747470733a2f2f6574686572732e7269636d6f6f2e776f726b6572732e6465762f746573742d636369702d726561642f7b73656e6465727d2f7b646174617d00000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d4d79206578747261206461746100000000000000000000000000000000000000"
            ]);
 
            return (isCallException(error) && error.data === offchainErrorData);
        });
    });
 
    it("testGetFail should fail if all URLs 5xx", async function() {
        this.timeout(60000);
 
        const provider = connect(networkName);
 
        // testGetFail(bytes callData = "0x1234")
        const tx = {
            to: address, enableCcipRead: true,
            data: "0x36f9cea6000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000"
        }
 
        await assert.rejects(async function() {
            const result = await provider.call(tx);
            console.log(result);
        }, (error: unknown) => {
            const infoJson = '{"urls":["https:/\/ethers.ricmoo.workers.dev/status/500/{sender}/{data}"],"errorMessages":["hello world"]}';
            return (isError(error, "OFFCHAIN_FAULT") && error.reason === "500_SERVER_ERROR" &&
                JSON.stringify(error.info) === infoJson);
        });
    });
 
    it("testGetSenderFail should fail if sender does not match", async function() {
        this.timeout(60000);
 
        const provider = connect(networkName);
 
        // testGetSenderFail(bytes callData = "0x1234")
        const tx = {
            to: address, enableCcipRead: true,
            data: "0x64bff6d1000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000",
        }
 
        await assert.rejects(async function() {
            const result = await provider.call(tx);
            console.log(result);
        }, (error: unknown) => {
            const errorArgsJson = '["0x0000000000000000000000000000000000000000",["https://ethers.ricmoo.workers.dev/test-ccip-read/{sender}/{data}"],"0x1234","0xb1494be1","0x4d792065787472612064617461"]';
            const offchainErrorData = "0x556f1830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000140b1494be100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004068747470733a2f2f6574686572732e7269636d6f6f2e776f726b6572732e6465762f746573742d636369702d726561642f7b73656e6465727d2f7b646174617d00000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d4d79206578747261206461746100000000000000000000000000000000000000";
            return (isCallException(error) && error.data === offchainErrorData &&
                error.revert &&
                error.revert.signature === "OffchainLookup(address,string[],bytes,bytes4,bytes)" &&
                JSON.stringify(error.revert.args) === errorArgsJson);
        });
    });
 
 
    it("testGetMissing should fail if early URL 4xx", async function() {
        this.timeout(60000);
 
        const provider = connect(networkName);
 
        // testGetMissing(bytes callData = "0x1234")
        const tx = {
            to: address, enableCcipRead: true,
            data: "0x4ece8d7d000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000"
        }
 
        await assert.rejects(async function() {
            const result = await provider.call(tx);
            console.log(result);
        }, (error: unknown) => {
            const infoJson = '{"url":"https:/\/ethers.ricmoo.workers.dev/status/404/{sender}/{data}","errorMessage":"hello world"}';
            return (isError(error, "OFFCHAIN_FAULT") && error.reason === "404_MISSING_RESOURCE" &&
                JSON.stringify(error.info || "") === infoJson);
        });
    });
 
 
    it("testGetFallback passes if any URL returns correctly", async function() {
        this.timeout(60000);
 
        const provider = connect(networkName);
 
        // testGetFallback(bytes callData = "0x1234")
        const tx = {
            to: address, enableCcipRead: true,
            data: "0xedf4a021000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000"
        }
 
        const result = await provider.call(tx);
        verify(address, calldata, result);
    });
 
    it("testGetDeadHostFallback passes if any URL returns correctly", async function() {
        this.timeout(60000);
 
        const provider = connect(networkName);
 
        // testGetDeadHostFallback(bytes callData = "0x1234")
        const tx = {
            to: address, enableCcipRead: true,
            data: "0x0324be5a000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000"
        }
 
        const result = await provider.call(tx);
        verify(address, calldata, result);
    });
 
    it("testPost passes under normal operation", async function() {
        this.timeout(60000);
 
        const provider = connect(networkName);
 
        // testPost(bytes callData = "0x1234")
        const tx = {
            to: address, enableCcipRead: true,
            data: "0x66cab49d000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000"
        }
 
        const result = await provider.call(tx);
        verify(address, calldata, result);
    });
})