-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
354f98f
commit b7bca9f
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package onchain | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
|
||
"github.com/massalabs/station/pkg/convert" | ||
) | ||
|
||
type ContractDatastore struct { | ||
Data []byte | ||
Args []byte | ||
Coins uint64 | ||
} | ||
|
||
type datastoreEntry struct { | ||
Key []byte | ||
Value []byte | ||
} | ||
|
||
|
||
/* | ||
* | ||
populateDatastore creates and serializes a datastore for the given contract. | ||
*/ | ||
func populateDatastore(contract ContractDatastore) ([]byte, error) { | ||
var datastore []datastoreEntry | ||
|
||
// nmber of contracts to deploy | ||
numberOfContractsKey := []byte{0} | ||
numberOfContracts := convert.U64ToBytes(1) | ||
datastore = append(datastore, datastoreEntry{Key: numberOfContractsKey, Value: numberOfContracts}) | ||
|
||
// contract data | ||
contractKey := convert.U64ToBytes(1) | ||
datastore = append(datastore, datastoreEntry{Key: contractKey, Value: contract.Data}) | ||
|
||
// args data | ||
argsKey := []byte{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0} // hardcoded for now, could be dynamix see: https://github.com/massalabs/massa-web3/blob/main/src/dataStore.ts | ||
datastore = append(datastore, datastoreEntry{Key: argsKey, Value: contract.Args}) | ||
|
||
// coins data | ||
coinsKey := []byte{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1} // 12/02/2024 single contract deployement is supported. Multiple not planned. see https://github.com/massalabs/station/issues/1364 | ||
datastore = append(datastore, datastoreEntry{Key: coinsKey, Value: convert.U64ToBytes(contract.Coins)}) | ||
|
||
// Serialize the datastore | ||
serializedDatastore, err := SerializeDatastore(datastore) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return serializedDatastore, nil | ||
} | ||
|
||
// SerializeDatastore serializes the datastore into a []byte array. | ||
func SerializeDatastore(datastore []datastoreEntry) ([]byte, error) { | ||
var buffer bytes.Buffer | ||
|
||
buf := make([]byte, binary.MaxVarintLen64) | ||
// Encode the number of key-value pairs | ||
datastoreSize := uint64(len(datastore)) | ||
uDatastoreSize := binary.PutUvarint(buf, datastoreSize) | ||
|
||
buffer.Write(buf[:uDatastoreSize]) | ||
|
||
// Encode each key-value pair | ||
for _, entry := range datastore { | ||
// Encode key | ||
keyLength := uint64(len(entry.Key)) | ||
uKeyLength := binary.PutUvarint(buf, keyLength) | ||
keyLengthInBytes := buf[:uKeyLength] | ||
buffer.Write(keyLengthInBytes) | ||
buffer.Write(entry.Key) | ||
|
||
// Encode value | ||
valueLength := uint64(len(entry.Value)) | ||
uValueLength := binary.PutUvarint(buf, valueLength) | ||
valueLengthInBytes := buf[:uValueLength] | ||
buffer.Write(valueLengthInBytes) | ||
buffer.Write(entry.Value) | ||
} | ||
|
||
return buffer.Bytes(), nil | ||
} |