Created
June 15, 2020 16:06
-
-
Save rhlsthrm/a08bca6994ceeaa5388b8130016b76c0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Sequelize } from "sequelize"; | |
import { getPostgresStore } from "@connext/store"; | |
import { connect as connext } from "@connext/client"; | |
import { | |
ColorfulLogger, | |
getTestReceiptToSign, | |
getTestVerifyingContract, | |
signReceiptMessage, | |
} from "@connext/utils"; | |
import { | |
ConditionalTransferTypes, | |
CONVENTION_FOR_ETH_ASSET_ID, | |
PublicParams, | |
EventNames, | |
EventPayloads, | |
} from "@connext/types"; | |
import { Wallet, utils, providers } from "ethers"; | |
const { hexlify, randomBytes, parseEther } = utils; | |
interface ConnectOptions { | |
host: string; | |
port?: number; | |
username: string; | |
password: string; | |
database: string; | |
logging?: (sql: string, timing?: number) => void; | |
} | |
export const connect = async (options: ConnectOptions) => { | |
let { host, port, username, password, database, logging } = options; | |
// Use port 5432 by default | |
port = port || 5432; | |
// Connect to the database | |
let sequelize = new Sequelize({ | |
dialect: "postgres", | |
host, | |
port, | |
username, | |
password, | |
database, | |
pool: { | |
max: 10, | |
min: 0, | |
}, | |
logging, | |
}); | |
// Test the connection | |
await sequelize.authenticate(); | |
// All good, return the connection | |
return sequelize; | |
}; | |
const getClient = async (name: string, mnemonic: string) => { | |
let sequelize = await connect({ | |
logging: undefined, | |
host: "localhost", | |
port: 5432, | |
username: "indra", | |
password: "indra", | |
database: "client", | |
}); | |
const store = getPostgresStore(sequelize, { | |
prefix: name, | |
}); | |
const wallet = Wallet.fromMnemonic(mnemonic); | |
console.log(`Created wallet for ${name}, signer address: ${wallet.address}`); | |
return await connext({ | |
ethProviderUrl: "http://localhost:8545", | |
nodeUrl: "http://localhost:8080", | |
messagingUrl: "nats://localhost:4222", | |
store, | |
signer: wallet.privateKey, | |
logLevel: 3, | |
logger: new ColorfulLogger(name, 3, true, ""), | |
}); | |
}; | |
const run = async () => { | |
const sender = await getClient( | |
"sender", | |
"rich awful vocal decade chaos horse cheese sadness just equip equip dismiss" | |
); | |
const receiver = await getClient( | |
"receiver", | |
"tragic robot dwarf ramp churn unusual flavor stairs choice noodle warm hello" | |
); | |
const receiverWallet = Wallet.fromMnemonic( | |
"tragic robot dwarf ramp churn unusual flavor stairs choice noodle warm hello" | |
); | |
const receipt = getTestReceiptToSign(); | |
const chainId = (await sender.ethProvider.getNetwork()).chainId; | |
const verifyingContract = getTestVerifyingContract(); | |
receiver.on(EventNames.CONDITIONAL_TRANSFER_CREATED_EVENT, async (data) => { | |
const payload = data as EventPayloads.SignedTransferCreated; | |
const signature = await signReceiptMessage( | |
receipt, | |
chainId, | |
verifyingContract, | |
receiverWallet.privateKey | |
); | |
await receiver.resolveCondition({ | |
conditionType: ConditionalTransferTypes.SignedTransfer, | |
paymentId: payload.paymentId, | |
responseCID: receipt.responseCID, | |
signature, | |
} as PublicParams.ResolveSignedTransfer); | |
}); | |
const senderBalance = await sender.ethProvider.getBalance( | |
sender.signerAddress | |
); | |
console.log("senderBalance: ", senderBalance.toString()); | |
if (senderBalance.lt(parseEther("1"))) { | |
console.log(`Balance too low, returning`); | |
return; | |
} | |
await sender.deposit({ amount: parseEther("0.1") }); | |
for (let i = 0; i < 10; i++) { | |
const paymentId = hexlify(randomBytes(32)); | |
await sender.conditionalTransfer({ | |
amount: parseEther("0.001"), | |
conditionType: ConditionalTransferTypes.SignedTransfer, | |
paymentId, | |
signerAddress: receiver.signerAddress, | |
chainId, | |
verifyingContract, | |
requestCID: receipt.requestCID, | |
subgraphDeploymentID: receipt.subgraphDeploymentID, | |
assetId: CONVENTION_FOR_ETH_ASSET_ID, | |
recipient: receiver.publicIdentifier, | |
meta: { message: `Payment ${i}` }, | |
} as PublicParams.SignedTransfer); | |
await sender.waitFor( | |
EventNames.CONDITIONAL_TRANSFER_UNLOCKED_EVENT, | |
10000, | |
(data) => data.paymentId === paymentId | |
); | |
console.log(`Sender unlocked ${paymentId}`); | |
} | |
}; | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment