Skip to content

Commit

Permalink
refacto escrow script
Browse files Browse the repository at this point in the history
  • Loading branch information
MSghais committed Jun 18, 2024
1 parent 7c589aa commit dd26e55
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 156 deletions.
36 changes: 10 additions & 26 deletions prototype/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const ACCOUNT_TEST_PROFILE = {
pubkey: "",
strkKey: "",
// nostrPk: "5b2b830f2778075ab3befb5a48c9d8138aef017fab2b26b5c31a2742a901afcc",
nostrPublicKey:"5b2b830f2778075ab3befb5a48c9d8138aef017fab2b26b5c31a2742a901afcc",
nostrPublicKey: "5b2b830f2778075ab3befb5a48c9d8138aef017fab2b26b5c31a2742a901afcc",
nostrPrivateKey: "59a772c0e643e4e2be5b8bac31b2ab5c5582b03a84444c81d6e2eec34a5e6c35",
// contract:"0x261d2434b2583293b7dd2048cb9c0984e262ed0a3eb70a19ed4eac6defef8b1",
contract:
Expand All @@ -15,32 +15,16 @@ export const ACCOUNT_TEST_PROFILE = {
pubkey: "",
strkKey: "",
/** FIRST TEST */

// nostrPrivateKey:"3730616361326139616237323262643536613961316161646165376633396263",
// nostrPk:"70aca2a9ab722bd56a9a1aadae7f39bc747c7d6735a04d677e0bc5dbefa71d47",
// contract:
// "0x16a0e5bb60649dfd66e8edf0b1eef7f5a196d99d41826c367f2eeea5794db2f",
/*** Dummy data */
nostrPrivateKey: "70aca2a9ab722bd56a9a1aadae7f39bc747c7d6735a04d677e0bc5dbefa71d47",
nostrPublicKey: "d6f1cf53f9f52d876505164103b1e25811ec4226a17c7449576ea48b00578171",


/** Last deploy */
// nostrPrivateKey:"e076036c244a8794f4dc351d36af05c99e7bff876dcf7c2e8015cc99d6ee318c",
// nostrPk:"043cc0b553450d17f1adcdc3bdeb533892624a8a892d943202add694f52fd4c6",
// contract:
// "0x59e5edd06f305aad0c71da5d1c5ddecdf0cd96766fa69f4d81947a97555165f",


/*** Dummy data */
nostrPrivateKey:"70aca2a9ab722bd56a9a1aadae7f39bc747c7d6735a04d677e0bc5dbefa71d47",

nostrPublicKey:"d6f1cf53f9f52d876505164103b1e25811ec4226a17c7449576ea48b00578171",
// nostrPk:"d6f1cf53f9f52d876505164103b1e25811ec4226a17c7449576ea48b00578171",

contract:
"0x1b5f5bee60ce25d6979c5b88cfbb74ad1dae197dba11719b2e06a5efa7e666d",
contract:
"0x1b5f5bee60ce25d6979c5b88cfbb74ad1dae197dba11719b2e06a5efa7e666d",
},
escrow:{
contract:"0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48",
SEPOLIA:"0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48"
escrow: {
contract: "0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48",
SEPOLIA: "0x53327953bddcb4ae216b14ea0b84261c6c1ad0af112a29be2dab11cf2e76c48"
}
};
export const ERROR_MESSAGES = {
Expand All @@ -64,7 +48,7 @@ export const TOKENS_ADDRESS = {
// TEST: "0x00148a15f9fbf4c015b927bf88608fbafb6d149abdd5ef5b3e3b296e6ac999a4",

STRK: "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d",
USDC:"0x02f37c3e00e75ee4135b32bb60c37e0599af264076376a618f138d2f9929ac74",
USDC: "0x02f37c3e00e75ee4135b32bb60c37e0599af264076376a618f138d2f9929ac74",
TEST: "0x00148a15f9fbf4c015b927bf88608fbafb6d149abdd5ef5b3e3b296e6ac999a4",
BIG_TOKEN: "0x00148a15f9fbf4c015b927bf88608fbafb6d149abdd5ef5b3e3b296e6ac999a4",

Expand Down
235 changes: 109 additions & 126 deletions prototype/test/escrow.test.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,73 @@
import {
connectToStarknet,
createStarknetWallet,
provider,
} from "../utils/starknet";
import { expect } from "chai";
import { generateKeypair, sendEvent } from "../utils/nostr";

import {
createSocialAccount,
createSocialContract,
prepareAndConnectContract,
} from "../utils/social_account";
import { createToken, getToken, transferToken } from "../utils/token";
import { Account, byteArray, cairo, uint256 } from "starknet";
import { SocialPayRequest } from "types";
import { ACCOUNT_TEST_PROFILE, TOKENS_ADDRESS } from "../constants";
import { stringToUint8Array } from "../utils/format";
import dotenv from "dotenv";
import { finalizeEvent, nip19, serializeEvent } from "nostr-tools";
import { createEscrowAccount } from "../utils/escrow";
import { cancel, claimDeposit, createEscrowAccount, deposit } from "../utils/escrow";
dotenv.config();
/** Testing tips flow:
*
* Deploy contract
* Deposit
* Claim
* Cancel
*/
// Sepolia params testing
const currentId = 1
const idToClaim = 1
const idToCancel = 1
let escrow_address = ACCOUNT_TEST_PROFILE.escrow.contract // change default address
let token_used_address = TOKENS_ADDRESS.DEVNET.ETH;

describe("Escrow End to end test", () => {
it("Deploy Escrow Deposit and Claim", async function () {
it("Deploy Escrow", async function () {
this.timeout(0); // Disable timeout for this test

// const resp = await provider.getSpecVersion();
// console.log("rpc version =", resp);
const privateKey0 = process.env.DEV_PK as string;
const accountAddress0 = process.env.DEV_PUBLIC_KEY as string;

const account = new Account(provider, accountAddress0, privateKey0, "1");
/*** Init account
* @description
* Get both account for Bob & Alice
* Send request of account bob to alice
***/

/** Generate keypair for both account*/
// Bob nostr account
// let { privateKey: pkBob, publicKey: bobPublicKey } = generateKeypair();

let pkBob = stringToUint8Array(ACCOUNT_TEST_PROFILE?.bob?.nostrPrivateKey);
const bobPublicKey = ACCOUNT_TEST_PROFILE?.bob?.nostrPublicKey;
console.log("privateKey Bob", new Buffer(pkBob).toString("hex"));
console.log("bobPublicKey", bobPublicKey);

/** @TODO Alice account key */
// Nostr account
// let { privateKey: privateKeyAlice, publicKey: alicePublicKey } = generateKeypair();

// let privateKeyAlice = stringToUint8Array(
// ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey
// );
let privateKeyAlice = ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey as any;
let escrow;
if (process.env.IS_DEPLOY_CONTRACT == "true") {
let escrowContract = await createEscrowAccount();

console.log("ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey", ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey);
console.log("escrow address", escrowContract?.contract_address)
escrow = await prepareAndConnectContract(
escrowContract?.contract_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, // uncomment if you recreate a contract
account
);
} else {
escrow = await prepareAndConnectContract(
escrow_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract,
account
);
}
});

it("Deposit", async function () {
this.timeout(0); // Disable timeout for this test
const privateKey0 = process.env.DEV_PK as string;
const accountAddress0 = process.env.DEV_PUBLIC_KEY as string;
const account = new Account(provider, accountAddress0, privateKey0, "1");
const alicePublicKey = ACCOUNT_TEST_PROFILE?.alice?.nostrPublicKey;
console.log("alicePublicKey", alicePublicKey);

let escrow;
let token_used_address = TOKENS_ADDRESS.DEVNET.ETH;

if (process.env.IS_DEPLOY_CONTRACT == "true") {
let accountBob = await createEscrowAccount();
let escrowContract = await createEscrowAccount();

escrow = await prepareAndConnectContract(
accountBob?.contract_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, // uncomment if you recreate a contract
escrowContract?.contract_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, // uncomment if you recreate a contract
account
);
} else {
escrow = await prepareAndConnectContract(
ACCOUNT_TEST_PROFILE?.escrow?.contract,
escrow_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract,
account
);
}
Expand All @@ -87,101 +80,91 @@ describe("Escrow End to end test", () => {
);

/** Deposit */

let firstId = 1
let currentId = 1; //
let nextId = currentId // await escrow.get_next_deposit_id(); // function need to be made?
console.log("nextId", nextId)

// let depositCurrentId = await escrow.get_deposit(currentId)
// console.log("depositCurrentId",depositCurrentId)

// const depositParams = {
// amount: cairo.uint256(amount), // amount int. Float need to be convert with bnToUint
// // amount: uint256.bnToUint256(BigInt(amount)), // amount
// token_address: strkToken?.address, // token address
// // nostr_recipient: uint256.bnToUint256(BigInt("0x" + alicePublicKey)),
// nostr_recipient: cairo.uint256(BigInt("0x"+alicePublicKey)),
// timelock: 100,
// };
let depositCurrentId = await escrow.get_deposit(currentId)
console.log("depositCurrentId", depositCurrentId)
console.log("try approve escrow erc20")
let txApprove = await strkToken.approve(
escrow?.address,
cairo.uint256(amount), // change for decimals float => uint256.bnToUint256("0x"+alicePublicKey)
)

await account?.waitForTransaction(txApprove?.transaction_hash)
// Need an approve before
console.log("deposit amount")

await deposit({
escrow,
amount,
account,
tokenAddress: strkToken?.address,
timelock: 100,
alicePublicKey: alicePublicKey
})

// expect(cairo.uint256(depositCurrentId?.amount)).to.deep.eq(depositParams?.amount)
// // console.log("try approve escrow erc20")

// let txApprove = await strkToken.approve(escrow?.address, depositParams?.amount)

// await account?.waitForTransaction(txApprove?.transaction_hash)
// // Need an approve before
// console.log("deposit amount")

// let txDeposit = await escrow.deposit(depositParams?.amount,
// depositParams?.token_address,
// depositParams?.nostr_recipient,
// depositParams?.timelock,

// );
// console.log("txDeposit",txDeposit)

// await account?.waitForTransaction(txDeposit?.transaction_hash);
});

// currentId++;
it("Claim", async function () {
this.timeout(0); // Disable timeout for this test
const privateKey0 = process.env.DEV_PK as string;
const accountAddress0 = process.env.DEV_PUBLIC_KEY as string;
const account = new Account(provider, accountAddress0, privateKey0, "1");
let privateKeyAlice = ACCOUNT_TEST_PROFILE?.alice?.nostrPrivateKey as any;
const alicePublicKey = ACCOUNT_TEST_PROFILE?.alice?.nostrPublicKey;

let escrow;
if (process.env.IS_DEPLOY_CONTRACT == "true") {
let escrowContract = await createEscrowAccount();
escrow = await prepareAndConnectContract(
escrowContract?.contract_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract, // uncomment if you recreate a contract
account
);
} else {
escrow = await prepareAndConnectContract(
escrow_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract,
account
);
}
/** Claim */
let timestamp = 1716285235;
// let timestamp = new Date().getTime();
let timestamp=1716285235;

let content = `claim ${cairo.felt(currentId)}`;
// let content =String(currentId)

console.log("content event", content);
console.log("create event for claim")
const event = finalizeEvent(
{
kind: 1,
tags: [],
content: content,
created_at: timestamp,
},
privateKeyAlice
);
let txClaim = await claimDeposit({
escrow,
account,
depositId: idToClaim,
content,
timestamp,
alicePublicKey,
privateKey: privateKeyAlice
})
console.log("tx claim", txClaim)
});

it("Cancel", async function () {
this.timeout(0); // Disable timeout for this test
const privateKey0 = process.env.DEV_PK as string;
const accountAddress0 = process.env.DEV_PUBLIC_KEY as string;
const account = new Account(provider, accountAddress0, privateKey0, "1");
const alicePublicKey = ACCOUNT_TEST_PROFILE?.alice?.nostrPublicKey;

console.log(
"event",
event
let escrow = await prepareAndConnectContract(
escrow_address ?? ACCOUNT_TEST_PROFILE?.escrow?.contract,
account
);
const signature = event.sig;
const signatureR = "0x" + signature.slice(0, signature.length / 2);
const signatureS = "0x" + signature.slice(signature.length / 2);
console.log("signature", signature);
console.log("signatureR", signatureR);
console.log("signatureS", signatureS);

if (signature) {
// let public_key=uint256.bnToUint256(BigInt("0x" + alicePublicKey))
let public_key = cairo.uint256(BigInt("0x" + alicePublicKey))
// expect(depositCurrentId?.recipient).to.eq(BigInt("0x" + alicePublicKey))
const claimParams = {
public_key: public_key,
created_at: timestamp,
kind: 1,
tags: byteArray.byteArrayFromString("[]"), // tags
// content: content, // currentId in felt
content:cairo.felt(currentId),
signature: {
r: cairo.uint256(signatureR),
s: cairo.uint256(signatureS),
},
};

console.log("claimParams", claimParams);
const tx = await account.execute({
contractAddress: escrow?.address,
calldata: claimParams,
entrypoint: "claim",
});

console.log("tx handle claim", tx);
}
// console.log("Alice balance STRK", balanceAlice);
// expect(balanceAlice).to.eq(cairo.uint256(amount));
/** Deposit */
let depositToCancel = await escrow.get_deposit(idToCancel)
console.log("deposit to cancel",depositToCancel)
await cancel({
escrow,
account,
depositId: idToCancel,
})

});
});
Loading

0 comments on commit dd26e55

Please sign in to comment.