Skip to content

Instantly share code, notes, and snippets.

@Jesse-Sawa
Created September 26, 2022 18:08
Show Gist options
  • Save Jesse-Sawa/f2a70af4af812f777c98e8268d1f0b98 to your computer and use it in GitHub Desktop.
Save Jesse-Sawa/f2a70af4af812f777c98e8268d1f0b98 to your computer and use it in GitHub Desktop.
Sanity check for staged ASv2 contracts. Has dependencies on associated ABI files
import { ContractKit, newKit } from "@celo/contractkit";
import { Contract } from "web3-eth-contract";
import {
ESCROW_CONTRACT,
FA_CONTRACT,
ODIS_PAYMENTS_CONTRACT,
} from "./constants";
// STAGING (private testnet)
const STAGING_RPC = "http://127.0.0.1:8545";
const STAGING_TEST_SIGNER = "0x4e3D385eCdEE402DA395a3b18575B05Cc5e8Ff21";
const FA_PROXY_ADDRESS = "0x21E9fc80a6792D1F5c05492cdA3598aAfBB1B66C";
const ESCROW_PROXY_ADDRESS = "0x741A4ADE8B8B690F85F0bf8b01e7b115cB3806FF";
const ODIS_PAYMENTS_PROXY_ADDRESS =
"0x0A359faaAe16dECB12A14b77628258e43fdc2a1D";
describe("escrow", () => {
let kit: ContractKit;
let escrowContractInstance: Contract;
beforeAll(async () => {
kit = await newKit(STAGING_RPC);
kit.defaultAccount = STAGING_TEST_SIGNER;
escrowContractInstance = new kit.web3.eth.Contract(
ESCROW_CONTRACT.abi,
ESCROW_PROXY_ADDRESS
);
});
test("version to be 1.2.0", async () => {
const escrowVersion = await escrowContractInstance.methods
.getVersionNumber()
.call();
expect(escrowVersion).toEqual({ "0": "1", "1": "2", "2": "0", "3": "0" });
});
test("default trusted issuers array should be empty", async () => {
const getDefaultTrustedIssuersResponse =
await escrowContractInstance.methods.getDefaultTrustedIssuers().call();
expect(getDefaultTrustedIssuersResponse).toEqual([]);
});
test("adding default trusted issuer from non-owner address should throw", async () => {
await expect(
escrowContractInstance.methods
.addDefaultTrustedIssuer(kit.defaultAccount)
.call()
).rejects.toThrow("Ownable: caller is not the owner");
});
test("removing default trusted issuer from non-owner address should throw", async () => {
await expect(
escrowContractInstance.methods
.removeDefaultTrustedIssuer(kit.defaultAccount, 0)
.call()
).rejects.toThrow("Ownable: caller is not the owner");
});
});
describe("federated attestations", () => {
let kit: ContractKit;
let federatedAttestationsInstance: Contract;
beforeAll(async () => {
kit = await newKit(STAGING_RPC);
kit.defaultAccount = STAGING_TEST_SIGNER;
federatedAttestationsInstance = new kit.web3.eth.Contract(
FA_CONTRACT.abi,
FA_PROXY_ADDRESS
);
});
test("version to be 1.1.0", async () => {
const version = await federatedAttestationsInstance.methods
.getVersionNumber()
.call();
expect(version).toEqual({ "0": "1", "1": "1", "2": "0", "3": "0" });
});
test("no identifiers should exist if none were registered", async () => {
const testAccount = kit.web3.eth.accounts.create();
const testIssuer = kit.web3.eth.accounts.create();
const identifiers = await federatedAttestationsInstance.methods
.lookupIdentifiers(testAccount.address, [testIssuer.address])
.call();
expect(identifiers).toEqual({
"0": ["0"],
"1": [],
countsPerIssuer: ["0"],
identifiers: [],
});
});
test("no attestations should exist if none were registered", async () => {
const testIdentifierAddress = kit.web3.eth.accounts.create().address;
const testIdentifierBytes32 = kit.web3.utils.soliditySha3({
t: "bytes32",
v: testIdentifierAddress,
});
const attestations = await federatedAttestationsInstance.methods
.lookupAttestations(testIdentifierBytes32, [kit.defaultAccount])
.call();
expect(attestations).toEqual({
"0": ["0"],
"1": [],
"2": [],
"3": [],
"4": [],
accounts: [],
countsPerIssuer: ["0"],
issuedOns: [],
publishedOns: [],
signers: [],
});
});
test("attestation & Identifier should exist after registration", async () => {
const testIdentifierAddress = kit.web3.eth.accounts.create().address;
const testIdentifierBytes32 = kit.web3.utils.soliditySha3({
t: "bytes32",
v: testIdentifierAddress,
});
const testAccount = kit.web3.eth.accounts.create().address;
const testIssuedOnTimestamp = Math.floor(new Date().getTime() / 1000);
await federatedAttestationsInstance.methods
.registerAttestationAsIssuer(
testIdentifierBytes32,
testAccount,
testIssuedOnTimestamp
)
.send({ from: kit.defaultAccount });
const attestations = await federatedAttestationsInstance.methods
.lookupAttestations(testIdentifierBytes32, [kit.defaultAccount])
.call();
const identifiers = await federatedAttestationsInstance.methods
.lookupIdentifiers(testAccount, [kit.defaultAccount])
.call();
expect(attestations[0]).toEqual(["1"]);
expect(attestations[1]).toEqual([testAccount]);
expect(attestations[2]).toEqual([kit.defaultAccount]);
expect(identifiers[0]).toEqual(["1"]);
expect(identifiers[1]).toEqual([testIdentifierBytes32]);
}, 50000);
});
describe("odis payments", () => {
let kit: ContractKit;
let odisPaymentsContractInstance: Contract;
beforeAll(async () => {
kit = await newKit(STAGING_RPC);
kit.defaultAccount = STAGING_TEST_SIGNER;
odisPaymentsContractInstance = new kit.web3.eth.Contract(
ODIS_PAYMENTS_CONTRACT.abi,
ODIS_PAYMENTS_PROXY_ADDRESS
);
});
test("version to be 1.1.0", async () => {
const version = await odisPaymentsContractInstance.methods
.getVersionNumber()
.call();
expect(version).toEqual({ "0": "1", "1": "1", "2": "0", "3": "0" });
});
test("pay into odis payments should be successful", async () => {
const stableTokenContract = await kit.contracts.getStableToken();
const paidInPast: number = await odisPaymentsContractInstance.methods
.totalPaidCUSD(STAGING_TEST_SIGNER)
.call();
const amountToPayNow: number = 1;
const approveTx = await stableTokenContract
.approve(ODIS_PAYMENTS_PROXY_ADDRESS, amountToPayNow)
.send({ from: kit.defaultAccount });
await approveTx.waitReceipt();
await odisPaymentsContractInstance.methods
.payInCUSD(STAGING_TEST_SIGNER, amountToPayNow)
.send({ from: kit.defaultAccount });
const totalPaid = await odisPaymentsContractInstance.methods
.totalPaidCUSD(STAGING_TEST_SIGNER)
.call();
expect(totalPaid).toEqual((+paidInPast + +amountToPayNow).toString());
}, 50000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment