mirror of
https://github.com/status-im/keycard-redeem.git
synced 2025-01-28 04:45:36 +00:00
adapt create-gift tool
This commit is contained in:
parent
8e7ab9ebc9
commit
0a10824c6a
@ -32,8 +32,6 @@ contract NFTBucket is IERC165, IERC721Receiver {
|
|||||||
bytes32 code;
|
bytes32 code;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint256 public redeemableSupply;
|
|
||||||
|
|
||||||
bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;
|
bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;
|
||||||
|
|
||||||
bytes32 constant EIP712DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
|
bytes32 constant EIP712DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
|
||||||
|
@ -4,15 +4,19 @@ import Web3 from 'web3';
|
|||||||
import parseArgs from 'minimist';
|
import parseArgs from 'minimist';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|
||||||
const argv = parseArgs(process.argv.slice(2), {boolean: ["deploy-factory", "deploy-bucket"], string: ["sender", "factory", "bucket", "token"], default: {"endpoint": "ws://127.0.0.1:8546", "validity-days": 365}});
|
const argv = parseArgs(process.argv.slice(2), {boolean: ["nft", "deploy-factory", "deploy-bucket"], string: ["sender", "factory", "bucket", "token"], default: {"endpoint": "ws://127.0.0.1:8546", "validity-days": 365}});
|
||||||
|
|
||||||
const web3 = new Web3(argv["endpoint"]);
|
const web3 = new Web3(argv["endpoint"]);
|
||||||
|
|
||||||
const GiftBucketConfig = loadEmbarkArtifact('./embarkArtifacts/contracts/GiftBucket.js');
|
const classPrefix = argv["nft"] ? "NFT" : "Gift";
|
||||||
const GiftBucketFactoryConfig = loadEmbarkArtifact('./embarkArtifacts/contracts/GiftBucketFactory.js');
|
|
||||||
|
|
||||||
const GiftBucketFactory = new web3.eth.Contract(GiftBucketFactoryConfig["abiDefinition"]);
|
const BucketConfig = loadEmbarkArtifact(`./embarkArtifacts/contracts/${classPrefix}Bucket.js`);
|
||||||
const GiftBucket = new web3.eth.Contract(GiftBucketConfig["abiDefinition"]);
|
const BucketFactoryConfig = loadEmbarkArtifact(`./embarkArtifacts/contracts/${classPrefix}BucketFactory.js`);
|
||||||
|
const IERC721 = loadEmbarkArtifact(`./embarkArtifacts/contracts/IERC721.js`);
|
||||||
|
|
||||||
|
const BucketFactory = new web3.eth.Contract(BucketFactoryConfig["abiDefinition"]);
|
||||||
|
const Bucket = new web3.eth.Contract(BucketConfig["abiDefinition"]);
|
||||||
|
const ERC721 = new web3.eth.Contract(IERC721["abiDefinition"]);
|
||||||
|
|
||||||
function loadEmbarkArtifact(path) {
|
function loadEmbarkArtifact(path) {
|
||||||
let file = fs.readFileSync(path, "utf-8");
|
let file = fs.readFileSync(path, "utf-8");
|
||||||
@ -49,8 +53,8 @@ async function sendMethod(methodCall, sender, to) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function deployFactory(sender) {
|
async function deployFactory(sender) {
|
||||||
let code = "0x" + GiftBucketFactoryConfig["code"];
|
let code = "0x" + BucketFactoryConfig["code"];
|
||||||
let methodCall = GiftBucketFactory.deploy({data: code});
|
let methodCall = BucketFactory.deploy({data: code});
|
||||||
let receipt = await sendMethod(methodCall, sender, null);
|
let receipt = await sendMethod(methodCall, sender, null);
|
||||||
return receipt.contractAddress;
|
return receipt.contractAddress;
|
||||||
}
|
}
|
||||||
@ -59,11 +63,11 @@ async function deployBucket(sender, factory, token, validityInDays) {
|
|||||||
let now = Math.round(new Date().getTime() / 1000);
|
let now = Math.round(new Date().getTime() / 1000);
|
||||||
let expirationDate = now + (60 * 60 * 24 * validityInDays);
|
let expirationDate = now + (60 * 60 * 24 * validityInDays);
|
||||||
|
|
||||||
GiftBucketFactory.options.address = factory;
|
BucketFactory.options.address = factory;
|
||||||
let methodCall = GiftBucketFactory.methods.create(token.toLowerCase(), expirationDate);
|
let methodCall = BucketFactory.methods.create(token.toLowerCase(), expirationDate);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let receipt = await sendMethod(methodCall, sender, GiftBucketFactory.options.address);
|
let receipt = await sendMethod(methodCall, sender, BucketFactory.options.address);
|
||||||
return receipt.events.BucketCreated.returnValues.bucket;
|
return receipt.events.BucketCreated.returnValues.bucket;
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@ -72,11 +76,38 @@ async function deployBucket(sender, factory, token, validityInDays) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createGift(sender, bucket, keycard) {
|
async function createGift(sender, bucket, keycard) {
|
||||||
GiftBucket.options.address = bucket;
|
Bucket.options.address = bucket;
|
||||||
let methodCall = GiftBucket.methods.createGift(keycard.keycard, keycard.amount, keycard.code);
|
let methodCall = Bucket.methods.createGift(keycard.keycard, keycard.amount, keycard.code);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let receipt = await sendMethod(methodCall, sender, GiftBucket.options.address);
|
let receipt = await sendMethod(methodCall, sender, Bucket.options.address);
|
||||||
|
return receipt;
|
||||||
|
} catch(err) {
|
||||||
|
console.error(err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createNFTData(keycard, code) {
|
||||||
|
return keycard.toLowerCase() + code.replace("0x", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
function senderAddress(sender) {
|
||||||
|
if (typeof(sender) == "string") {
|
||||||
|
return sender;
|
||||||
|
} else {
|
||||||
|
return sender.address;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function transferNFT(sender, token, bucket, keycard) {
|
||||||
|
Bucket.options.address = bucket;
|
||||||
|
ERC721.options.address = token ? token : await Bucket.methods.tokenContract().call();
|
||||||
|
|
||||||
|
let methodCall = ERC721.methods.safeTransferFrom(senderAddress(sender), bucket, keycard.amount, createNFTData(keycard.keycard, keycard.code));
|
||||||
|
|
||||||
|
try {
|
||||||
|
let receipt = await sendMethod(methodCall, sender, ERC721.options.address);
|
||||||
return receipt;
|
return receipt;
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@ -90,8 +121,9 @@ function processLine(line) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
GiftBucketFactory.transactionConfirmationBlocks = 3;
|
BucketFactory.transactionConfirmationBlocks = 3;
|
||||||
GiftBucket.transactionConfirmationBlocks = 3;
|
Bucket.transactionConfirmationBlocks = 3;
|
||||||
|
ERC721.transactionConfirmationBlocks = 3;
|
||||||
|
|
||||||
let sender;
|
let sender;
|
||||||
let hasDoneSomething = false;
|
let hasDoneSomething = false;
|
||||||
@ -152,7 +184,7 @@ async function run() {
|
|||||||
let file = fs.readFileSync(argv["file"], 'utf8');
|
let file = fs.readFileSync(argv["file"], 'utf8');
|
||||||
keycards = file.split("\n").map(processLine);
|
keycards = file.split("\n").map(processLine);
|
||||||
for (let keycard of keycards) {
|
for (let keycard of keycards) {
|
||||||
await createGift(sender, bucket, keycard)
|
await argv["nft"] ? createGift(sender, bucket, keycard) : transferNFT(sender, argv["token"], bucket, keycard);
|
||||||
}
|
}
|
||||||
} else if (!hasDoneSomething) {
|
} else if (!hasDoneSomething) {
|
||||||
console.error("the --file option must be specified");
|
console.error("the --file option must be specified");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user