Skip to content

Instantly share code, notes, and snippets.

@cyrusadkisson
Created October 17, 2015 15:35
Show Gist options
  • Save cyrusadkisson/a4e19431c1e257ae4382 to your computer and use it in GitHub Desktop.
Save cyrusadkisson/a4e19431c1e257ae4382 to your computer and use it in GitHub Desktop.
Gathering 2D array into a contract from an already-deployed contract
// NOTE this is already deployed at address 0xed9c3aead241f6fd8e6b6951e29c3dcb5b3662c1 I want to interact with.
//contract BlockDefStorage
//{
// bool[32] occupiesInitialized;
// bool[32] attachestoInitialized;
// bool allOccupiesInitialized;
// bool allAttachestoInitialized;
//
// Block[32] blocks;
// struct Block
// {
// int8[3][8] occupies; // [x,y,z] 8 times
// int8[3][] attachesto; // [x,y,z]
// }
//
// function getOccupies(uint8 which) public constant returns (int8[3][8])
// {
// return blocks[which].occupies;
// }
//
// function getAttachesto(uint8 which) public constant returns (int8[3][])
// {
// return blocks[which].attachesto;
// }
//
// function initOccupies(uint8 which, int8[3][8] occupies) public
// {
// if(allOccupiesInitialized) // lockout
// return;
// blocks[which].occupies = occupies;
// occupiesInitialized[which] = true;
// for(uint8 b = 0; b < 32; b++)
// {
// if(occupiesInitialized[b] == false)
// return;
// }
// allOccupiesInitialized = true;
// }
//
// function initAttachesto(uint8 which, int8[3][] attachesto) public
// {
// if(allAttachestoInitialized) // lockout
// return;
// blocks[which].attachesto.length = attachesto.length;
// blocks[which].attachesto = attachesto;
// attachestoInitialized[which] = true;
// for(uint8 b = 0; b < 32; b++)
// {
// if(attachestoInitialized[b] == false)
// return;
// }
// allAttachestoInitialized = true;
// }
//}
import 'mortal';
contract BlockDefStorage
{
function getOccupies(uint8 which) public returns (int8[3][8]);
function getAttachesto(uint8 which) public returns (int8[3][8]);
function initOccupies(uint8 which, int8[3][8] occupies) public;
function initAttachesto(uint8 which, int8[3][] attachesto) public;
}
contract BlockDefStorageTester is mortal
{
function BlockDefStorageTester() {
}
function getOccupiesFromBDRDirect(uint8 which) public constant returns(int8[3][8])
{
address BlockDefStorageAddress = 0xed9c3aead241f6fd8e6b6951e29c3dcb5b3662c1;
return BlockDefStorage(BlockDefStorageAddress).getOccupies(which);
}
function getOccupiesFromBDRMemory(uint8 which) public constant returns(int8[3][8])
{
address BlockDefStorageAddress = 0xed9c3aead241f6fd8e6b6951e29c3dcb5b3662c1;
int8[3][8] memory occ = BlockDefStorage(BlockDefStorageAddress).getOccupies(which);
return occ;
}
}
// To request block definitions directly from BlockDefStorage contract use this abi and address:
// > var abi = [{"constant":true,"inputs":[{"name":"which","type":"uint8"}],"name":"getAttachesto","outputs":[{"name":"","type":"int8[3][]"}],"type":"function"},{"constant":false,"inputs":[{"name":"which","type":"uint8"},{"name":"occupies","type":"int8[3][8]"}],"name":"initOccupies","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"which","type":"uint8"},{"name":"attachesto","type":"int8[3][]"}],"name":"initAttachesto","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"which","type":"uint8"}],"name":"getOccupies","outputs":[{"name":"","type":"int8[3][8]"}],"type":"function"}]
// > var blockdefstorage = web3.eth.contract(abi).at('0xed9c3aead241f6fd8e6b6951e29c3dcb5b3662c1');
// > blockdefstorage.getOccupies(0);
// [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 0, 4], [0, 0, 5], [0, 0, 6], [0, 0, 7]] // CORRECT ANSWER
// To request block definitions from the BlockDefStorage contract THROUGH the BlockDefStorageTester (and its BlockDefRetriever)
// > var blockdefstoragetesterContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"which","type":"uint8"}],"name":"getOccupiesFromBDRMemory","outputs":[{"name":"","type":"int8[3][8]"}],"type":"function"},{"constant":true,"inputs":[{"name":"which","type":"uint8"}],"name":"getOccupiesFromBDRDirect","outputs":[{"name":"","type":"int8[3][8]"}],"type":"function"},{"inputs":[],"type":"constructor"}]);
// > var blockdefstoragetester = blockdefstoragetesterContract.new(
// {
// from: web3.eth.accounts[0],
// data: '606060405260008054600160a060020a03191633179055610275806100246000396000f3606060405260e060020a600035046341c0e1b58114610031578063c193f67014610059578063c8a009ea14610160575b005b61002f60005433600160a060020a039081169116141561027357600054600160a060020a0316ff5b61022f60043561036060405260606008815b6060604051908101604052806003905b600081526020019060019003908161007b5750508152600019909101906020018161006b579050506000610300604051908101604052806008905b6060604051908101604052806003905b60008152602001906001900390816100c6575050815260001990910190602001816100b65790505073ed9c3aead241f6fd8e6b6951e29c3dcb5b3662c1915081600160a060020a0316631bcf5758856040518260e060020a028152600401808260ff168152602001915050610300604051808303816000876161da5a03f11561000257505060408051610100810190915295945050505050565b61022f60043561036060405260606008815b6060604051908101604052806003905b600081526020019060019003908161018257505081526000199091019060200181610172575050604080517f1bcf575800000000000000000000000000000000000000000000000000000000815260ff84166004820152905173ed9c3aead241f6fd8e6b6951e29c3dcb5b3662c1918291631bcf5758916024818101926103009290919082900301816000876161da5a03f115610002575050506040518061010001604052915050919050565b6040516000826008835b818410156102635760208402830151606080838184600060046018f1509050019260010192610239565b9250505091505060405180910390f35b56',
// gas: 1500000
// }, function(e, contract){
// if (typeof contract.address != 'undefined') {
// console.log(e, contract);
// console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
// }
// })
// > blockdefstoragetester.getOccupiesFromBDRMemory(0);
// [[0, 0, 3424], [0, 0, 3424], [0, 0, 3424], [0, 0, 3424], [0, 0, 3424], [0, 0, 876544], [0, 0, 3424], [0, 0, 3424]] // WRONG
// > {address != 'undefined') {ccupiesFromBDRMemory(0);
// console.log(e, contract);
// console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
// }
// > blockdefstoragetester.getOccupiesFromBDRDirect(0);
// [[0, 0, 1888], [0, 0, 1888], [0, 0, 1888], [0, 0, 1888], [0, 0, 1888], [0, 0, 483328], [0, 0, 1888], [0, 0, 1888]] // WRONG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment