Skip to content

Instantly share code, notes, and snippets.

@0xNB-dev
Created November 19, 2023 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xNB-dev/194cee3fc8618c8365364d2f118d77cd to your computer and use it in GitHub Desktop.
Save 0xNB-dev/194cee3fc8618c8365364d2f118d77cd to your computer and use it in GitHub Desktop.
const fs = require('fs');
const xMonaco = fs.readFileSync('../data/0xMonaco.sol').toString();
const ICar = fs.readFileSync('../data/ICar.sol').toString();
const CarData = fs.readFileSync('../data/CarData.sol').toString();
const strategy1 = fs.readFileSync('../data/strategy_1.sol').toString();
const strategy2 = fs.readFileSync('../data/strategy_2.sol').toString();
const strategy3 = fs.readFileSync('../data/strategy_3.sol').toString();
// console.log(xMonaco);
// console.log(ICar);
// console.log(strategy1);
// console.log(strategy2);
// console.log(strategy3);
const prompt = '\n' +
'### System Prompt \n' +
'You are a Solidity Engineer AI. You have designed and written a game called 0xMonaco, written in solidity' +
'that allows 3 solidity ICar strategies to compete against each other in a turn by turn race, each strategy starts ' +
'with 17500 coins. The first strategy reaching y = 1000 wins. \n' +
'In order to get there, each strategy has to buy actions, each of which have a cost, and the cost goes up the more' +
' they are bought. The cost function follows a Variable Rate Gradual Dutch Auction formula, which is essentially a' +
' logarithmic cost they more they are bought.\n' +
'The actions are:\n' +
'- acceleration: each purchase allows you to move faster. You can buy 1 to N acceleration each turn.\n' +
'- shell: it will cancel all accelerations for the strategy / ICar in front of the current player\n' +
'- super-shell: it will cancel all accelerations for the strategy / ICar in front of the current player, all the way' +
' to the first player.\n' +
'- banana: it will stay where the strategy / ICar has dropped it and the next strategy / ICar running into it will' +
' have its speed cut in half.\n' +
'- shield: it will prevent the current player from being hit by a banana, a shell or a super shell.\n' +
'\n' +
'The SDK for checking the cost of buying actions and for buying actions is as follows:' +
' You can get the cost for each action by using the following methods:\n' +
' monaco.getShellCost(1) to get the cost for 1 shell\n' +
' monaco.getSuperShellCost(1) to get the cost for 1 super shell\n' +
' monaco.getBananaCost() to get the cost for 1 banana\n' +
' monaco.getShieldCost(1) to get the cost for 1 shield\n' +
' monaco.getAccelerateCost(N) to get the cost for N accelerations\n' +
' You can buy each action by using the following methods:\n' +
' monaco.buyShell(1) to buy 1 shell\n' +
' monaco.buySuperShell(1) to buy 1 super shell\n' +
' monaco.buyBanana() to buy 1 banana\n' +
' monaco.buyShield(1) to buy 1 shield\n' +
' monaco.buyAcceleration(N) to buy N accelerations\n' +
' Note that due to the cost function, buy 5 accelerations is exponentially more expensive than buying 1. You can get the cost for 5 or 10 to check first the price impact if needed.\n' +
'\n' +
'Each strategy / car has a data structure called CarData, described as follows:\n' +
'```solidity\n' +
`${CarData}\n` +
'```\n' +
'\n' +
'Your task is to write strategies / ICar solidity contracts following the user request. For example, if the user ' +
'asks for an aggressive strategy, the strategy / ICar solidity contract should favor buying shells / super-shells.\n' +
'If the user asks for a balanced strategy, the strategy / ICar solidity contract should promote a healthy balance' +
' between buying accelerations, setting a shield, dropping bananas when there are players behind and sending shells' +
' when the cost is affordable and the player is second, or super-shells when the cost is affordable and the player' +
' is last.\n' +
'\n' +
'Provide the solidity code implementation for the strategy, wrapped around backticks code block, and explain your' +
'reasoning step by step.\n' +
'\n' +
'Make sure to extract the variables from the user request so you can reuse them appropriately within the code.\n' +
'The code you will produce will be ran in a docker container and you will get the output from it, so you can fix' +
' any error in the code. When that happens, fix the errors and send a new version of the code. Only send the full' +
' code back wrapped in a backtick-ed code block so it can be ran again so we can repeat the fixing if needed.\n' +
'\n' +
'Only provide complete and working code. Do not use comments to suggest some code should be manually added. The user' +
' will not add code. Only you will provide code. \n\n' +
'The Strategy should have 2 methods: \n' +
'- takeYourTurn: allows the strategy to play a turn. The strategy receives the CarData for every strategy.\n' +
'- sayMyName: will return a string that is the strategy name.\n' +
'\n' +
'The takeYourTurn method will be called once per turn for each car, car by car, until one car crosses the y=1000 finish line.\n' +
'At each turn, you get your position via CarData.y, your speed by CarData.speed, and your balance via CarData.balance.\n' +
'You will need to do the following:\n' +
'1. Check the current race conditions\n' +
'2. Depending on the conditions, and the strategy parameters (focus on speed, economic balance, aggressiveness, banana-focus, defender, etc...)' +
' you will check the cost of the items you think should be used to get an edge over the other 2 strategies\n' +
'3. Check the remaining balance, and buy the items using the buy functions described as follows:\n' +
'4. Verify that you have completed the actions you want the strategy to complete for this turn.\n' +
'\n' +
' Here the code for the ICar interface:\n' +
'\n' +
`${ICar}\n\n` +
// `${xMonaco}\n\n` +
/////Optional////
'\n' +
' Here an example of a strategy:\n' +
'\n' +
`${strategy1}\n\n` +
' Here an example of a strategy:\n' +
'\n' +
`${strategy2}\n\n` +
' Here an example of a strategy:\n' +
'\n' +
`${strategy3}` +
'\n' +
'' +
'### User Message\n' +
'Provide the complete code for an implementation of a 0xMonaco Strategy game that focuses on a healthy economic balance.\n' +
'PROVIDE THE FULL IMPLEMENTATION CODE, well tested and thoroughly designed. Do NOT provide a sample implementation!\n' +
'\n' +
'### Assistant\n' +
'\n';
async function run() {
const response = await fetch('https://api.together.xyz/inference', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer <Insert API key>',
'Accept': 'application/json',
},
body: JSON.stringify({
"model": "Phind/Phind-CodeLlama-34B-v2",
"max_tokens": 16384,
"prompt": prompt,
"request_type": "language-model-inference",
"temperature": 0.5,
// "top_p": 0.8,
"top_k": 20,
"repetition_penalty": 0,
"stream_tokens": true,
"stop": [
"</s>"
],
"negative_prompt": "Incomplete code, unfinished",
"sessionKey": "43c167f3bad139127aa0b9586e921fde9c76486b",
"prompt_format_string": ""
})
});
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader()
let finalOutput = "";
while(true) {
const {value: rawValue, done} = await reader.read();
if (done) break;
const values = rawValue.split("\n\n");
console.log(JSON.stringify(values));
for(const value of values) {
if(value === "") continue;
if(value === "data: [DONE]") break;
console.log(JSON.stringify(value));
const valueData = value.replaceAll(/data:[^{]*/g, "");
const valueJSON = JSON.parse(valueData);
const newToken = valueJSON.choices[0].text;
finalOutput += newToken.replaceAll(/\\n/g, "\n");
}
}
console.log(finalOutput);
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment