Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dvcpull committed Sep 20, 2024
1 parent 4d3e3d7 commit 820fc5a
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 32 deletions.
51 changes: 51 additions & 0 deletions deploy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path');
const { create_app_agent } = require('./utils/app_agent');
const { create_fungible_token } = require('./utils/fungible');
const { create_nft_collection, create_nft_token } = require('./utils/nft');
const { retryOperation, maxWaitTime } = require('./utils/utils');

const aws_s3_assets_path = path.join(__dirname, '..', 'aws_s3_assets');
const game_a_path = path.join(aws_s3_assets_path, 'game-a/');
Expand Down Expand Up @@ -102,6 +103,9 @@ async function main() {
.catch(reject);
});

await create_balance_transfers(api, demo_user_one, demo_user_two);
await create_balance_transfers(api, demo_user_three, demo_user_one);

console.log("Traverse the game folders and create app-agents and assets for each game");
for (const [game_index, game_folder] of game_folders.entries()) {
console.log("Game folder:", game_folder);
Expand Down Expand Up @@ -209,6 +213,53 @@ function getObjectMetadataURL(directory) {
return null;
}

async function create_balance_transfers(api, token_recipient, token_recipient_two) {
console.log("Generate free transfers between the two users");

for (let i = 0; i < 5; i++) {
await retryOperation(async () => {
return new Promise(async (resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error(`Timeout: Transfer took too long`));
}, maxWaitTime);


const unsubscribe = await api.tx.playerTransfers.submitTransferBalances(
token_recipient_two.address,
1000000
).signAndSend(token_recipient, { nonce: -1 }, ({ status, events }) => {
if (status.isInBlock) {
let extrinsicSuccess = false;
events.forEach(({ event }) => {
if (api.events.system.ExtrinsicSuccess.is(event)) {
extrinsicSuccess = true;
}
});

if (extrinsicSuccess) {
console.log(`Transfer is in block and successful`);
clearTimeout(timeout);
unsubscribe();
resolve();
} else {
clearTimeout(timeout);
unsubscribe();
reject(new Error(`Transfer failed: ExtrinsicSuccess event not found`));
}
} else if (status.isError) {
clearTimeout(timeout);
// unsubscribe();
// reject(new Error(`Transfer failed with error status`));
}
});
});
}, `creating free transfer`);
}

console.log(`Free transfer created and confirmed`);

}

main()
.catch(console.error)
.finally(() => process.exit());
70 changes: 45 additions & 25 deletions deploy/utils/fungible.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ async function create_fungible_token(api, appAgentOwner, appAgentId, token_recip

await retryOperation(sendConfigureFungibleTokenTx, "configuring Fungible Token");

// wait 12 secs
await new Promise(resolve => setTimeout(resolve, 12000));
await create_token_transfers(api, token_id, token_recipient, token_recipient_two);
}

Expand All @@ -104,37 +106,55 @@ async function create_token_transfers(api, token_id, token_recipient, token_reci
reject(new Error(`Timeout: Transfer ${i + 1} took too long`));
}, maxWaitTime);

const unsubscribe = await api.tx.playerTransfers.submitTransferAssets(
token_id,
token_recipient_two.address,
10
).signAndSend(token_recipient, { nonce: -1 }, ({ status, events }) => {
if (status.isInBlock) {
let extrinsicSuccess = false;
events.forEach(({ event }) => {
if (api.events.system.ExtrinsicSuccess.is(event)) {
extrinsicSuccess = true;
try {
const unsubscribe = await api.tx.playerTransfers.submitTransferAssets(
token_id,
token_recipient_two.address,
10
).signAndSend(token_recipient, { nonce: -1 }, ({ status, events }) => {
if (status.isInBlock) {
let extrinsicSuccess = false;
events.forEach(({ event }) => {
if (api.events.system.ExtrinsicSuccess.is(event)) {
extrinsicSuccess = true;
}
});

if (extrinsicSuccess) {
console.log(`Transfer ${i + 1} is in block and successful`);
clearTimeout(timeout);
unsubscribe();
resolve();
} else {
clearTimeout(timeout);
unsubscribe();
reject(new Error(`Transfer ${i + 1} failed: ExtrinsicSuccess event not found`));
}
});

if (extrinsicSuccess) {
console.log(`Transfer ${i + 1} is in block and successful`);
} else if (status.isError) {
clearTimeout(timeout);
unsubscribe();
resolve();
} else {
clearTimeout(timeout);
unsubscribe();
reject(new Error(`Transfer ${i + 1} failed: ExtrinsicSuccess event not found`));
reject(new Error(`Transfer ${i + 1} failed with error status`));
}
} else if (status.isError) {
clearTimeout(timeout);
unsubscribe();
reject(new Error(`Transfer ${i + 1} failed with error status`));
});
} catch (error) {
clearTimeout(timeout);
if (error.message.includes("Priority is too low")) {
console.log("Priority too low. Retrying with increased delay.");
await new Promise(resolve => setTimeout(resolve, 5000)); // Additional delay
}
});
reject(error);
}
});
}, `creating free transfer ${i + 1}`);
}, `creating free transfer ${i + 1}`, {
retryInterval: (attempt) => Math.min(2000 * Math.pow(2, attempt), 30000), // Exponential backoff
shouldRetry: (error) => {
if (error.message.includes("Priority is too low")) {
console.log("Priority too low. Will retry with increased delay.");
return true;
}
return error.message.includes("Timeout") || error.message.includes("failed");
}
});

console.log(`Free transfer ${i + 1} created and in block`);
}
Expand Down
22 changes: 15 additions & 7 deletions deploy/utils/nft.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ async function create_nft_transfers(api, token_recipient, token_recipient_two, c
await retryOperation(async () => {
return new Promise(async (resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error(`Timeout: Transfer took too long`));
}, maxWaitTime);

reject(new Error(`Timeout: Transfer took too long`));
}, maxWaitTime);

try {
const unsubscribe = await api.tx.playerTransfers.submitTransferNfts(
collection_id,
token_id,
Expand All @@ -347,12 +347,20 @@ async function create_nft_transfers(api, token_recipient, token_recipient_two, c
}
} else if (status.isError) {
clearTimeout(timeout);
unsubscribe();
reject(new Error(`Transfer failed with error status`));
// unsubscribe();
// reject(new Error(`Transfer failed with error status`));
}
});
});
}, `creating free transfer`);
} catch (error) {
clearTimeout(timeout);
if (error.message.includes("Priority is too low")) {
console.log("Priority too low. Retrying with increased delay.");
await new Promise(resolve => setTimeout(resolve, 5000)); // Additional delay
}
reject(error);
}
});
}, `creating free transfer`);

console.log(`Free transfer created and confirmed`);

Expand Down

0 comments on commit 820fc5a

Please sign in to comment.